[Bf-blender-cvs] [d5261e973b5] master: BLF: Do Not Preload Glyph Cache

Harley Acheson noreply at git.blender.org
Sat Aug 14 22:52:17 CEST 2021


Commit: d5261e973b5624b40b80771903e9d2a0c9ff37e4
Author: Harley Acheson
Date:   Sat Aug 14 13:50:51 2021 -0700
Branches: master
https://developer.blender.org/rBd5261e973b5624b40b80771903e9d2a0c9ff37e4

BLF: Do Not Preload Glyph Cache

This patch turns off the preloading of ascii glyphs and instead caches
each glyph the first time it is actually used.

See D12215 for much more detail.

Differential Revision: https://developer.blender.org/D12215

Reviewed by Campbell Barton

===================================================================

M	source/blender/blenfont/intern/blf_font.c
M	source/blender/blenfont/intern/blf_internal_types.h

===================================================================

diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c
index 0dfb2e843c9..28039ff11ac 100644
--- a/source/blender/blenfont/intern/blf_font.c
+++ b/source/blender/blenfont/intern/blf_font.c
@@ -308,29 +308,6 @@ void blf_font_size(FontBLF *font, unsigned int size, unsigned int dpi)
   blf_glyph_cache_release(font);
 }
 
-static GlyphBLF **blf_font_ensure_ascii_table(FontBLF *font, GlyphCacheBLF *gc)
-{
-  GlyphBLF **glyph_ascii_table;
-
-  glyph_ascii_table = gc->glyph_ascii_table;
-
-  /* build ascii on demand */
-  if (glyph_ascii_table['0'] == NULL) {
-    GlyphBLF *g;
-    /* Skip control characters and just cache rendered glyphs for visible ASCII range. */
-    for (uint i = GLYPH_ASCII_CACHE_MIN; i <= GLYPH_ASCII_CACHE_MAX; i++) {
-      g = blf_glyph_search(gc, i);
-      if (!g) {
-        FT_UInt glyph_index = FT_Get_Char_Index(font->face, i);
-        g = blf_glyph_add(font, gc, glyph_index, i);
-      }
-      glyph_ascii_table[i] = g;
-    }
-  }
-
-  return glyph_ascii_table;
-}
-
 static void blf_font_ensure_ascii_kerning(FontBLF *font,
                                           GlyphCacheBLF *gc,
                                           const FT_UInt kern_mode)
@@ -352,11 +329,12 @@ static void blf_font_ensure_ascii_kerning(FontBLF *font,
  * characters.
  */
 
-/* NOTE: `blf_font_ensure_ascii_table(font, gc);` must be called before this macro. */
-
-#define BLF_UTF8_NEXT_FAST(_font, _gc, _g, _str, _i, _c, _glyph_ascii_table) \
+#define BLF_UTF8_NEXT_FAST(_font, _gc, _g, _str, _i, _c) \
   if (((_c) = (_str)[_i]) < GLYPH_ASCII_TABLE_SIZE) { \
-    _g = (_glyph_ascii_table)[_c]; \
+    if ((_g = (_gc->glyph_ascii_table)[_c]) == NULL) { \
+       _g = blf_glyph_add(_font, _gc, FT_Get_Char_Index((_font)->face, _c), _c); \
+       _gc->glyph_ascii_table[_c] = _g; \
+    } \
     _i++; \
   } \
   else if ((_c = BLI_str_utf8_as_unicode_step(_str, &(_i))) != BLI_UTF8_ERR) { \
@@ -420,8 +398,6 @@ static void blf_font_draw_ex(FontBLF *font,
     return;
   }
 
-  GlyphBLF **glyph_ascii_table = blf_font_ensure_ascii_table(font, gc);
-
   BLF_KERNING_VARS(font, has_kerning, kern_mode);
 
   blf_font_ensure_ascii_kerning(font, gc, kern_mode);
@@ -429,7 +405,7 @@ static void blf_font_draw_ex(FontBLF *font,
   blf_batch_draw_begin(font);
 
   while ((i < len) && str[i]) {
-    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c, glyph_ascii_table);
+    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c);
 
     if (UNLIKELY(c == BLI_UTF8_ERR)) {
       break;
@@ -472,7 +448,6 @@ static void blf_font_draw_ascii_ex(
   int pen_x = 0;
 
   GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
-  GlyphBLF **glyph_ascii_table = blf_font_ensure_ascii_table(font, gc);
 
   BLF_KERNING_VARS(font, has_kerning, kern_mode);
 
@@ -482,8 +457,11 @@ static void blf_font_draw_ascii_ex(
 
   while ((c = *(str++)) && len--) {
     BLI_assert(c < GLYPH_ASCII_TABLE_SIZE);
-    if ((g = glyph_ascii_table[c]) == NULL) {
-      continue;
+    if ((g = gc->glyph_ascii_table[c]) == NULL) {
+      g = blf_glyph_add(font, gc, FT_Get_Char_Index((font)->face, c), c);
+      if ((gc->glyph_ascii_table[c] = g) == NULL) {
+        continue;
+      }
     }
     if (has_kerning) {
       BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, pen_x);
@@ -522,12 +500,11 @@ int blf_font_draw_mono(FontBLF *font, const char *str, size_t len, int cwidth)
   size_t i = 0;
 
   GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
-  GlyphBLF **glyph_ascii_table = blf_font_ensure_ascii_table(font, gc);
 
   blf_batch_draw_begin(font);
 
   while ((i < len) && str[i]) {
-    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c, glyph_ascii_table);
+    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c);
 
     if (UNLIKELY(c == BLI_UTF8_ERR)) {
       break;
@@ -568,8 +545,6 @@ static void blf_font_draw_buffer_ex(FontBLF *font,
   int pen_y_basis = (int)font->pos[1] + pen_y;
   size_t i = 0;
 
-  GlyphBLF **glyph_ascii_table = blf_font_ensure_ascii_table(font, gc);
-
   /* buffer specific vars */
   FontBufInfoBLF *buf_info = &font->buf_info;
   const float *b_col_float = buf_info->col_float;
@@ -584,7 +559,7 @@ static void blf_font_draw_buffer_ex(FontBLF *font,
   /* another buffer specific call for color conversion */
 
   while ((i < len) && str[i]) {
-    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c, glyph_ascii_table);
+    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c);
 
     if (UNLIKELY(c == BLI_UTF8_ERR)) {
       break;
@@ -741,7 +716,6 @@ size_t blf_font_width_to_strlen(
   size_t i, i_prev;
 
   GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
-  GlyphBLF **glyph_ascii_table = blf_font_ensure_ascii_table(font, gc);
   const int width_i = (int)width;
 
   BLF_KERNING_VARS(font, has_kerning, kern_mode);
@@ -752,7 +726,7 @@ size_t blf_font_width_to_strlen(
 
   for (i_prev = i = 0, width_new = pen_x = 0, g_prev = NULL, c_prev = 0; (i < len) && str[i];
        i_prev = i, width_new = pen_x, c_prev = c, g_prev = g) {
-    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c, glyph_ascii_table);
+    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c);
 
     if (blf_font_width_to_strlen_glyph_process(
             font, has_kerning, kern_mode, c_prev, c, g_prev, g, &pen_x, width_i)) {
@@ -778,7 +752,6 @@ size_t blf_font_width_to_rstrlen(
   char *s, *s_prev;
 
   GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
-  GlyphBLF **glyph_ascii_table = blf_font_ensure_ascii_table(font, gc);
   const int width_i = (int)width;
 
   BLF_KERNING_VARS(font, has_kerning, kern_mode);
@@ -794,7 +767,7 @@ size_t blf_font_width_to_rstrlen(
   i_prev = (size_t)((s_prev != NULL) ? s_prev - str : 0);
 
   i_tmp = i;
-  BLF_UTF8_NEXT_FAST(font, gc, g, str, i_tmp, c, glyph_ascii_table);
+  BLF_UTF8_NEXT_FAST(font, gc, g, str, i_tmp, c);
   for (width_new = pen_x = 0; (s != NULL);
        i = i_prev, s = s_prev, c = c_prev, g = g_prev, g_prev = NULL, width_new = pen_x) {
     s_prev = BLI_str_find_prev_char_utf8(str, s);
@@ -802,7 +775,7 @@ size_t blf_font_width_to_rstrlen(
 
     if (s_prev != NULL) {
       i_tmp = i_prev;
-      BLF_UTF8_NEXT_FAST(font, gc, g_prev, str, i_tmp, c_prev, glyph_ascii_table);
+      BLF_UTF8_NEXT_FAST(font, gc, g_prev, str, i_tmp, c_prev);
       BLI_assert(i_tmp == i);
     }
 
@@ -832,9 +805,6 @@ static void blf_font_boundbox_ex(FontBLF *font,
   GlyphBLF *g, *g_prev = NULL;
   int pen_x = 0;
   size_t i = 0;
-
-  GlyphBLF **glyph_ascii_table = blf_font_ensure_ascii_table(font, gc);
-
   rctf gbox;
 
   BLF_KERNING_VARS(font, has_kerning, kern_mode);
@@ -847,7 +817,7 @@ static void blf_font_boundbox_ex(FontBLF *font,
   blf_font_ensure_ascii_kerning(font, gc, kern_mode);
 
   while ((i < len) && str[i]) {
-    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c, glyph_ascii_table);
+    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c);
 
     if (UNLIKELY(c == BLI_UTF8_ERR)) {
       break;
@@ -937,7 +907,6 @@ static void blf_font_wrap_apply(FontBLF *font,
   int pen_x_next = 0;
 
   GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
-  GlyphBLF **glyph_ascii_table = blf_font_ensure_ascii_table(font, gc);
 
   BLF_KERNING_VARS(font, has_kerning, kern_mode);
 
@@ -953,7 +922,7 @@ static void blf_font_wrap_apply(FontBLF *font,
     size_t i_curr = i;
     bool do_draw = false;
 
-    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c, glyph_ascii_table);
+    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c);
 
     if (UNLIKELY(c == BLI_UTF8_ERR)) {
       break;
@@ -1154,8 +1123,6 @@ float blf_font_fixed_width(FontBLF *font)
   const unsigned int c = ' ';
 
   GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
-  blf_font_ensure_ascii_table(font, gc);
-
   GlyphBLF *g = blf_glyph_search(gc, c);
   if (!g) {
     g = blf_glyph_add(font, gc, FT_Get_Char_Index(font->face, c), c);
@@ -1195,15 +1162,13 @@ static void blf_font_boundbox_foreach_glyph_ex(FontBLF *font,
     return;
   }
 
-  GlyphBLF **glyph_ascii_table = blf_font_ensure_ascii_table(font, gc);
-
   BLF_KERNING_VARS(font, has_kerning, kern_mode);
 
   blf_font_ensure_ascii_kerning(font, gc, kern_mode);
 
   while ((i < len) && str[i]) {
     i_curr = i;
-    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c, glyph_ascii_table);
+    BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c);
 
     if (UNLIKELY(c == BLI_UTF8_ERR)) {
       break;
@@ -1429,7 +1394,6 @@ int blf_font_height_max(FontBLF *font)
   int height_max;
 
   GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
-  blf_font_ensure_ascii_table(font, gc);
   height_max = gc->glyph_height_max;
 
   blf_glyph_cache_release(font);
@@ -1441,7 +1405,6 @@ int blf_font_width_max(FontBLF *font)
   int width_max;
 
   GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
-  blf_font_ensure_ascii_table(font, gc);
   width_max = gc->glyph_width_max;
 
   blf_glyph_cache_release(font);
@@ -1453,7 +1416,6 @@ float blf_font_descender(FontBLF *font)
   float descender;
 
   GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
-  blf_font_ensure_ascii_table(font, gc);
   descender = gc->descender;
 
   blf_glyph_cache_release(font);
@@ -1465,7 +1427,6 @@ float blf_font_ascender(FontBLF *font)
   float ascender;
 
   GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
-  blf_font_ensure_ascii_table(font, gc);
   ascender = gc->ascender;
 
   blf_glyph_cache_release(font);
diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h
index 1cd004ce77f..6bcacc62a30 100644
--- a/source/blender/blenfont/intern/blf_internal_types.h
+++ b/source/blender/blenfont/intern/blf_internal_types.h
@@ -31,10 +31,6 @@
 /* Number of characters in GlyphCacheBLF.glyph_ascii_table. */
 #define GLYPH_ASCII_TABLE_SIZE 128
 
-/* First and last characters (inclusive) of range to cache within GLYPH_ASCII_TABLE_SIZE. */
-#define GLYPH_ASCII_CACHE_MIN 32  /* Space */
-#define GLYPH_ASCII_CACHE_MAX 126 /* Tilde */
-
 /* Number of characters in KerningCacheBLF.table. */
 #define KERNING_CACHE_TABLE_SIZE 128



More information about the Bf-blender-cvs mailing list