[Bf-blender-cvs] [47e68537f83] master: Cleanup: organize blf_font.c functions using doxy-sections

Campbell Barton noreply at git.blender.org
Sat Aug 21 09:50:06 CEST 2021


Commit: 47e68537f83075dd78ed66aac3216643389b0e3a
Author: Campbell Barton
Date:   Sat Aug 21 17:41:40 2021 +1000
Branches: master
https://developer.blender.org/rB47e68537f83075dd78ed66aac3216643389b0e3a

Cleanup: organize blf_font.c functions using doxy-sections

Functions in this file were scattered and not well organized.

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

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

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

diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c
index 436c4712e25..47fa3544474 100644
--- a/source/blender/blenfont/intern/blf_font.c
+++ b/source/blender/blenfont/intern/blf_font.c
@@ -71,6 +71,38 @@ static FT_Library ft_lib;
 static SpinLock ft_lib_mutex;
 static SpinLock blf_glyph_cache_mutex;
 
+/* -------------------------------------------------------------------- */
+/** \name FreeType Utilities (Internal)
+ * \{ */
+
+/**
+ * Convert a FreeType 26.6 value representing an unscaled design size to pixels.
+ * This is an exact copy of the scaling done inside FT_Get_Kerning when called
+ * with #FT_KERNING_DEFAULT, including arbitrary resizing for small fonts.
+ */
+static int blf_unscaled_F26Dot6_to_pixels(FontBLF *font, FT_Pos value)
+{
+  /* Scale value by font size using integer-optimized multiplication. */
+  FT_Long scaled = FT_MulFix(value, font->face->size->metrics.x_scale);
+
+  /* FreeType states that this '25' has been determined heuristically. */
+  if (font->face->size->metrics.x_ppem < 25) {
+    scaled = FT_MulDiv(scaled, font->face->size->metrics.x_ppem, 25);
+  }
+
+  /* Copies of internal FreeType macros needed here. */
+#define FT_PIX_FLOOR(x) ((x) & ~63)
+#define FT_PIX_ROUND(x) FT_PIX_FLOOR((x) + 32)
+
+  /* Round to even 64ths, then divide by 64. */
+  return (int)FT_PIX_ROUND(scaled) >> 6;
+
+#undef FT_PIX_FLOOR
+#undef FT_PIX_ROUND
+}
+
+/** \} */
+
 /* -------------------------------------------------------------------- */
 /** \name Glyph Batching
  * \{ */
@@ -257,56 +289,8 @@ static void blf_batch_draw_end(void)
 /** \} */
 
 /* -------------------------------------------------------------------- */
-
-int blf_font_init(void)
-{
-  memset(&g_batch, 0, sizeof(g_batch));
-  BLI_spin_init(&ft_lib_mutex);
-  BLI_spin_init(&blf_glyph_cache_mutex);
-  return FT_Init_FreeType(&ft_lib);
-}
-
-void blf_font_exit(void)
-{
-  FT_Done_FreeType(ft_lib);
-  BLI_spin_end(&ft_lib_mutex);
-  BLI_spin_end(&blf_glyph_cache_mutex);
-  blf_batch_draw_exit();
-}
-
-void blf_font_size(FontBLF *font, unsigned int size, unsigned int dpi)
-{
-  GlyphCacheBLF *gc;
-  FT_Error err;
-
-  blf_glyph_cache_acquire(font);
-
-  gc = blf_glyph_cache_find(font, size, dpi);
-  if (gc) {
-    /* Optimization: do not call FT_Set_Char_Size if size did not change. */
-    if (font->size == size && font->dpi == dpi) {
-      blf_glyph_cache_release(font);
-      return;
-    }
-  }
-
-  err = FT_Set_Char_Size(font->face, 0, ((FT_F26Dot6)(size)) * 64, 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, %u and dpi, %u\n", size, dpi);
-
-    blf_glyph_cache_release(font);
-    return;
-  }
-
-  font->size = size;
-  font->dpi = dpi;
-
-  if (!gc) {
-    blf_glyph_cache_new(font);
-  }
-  blf_glyph_cache_release(font);
-}
+/** \name Glyph Stepping Utilities (Internal)
+ * \{ */
 
 /* Fast path for runs of ASCII characters. Given that common UTF-8
  * input will consist of an overwhelming majority of ASCII
@@ -337,31 +321,6 @@ BLI_INLINE GlyphBLF *blf_utf8_next_fast(
   return g;
 }
 
-/* Convert a FreeType 26.6 value representing an unscaled design size to pixels.
- * This is an exact copy of the scaling done inside FT_Get_Kerning when called
- * with FT_KERNING_DEFAULT, including arbitrary resizing for small fonts.
- */
-static int blf_unscaled_F26Dot6_to_pixels(FontBLF *font, FT_Pos value)
-{
-  /* Scale value by font size using integer-optimized multiplication. */
-  FT_Long scaled = FT_MulFix(value, font->face->size->metrics.x_scale);
-
-  /* FreeType states that this '25' has been determined heuristically. */
-  if (font->face->size->metrics.x_ppem < 25) {
-    scaled = FT_MulDiv(scaled, font->face->size->metrics.x_ppem, 25);
-  }
-
-  /* Copies of internal FreeType macros needed here. */
-#define FT_PIX_FLOOR(x) ((x) & ~63)
-#define FT_PIX_ROUND(x) FT_PIX_FLOOR((x) + 32)
-
-  /* Round to even 64ths, then divide by 64. */
-  return (int)FT_PIX_ROUND(scaled) >> 6;
-
-#undef FT_PIX_FLOOR
-#undef FT_PIX_ROUND
-}
-
 BLI_INLINE void blf_kerning_step_fast(FontBLF *font,
                                       const GlyphBLF *g_prev,
                                       const GlyphBLF *g,
@@ -395,6 +354,12 @@ BLI_INLINE void blf_kerning_step_fast(FontBLF *font,
   }
 }
 
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Text Drawing: GPU
+ * \{ */
+
 static void blf_font_draw_ex(FontBLF *font,
                              GlyphCacheBLF *gc,
                              const char *str,
@@ -535,6 +500,12 @@ int blf_font_draw_mono(FontBLF *font, const char *str, size_t len, int cwidth)
   return columns;
 }
 
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Text Drawgin: Buffer
+ * \{ */
+
 /* Sanity checks are done by BLF_draw_buffer() */
 static void blf_font_draw_buffer_ex(FontBLF *font,
                                     GlyphCacheBLF *gc,
@@ -680,6 +651,16 @@ void blf_font_draw_buffer(FontBLF *font, const char *str, size_t len, struct Res
   blf_glyph_cache_release(font);
 }
 
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Text Evaluation: Width to Sting Length
+ *
+ * Use to implement exported functions:
+ * - #BLF_width_to_strlen
+ * - #BLF_width_to_rstrlen
+ * \{ */
+
 static bool blf_font_width_to_strlen_glyph_process(FontBLF *font,
                                                    const uint c_prev,
                                                    const uint c,
@@ -773,6 +754,12 @@ size_t blf_font_width_to_rstrlen(
   return i;
 }
 
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Text Evaluation: Glyph Bound Box with Callback
+ * \{ */
+
 static void blf_font_boundbox_ex(FontBLF *font,
                                  GlyphCacheBLF *gc,
                                  const char *str,
@@ -847,73 +834,230 @@ void blf_font_boundbox(
   blf_glyph_cache_release(font);
 }
 
-/* -------------------------------------------------------------------- */
-/** \name Word-Wrap Support
- * \{ */
+void blf_font_width_and_height(FontBLF *font,
+                               const char *str,
+                               size_t len,
+                               float *r_width,
+                               float *r_height,
+                               struct ResultBLF *r_info)
+{
+  float xa, ya;
+  rctf box;
 
-/**
- * Generic function to add word-wrap support for other existing functions.
- *
- * Wraps on spaces and respects newlines.
- * Intentionally ignores non-unix newlines, tabs and more advanced text formatting.
- *
- * \note If we want rich text - we better have a higher level API to handle that
- * (color, bold, switching fonts... etc).
- */
-static void blf_font_wrap_apply(FontBLF *font,
-                                const char *str,
-                                size_t len,
-                                struct ResultBLF *r_info,
-                                void (*callback)(FontBLF *font,
-                                                 GlyphCacheBLF *gc,
-                                                 const char *str,
-                                                 size_t len,
-                                                 int pen_y,
-                                                 void *userdata),
-                                void *userdata)
+  if (font->flags & BLF_ASPECT) {
+    xa = font->aspect[0];
+    ya = font->aspect[1];
+  }
+  else {
+    xa = 1.0f;
+    ya = 1.0f;
+  }
+
+  if (font->flags & BLF_WORD_WRAP) {
+    blf_font_boundbox__wrap(font, str, len, &box, r_info);
+  }
+  else {
+    blf_font_boundbox(font, str, len, &box, r_info);
+  }
+  *r_width = (BLI_rctf_size_x(&box) * xa);
+  *r_height = (BLI_rctf_size_y(&box) * ya);
+}
+
+float blf_font_width(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
 {
-  unsigned int c, c_prev = BLI_UTF8_ERR;
-  GlyphBLF *g, *g_prev = NULL;
-  int pen_x = 0, pen_y = 0;
-  size_t i = 0;
-  int lines = 0;
-  int pen_x_next = 0;
+  float xa;
+  rctf box;
 
-  GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
+  if (font->flags & BLF_ASPECT) {
+    xa = font->aspect[0];
+  }
+  else {
+    xa = 1.0f;
+  }
 
-  struct WordWrapVars {
-    int wrap_width;
-    size_t start, last[2];
-  } wrap = {font->wrap_width != -1 ? font->wrap_width : INT_MAX, 0, {0, 0}};
+  if (font->flags & BLF_WORD_WRAP) {
+    blf_font_boundbox__wrap(font, str, len, &box, r_info);
+  }
+  else {
+    blf_font_boundbox(font, str, len, &box, r_info);
+  }
+  return BLI_rctf_size_x(&box) * xa;
+}
 
-  // printf("%s wrapping (%d, %d) `%s`:\n", __func__, len, strlen(str), str);
-  while ((i < len) && str[i]) {
+float blf_font_height(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
+{
+  float ya;
+  rctf box;
 
-    /* wrap vars */
-    size_t i_curr = i;
-    bool do_draw = false;
+  if (font->flags & BLF_ASPECT) {
+    ya = font->aspect[1];
+  }
+  else {
+    ya = 1.0f;
+  }
 
-    g = blf_utf8_next_fast(font, gc, str, &i, &c);
+  if (font->flags & BLF_WORD_WRAP) {
+    blf_font_boundbox__wrap(font, str, len, &box, r_info);
+  }
+  else {
+    blf_font_boundbox(font, str, len, &box, r_info);
+  }
+  return BLI_rctf_size_y(&box) * ya;
+}
 
-    if (UNLIKELY(c == BLI_UTF8_ERR)) {
-      break;
-    }
-    if (UNLIKELY(g == NULL)) {
-      continue;
-    }
-    blf_kerning_step_fast(font, g_prev, g, c_prev, c, &pen_x);
+float blf_font_fixed_width(FontBLF *font)
+{
+  const unsigned int c = ' ';
 
-    /**
-     * Implementation Detail (utf8).
-     *
-     * Take care with single byte offsets here,
-     * since this is utf8 we can't be sure a single byte is a single character.
-     *
-     * This is _only_ done when we know for sure the character is ascii (newline or a space).
-     */
-    pen_x_next = pen_x + g->advance_i;
-    if (UNLIKELY((pen_x_next >= wrap.wrap_width) && (wrap.start != wrap.last[0]))) {
-      do_draw = true;
+  GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
+  GlyphBLF 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list