Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-kern #658

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/fontstash.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ typedef struct FONSquad FONSquad;

struct FONStextIter {
float x, y, nextx, nexty, scale, spacing;
int kern_adjust;
unsigned int codepoint;
short isize, iblur;
struct FONSfont* font;
Expand Down Expand Up @@ -1210,16 +1211,20 @@ static FONSglyph* fons__getGlyph(FONScontext* stash, FONSfont* font, unsigned in
return glyph;
}

static void fons__getQuad(FONScontext* stash, FONSfont* font,
static int fons__getQuad(FONScontext* stash, FONSfont* font,
int prevGlyphIndex, FONSglyph* glyph,
float scale, float spacing, float* x, float* y, FONSquad* q)
{
int kern_adjust;
float rx,ry,xoff,yoff,x0,y0,x1,y1;

if (prevGlyphIndex != -1) {
float adv = fons__tt_getGlyphKernAdvance(&font->font, prevGlyphIndex, glyph->index) * scale;
*x += (int)(adv + spacing + 0.5f);
kern_adjust = (int)(adv + spacing + 0.5f);
*x += kern_adjust;
}
else
kern_adjust = 0;

// Each glyph has 2px border to allow good interpolation,
// one pixel to prevent leaking, and one to allow good interpolation for rendering.
Expand Down Expand Up @@ -1260,6 +1265,7 @@ static void fons__getQuad(FONScontext* stash, FONSfont* font,
}

*x += (int)(glyph->xadv / 10.0f + 0.5f);
return kern_adjust;
}

static void fons__flush(FONScontext* stash)
Expand Down Expand Up @@ -1416,6 +1422,7 @@ int fonsTextIterInit(FONScontext* stash, FONStextIter* iter,
if (end == NULL)
end = str + strlen(str);

iter->kern_adjust = 0;
iter->x = iter->nextx = x;
iter->y = iter->nexty = y;
iter->spacing = state->spacing;
Expand Down Expand Up @@ -1448,7 +1455,10 @@ int fonsTextIterNext(FONScontext* stash, FONStextIter* iter, FONSquad* quad)
glyph = fons__getGlyph(stash, iter->font, iter->codepoint, iter->isize, iter->iblur, iter->bitmapOption);
// If the iterator was initialized with FONS_GLYPH_BITMAP_OPTIONAL, then the UV coordinates of the quad will be invalid.
if (glyph != NULL)
fons__getQuad(stash, iter->font, iter->prevGlyphIndex, glyph, iter->scale, iter->spacing, &iter->nextx, &iter->nexty, quad);
iter->kern_adjust = fons__getQuad(stash, iter->font, iter->prevGlyphIndex, glyph, iter->scale, iter->spacing, &iter->nextx, &iter->nexty, quad);
else
iter->kern_adjust = 0;

iter->prevGlyphIndex = glyph != NULL ? glyph->index : -1;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/nanovg.c
Original file line number Diff line number Diff line change
Expand Up @@ -2602,7 +2602,7 @@ int nvgTextGlyphPositions(NVGcontext* ctx, float x, float y, const char* string,
}
prevIter = iter;
positions[npos].str = iter.str;
positions[npos].x = iter.x * invscale;
positions[npos].x = (iter.x + iter.kern_adjust) * invscale;
positions[npos].minx = nvg__minf(iter.x, q.x0) * invscale;
positions[npos].maxx = nvg__maxf(iter.nextx, q.x1) * invscale;
npos++;
Expand Down