Browse Source

Fix text rendering flickering under rotation

- quantize font rendering scale
- round kerning values for less flicker
shared-context
Mikko Mononen 11 years ago
parent
commit
c8e2466447
2 changed files with 10 additions and 8 deletions
  1. +4
    -7
      src/fontstash.h
  2. +6
    -1
      src/nanovg.c

+ 4
- 7
src/fontstash.h View File

@@ -819,18 +819,15 @@ static struct FONSglyph* fons__getGlyph(struct FONScontext* stash, struct FONSfo
dst[x + (gh-1)*stash->params.width] = 0; dst[x + (gh-1)*stash->params.width] = 0;
} }


/*
// Debug code to color the glyph background // Debug code to color the glyph background
int x,y;
unsigned char* fdst = &stash->texData[glyph->x0 + glyph->y0 * stash->params.width];
/* unsigned char* fdst = &stash->texData[glyph->x0 + glyph->y0 * stash->params.width];
for (y = 0; y < gh; y++) { for (y = 0; y < gh; y++) {
for (x = 0; x < gw; x++) { for (x = 0; x < gw; x++) {
int a = (int)fdst[x+y*stash->params.width] + 20; int a = (int)fdst[x+y*stash->params.width] + 20;
if (a > 255) a = 255; if (a > 255) a = 255;
fdst[x+y*stash->params.width] = a; fdst[x+y*stash->params.width] = a;
} }
}
*/
}*/


// Blur // Blur
if (iblur > 0) { if (iblur > 0) {
@@ -851,11 +848,11 @@ static void fons__getQuad(struct FONScontext* stash, struct FONSfont* font,
struct FONSglyph* prevGlyph, struct FONSglyph* glyph, struct FONSglyph* prevGlyph, struct FONSglyph* glyph,
float scale, float spacing, float* x, float* y, struct FONSquad* q) float scale, float spacing, float* x, float* y, struct FONSquad* q)
{ {
int rx,ry,xoff,yoff,x0,y0,x1,y1;
float rx,ry,xoff,yoff,x0,y0,x1,y1;


if (prevGlyph) { if (prevGlyph) {
float adv = stbtt_GetGlyphKernAdvance(&font->font, prevGlyph->index, glyph->index) * scale; float adv = stbtt_GetGlyphKernAdvance(&font->font, prevGlyph->index, glyph->index) * scale;
*x += adv;
*x += (int)(adv + 0.5f);
} }


// Each glyph has 2px border to allow good interpolation, // Each glyph has 2px border to allow good interpolation,


+ 6
- 1
src/nanovg.c View File

@@ -1552,13 +1552,18 @@ void nvgFontFace(struct NVGcontext* ctx, const char* font)
state->fontId = fonsGetFontByName(ctx->fs, font); state->fontId = fonsGetFontByName(ctx->fs, font);
} }


static float nvg__quantize(float a, float d)
{
return ((int)(a / d)) * d;
}

float nvgText(struct NVGcontext* ctx, float x, float y, const char* string, const char* end) float nvgText(struct NVGcontext* ctx, float x, float y, const char* string, const char* end)
{ {
struct NVGstate* state = nvg__getState(ctx); struct NVGstate* state = nvg__getState(ctx);
struct FONStextIter iter; struct FONStextIter iter;
struct FONSquad q; struct FONSquad q;
struct NVGvertex* verts; struct NVGvertex* verts;
float scale = nvg__minf(nvg__getAverageScale(state->xform), 4.0f);
float scale = nvg__minf(nvg__quantize(nvg__getAverageScale(state->xform), 0.01f), 4.0f);
float invscale = 1.0f / scale; float invscale = 1.0f / scale;
int dirty[4]; int dirty[4];
int cverts = 0; int cverts = 0;


Loading…
Cancel
Save