[Bf-blender-cvs] [51dc5aad4cb] soc-2020-info-editor: Refactor blf (blender fonts) to use CLOG instead of printf

Mateusz Grzeliński noreply at git.blender.org
Wed Jul 1 23:44:12 CEST 2020


Commit: 51dc5aad4cbb65702c46ecb49691cda6aefc50ec
Author: Mateusz Grzeliński
Date:   Wed Jul 1 22:08:52 2020 +0200
Branches: soc-2020-info-editor
https://developer.blender.org/rB51dc5aad4cbb65702c46ecb49691cda6aefc50ec

Refactor blf (blender fonts) to use CLOG instead of printf

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

M	source/blender/blenfont/BLF_api.h
M	source/blender/blenfont/CMakeLists.txt
M	source/blender/blenfont/intern/blf.c
M	source/blender/blenfont/intern/blf_font.c
M	source/blender/blenfont/intern/blf_font_default.c
M	source/blender/blenfont/intern/blf_thumbs.c

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

diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h
index 1d0cb2f524c..924bb63761b 100644
--- a/source/blender/blenfont/BLF_api.h
+++ b/source/blender/blenfont/BLF_api.h
@@ -34,6 +34,8 @@ extern "C" {
 /* enable this only if needed (unused circa 2016) */
 #define BLF_BLUR_ENABLE 0
 
+extern struct CLG_LogRef *BLENFONT_LOG;
+
 struct ColorManagedDisplay;
 struct ResultBLF;
 struct rctf;
@@ -263,9 +265,7 @@ void BLF_thumb_preview(const char *filename,
 int BLF_load_default(const bool unique);
 int BLF_load_mono_default(const bool unique);
 
-#ifdef DEBUG
-void BLF_state_print(int fontid);
-#endif
+char *BLF_state_sprintN(int fontid);
 
 /* font->flags. */
 #define BLF_ROTATION (1 << 0)
diff --git a/source/blender/blenfont/CMakeLists.txt b/source/blender/blenfont/CMakeLists.txt
index ac927dd388d..224c447376a 100644
--- a/source/blender/blenfont/CMakeLists.txt
+++ b/source/blender/blenfont/CMakeLists.txt
@@ -30,6 +30,7 @@ set(INC
   ../makesrna
   ../../../intern/glew-mx
   ../../../intern/guardedalloc
+  ../../../intern/clog
 )
 
 set(INC_SYS
diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c
index 2f7d5a60a6f..bd78d7951d4 100644
--- a/source/blender/blenfont/intern/blf.c
+++ b/source/blender/blenfont/intern/blf.c
@@ -30,6 +30,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <BLI_dynstr.h>
+#include <CLG_log.h>
 #include <ft2build.h>
 
 #include FT_FREETYPE_H
@@ -70,6 +72,8 @@
   } \
   ((void)0)
 
+CLG_LOGREF_DECLARE_GLOBAL(BLENFONT_LOG, "blenfont");
+
 /* Font array. */
 static FontBLF *global_font[BLF_MAX_FONT] = {NULL};
 
@@ -222,13 +226,13 @@ int BLF_load_unique(const char *name)
    */
   i = blf_search_available();
   if (i == -1) {
-    printf("Too many fonts!!!\n");
+    CLOG_ERROR(BLENFONT_LOG, "Too many fonts (max %d)!!!", BLF_MAX_FONT);
     return -1;
   }
 
   filename = blf_dir_search(name);
   if (!filename) {
-    printf("Can't find font: %s\n", name);
+    CLOG_ERROR(BLENFONT_LOG, "Can't find font: %s", name);
     return -1;
   }
 
@@ -236,7 +240,7 @@ int BLF_load_unique(const char *name)
   MEM_freeN(filename);
 
   if (!font) {
-    printf("Can't load font: %s\n", name);
+    CLOG_ERROR(BLENFONT_LOG, "Can't load font: %s", name);
     return -1;
   }
 
@@ -277,18 +281,18 @@ int BLF_load_mem_unique(const char *name, const unsigned char *mem, int mem_size
    */
   i = blf_search_available();
   if (i == -1) {
-    printf("Too many fonts!!!\n");
+    CLOG_ERROR(BLENFONT_LOG, "Too many fonts (max %d)!!!", BLF_MAX_FONT);
     return -1;
   }
 
   if (!mem_size) {
-    printf("Can't load font: %s from memory!!\n", name);
+    CLOG_ERROR(BLENFONT_LOG, "Can't load font: %s from memory!!", name);
     return -1;
   }
 
   font = blf_font_new_from_mem(name, mem, mem_size);
   if (!font) {
-    printf("Can't load font: %s from memory!!\n", name);
+    CLOG_ERROR(BLENFONT_LOG, "Can't load font: %s from memory!!", name);
     return -1;
   }
 
@@ -1000,25 +1004,38 @@ void BLF_draw_buffer(int fontid, const char *str, size_t len)
   BLF_draw_buffer_ex(fontid, str, len, NULL);
 }
 
-#ifdef DEBUG
-void BLF_state_print(int fontid)
+/* example usage: CLOG_STR_INFO_N(BLENFONT_LOG, 2, BLF_state_sprintN(font_id)); */
+char *BLF_state_sprintN(int fontid)
 {
+  DynStr *message = BLI_dynstr_new();
   FontBLF *font = blf_get(fontid);
   if (font) {
-    printf("fontid %d %p\n", fontid, (void *)font);
-    printf("  name:    '%s'\n", font->name);
-    printf("  size:     %u\n", font->size);
-    printf("  dpi:      %u\n", font->dpi);
-    printf("  pos:      %.6f %.6f %.6f\n", UNPACK3(font->pos));
-    printf("  aspect:   (%d) %.6f %.6f %.6f\n",
-           (font->flags & BLF_ROTATION) != 0,
-           UNPACK3(font->aspect));
-    printf("  angle:    (%d) %.6f\n", (font->flags & BLF_ASPECT) != 0, font->angle);
-    printf("  flag:     %d\n", font->flags);
+    BLI_dynstr_appendf(message,
+                       "fontid %d %p\n"
+                       "  name:    '%s'\n"
+                       "  size:     %u\n"
+                       "  dpi:      %u\n"
+                       "  pos:      %.6f %.6f %.6f\n"
+                       "  aspect:   (%d) %.6f %.6f %.6f\n"
+                       "  angle:    (%d) %.6f\n"
+                       "  flag:     %d",
+                       fontid,
+                       (void *)font,
+                       font->name,
+                       font->size,
+                       font->dpi,
+                       UNPACK3(font->pos),
+                       (font->flags & BLF_ROTATION) != 0,
+                       UNPACK3(font->aspect),
+                       (font->flags & BLF_ASPECT) != 0,
+                       font->angle,
+                       font->flags);
   }
   else {
-    printf("fontid %d (NULL)\n", fontid);
+    BLI_dynstr_appendf(message, "fontid %d (NULL)", fontid);
   }
-  fflush(stdout);
+
+  char *cstring = BLI_dynstr_get_cstring(message);
+  BLI_dynstr_free(message);
+  return cstring;
 }
-#endif
diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c
index da6224cff7f..778fd50fb9e 100644
--- a/source/blender/blenfont/intern/blf_font.c
+++ b/source/blender/blenfont/intern/blf_font.c
@@ -30,6 +30,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <CLG_log.h>
 #include <ft2build.h>
 
 #include FT_FREETYPE_H
@@ -299,8 +300,9 @@ void blf_font_size(FontBLF *font, unsigned int size, unsigned int dpi)
 
   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);
+    /* FIXME: here we can go through the fixed size and choose a close one */
+    CLOG_ERROR(
+        BLENFONT_LOG, "The current font doesn't support the size, %u and dpi, %u", size, dpi);
 
     blf_glyph_cache_release(font);
     return;
@@ -969,7 +971,7 @@ static void blf_font_wrap_apply(FontBLF *font,
     size_t start, last[2];
   } wrap = {font->wrap_width != -1 ? font->wrap_width : INT_MAX, 0, {0, 0}};
 
-  // printf("%s wrapping (%d, %d) `%s`:\n", __func__, len, strlen(str), str);
+  CLOG_INFO(BLENFONT_LOG, 2, "wrapping (%d, %d) `%s`:", len, strlen(str), str);
   while ((i < len) && str[i]) {
 
     /* wrap vars */
@@ -1017,8 +1019,13 @@ static void blf_font_wrap_apply(FontBLF *font,
     }
 
     if (UNLIKELY(do_draw)) {
-      // printf("(%03d..%03d)  `%.*s`\n",
-      //        wrap.start, wrap.last[0], (wrap.last[0] - wrap.start) - 1, &str[wrap.start]);
+      CLOG_INFO(BLENFONT_LOG,
+                2,
+                "(%03d..%03d)  `%.*s`",
+                wrap.start,
+                wrap.last[0],
+                (wrap.last[0] - wrap.start) - 1,
+                &str[wrap.start]);
 
       callback(font, gc, &str[wrap.start], (wrap.last[0] - wrap.start) - 1, pen_y, userdata);
       wrap.start = wrap.last[0];
@@ -1034,7 +1041,7 @@ static void blf_font_wrap_apply(FontBLF *font,
     g_prev = g;
   }
 
-  // printf("done! lines: %d, width, %d\n", lines, pen_x_next);
+  CLOG_INFO(BLENFONT_LOG, 2, "done! lines: %d, width, %d", lines, pen_x_next);
 
   if (r_info) {
     r_info->lines = lines;
@@ -1384,7 +1391,7 @@ FontBLF *blf_font_new(const char *name, const char *filename)
 
   err = FT_Select_Charmap(font->face, ft_encoding_unicode);
   if (err) {
-    printf("Can't set the unicode character map!\n");
+    CLOG_ERROR(BLENFONT_LOG, "Can't set the unicode character map!");
     FT_Done_Face(font->face);
     MEM_freeN(font);
     return NULL;
@@ -1394,7 +1401,8 @@ FontBLF *blf_font_new(const char *name, const char *filename)
   if (mfile) {
     err = FT_Attach_File(font->face, mfile);
     if (err) {
-      fprintf(stderr, "FT_Attach_File failed to load '%s' with error %d\n", filename, (int)err);
+      CLOG_ERROR(
+          BLENFONT_LOG, "FT_Attach_File failed to load '%s' with error %d", filename, (int)err);
     }
     MEM_freeN(mfile);
   }
@@ -1429,7 +1437,7 @@ FontBLF *blf_font_new_from_mem(const char *name, const unsigned char *mem, int m
 
   err = FT_Select_Charmap(font->face, ft_encoding_unicode);
   if (err) {
-    printf("Can't set the unicode character map!\n");
+    CLOG_ERROR(BLENFONT_LOG, "Can't set the unicode character map!");
     FT_Done_Face(font->face);
     MEM_freeN(font);
     return NULL;
diff --git a/source/blender/blenfont/intern/blf_font_default.c b/source/blender/blenfont/intern/blf_font_default.c
index f33d7cd4203..bc727a1a1ed 100644
--- a/source/blender/blenfont/intern/blf_font_default.c
+++ b/source/blender/blenfont/intern/blf_font_default.c
@@ -23,6 +23,7 @@
  * API for loading default font files.
  */
 
+#include <CLG_log.h>
 #include <stdio.h>
 
 #include "BLF_api.h"
@@ -35,10 +36,9 @@ static int blf_load_font_default(const char *filename, const bool unique)
 {
   const char *dir = BKE_appdir_folder_id(BLENDER_DATAFILES, "fonts");
   if (dir == NULL) {
-    fprintf(stderr,
-            "%s: 'fonts' data path not found for '%s', will not be able to display text\n",
-            __func__,
-            filename);
+    CLOG_ERROR(BLENFONT_LOG,
+               "'fonts' data path not found for '%s', will not be able to display text",
+               filename);
     return -1;
   }
 
diff --git a/source/blender/blenfont/intern/blf_thumbs.c b/source/blender/blenfont/intern/blf_thumbs.c
index 6aa39e3aa71..c042144c628 100644
--- a/source/blender/blenfont/intern/blf_thumbs.c
+++ b/source/blender/blenfont/intern/blf_thumbs.c
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <CLG_log.h>
 #include <ft2build.h>
 
 #include FT_FREETYPE_H
@@ -72,7 +73,7 @@ void BLF_thumb_preview(const char *filename,
   /* Create a new blender font obj and fill it with default values */
   font = blf_font_new("thumb_font", filename);
   if (!font) {
-    printf("Info: Can't load font '%s', no preview possible\n", filename);
+    CLOG_INFO(BLENFONT_LOG, 0, "Can't load font '%s', no preview possible", filename);
     return;
   }



More information about the Bf-blender-cvs mailing list