[Bf-blender-cvs] [cd1631b17dd] master: BLF: Refactor of DPI

Harley Acheson noreply at git.blender.org
Sat Sep 24 02:38:14 CEST 2022


Commit: cd1631b17dd0e25a8a398fb00a982ca5f0633558
Author: Harley Acheson
Date:   Fri Sep 23 17:36:49 2022 -0700
Branches: master
https://developer.blender.org/rBcd1631b17dd0e25a8a398fb00a982ca5f0633558

BLF: Refactor of DPI

Correction of U.dpi to hold actual monitor DPI. Simplify font sizing by
omitting DPI as API argument, always using 72 internally.

See D15961 for more details.

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

Reviewed by Campbell Barton

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

M	source/blender/blenfont/BLF_api.h
M	source/blender/blenfont/intern/blf.c
M	source/blender/blenfont/intern/blf_default.c
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_internal_types.h
M	source/blender/blenfont/intern/blf_thumbs.c
M	source/blender/blenkernel/intern/image.cc
M	source/blender/blenkernel/intern/image_gen.c
M	source/blender/draw/intern/draw_manager_text.c
M	source/blender/editors/interface/interface_icons_event.c
M	source/blender/editors/interface/interface_panel.cc
M	source/blender/editors/interface/interface_region_tooltip.cc
M	source/blender/editors/interface/interface_style.cc
M	source/blender/editors/mesh/editmesh_bevel.c
M	source/blender/editors/mesh/editmesh_knife.c
M	source/blender/editors/object/object_remesh.cc
M	source/blender/editors/screen/area.c
M	source/blender/editors/space_clip/clip_dopesheet_draw.c
M	source/blender/editors/space_clip/clip_draw.c
M	source/blender/editors/space_image/image_draw.c
M	source/blender/editors/space_info/textview.c
M	source/blender/editors/space_node/node_draw.cc
M	source/blender/editors/space_spreadsheet/space_spreadsheet.cc
M	source/blender/editors/space_text/text_draw.c
M	source/blender/editors/space_view3d/view3d_gizmo_navigate_type.c
M	source/blender/editors/space_view3d/view3d_gizmo_ruler.c
M	source/blender/editors/util/ed_draw.c
M	source/blender/python/generic/blf_py_api.c
M	source/blender/sequencer/intern/effects.c
M	source/blender/windowmanager/intern/wm_operators.c
M	source/blender/windowmanager/intern/wm_playanim.c
M	source/blender/windowmanager/intern/wm_window.c

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

diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h
index d3226a8f609..992107a30e9 100644
--- a/source/blender/blenfont/BLF_api.h
+++ b/source/blender/blenfont/BLF_api.h
@@ -69,7 +69,7 @@ void BLF_metrics_attach(int fontid, unsigned char *mem, int mem_size);
 
 void BLF_aspect(int fontid, float x, float y, float z);
 void BLF_position(int fontid, float x, float y, float z);
-void BLF_size(int fontid, float size, int dpi);
+void BLF_size(int fontid, float size);
 
 /* Goal: small but useful color API. */
 
@@ -303,7 +303,6 @@ void BLF_thumb_preview(const char *filepath,
 
 /* blf_default.c */
 
-void BLF_default_dpi(int dpi);
 void BLF_default_size(float size);
 void BLF_default_set(int fontid);
 /**
diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c
index 9d9cc51ebcc..4e904768f79 100644
--- a/source/blender/blenfont/intern/blf.c
+++ b/source/blender/blenfont/intern/blf.c
@@ -63,8 +63,6 @@ int BLF_init(void)
     global_font[i] = NULL;
   }
 
-  BLF_default_dpi(72);
-
   return blf_font_init();
 }
 
@@ -361,12 +359,12 @@ void BLF_position(int fontid, float x, float y, float z)
   }
 }
 
-void BLF_size(int fontid, float size, int dpi)
+void BLF_size(int fontid, float size)
 {
   FontBLF *font = blf_get(fontid);
 
   if (font) {
-    blf_font_size(font, size, dpi);
+    blf_font_size(font, size);
   }
 }
 
@@ -912,7 +910,6 @@ void BLF_state_print(int fontid)
     printf("fontid %d %p\n", fontid, (void *)font);
     printf("  name:    '%s'\n", font->name);
     printf("  size:     %f\n", font->size);
-    printf("  dpi:      %u\n", font->dpi);
     printf("  pos:      %d %d %d\n", UNPACK3(font->pos));
     printf("  aspect:   (%d) %.6f %.6f %.6f\n",
            (font->flags & BLF_ROTATION) != 0,
diff --git a/source/blender/blenfont/intern/blf_default.c b/source/blender/blenfont/intern/blf_default.c
index a1f1b84636f..a521d65fe30 100644
--- a/source/blender/blenfont/intern/blf_default.c
+++ b/source/blender/blenfont/intern/blf_default.c
@@ -20,15 +20,9 @@
 
 /* Default size and dpi, for BLF_draw_default. */
 static int global_font_default = -1;
-static int global_font_dpi = 72;
 /* Keep in sync with `UI_DEFAULT_TEXT_POINTS` */
 static float global_font_size = 11.0f;
 
-void BLF_default_dpi(int dpi)
-{
-  global_font_dpi = dpi;
-}
-
 void BLF_default_size(float size)
 {
   global_font_size = size;
@@ -51,7 +45,7 @@ int BLF_set_default(void)
 {
   ASSERT_DEFAULT_SET;
 
-  BLF_size(global_font_default, global_font_size, global_font_dpi);
+  BLF_size(global_font_default, global_font_size);
 
   return global_font_default;
 }
@@ -59,7 +53,7 @@ int BLF_set_default(void)
 void BLF_draw_default(float x, float y, float z, const char *str, const size_t str_len)
 {
   ASSERT_DEFAULT_SET;
-  BLF_size(global_font_default, global_font_size, global_font_dpi);
+  BLF_size(global_font_default, global_font_size * U.dpi_fac);
   BLF_position(global_font_default, x, y, z);
   BLF_draw(global_font_default, str, str_len);
 }
diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c
index fcded5a13cd..3d7e83bc22f 100644
--- a/source/blender/blenfont/intern/blf_font.c
+++ b/source/blender/blenfont/intern/blf_font.c
@@ -1300,7 +1300,6 @@ static void blf_font_fill(FontBLF *font)
   font->clip_rec.ymin = 0;
   font->clip_rec.ymax = 0;
   font->flags = 0;
-  font->dpi = 0;
   font->size = 0;
   BLI_listbase_clear(&font->cache);
   font->kerning_cache = NULL;
@@ -1613,8 +1612,8 @@ void blf_ensure_size(FontBLF *font)
   scaler.width = 0;
   scaler.height = round_fl_to_uint(font->size * 64.0f);
   scaler.pixel = 0;
-  scaler.x_res = font->dpi;
-  scaler.y_res = font->dpi;
+  scaler.x_res = BLF_DPI;
+  scaler.y_res = BLF_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;
@@ -1624,7 +1623,7 @@ void blf_ensure_size(FontBLF *font)
   BLI_assert_unreachable();
 }
 
-bool blf_font_size(FontBLF *font, float size, uint dpi)
+bool blf_font_size(FontBLF *font, float size)
 {
   if (!blf_ensure_face(font)) {
     return false;
@@ -1635,15 +1634,15 @@ bool blf_font_size(FontBLF *font, float size, uint dpi)
   /* Adjust our new size to be on even 64ths. */
   size = (float)ft_size / 64.0f;
 
-  if (font->size != size || font->dpi != dpi) {
+  if (font->size != size) {
     if (font->flags & BLF_CACHED) {
       FTC_ScalerRec scaler = {0};
       scaler.face_id = font;
       scaler.width = 0;
       scaler.height = ft_size;
       scaler.pixel = 0;
-      scaler.x_res = dpi;
-      scaler.y_res = dpi;
+      scaler.x_res = BLF_DPI;
+      scaler.y_res = BLF_DPI;
       if (FTC_Manager_LookupSize(ftc_manager, &scaler, &font->ft_size) != FT_Err_Ok) {
         return false;
       }
@@ -1651,7 +1650,7 @@ bool blf_font_size(FontBLF *font, float size, uint dpi)
       font->ft_size->generic.finalizer = blf_size_finalizer;
     }
     else {
-      if (FT_Set_Char_Size(font->face, 0, ft_size, dpi, dpi) != FT_Err_Ok) {
+      if (FT_Set_Char_Size(font->face, 0, ft_size, BLF_DPI, BLF_DPI) != FT_Err_Ok) {
         return false;
       }
       font->ft_size = font->face->size;
@@ -1659,7 +1658,6 @@ bool blf_font_size(FontBLF *font, float size, uint dpi)
   }
 
   font->size = size;
-  font->dpi = dpi;
   return true;
 }
 
diff --git a/source/blender/blenfont/intern/blf_glyph.c b/source/blender/blenfont/intern/blf_glyph.c
index c08d52307b7..4704199c8a1 100644
--- a/source/blender/blenfont/intern/blf_glyph.c
+++ b/source/blender/blenfont/intern/blf_glyph.c
@@ -63,11 +63,11 @@ static FT_Fixed to_16dot16(double val)
 /** \name Glyph Cache
  * \{ */
 
-static GlyphCacheBLF *blf_glyph_cache_find(const FontBLF *font, const float size, uint dpi)
+static GlyphCacheBLF *blf_glyph_cache_find(const FontBLF *font, const float size)
 {
   GlyphCacheBLF *gc = (GlyphCacheBLF *)font->cache.first;
   while (gc) {
-    if (gc->size == size && gc->dpi == dpi && (gc->bold == ((font->flags & BLF_BOLD) != 0)) &&
+    if (gc->size == size && (gc->bold == ((font->flags & BLF_BOLD) != 0)) &&
         (gc->italic == ((font->flags & BLF_ITALIC) != 0)) &&
         (gc->char_weight == font->char_weight) && (gc->char_slant == font->char_slant) &&
         (gc->char_width == font->char_width) && (gc->char_spacing == font->char_spacing)) {
@@ -85,7 +85,6 @@ static GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font)
   gc->next = NULL;
   gc->prev = NULL;
   gc->size = font->size;
-  gc->dpi = font->dpi;
   gc->bold = ((font->flags & BLF_BOLD) != 0);
   gc->italic = ((font->flags & BLF_ITALIC) != 0);
   gc->char_weight = font->char_weight;
@@ -122,7 +121,7 @@ GlyphCacheBLF *blf_glyph_cache_acquire(FontBLF *font)
 {
   BLI_mutex_lock(&font->glyph_cache_mutex);
 
-  GlyphCacheBLF *gc = blf_glyph_cache_find(font, font->size, font->dpi);
+  GlyphCacheBLF *gc = blf_glyph_cache_find(font, font->size);
 
   if (!gc) {
     gc = blf_glyph_cache_new(font);
@@ -967,7 +966,7 @@ static FT_GlyphSlot blf_glyph_render(FontBLF *settings_font,
                                      int fixed_width)
 {
   if (glyph_font != settings_font) {
-    blf_font_size(glyph_font, settings_font->size, settings_font->dpi);
+    blf_font_size(glyph_font, settings_font->size);
   }
 
   blf_ensure_size(glyph_font);
diff --git a/source/blender/blenfont/intern/blf_internal.h b/source/blender/blenfont/intern/blf_internal.h
index 39d3af22562..e1001cfc1ba 100644
--- a/source/blender/blenfont/intern/blf_internal.h
+++ b/source/blender/blenfont/intern/blf_internal.h
@@ -25,6 +25,13 @@ struct rcti;
 /* Maximum number of bytes to use for cached data nodes. 0 is default of 200,000. */
 #define BLF_CACHE_BYTES 400000
 
+/* We assume square pixels at a fixed DPI of 72, scaling only the size. Therefore
+ * font size = points = pixels, i.e. a size of 20 will result in a 20-pixel EM square.
+ * Although we could use the actual monitor DPI instead, we would then have to scale
+ * the size to cancel that out. Other libraries like Skia use this same fixed value.
+ */
+#define BLF_DPI 72
+
 extern struct FontBLF *global_font[BLF_MAX_FONT];
 
 void blf_batch_draw_begin(struct FontBLF *font);
@@ -70,7 +77,7 @@ void blf_font_attach_from_mem(struct FontBLF *font, const unsigned char *mem, si
 /**
  * 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);
+bool blf_font_size(struct FontBLF *font, float size);
 
 void blf_font_draw(struct FontBLF *font,
                    const char *str,
diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h
index d64bd9c5452..cc4be9f7f0e 100644
--- a/source/blender/blenfont/intern/blf_internal_types.h
+++ b/source/blender/blenfont/intern/blf_internal_types.h
@@ -142,9 +142,6 @@ typedef struct GlyphCacheBLF {
   /** Font size. */
   float size;
 
-  /** DPI. */
-  unsigned int dpi;
-
   float char_weight;
   float char_slant;
   float char_width;
@@ -300,9 +297,6 @@ typedef struct FontBLF {
   /** The width to wrap the text, see #BLF_WORD_WRAP. */
   int wrap_width;
 
-  /** Font DPI (default 72). */
-  unsigned int dpi;
-
   /** Font size. */
   float size;
 
diff --git a/source/blender/blenfont/intern/blf_thumbs.c b/source/blender/blenfont/intern/blf_thumbs.c
index 1670674ebba..cba4bb96f73 100644
--- a/source/blender/blenfont/intern/blf_thumbs.c
+++ b/source/blender/blenfont/intern/blf_thumbs.c
@@ -40,7 +40,6 @@ void BLF_thumb_preview(const char *filepath,
                        const int h,
                        const int channels)
 {
-  const uint dpi = 72;
   const int font_size_min = 6;
   int font_size_curr;
   /* shrink 1/th each line */
@@ -84,7 +83,7 @@ void BLF_thumb_preview(const char *filepath,
     int draw_str_i18_count = 0;
 
     CLAMP_MIN(font_size_curr, font_size_min);
-    if (!blf_font_size(font, (float)font_size_curr, dpi)) {
+    if (!blf_font_size(font, (float)font_size_curr)) {
       break;
     }
 
diff --git a/source/blender/blenkernel/intern/image.cc b/source/blender/blenkernel/intern/image.cc
index 2edc51f6329..3c

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list