Small footprint UI library for hardware accelerated games & applications
修订版 | 8a55432524e095dd7d457a3418c57ac0dc319861 (tree) |
---|---|
时间 | 2016-12-22 05:29:38 |
作者 | Emil Segerås <emilsegers@gmai...> |
Commiter | Emil Segerås |
Fix for STB glyph positioning, font sizes, and optimization.
tbtt_GetGlyphBitmapBoxSubpixel scale_x was 0, resulting in incorrect x
offset for glyphs.
Use calls taking glyph index instead of cp to reduce glyph lookups.
Removed magic constant since it only worked for Vera in 14px. The real
issue is that FT remaps font height (depending on table in font) while
STB don't.
@@ -878,13 +878,20 @@ void DemoApplication::OnBackendAttached(AppBackend *backend, int width, int heig | ||
878 | 878 | #endif |
879 | 879 | |
880 | 880 | // Set the default font description for widgets to one of the fonts we just added |
881 | + // STB renders Lato smaller than FreeType since it ignores the size table in fonts. | |
882 | + // To make demo look similar with all font engines, we use slightly different sizes. | |
883 | + // This should match when running in 96dp, but will likely diverge for other DPIs (and fonts). | |
881 | 884 | TBFontDescription fd; |
882 | -#if defined(TB_FONT_RENDERER_STB) || defined(TB_FONT_RENDERER_FREETYPE) | |
885 | +#if defined(TB_FONT_RENDERER_FREETYPE) | |
886 | + fd.SetID(TBIDC("Lato")); | |
887 | + fd.SetSize(g_tb_skin->GetDimensionConverter()->DpToPx(15)); | |
888 | +#elif defined(TB_FONT_RENDERER_STB) | |
883 | 889 | fd.SetID(TBIDC("Lato")); |
890 | + fd.SetSize(g_tb_skin->GetDimensionConverter()->DpToPx(18)); | |
884 | 891 | #else |
885 | 892 | fd.SetID(TBIDC("Segoe")); |
886 | -#endif | |
887 | 893 | fd.SetSize(g_tb_skin->GetDimensionConverter()->DpToPx(14)); |
894 | +#endif | |
888 | 895 | g_font_manager->SetDefaultFontDescription(fd); |
889 | 896 | |
890 | 897 | // Create the font now. |
@@ -36,7 +36,6 @@ private: | ||
36 | 36 | stbtt_fontinfo font; |
37 | 37 | TBTempBuffer ttf_buffer; |
38 | 38 | unsigned char *render_data; |
39 | - int font_size; | |
40 | 39 | float scale; |
41 | 40 | }; |
42 | 41 |
@@ -74,10 +73,12 @@ bool STBFontRenderer::RenderGlyph(TBFontGlyphData *data, UCS4 cp) | ||
74 | 73 | void STBFontRenderer::GetGlyphMetrics(TBGlyphMetrics *metrics, UCS4 cp) |
75 | 74 | { |
76 | 75 | int advance, leftSideBearing; |
77 | - stbtt_GetCodepointHMetrics(&font, cp, &advance, &leftSideBearing); | |
76 | + const int gi = stbtt_FindGlyphIndex(&font, cp); | |
77 | + stbtt_GetGlyphHMetrics(&font, gi, &advance, &leftSideBearing); | |
78 | 78 | metrics->advance = (int) roundf(advance * scale); |
79 | - int ix0, iy0, ix1, iy1; | |
80 | - stbtt_GetCodepointBitmapBox(&font, cp, 0, scale, &ix0, &iy0, &ix1, &iy1); | |
79 | + | |
80 | + int ix0, iy0; | |
81 | + stbtt_GetGlyphBitmapBoxSubpixel(&font, gi, scale, scale, 0.f, 0.f, &ix0, &iy0, 0, 0); | |
81 | 82 | metrics->x = ix0; |
82 | 83 | metrics->y = iy0; |
83 | 84 | } |
@@ -100,8 +101,7 @@ bool STBFontRenderer::Load(const char *filename, int size) | ||
100 | 101 | const unsigned char *ttf_ptr = (const unsigned char *) ttf_buffer.GetData(); |
101 | 102 | stbtt_InitFont(&font, ttf_ptr, stbtt_GetFontOffsetForIndex(ttf_ptr, 0)); |
102 | 103 | |
103 | - font_size = (int) (size * 1.3f); // FIX: Constant taken out of thin air because fonts get too small. | |
104 | - scale = stbtt_ScaleForPixelHeight(&font, (float)font_size); | |
104 | + scale = stbtt_ScaleForPixelHeight(&font, (float) size); | |
105 | 105 | return true; |
106 | 106 | } |
107 | 107 |