Commit: bd7eb84fb5a0cb65f273e6950f98ecac04f2e9ea
Author: Campbell Barton
Date:   Sat Aug 29 20:42:55 2015 +1000
Branches: temp-blf-wordwrap
https://developer.blender.org/rBbd7eb84fb5a0cb65f273e6950f98ecac04f2e9ea

BLF: expose wordwrapped lines though the API

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

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/blenfont/intern/blf_internal_types.h

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

diff --git a/source/blender/blenfont/BLF_api.h 
b/source/blender/blenfont/BLF_api.h
index 49fc2be..bf486cc 100644
--- a/source/blender/blenfont/BLF_api.h
+++ b/source/blender/blenfont/BLF_api.h
@@ -97,7 +97,9 @@ void BLF_boundbox(int fontid, const char *str, size_t len, 
struct rctf *box) ATT
  * 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(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(int fontid, const char *str, size_t len) 
ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
 
 /* Return dimensions of the font without any sample text. */
@@ -137,6 +139,7 @@ void BLF_disable_default(int option);
 void BLF_rotation(int fontid, float angle);
 void BLF_clipping(int fontid, float xmin, float ymin, float xmax, float ymax);
 void BLF_clipping_default(float xmin, float ymin, float xmax, float ymax);
+void BLF_wordwrap_width(int fontid, int wrap_width);
 void BLF_blur(int fontid, int size);
 
 void BLF_enable(int fontid, int option);
diff --git a/source/blender/blenfont/intern/blf.c 
b/source/blender/blenfont/intern/blf.c
index ca25ef8..41d0a10 100644
--- a/source/blender/blenfont/intern/blf.c
+++ b/source/blender/blenfont/intern/blf.c
@@ -657,7 +657,7 @@ void BLF_width_and_height(int fontid, const char *str, 
size_t len, float *r_widt
        FontBLF *font = blf_get(fontid);
 
        if (font && font->glyph_cache) {
-               blf_font_width_and_height(font, str, len, r_width, r_height);
+               blf_font_width_and_height(font, str, len, r_width, r_height, 
NULL);
        }
        else {
                *r_width = *r_height = 0.0f;
@@ -672,16 +672,20 @@ 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(int fontid, const char *str, size_t len)
+float BLF_width_ex(int fontid, const char *str, size_t len, int *r_lines)
 {
        FontBLF *font = blf_get(fontid);
 
        if (font && font->glyph_cache) {
-               return blf_font_width(font, str, len);
+               return blf_font_width(font, str, len, r_lines);
        }
 
        return 0.0f;
 }
+float BLF_width(int fontid, const char *str, size_t len)
+{
+       return BLF_width_ex(fontid, str, len, NULL);
+}
 
 float BLF_fixed_width(int fontid)
 {
@@ -702,16 +706,20 @@ float BLF_width_default(const char *str, size_t len)
        return BLF_width(global_font_default, str, len);
 }
 
-float BLF_height(int fontid, const char *str, size_t len)
+float BLF_height_ex(int fontid, const char *str, size_t len, int *r_lines)
 {
        FontBLF *font = blf_get(fontid);
 
        if (font && font->glyph_cache) {
-               return blf_font_height(font, str, len);
+               return blf_font_height(font, str, len, r_lines);
        }
 
        return 0.0f;
 }
+float BLF_height(int fontid, const char *str, size_t len)
+{
+       return BLF_height_ex(fontid, str, len, NULL);
+}
 
 float BLF_height_max(int fontid)
 {
@@ -799,6 +807,15 @@ void BLF_clipping_default(float xmin, float ymin, float 
xmax, float ymax)
        }
 }
 
+void BLF_wordwrap_width(int fontid, int wrap_width)
+{
+       FontBLF *font = blf_get(global_font_default);
+
+       if (font) {
+               font->wrap_width = wrap_width;
+       }
+}
+
 void BLF_shadow(int fontid, int level, float r, float g, float b, float a)
 {
        FontBLF *font = blf_get(fontid);
diff --git a/source/blender/blenfont/intern/blf_font.c 
b/source/blender/blenfont/intern/blf_font.c
index 8bff51c..edf63f3 100644
--- a/source/blender/blenfont/intern/blf_font.c
+++ b/source/blender/blenfont/intern/blf_font.c
@@ -634,12 +634,14 @@ struct WordWrapVars {
 };
 
 #define BLF_WORDWRAP_VARS(_font, _wrap) \
-       struct WordWrapVars _wrap = {(int)_font->clip_rec.xmax - 
(int)_font->pos[0], 0, {0, 0}}
+       struct WordWrapVars _wrap = {(int)_font->wrap_width, 0, {0, 0}}
 
 /**
  * Generic function to add word-wrap support for other existing functions.
+ *
+ * \return number of lines.
  */
-static void blf_font_wrap_apply(
+static int blf_font_wrap_apply(
         FontBLF *font, const char *str, size_t len,
         void (*callback)(FontBLF *font, const char *str, size_t len, int 
pen_y, void *userdata),
         void *userdata)
@@ -650,13 +652,14 @@ static void blf_font_wrap_apply(
        int pen_x = 0, pen_y = 0;
        size_t i = 0;
        GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
+       int lines = 0;
 
        BLF_KERNING_VARS(font, has_kerning, kern_mode);
 
        BLF_WORDWRAP_VARS(font, wrap);
 
        blf_font_ensure_ascii_table(font);
-
+       // printf("%s wrapping `%s`:\n", __func__, str);
        while ((i < len) && str[i]) {
 
                /* wrap vars */
@@ -688,18 +691,24 @@ static void blf_font_wrap_apply(
                }
 
                if (UNLIKELY(do_draw)) {
-                       callback(font, &str[wrap.start], (wrap.last[0] - 
wrap.start), pen_y, userdata);
+                       // printf("... `%.*s`\n", wrap.last[0] - wrap.start, 
&str[wrap.start]);
+                       callback(font, &str[wrap.start], wrap.last[0] - 
wrap.start, pen_y, userdata);
                        wrap.start = wrap.last[0];
                        i = wrap.last[1];
                        pen_x = 0;
                        pen_y -= font->glyph_cache->max_glyph_height;
                        g_prev = NULL;
+                       lines += 1;
                        continue;
                }
 
                pen_x = pen_x_next;
                g_prev = g;
        }
+
+       // printf("done! %d lines\n", lines);
+
+       return lines;
 }
 
 /* blf_font_draw__wrap */
@@ -731,20 +740,26 @@ static void blf_font_boundbox_wrap_cb(FontBLF *font, 
const char *str, size_t len
        blf_font_boundbox_ex(font, str, len, &box_single, pen_y);
        BLI_rctf_union(box, &box_single);
 }
-void blf_font_boundbox__wrap(FontBLF *font, const char *str, size_t len, rctf 
*box)
+void blf_font_boundbox__wrap(FontBLF *font, const char *str, size_t len, rctf 
*box, int *r_lines)
 {
+       int lines;
+
        box->xmin = 32000.0f;
        box->xmax = -32000.0f;
        box->ymin = 32000.0f;
        box->ymax = -32000.0f;
 
-       blf_font_wrap_apply(font, str, len, blf_font_boundbox_wrap_cb, box);
+       lines = blf_font_wrap_apply(font, str, len, blf_font_boundbox_wrap_cb, 
box);
+
+       if (r_lines) {
+               *r_lines = lines;
+       }
 }
 
 /** \} */
 
 
-void blf_font_width_and_height(FontBLF *font, const char *str, size_t len, 
float *width, float *height)
+void blf_font_width_and_height(FontBLF *font, const char *str, size_t len, 
float *width, float *height, int *r_lines)
 {
        float xa, ya;
        rctf box;
@@ -759,16 +774,19 @@ void blf_font_width_and_height(FontBLF *font, const char 
*str, size_t len, float
        }
 
        if (font->flags & BLF_WORDWRAP) {
-               blf_font_boundbox__wrap(font, str, len, &box);
+               blf_font_boundbox__wrap(font, str, len, &box, r_lines);
        }
        else {
+               if (r_lines) {
+                       *r_lines = 1;
+               }
                blf_font_boundbox(font, str, len, &box);
        }
        *width  = (BLI_rctf_size_x(&box) * xa);
        *height = (BLI_rctf_size_y(&box) * ya);
 }
 
-float blf_font_width(FontBLF *font, const char *str, size_t len)
+float blf_font_width(FontBLF *font, const char *str, size_t len, int *r_lines)
 {
        float xa;
        rctf box;
@@ -779,15 +797,18 @@ float blf_font_width(FontBLF *font, const char *str, 
size_t len)
                xa = 1.0f;
 
        if (font->flags & BLF_WORDWRAP) {
-               blf_font_boundbox__wrap(font, str, len, &box);
+               blf_font_boundbox__wrap(font, str, len, &box, r_lines);
        }
        else {
+               if (r_lines) {
+                       r_lines = 1;
+               }
                blf_font_boundbox(font, str, len, &box);
        }
        return BLI_rctf_size_x(&box) * xa;
 }
 
-float blf_font_height(FontBLF *font, const char *str, size_t len)
+float blf_font_height(FontBLF *font, const char *str, size_t len, int *r_lines)
 {
        float ya;
        rctf box;
@@ -798,9 +819,12 @@ float blf_font_height(FontBLF *font, const char *str, 
size_t len)
                ya = 1.0f;
 
        if (font->flags & BLF_WORDWRAP) {
-               blf_font_boundbox__wrap(font, str, len, &box);
+               blf_font_boundbox__wrap(font, str, len, &box, r_lines);
        }
        else {
+               if (r_lines) {
+                       r_lines = 1;
+               }
                blf_font_boundbox(font, str, len, &box);
        }
        return BLI_rctf_size_y(&box) * ya;
diff --git a/source/blender/blenfont/intern/blf_internal.h 
b/source/blender/blenfont/intern/blf_internal.h
index ed77e3a..1a111bf 100644
--- a/source/blender/blenfont/intern/blf_internal.h
+++ b/source/blender/blenfont/intern/blf_internal.h
@@ -60,10 +60,10 @@ void blf_font_buffer(struct FontBLF *font, const char *str);
 size_t blf_font_width_to_strlen(struct FontBLF *font, const char *str, size_t 
len, float width, float *r_width);
 size_t blf_font_width_to_rstrlen(struct FontBLF *font, const char *str, size_t 
len, float width, float *r_width);
 void blf_font_boundbox(struct FontBLF *font, const char *str, size_t len, 
struct rctf *box);
-void blf_font_boundbox__wrap(struct FontBLF *font, const char *str, size_t 
len, struct rctf *box);
-void blf_font_width_and_height(struct FontBLF *font, const char *str, size_t 
len, float *width, float *height);
-float blf_font_width(struct FontBLF *font, const char *str, size_t len);
-float blf_font_height(struct FontBLF *font, const char *str, size_t len);
+void blf_font_boundbox__wrap(struct FontBLF *font, const char *str, size_t 
len, struct rctf *box, int *r_lines);
+void blf_font_width_and_height(struct FontBLF *font, const char *str, size_t 
len, float *width, float *height, int *r_lines);
+float blf_font_width(struct FontBLF *font, const char *str, size_t len, int 
*r_lines);
+float blf_font_height(struct FontBLF *font, const char *str, size_t len, int 
*r_lines);
 float blf_font_fixed_width(struct FontBLF *font);
 
 int blf_font_count_missing_chars(struct FontBLF *font, const char *str, const 
size_t len, int *r_tot_chars);
diff --git a/source/blender/blenfont/intern/blf_internal_types.h 
b/source/blender/blenfont/intern/blf_internal_types.h
index 1404b9d..090659e 100644
--- a/source/blender/blenfont/intern/blf_internal_types.h
+++ b/source/blender/blenfont/intern/blf_internal_types.h
@@ -195,6 +195,9 @@ typedef struct FontBLF {
        /* clipping rectangle. */
        rctf clip_rec;
 
+       /* the width to wrap the text, see BLF_WORDWRAP */
+       int wrap_width;
+
        /* font dpi (default 72). */
        unsigned int dpi;

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

Reply via email to