diff options
Diffstat (limited to 'source/blender/blenfont/intern/blf_glyph.c')
-rw-r--r-- | source/blender/blenfont/intern/blf_glyph.c | 49 |
1 files changed, 33 insertions, 16 deletions
diff --git a/source/blender/blenfont/intern/blf_glyph.c b/source/blender/blenfont/intern/blf_glyph.c index 4f25f99b65c..bcd9e1fb3a2 100644 --- a/source/blender/blenfont/intern/blf_glyph.c +++ b/source/blender/blenfont/intern/blf_glyph.c @@ -34,6 +34,7 @@ #include FT_GLYPH_H #include FT_OUTLINE_H #include FT_BITMAP_H +#include FT_ADVANCES_H /* For FT_Get_Advance. */ #include "MEM_guardedalloc.h" @@ -73,7 +74,7 @@ static FT_Fixed to_16dot16(double val) /** \name Glyph Cache * \{ */ -GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, float size, unsigned int dpi) +static GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, float size, unsigned int dpi) { GlyphCacheBLF *gc = (GlyphCacheBLF *)font->cache.first; while (gc) { @@ -86,7 +87,7 @@ GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, float size, unsigned int dpi) return NULL; } -GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font) +static GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font) { GlyphCacheBLF *gc = (GlyphCacheBLF *)MEM_callocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new"); @@ -100,6 +101,22 @@ GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font) memset(gc->glyph_ascii_table, 0, sizeof(gc->glyph_ascii_table)); memset(gc->bucket, 0, sizeof(gc->bucket)); + /* Determine ideal fixed-width size for monospaced output. */ + FT_UInt gindex = FT_Get_Char_Index(font->face, U'0'); + if (gindex) { + FT_Fixed advance = 0; + FT_Get_Advance(font->face, gindex, FT_LOAD_NO_HINTING, &advance); + /* Use CSS 'ch unit' width, advance of zero character. */ + gc->fixed_width = (int)(advance >> 16); + } + else { + /* Font does not contain "0" so use CSS fallback of 1/2 of em. */ + gc->fixed_width = (int)((font->face->size->metrics.height / 2) >> 6); + } + if (gc->fixed_width < 1) { + gc->fixed_width = 1; + } + BLI_addhead(&font->cache, gc); return gc; } @@ -122,20 +139,7 @@ void blf_glyph_cache_release(FontBLF *font) BLI_spin_unlock(font->glyph_cache_mutex); } -void blf_glyph_cache_clear(FontBLF *font) -{ - GlyphCacheBLF *gc; - - BLI_spin_lock(font->glyph_cache_mutex); - - while ((gc = BLI_pophead(&font->cache))) { - blf_glyph_cache_free(gc); - } - - BLI_spin_unlock(font->glyph_cache_mutex); -} - -void blf_glyph_cache_free(GlyphCacheBLF *gc) +static void blf_glyph_cache_free(GlyphCacheBLF *gc) { GlyphBLF *g; for (uint i = 0; i < ARRAY_SIZE(gc->bucket); i++) { @@ -152,6 +156,19 @@ void blf_glyph_cache_free(GlyphCacheBLF *gc) MEM_freeN(gc); } +void blf_glyph_cache_clear(FontBLF *font) +{ + GlyphCacheBLF *gc; + + BLI_spin_lock(font->glyph_cache_mutex); + + while ((gc = BLI_pophead(&font->cache))) { + blf_glyph_cache_free(gc); + } + + BLI_spin_unlock(font->glyph_cache_mutex); +} + /** * Try to find a glyph in cache. * |