[Bf-blender-cvs] [835dd950467] master: BLF: Cleanup blf_glyph_cache_find & blf_font_size

Harley Acheson noreply at git.blender.org
Sat Feb 5 03:33:22 CET 2022


Commit: 835dd950467fe5215911a81485393fb6ce3a03cd
Author: Harley Acheson
Date:   Fri Feb 4 18:32:32 2022 -0800
Branches: master
https://developer.blender.org/rB835dd950467fe5215911a81485393fb6ce3a03cd

BLF: Cleanup blf_glyph_cache_find & blf_font_size

Removes unnecessary calls to blf_glyph_cache_find, simplifies
blf_font_size, and reduces calls to it. blf_glyph_cache_new
and blf_glyph_cache_find made static.

See D13374 for more details.

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

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.h
M	source/blender/blenfont/intern/blf_thumbs.c
M	source/blender/editors/interface/interface_style.c

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

diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c
index da533820d72..c1410447de6 100644
--- a/source/blender/blenfont/intern/blf_font.c
+++ b/source/blender/blenfont/intern/blf_font.c
@@ -1342,35 +1342,25 @@ void blf_font_free(FontBLF *font)
 /** \name Font Configure
  * \{ */
 
-void blf_font_size(FontBLF *font, float size, unsigned int dpi)
+bool blf_font_size(FontBLF *font, float size, unsigned int dpi)
 {
-  blf_glyph_cache_acquire(font);
-
   /* FreeType uses fixed-point integers in 64ths. */
   FT_F26Dot6 ft_size = lroundf(size * 64.0f);
-  /* Adjust our size to be on even 64ths. */
+  /* Adjust our new size to be on even 64ths. */
   size = (float)ft_size / 64.0f;
 
-  GlyphCacheBLF *gc = blf_glyph_cache_find(font, size, dpi);
-  if (gc && (font->size == size && font->dpi == dpi)) {
-    /* Optimization: do not call FT_Set_Char_Size if size did not change. */
-  }
-  else {
-    const FT_Error err = FT_Set_Char_Size(font->face, 0, ft_size, dpi, dpi);
-    if (err) {
-      /* FIXME: here we can go through the fixed size and choice a close one */
-      printf("The current font don't support the size, %f and dpi, %u\n", size, dpi);
-    }
-    else {
+  if (font->size != size || font->dpi != dpi) {
+    if (FT_Set_Char_Size(font->face, 0, ft_size, dpi, dpi) == 0) {
       font->size = size;
       font->dpi = dpi;
-      if (gc == NULL) {
-        blf_glyph_cache_new(font);
-      }
+    }
+    else {
+      printf("The current font does not support the size, %f and dpi, %u\n", size, dpi);
+      return false;
     }
   }
 
-  blf_glyph_cache_release(font);
+  return true;
 }
 
 /** \} */
diff --git a/source/blender/blenfont/intern/blf_glyph.c b/source/blender/blenfont/intern/blf_glyph.c
index c8cc15e804b..bcd9e1fb3a2 100644
--- a/source/blender/blenfont/intern/blf_glyph.c
+++ b/source/blender/blenfont/intern/blf_glyph.c
@@ -74,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) {
@@ -87,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");
 
diff --git a/source/blender/blenfont/intern/blf_internal.h b/source/blender/blenfont/intern/blf_internal.h
index 1a80be0503c..d0bb3385e2c 100644
--- a/source/blender/blenfont/intern/blf_internal.h
+++ b/source/blender/blenfont/intern/blf_internal.h
@@ -56,7 +56,11 @@ struct FontBLF *blf_font_new(const char *name, const char *filename);
 struct FontBLF *blf_font_new_from_mem(const char *name, const unsigned char *mem, int mem_size);
 void blf_font_attach_from_mem(struct FontBLF *font, const unsigned char *mem, int mem_size);
 
-void blf_font_size(struct FontBLF *font, float size, unsigned int dpi);
+/**
+ * Change font's output size. Returns true if successful in changing the size.
+ */
+bool blf_font_size(struct FontBLF *font, float size, unsigned int dpi);
+
 void blf_font_draw(struct FontBLF *font,
                    const char *str,
                    size_t str_len,
@@ -134,14 +138,6 @@ int blf_font_count_missing_chars(struct FontBLF *font,
 
 void blf_font_free(struct FontBLF *font);
 
-/**
- * Find a glyph cache that matches a size, DPI & styles.
- */
-struct GlyphCacheBLF *blf_glyph_cache_find(struct FontBLF *font, float size, unsigned int dpi);
-/**
- * Create a new glyph cache for the current size, DPI & styles.
- */
-struct GlyphCacheBLF *blf_glyph_cache_new(struct FontBLF *font);
 struct GlyphCacheBLF *blf_glyph_cache_acquire(struct FontBLF *font);
 void blf_glyph_cache_release(struct FontBLF *font);
 void blf_glyph_cache_clear(struct FontBLF *font);
diff --git a/source/blender/blenfont/intern/blf_thumbs.c b/source/blender/blenfont/intern/blf_thumbs.c
index 06bbd0cf521..f666976547e 100644
--- a/source/blender/blenfont/intern/blf_thumbs.c
+++ b/source/blender/blenfont/intern/blf_thumbs.c
@@ -61,7 +61,6 @@ void BLF_thumb_preview(const char *filename,
   int font_shrink = 4;
 
   FontBLF *font;
-  GlyphCacheBLF *gc;
 
   /* Create a new blender font obj and fill it with default values */
   font = blf_font_new("thumb_font", filename);
@@ -90,10 +89,8 @@ void BLF_thumb_preview(const char *filename,
     const size_t draw_str_i18n_len = strlen(draw_str_i18n);
     int draw_str_i18n_nbr = 0;
 
-    blf_font_size(font, (float)MAX2(font_size_min, font_size_curr), dpi);
-    gc = blf_glyph_cache_find(font, font->size, font->dpi);
-    /* There will be no matching glyph cache if blf_font_size() failed to set font size. */
-    if (!gc) {
+    CLAMP_MIN(font_size_curr, font_size_min);
+    if (!blf_font_size(font, (float)font_size_curr, dpi)) {
       break;
     }
 
diff --git a/source/blender/editors/interface/interface_style.c b/source/blender/editors/interface/interface_style.c
index 1fe538eca5d..bf3fa6e62d4 100644
--- a/source/blender/editors/interface/interface_style.c
+++ b/source/blender/editors/interface/interface_style.c
@@ -453,15 +453,6 @@ void uiStyleInit(void)
         printf("%s: error, no fonts available\n", __func__);
       }
     }
-    else {
-      /* ? just for speed to initialize?
-       * Yes, this build the glyph cache and create
-       * the texture.
-       */
-      BLF_size(font->blf_id, 11.0f * U.pixelsize, U.dpi);
-      BLF_size(font->blf_id, 12.0f * U.pixelsize, U.dpi);
-      BLF_size(font->blf_id, 14.0f * U.pixelsize, U.dpi);
-    }
   }
 
   if (style == NULL) {
@@ -485,8 +476,6 @@ void uiStyleInit(void)
     blf_mono_font = BLF_load_mono_default(unique);
   }
 
-  BLF_size(blf_mono_font, 12.0f * U.pixelsize, 72);
-
   /* Set default flags based on UI preferences (not render fonts) */
   {
     const int flag_disable = (BLF_MONOCHROME | BLF_HINTING_NONE | BLF_HINTING_SLIGHT |
@@ -529,8 +518,6 @@ void uiStyleInit(void)
     const bool unique = true;
     blf_mono_font_render = BLF_load_mono_default(unique);
   }
-
-  BLF_size(blf_mono_font_render, 12.0f * U.pixelsize, 72);
 }
 
 void UI_fontstyle_set(const uiFontStyle *fs)



More information about the Bf-blender-cvs mailing list