Browse Source

Font stash: store pref glyph as index instead of pointer

shared-context
Mikko Mononen 11 years ago
parent
commit
f0b503d9cc
1 changed files with 22 additions and 21 deletions
  1. +22
    -21
      src/fontstash.h

+ 22
- 21
src/fontstash.h View File

@@ -71,7 +71,7 @@ struct FONStextIter {
unsigned int codepoint;
short isize, iblur;
struct FONSfont* font;
struct FONSglyph* prevGlyph;
int prevGlyphIndex;
const char* str;
const char* next;
const char* end;
@@ -396,7 +396,7 @@ struct FONScontext
float tcoords[FONS_VERTEX_COUNT*2];
unsigned int colors[FONS_VERTEX_COUNT];
int nverts;
unsigned char *scratch;
unsigned char* scratch;
int nscratch;
struct FONSstate states[FONS_MAX_STATES];
int nstates;
@@ -840,7 +840,7 @@ int fonsAddFont(struct FONScontext* stash, const char* name, const char* path)

// Read in the font data.
fp = fopen(path, "rb");
if (!fp) goto error;
if (fp == NULL) goto error;
fseek(fp,0,SEEK_END);
dataSize = (int)ftell(fp);
fseek(fp,0,SEEK_SET);
@@ -1093,13 +1093,13 @@ static struct FONSglyph* fons__getGlyph(struct FONScontext* stash, struct FONSfo
}

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

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

@@ -1209,8 +1209,8 @@ float fonsDrawText(struct FONScontext* stash,
unsigned int codepoint;
unsigned int utf8state = 0;
struct FONSglyph* glyph = NULL;
struct FONSglyph* prevGlyph = NULL;
struct FONSquad q;
int prevGlyphIndex = -1;
short isize = (short)(state->size*10.0f);
short iblur = (short)state->blur;
float scale;
@@ -1220,7 +1220,7 @@ float fonsDrawText(struct FONScontext* stash,
if (stash == NULL) return x;
if (state->font < 0 || state->font >= stash->nfonts) return x;
font = stash->fonts[state->font];
if (!font->data) return x;
if (font->data == NULL) return x;

scale = fons__tt_getPixelHeightScale(&font->font, (float)isize/10.0f);

@@ -1244,8 +1244,8 @@ float fonsDrawText(struct FONScontext* stash,
if (fons__decutf8(&utf8state, &codepoint, *(const unsigned char*)str))
continue;
glyph = fons__getGlyph(stash, font, codepoint, isize, iblur);
if (glyph) {
fons__getQuad(stash, font, prevGlyph, glyph, scale, state->spacing, &x, &y, &q);
if (glyph != NULL) {
fons__getQuad(stash, font, prevGlyphIndex, glyph, scale, state->spacing, &x, &y, &q);

if (stash->nverts+6 > FONS_VERTEX_COUNT)
fons__flush(stash);
@@ -1258,7 +1258,7 @@ float fonsDrawText(struct FONScontext* stash,
fons__vertex(stash, q.x0, q.y1, q.s0, q.t1, state->color);
fons__vertex(stash, q.x1, q.y1, q.s1, q.t1, state->color);
}
prevGlyph = glyph;
prevGlyphIndex = glyph != NULL ? glyph->index : -1;
}
fons__flush(stash);

@@ -1276,7 +1276,7 @@ int fonsTextIterInit(struct FONScontext* stash, struct FONStextIter* iter,
if (stash == NULL) return 0;
if (state->font < 0 || state->font >= stash->nfonts) return 0;
iter->font = stash->fonts[state->font];
if (!iter->font->data) return 0;
if (iter->font->data == NULL) return 0;

iter->isize = (short)(state->size*10.0f);
iter->iblur = (short)state->blur;
@@ -1305,6 +1305,7 @@ int fonsTextIterInit(struct FONScontext* stash, struct FONStextIter* iter,
iter->next = str;
iter->end = end;
iter->codepoint = 0;
iter->prevGlyphIndex = -1;

return 1;
}
@@ -1327,8 +1328,8 @@ int fonsTextIterNext(struct FONScontext* stash, struct FONStextIter* iter, struc
iter->y = iter->nexty;
glyph = fons__getGlyph(stash, iter->font, iter->codepoint, iter->isize, iter->iblur);
if (glyph != NULL)
fons__getQuad(stash, iter->font, iter->prevGlyph, glyph, iter->scale, iter->spacing, &iter->nextx, &iter->nexty, quad);
iter->prevGlyph = glyph;
fons__getQuad(stash, iter->font, iter->prevGlyphIndex, glyph, iter->scale, iter->spacing, &iter->nextx, &iter->nexty, quad);
iter->prevGlyphIndex = glyph != NULL ? glyph->index : -1;
break;
}
iter->next = str;
@@ -1394,7 +1395,7 @@ float fonsTextBounds(struct FONScontext* stash,
unsigned int utf8state = 0;
struct FONSquad q;
struct FONSglyph* glyph = NULL;
struct FONSglyph* prevGlyph = NULL;
int prevGlyphIndex = -1;
short isize = (short)(state->size*10.0f);
short iblur = (short)state->blur;
float scale;
@@ -1405,7 +1406,7 @@ float fonsTextBounds(struct FONScontext* stash,
if (stash == NULL) return 0;
if (state->font < 0 || state->font >= stash->nfonts) return 0;
font = stash->fonts[state->font];
if (!font->data) return 0;
if (font->data == NULL) return 0;

scale = fons__tt_getPixelHeightScale(&font->font, (float)isize/10.0f);

@@ -1423,8 +1424,8 @@ float fonsTextBounds(struct FONScontext* stash,
if (fons__decutf8(&utf8state, &codepoint, *(const unsigned char*)str))
continue;
glyph = fons__getGlyph(stash, font, codepoint, isize, iblur);
if (glyph) {
fons__getQuad(stash, font, prevGlyph, glyph, scale, state->spacing, &x, &y, &q);
if (glyph != NULL) {
fons__getQuad(stash, font, prevGlyphIndex, glyph, scale, state->spacing, &x, &y, &q);
if (q.x0 < minx) minx = q.x0;
if (q.x1 > maxx) maxx = q.x1;
if (stash->params.flags & FONS_ZERO_TOPLEFT) {
@@ -1435,7 +1436,7 @@ float fonsTextBounds(struct FONScontext* stash,
if (q.y0 > maxy) maxy = q.y0;
}
}
prevGlyph = glyph;
prevGlyphIndex = glyph != NULL ? glyph->index : -1;
}

advance = x - startx;
@@ -1472,7 +1473,7 @@ void fonsVertMetrics(struct FONScontext* stash,
if (state->font < 0 || state->font >= stash->nfonts) return;
font = stash->fonts[state->font];
isize = (short)(state->size*10.0f);
if (!font->data) return;
if (font->data == NULL) return;

if (ascender)
*ascender = font->ascender*isize/10.0f;
@@ -1492,7 +1493,7 @@ void fonsLineBounds(struct FONScontext* stash, float y, float* miny, float* maxy
if (state->font < 0 || state->font >= stash->nfonts) return;
font = stash->fonts[state->font];
isize = (short)(state->size*10.0f);
if (!font->data) return;
if (font->data == NULL) return;

y += fons__getVertAlign(stash, font, state->align, isize);



Loading…
Cancel
Save