From f9691bae840a136ff0f55e8a99b42dd10f0fa8d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Sat, 31 Mar 2018 15:24:10 +0200 Subject: BLF: Perf: Add a kerning cache table for ascii chars. This adds less than a megabyte of mem usage. FT_Get_Kerning was the 2nd hotspot when profilling. This commit completly remove this cost. One concern though: I don't know if the kerning data is constant for every sizes but it seems to be the case. I tested different fonts at different dpi scalling and saw no differences. --- source/blender/blenfont/intern/blf_glyph.c | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'source/blender/blenfont/intern/blf_glyph.c') diff --git a/source/blender/blenfont/intern/blf_glyph.c b/source/blender/blenfont/intern/blf_glyph.c index d472825e940..f5a645af5e0 100644 --- a/source/blender/blenfont/intern/blf_glyph.c +++ b/source/blender/blenfont/intern/blf_glyph.c @@ -65,6 +65,61 @@ #include "BLI_strict_flags.h" #include "BLI_math_vector.h" +KerningCacheBLF *blf_kerning_cache_find(FontBLF *font) +{ + KerningCacheBLF *p; + + p = (KerningCacheBLF *)font->kerning_caches.first; + while (p) { + if (p->mode == font->kerning_mode) + return p; + p = p->next; + } + return NULL; +} + +/* Create a new glyph cache for the current kerning mode. */ +KerningCacheBLF *blf_kerning_cache_new(FontBLF *font) +{ + KerningCacheBLF *kc; + + kc = (KerningCacheBLF *)MEM_callocN(sizeof(KerningCacheBLF), "blf_kerning_cache_new"); + kc->next = NULL; + kc->prev = NULL; + kc->mode = font->kerning_mode; + + unsigned int i, j; + for (i = 0; i < 0x80; i++) { + for (j = 0; j < 0x80; j++) { + GlyphBLF *g = blf_glyph_search(font->glyph_cache, i); + if (!g) { + FT_UInt glyph_index = FT_Get_Char_Index(font->face, i); + g = blf_glyph_add(font, glyph_index, i); + } + /* Cannot fail since it has been added just before. */ + GlyphBLF *g_prev = blf_glyph_search(font->glyph_cache, j); + + FT_Vector delta = {.x = 0, .y = 0}; + if (FT_Get_Kerning(font->face, g_prev->idx, g->idx, kc->mode, + &delta) == 0) { + kc->table[i][j] = (int)delta.x >> 6; + } + else { + kc->table[i][j] = 0; + } + } + } + + BLI_addhead(&font->kerning_caches, kc); + return kc; +} + +void blf_kerning_cache_clear(FontBLF *font) +{ + font->kerning_cache = NULL; + BLI_freelistN(&font->kerning_caches); +} + GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, unsigned int size, unsigned int dpi) { GlyphCacheBLF *p; -- cgit v1.2.3