Code refactoring for #299.

This commit fixes the formats as suggested in #299 and fixes stack overflow when using `freetype` backend.
master
Olli Wang 2016-08-21 02:31:57 +08:00
parent 40e999af3c
commit c97444c96f
6 changed files with 23 additions and 29 deletions

View File

@ -839,8 +839,8 @@ int loadDemoData(NVGcontext* vg, DemoData* data)
printf("Could not add font emoji.\n");
return -1;
}
nvgAddFallbackFontId(vg,data->fontNormal,data->fontEmoji);
nvgAddFallbackFontId(vg,data->fontBold,data->fontEmoji);
nvgAddFallbackFontId(vg, data->fontNormal, data->fontEmoji);
nvgAddFallbackFontId(vg, data->fontBold, data->fontEmoji);
return 0;
}

View File

@ -8,7 +8,7 @@ extern "C" {
#endif
struct DemoData {
int fontNormal, fontBold, fontIcons,fontEmoji;
int fontNormal, fontBold, fontIcons, fontEmoji;
int images[12];
};
typedef struct DemoData DemoData;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 126 KiB

After

Width:  |  Height:  |  Size: 966 KiB

View File

@ -193,15 +193,17 @@ int fons__tt_buildGlyphBitmap(FONSttFontImpl *font, int glyph, float size, float
{
FT_Error ftError;
FT_GlyphSlot ftGlyph;
FT_Fixed advFixed;
FONS_NOTUSED(scale);
ftError = FT_Set_Pixel_Sizes(font->font, 0, (FT_UInt)(size * (float)font->font->units_per_EM / (float)(font->font->ascender - font->font->descender)));
if (ftError) return 0;
ftError = FT_Load_Glyph(font->font, glyph, FT_LOAD_RENDER);
if (ftError) return 0;
ftError = FT_Get_Advance(font->font, glyph, FT_LOAD_NO_SCALE, (FT_Fixed*)advance);
ftError = FT_Get_Advance(font->font, glyph, FT_LOAD_NO_SCALE, &advFixed);
if (ftError) return 0;
ftGlyph = font->font->glyph;
*advance = (int)advFixed;
*lsb = (int)ftGlyph->metrics.horiBearingX;
*x0 = ftGlyph->bitmap_left;
*x1 = *x0 + ftGlyph->bitmap.width;
@ -360,7 +362,6 @@ struct FONSglyph
};
typedef struct FONSglyph FONSglyph;
struct FONSfont
{
FONSttFontImpl font;
@ -761,11 +762,10 @@ static FONSstate* fons__getState(FONScontext* stash)
return &stash->states[stash->nstates-1];
}
int fonsAddFallbackFont(FONScontext* stash,int base,int fallback)
int fonsAddFallbackFont(FONScontext* stash, int base, int fallback)
{
FONSfont* baseFont = stash->fonts[base];
if (baseFont->nfallbacks < FONS_MAX_FALLBACKS)
{
if (baseFont->nfallbacks < FONS_MAX_FALLBACKS) {
baseFont->fallbacks[baseFont->nfallbacks++] = fallback;
return 1;
}
@ -1058,15 +1058,12 @@ static FONSglyph* fons__getGlyph(FONScontext* stash, FONSfont* font, unsigned in
// Could not find glyph, create it.
scale = fons__tt_getPixelHeightScale(&font->font, size);
g = fons__tt_getGlyphIndex(&font->font, codepoint);
//If tofu glyph fallback to other fonts
if(g == 0)
{
for (i=0; i<font->nfallbacks; ++i)
{
glyph = fons__getGlyph(stash,stash->fonts[font->fallbacks[i]],codepoint,isize,iblur);
if(glyph->index != 0)
{
return glyph;
// Try to find the glyph in fallback fonts.
if (g == 0) {
for (i = 0; i < font->nfallbacks; ++i) {
FONSglyph* fallbackGlyph = fons__getGlyph(stash, stash->fonts[font->fallbacks[i]], codepoint, isize, iblur);
if (fallbackGlyph != NULL && fallbackGlyph->index != 0) {
return fallbackGlyph;
}
}
}

View File

@ -2268,18 +2268,15 @@ int nvgFindFont(NVGcontext* ctx, const char* name)
}
int nvgAddFallbackFontId(NVGcontext* ctx,int baseFont,int fallbackFont)
int nvgAddFallbackFontId(NVGcontext* ctx, int baseFont, int fallbackFont)
{
if(baseFont == -1 || fallbackFont == -1)
{
return 0;
}
return fonsAddFallbackFont(ctx->fs, baseFont,fallbackFont);
if(baseFont == -1 || fallbackFont == -1) return 0;
return fonsAddFallbackFont(ctx->fs, baseFont, fallbackFont);
}
int nvgAddFallbackFont(NVGcontext* ctx,const char* baseFont,const char* fallbackFont)
int nvgAddFallbackFont(NVGcontext* ctx, const char* baseFont, const char* fallbackFont)
{
return nvgAddFallbackFontId(ctx,nvgFindFont(ctx,baseFont),nvgFindFont(ctx,fallbackFont));
return nvgAddFallbackFontId(ctx, nvgFindFont(ctx, baseFont), nvgFindFont(ctx, fallbackFont));
}
// State setting

View File

@ -546,11 +546,11 @@ int nvgCreateFontMem(NVGcontext* ctx, const char* name, unsigned char* data, int
// Finds a loaded font of specified name, and returns handle to it, or -1 if the font is not found.
int nvgFindFont(NVGcontext* ctx, const char* name);
// add fallback font.
int nvgAddFallbackFontId(NVGcontext* ctx,int baseFont,int fallbackFont);
// Adds a fallback font by handle.
int nvgAddFallbackFontId(NVGcontext* ctx, int baseFont, int fallbackFont);
// add fallback font by name.
int nvgAddFallbackFont(NVGcontext* ctx,const char* baseFont,const char* fallbackFont);
// Adds a fallback font by name.
int nvgAddFallbackFont(NVGcontext* ctx, const char* baseFont, const char* fallbackFont);
// Sets the font size of current text style.
void nvgFontSize(NVGcontext* ctx, float size);