[Bf-blender-cvs] [8b3e3c18100] master: Fix T100242: Handle Flushed FT Sizes

Harley Acheson noreply at git.blender.org
Tue Aug 9 02:24:15 CEST 2022


Commit: 8b3e3c18100aac6ac956e782dc108aae500eac93
Author: Harley Acheson
Date:   Mon Aug 8 17:23:25 2022 -0700
Branches: master
https://developer.blender.org/rB8b3e3c18100aac6ac956e782dc108aae500eac93

Fix T100242: Handle Flushed FT Sizes

Properly deal with FreeType cache flushing a font's ft_size. Set this
to NULL in finalizer, and add a blf_ensure_size to make sure it
exists only when needed.

See D15639 for more details.

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

Reviewed by Brecht Van Lommel

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

M	source/blender/blenfont/intern/blf_font.c
M	source/blender/blenfont/intern/blf_glyph.c
M	source/blender/blenfont/intern/blf_internal.h

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

diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c
index 17145bdbe99..10e65cd6827 100644
--- a/source/blender/blenfont/intern/blf_font.c
+++ b/source/blender/blenfont/intern/blf_font.c
@@ -109,6 +109,14 @@ static FT_Error blf_cache_face_requester(FTC_FaceID faceID,
   return err;
 }
 
+/* Called when FreeType is removing a font size. */
+static void blf_size_finalizer(void *object)
+{
+  FT_Size size = object;
+  FontBLF *font = (FontBLF *)size->generic.data;
+  font->ft_size = NULL;
+}
+
 /* Use cache, not blf_get_char_index, to return glyph id from charcode. */
 uint blf_get_char_index(struct FontBLF *font, uint charcode)
 {
@@ -122,6 +130,9 @@ uint blf_get_char_index(struct FontBLF *font, uint charcode)
 /* Convert a FreeType 26.6 value representing an unscaled design size to fractional pixels. */
 static ft_pix blf_unscaled_F26Dot6_to_pixels(FontBLF *font, FT_Pos value)
 {
+  /* Make sure we have a valid font->ft_size. */
+  blf_ensure_size(font);
+
   /* Scale value by font size using integer-optimized multiplication. */
   FT_Long scaled = FT_MulFix(value, font->ft_size->metrics.x_scale);
 
@@ -1157,6 +1168,7 @@ int blf_font_count_missing_chars(FontBLF *font,
 
 static ft_pix blf_font_height_max_ft_pix(FontBLF *font)
 {
+  blf_ensure_size(font);
   /* Metrics.height is rounded to pixel. Force minimum of one pixel. */
   return MAX2((ft_pix)font->ft_size->metrics.height, ft_pix_from_int(1));
 }
@@ -1168,6 +1180,7 @@ int blf_font_height_max(FontBLF *font)
 
 static ft_pix blf_font_width_max_ft_pix(FontBLF *font)
 {
+  blf_ensure_size(font);
   /* Metrics.max_advance is rounded to pixel. Force minimum of one pixel. */
   return MAX2((ft_pix)font->ft_size->metrics.max_advance, ft_pix_from_int(1));
 }
@@ -1179,11 +1192,13 @@ int blf_font_width_max(FontBLF *font)
 
 int blf_font_descender(FontBLF *font)
 {
+  blf_ensure_size(font);
   return ft_pix_to_int((ft_pix)font->ft_size->metrics.descender);
 }
 
 int blf_font_ascender(FontBLF *font)
 {
+  blf_ensure_size(font);
   return ft_pix_to_int((ft_pix)font->ft_size->metrics.ascender);
 }
 
@@ -1519,35 +1534,44 @@ void blf_font_free(FontBLF *font)
 /** \name Font Configure
  * \{ */
 
-bool blf_font_size(FontBLF *font, float size, unsigned int dpi)
+bool blf_ensure_size(FontBLF *font)
 {
-  if (!blf_ensure_face(font)) {
-    return false;
+  if (font->ft_size) {
+    return true;
   }
 
-  /* FreeType uses fixed-point integers in 64ths. */
-  FT_UInt ft_size = round_fl_to_uint(size * 64.0f);
-  /* Adjust our new size to be on even 64ths. */
-  size = (float)ft_size / 64.0f;
-
   FTC_ScalerRec scaler = {0};
   scaler.face_id = font;
   scaler.width = 0;
-  scaler.height = ft_size;
+  scaler.height = round_fl_to_uint(font->size * 64.0f);
   scaler.pixel = 0;
-  scaler.x_res = dpi;
-  scaler.y_res = dpi;
+  scaler.x_res = font->dpi;
+  scaler.y_res = font->dpi;
 
-  if (FTC_Manager_LookupSize(ftc_manager, &scaler, &font->ft_size) != FT_Err_Ok) {
-    printf("The current font don't support the size, %f and dpi, %u\n", size, dpi);
+  if (FTC_Manager_LookupSize(ftc_manager, &scaler, &font->ft_size) == FT_Err_Ok) {
+    font->ft_size->generic.data = (void *)font;
+    font->ft_size->generic.finalizer = blf_size_finalizer;
+    return true;
+  }
+
+  BLI_assert_unreachable();
+  return false;
+}
+
+bool blf_font_size(FontBLF *font, float size, unsigned int dpi)
+{
+  if (!blf_ensure_face(font)) {
     return false;
   }
 
-  font->size = size;
+  /* FreeType uses fixed-point integers in 64ths. */
+  FT_UInt ft_size = round_fl_to_uint(size * 64.0f);
+  /* Adjust our new size to be on even 64ths. */
+  font->size = (float)ft_size / 64.0f;
   font->dpi = dpi;
-  font->ft_size->generic.data = (void *)font;
+  font->ft_size = NULL;
 
-  return true;
+  return blf_ensure_size(font);
 }
 
 /** \} */
diff --git a/source/blender/blenfont/intern/blf_glyph.c b/source/blender/blenfont/intern/blf_glyph.c
index 780b75c6296..3b73224152f 100644
--- a/source/blender/blenfont/intern/blf_glyph.c
+++ b/source/blender/blenfont/intern/blf_glyph.c
@@ -103,6 +103,7 @@ static GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font)
   }
   else {
     /* Font does not have a face or does not contain "0" so use CSS fallback of 1/2 of em. */
+    blf_ensure_size(font);
     gc->fixed_width = (int)((font->ft_size->metrics.height / 2) >> 6);
   }
   if (gc->fixed_width < 1) {
@@ -900,6 +901,8 @@ static FT_GlyphSlot blf_glyph_render(FontBLF *settings_font,
     blf_font_size(glyph_font, settings_font->size, settings_font->dpi);
   }
 
+  blf_ensure_size(glyph_font);
+
   /* We need to keep track if changes are still needed. */
   bool weight_done = false;
   bool slant_done = false;
diff --git a/source/blender/blenfont/intern/blf_internal.h b/source/blender/blenfont/intern/blf_internal.h
index 8ff00d05e02..b7dea9d5ef5 100644
--- a/source/blender/blenfont/intern/blf_internal.h
+++ b/source/blender/blenfont/intern/blf_internal.h
@@ -49,6 +49,7 @@ bool blf_font_id_is_valid(int fontid);
 uint blf_get_char_index(struct FontBLF *font, uint charcode);
 
 bool blf_ensure_face(struct FontBLF *font);
+bool blf_ensure_size(struct FontBLF *font);
 
 void blf_draw_buffer__start(struct FontBLF *font);
 void blf_draw_buffer__end(void);



More information about the Bf-blender-cvs mailing list