Commit: 9faa512173a56f18419dd97f3ba8f44b5977ce08
Author: Campbell Barton
Date:   Sat Aug 29 23:19:54 2015 +1000
Branches: temp-blf-wordwrap
https://developer.blender.org/rB9faa512173a56f18419dd97f3ba8f44b5977ce08

BLF: replace pointer to int arg with a struct

This way we can get the result of drawing without adding new args all the time.

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

M       source/blender/blenfont/BLF_api.h
M       source/blender/blenfont/intern/blf.c
M       source/blender/blenfont/intern/blf_font.c
M       source/blender/blenfont/intern/blf_internal.h
M       source/blender/editors/interface/interface_regions.c

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

diff --git a/source/blender/blenfont/BLF_api.h 
b/source/blender/blenfont/BLF_api.h
index 474764a..52de93f 100644
--- a/source/blender/blenfont/BLF_api.h
+++ b/source/blender/blenfont/BLF_api.h
@@ -36,6 +36,7 @@
 
 struct rctf;
 struct ColorManagedDisplay;
+struct ResultBLF;
 
 int BLF_init(int points, int dpi);
 void BLF_exit(void);
@@ -79,9 +80,11 @@ void BLF_draw_default(float x, float y, float z, const char 
*str, size_t len) AT
 void BLF_draw_default_ascii(float x, float y, float z, const char *str, size_t 
len) ATTR_NONNULL();
 
 /* Draw the string using the current font. */
-void BLF_draw(int fontid, const char *str, size_t len);
-void BLF_draw_ascii(int fontid, const char *str, size_t len);
-int BLF_draw_mono(int fontid, const char *str, size_t len, int cwidth);
+void BLF_draw_ex(int fontid, const char *str, size_t len, struct ResultBLF 
*r_info) ATTR_NONNULL(2);
+void BLF_draw(int fontid, const char *str, size_t len) ATTR_NONNULL(2);
+void BLF_draw_ascii_ex(int fontid, const char *str, size_t len, struct 
ResultBLF *r_info) ATTR_NONNULL(2);
+void BLF_draw_ascii(int fontid, const char *str, size_t len) ATTR_NONNULL(2);
+int BLF_draw_mono(int fontid, const char *str, size_t len, int cwidth) 
ATTR_NONNULL(2);
 
 /* Get the string byte offset that fits within a given width */
 size_t BLF_width_to_strlen(int fontid, const char *str, size_t len, float 
width, float *r_width) ATTR_NONNULL(2);
@@ -91,15 +94,16 @@ size_t BLF_width_to_rstrlen(int fontid, const char *str, 
size_t len, float width
 /* This function return the bounding box of the string
  * and are not multiplied by the aspect.
  */
+void BLF_boundbox_ex(int fontid, const char *str, size_t len, struct rctf 
*box, struct ResultBLF *r_info) ATTR_NONNULL(2);
 void BLF_boundbox(int fontid, const char *str, size_t len, struct rctf *box) 
ATTR_NONNULL();
 
 /* The next both function return the width and height
  * of the string, using the current font and both value 
  * are multiplied by the aspect of the font.
  */
-float BLF_width_ex(int fontid, const char *str, size_t len, int *r_lines) 
ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2);
+float BLF_width_ex(int fontid, const char *str, size_t len, struct ResultBLF 
*r_info) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2);
 float BLF_width(int fontid, const char *str, size_t len) 
ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
-float BLF_height_ex(int fontid, const char *str, size_t len, int *r_lines) 
ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2);
+float BLF_height_ex(int fontid, const char *str, size_t len, struct ResultBLF 
*r_info) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2);
 float BLF_height(int fontid, const char *str, size_t len) 
ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
 
 /* Return dimensions of the font without any sample text. */
@@ -221,4 +225,16 @@ void BLF_state_print(int fontid);
 extern int blf_mono_font;
 extern int blf_mono_font_render; /* don't mess drawing with render threads. */
 
+/**
+ * Result of drawing/evaluating the string
+ */
+struct ResultBLF {
+       /**
+        * Number of lines drawn when #BLF_WORDWRAP is enabled
+        */
+       int lines_wrap;
+
+       int width;
+};
+
 #endif /* __BLF_API_H__ */
diff --git a/source/blender/blenfont/intern/blf.c 
b/source/blender/blenfont/intern/blf.c
index cb25dca..975dcea 100644
--- a/source/blender/blenfont/intern/blf.c
+++ b/source/blender/blenfont/intern/blf.c
@@ -66,6 +66,11 @@
 /* call BLF_default_set first! */
 #define ASSERT_DEFAULT_SET BLI_assert(global_font_default != -1)
 
+#define BLF_RESULT_CHECK_INIT(r_info) \
+if (r_info) { \
+       memset(r_info, 0, sizeof(*(r_info))); \
+} ((void)0)
+
 /* Font array. */
 static FontBLF *global_font[BLF_MAX_FONT] = {NULL};
 
@@ -554,32 +559,45 @@ static void blf_draw__end(GLint mode, GLint param)
        glDisable(GL_TEXTURE_2D);
 }
 
-void BLF_draw(int fontid, const char *str, size_t len)
+void BLF_draw_ex(
+        int fontid, const char *str, size_t len,
+        struct ResultBLF *r_info)
 {
        FontBLF *font = blf_get(fontid);
        GLint mode, param;
 
+       BLF_RESULT_CHECK_INIT(r_info);
+
        if (font && font->glyph_cache) {
                blf_draw__start(font, &mode, &param);
                if (font->flags & BLF_WORDWRAP) {
-                       blf_font_draw__wrap(font, str, len);
+                       blf_font_draw__wrap(font, str, len, r_info);
                }
                else {
-                       blf_font_draw(font, str, len);
+                       blf_font_draw(font, str, len, r_info);
                }
                blf_draw__end(mode, param);
        }
 }
+void BLF_draw(int fontid, const char *str, size_t len)
+{
+       BLF_draw_ex(fontid, str, len, NULL);
+}
 
-void BLF_draw_ascii(int fontid, const char *str, size_t len)
+void BLF_draw_ascii_ex(
+        int fontid, const char *str, size_t len,
+        struct ResultBLF *r_info)
 {
        FontBLF *font = blf_get(fontid);
        GLint mode, param;
 
+       BLF_RESULT_CHECK_INIT(r_info);
+
        if (font && font->glyph_cache) {
                blf_draw__start(font, &mode, &param);
                if (font->flags & BLF_WORDWRAP) {
-                       blf_font_draw_ascii__wrap(font, str, len);
+                       /* use non-ascii draw function for word-wrap */
+                       blf_font_draw__wrap(font, str, len, r_info);
                }
                else {
                        blf_font_draw_ascii(font, str, len);
@@ -587,6 +605,10 @@ void BLF_draw_ascii(int fontid, const char *str, size_t 
len)
                blf_draw__end(mode, param);
        }
 }
+void BLF_draw_ascii(int fontid, const char *str, size_t len)
+{
+       BLF_draw_ascii_ex(fontid, str, len, NULL);
+}
 
 int BLF_draw_mono(int fontid, const char *str, size_t len, int cwidth)
 {
@@ -643,14 +665,27 @@ size_t BLF_width_to_rstrlen(int fontid, const char *str, 
size_t len, float width
        return 0;
 }
 
-void BLF_boundbox(int fontid, const char *str, size_t len, rctf *box)
+void BLF_boundbox_ex(
+        int fontid, const char *str, size_t len, rctf *r_box,
+        struct ResultBLF *r_info)
 {
        FontBLF *font = blf_get(fontid);
 
+       BLF_RESULT_CHECK_INIT(r_info);
+
        if (font) {
-               blf_font_boundbox(font, str, len, box);
+               if (font->flags & BLF_WORDWRAP) {
+                       blf_font_boundbox__wrap(font, str, len, r_box, r_info);
+               }
+               else {
+                       blf_font_boundbox(font, str, len, r_box, r_info);
+               }
        }
 }
+void BLF_boundbox(int fontid, const char *str, size_t len, rctf *r_box)
+{
+       BLF_boundbox_ex(fontid, str, len, r_box, NULL);
+}
 
 void BLF_width_and_height(int fontid, const char *str, size_t len, float 
*r_width, float *r_height)
 {
@@ -672,12 +707,16 @@ void BLF_width_and_height_default(const char *str, size_t 
len, float *r_width, f
        BLF_width_and_height(global_font_default, str, len, r_width, r_height);
 }
 
-float BLF_width_ex(int fontid, const char *str, size_t len, int *r_lines)
+float BLF_width_ex(
+        int fontid, const char *str, size_t len,
+        struct ResultBLF *r_info)
 {
        FontBLF *font = blf_get(fontid);
 
+       BLF_RESULT_CHECK_INIT(r_info);
+
        if (font && font->glyph_cache) {
-               return blf_font_width(font, str, len, r_lines);
+               return blf_font_width(font, str, len, r_info);
        }
 
        return 0.0f;
@@ -706,12 +745,16 @@ float BLF_width_default(const char *str, size_t len)
        return BLF_width(global_font_default, str, len);
 }
 
-float BLF_height_ex(int fontid, const char *str, size_t len, int *r_lines)
+float BLF_height_ex(
+        int fontid, const char *str, size_t len,
+        struct ResultBLF *r_info)
 {
        FontBLF *font = blf_get(fontid);
 
+       BLF_RESULT_CHECK_INIT(r_info);
+
        if (font && font->glyph_cache) {
-               return blf_font_height(font, str, len, r_lines);
+               return blf_font_height(font, str, len, r_info);
        }
 
        return 0.0f;
@@ -809,7 +852,7 @@ void BLF_clipping_default(float xmin, float ymin, float 
xmax, float ymax)
 
 void BLF_wordwrap(int fontid, int wrap_width)
 {
-       FontBLF *font = blf_get(global_font_default);
+       FontBLF *font = blf_get(fontid);
 
        if (font) {
                font->wrap_width = wrap_width;
diff --git a/source/blender/blenfont/intern/blf_font.c 
b/source/blender/blenfont/intern/blf_font.c
index edf63f3..19260b5 100644
--- a/source/blender/blenfont/intern/blf_font.c
+++ b/source/blender/blenfont/intern/blf_font.c
@@ -174,7 +174,9 @@ static void blf_font_ensure_ascii_table(FontBLF *font)
        }                                                                       
     \
 } (void)0
 
-static void blf_font_draw_ex(FontBLF *font, const char *str, size_t len, int 
pen_y)
+static void blf_font_draw_ex(
+        FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info,
+        int pen_y)
 {
        unsigned int c;
        GlyphBLF *g, *g_prev = NULL;
@@ -203,10 +205,14 @@ static void blf_font_draw_ex(FontBLF *font, const char 
*str, size_t len, int pen
                pen_x += g->advance_i;
                g_prev = g;
        }
+
+       if (r_info) {
+               r_info->width = pen_x;
+       }
 }
-void blf_font_draw(FontBLF *font, const char *str, size_t len)
+void blf_font_draw(FontBLF *font, const char *str, size_t len, struct 
ResultBLF *r_info)
 {
-       blf_font_draw_ex(font, str, len, 0);
+       blf_font_draw_ex(font, str, len, r_info, 0);
 }
 
 /* faster version of blf_font_draw, ascii only for view dimensions */
@@ -566,7 +572,9 @@ size_t blf_font_width_to_rstrlen(FontBLF *font, const char 
*str, size_t len, flo
        return i_prev;
 }
 
-static void blf_font_boundbox_ex(FontBLF *font, const char *str, size_t len, 
rctf *box, int pen_y)
+static void blf_font_boundbox_ex(
+        FontBLF *font, const char *str, size_t len, rctf *box, struct 
ResultBLF *r_info,
+        int pen_y)
 {
        unsigned int c;
        GlyphBLF *g, *g_prev = NULL;
@@ -617,10 +625,14 @@ static void blf_font_boundbox_ex(FontBLF *font, const 
char *str, size_t len, rct
                box->xmax = 0.0f;
                box->ymax = 0.0f;
        }
+
+       if (r_info) {
+               r_info->width = pen_x;
+       }
 }
-void blf_font_boundbox(FontBLF *font, const char *str, size_t len, rctf *box)
+void blf_font_boundbox(FontBLF *font, const char *str, size_t len, rctf 
*r_box, struct ResultBLF *r_info)
 {
-       blf_font_boundbox_ex(font, str, len, box, 0);
+       blf_font_boundbox_ex(font, str, len, r_box, r_info, 0);
 }
 
 
@@ -714,21 +726,17 @@ static int blf_font_wrap_apply(
 /* blf_font_draw__wrap */
 static void blf_font_draw__wrap_cb(FontBLF *font, const char *str, size_t len, 
int pen_y, void *UNUSED(userdata))
 {
-       blf_font_draw_ex(font, str, len, pen_y);
+       blf_font_draw_ex(font, str, len, NULL, pen_y);
 }
-void blf_font_draw__wrap(FontBLF *font, const char *str, size_t len)
+void blf_font_draw__wrap(FontBLF *font, const char *str, size_t len, struct 
ResultBLF *r_info)
 {
-       blf_font_wrap_apply(font, str, len, blf_font_draw__wrap_

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to