[Bf-blender-cvs] [bb3bcf744c9] master: BLF: Save fixed_width value in cache

Harley Acheson noreply at git.blender.org
Sat Feb 5 02:50:19 CET 2022


Commit: bb3bcf744c9c4d316ddbdcffcdf81c1367bab2b1
Author: Harley Acheson
Date:   Fri Feb 4 17:49:21 2022 -0800
Branches: master
https://developer.blender.org/rBbb3bcf744c9c4d316ddbdcffcdf81c1367bab2b1

BLF: Save fixed_width value in cache

Cache the font size's ideal fixed width column size in the glyph cache
rather than the font itself to improve performance.

See D13226 for more details.

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

Reviewed by Campbell Barton

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

M	source/blender/blenfont/intern/blf_font.c
M	source/blender/blenfont/intern/blf_glyph.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 c03cdc3d16d..da533820d72 100644
--- a/source/blender/blenfont/intern/blf_font.c
+++ b/source/blender/blenfont/intern/blf_font.c
@@ -34,7 +34,6 @@
 
 #include FT_FREETYPE_H
 #include FT_GLYPH_H
-#include FT_ADVANCES_H /* For FT_Get_Advance */
 
 #include "MEM_guardedalloc.h"
 
@@ -826,7 +825,10 @@ float blf_font_height(FontBLF *font,
 
 float blf_font_fixed_width(FontBLF *font)
 {
-  return (float)font->fixed_width;
+  GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
+  float width = (gc) ? (float)gc->fixed_width : font->size / 2.0f;
+  blf_glyph_cache_release(font);
+  return width;
 }
 
 static void blf_font_boundbox_foreach_glyph_ex(FontBLF *font,
@@ -1369,22 +1371,6 @@ void blf_font_size(FontBLF *font, float size, unsigned int dpi)
   }
 
   blf_glyph_cache_release(font);
-
-  /* Set 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. */
-    font->fixed_width = (int)(advance >> 16);
-  }
-  else {
-    /* Font does not contain "0" so use CSS fallback of 1/2 of em. */
-    font->fixed_width = (int)((font->face->size->metrics.height / 2) >> 6);
-  }
-  if (font->fixed_width < 1) {
-    font->fixed_width = 1;
-  }
 }
 
 /** \} */
diff --git a/source/blender/blenfont/intern/blf_glyph.c b/source/blender/blenfont/intern/blf_glyph.c
index b87a784adff..c8cc15e804b 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"
 
@@ -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;
 }
diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h
index 46156edbb1f..c96febd71ae 100644
--- a/source/blender/blenfont/intern/blf_internal_types.h
+++ b/source/blender/blenfont/intern/blf_internal_types.h
@@ -73,6 +73,9 @@ typedef struct GlyphCacheBLF {
   bool bold;
   bool italic;
 
+  /* Column width when printing monospaced. */
+  int fixed_width;
+
   /* and the glyphs. */
   ListBase bucket[257];
 
@@ -207,9 +210,6 @@ typedef struct FontBLF {
   /* font size. */
   float size;
 
-  /* Column width when printing monospaced. */
-  int fixed_width;
-
   /* max texture size. */
   int tex_size_max;



More information about the Bf-blender-cvs mailing list