[Bf-blender-cvs] [f2341f8] master: BLF: initial word-wrap support

Campbell Barton noreply at git.blender.org
Fri Sep 18 12:49:26 CEST 2015


Commit: f2341f829654c4dc97fcf9fd1f74a6526c4f50ff
Author: Campbell Barton
Date:   Fri Sep 18 20:10:26 2015 +1000
Branches: master
https://developer.blender.org/rBf2341f829654c4dc97fcf9fd1f74a6526c4f50ff

BLF: initial word-wrap support

- Adds support for word wrapping to Blender's BLF font library.
- Splits lines when width limit is reached or on explicit \n newlines.

Details:

- Word wrapping is used when `BLF_WORD_WRAP` flag is enabled.
- There is a single loop to handle line wrapping,
  this runs callback, passing in a substring,
  this way we can avoid code-duplication for all word-wrapped
  versions of functions... OR... avoid having to add support
  for word-wrapping directly into each function.
- The `ResultBLF` struct was added to be able to get the number
  of wrapped lines, when calling otherwise unrelated functions
  such as `BLF_draw/BLF_width/BLF_boundbox`,
  which can be passed as the last argument to `BLF_*_ex()` functions.
- The `ResultBLF` struct is used to store the result of drawing
  (currently only the number of lines wrapped, and the width).

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

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
M	source/blender/blenfont/intern/blf_thumbs.c
M	source/blender/blenkernel/intern/image.c
M	source/blender/blenkernel/intern/image_gen.c
M	source/blender/blenkernel/intern/seqeffects.c
M	source/blender/python/generic/blf_py_api.c

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

diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h
index d8dee71..9527c4e 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,17 +94,20 @@ 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, 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, 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. */
-float BLF_height_max(int fontid) ATTR_WARN_UNUSED_RESULT;
+int BLF_height_max(int fontid) ATTR_WARN_UNUSED_RESULT;
 float BLF_width_max(int fontid) ATTR_WARN_UNUSED_RESULT;
 float BLF_descender(int fontid) ATTR_WARN_UNUSED_RESULT;
 float BLF_ascender(int fontid) ATTR_WARN_UNUSED_RESULT;
@@ -137,6 +143,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(int fontid, int wrap_width);
 void BLF_blur(int fontid, int size);
 
 void BLF_enable(int fontid, int option);
@@ -172,7 +179,8 @@ void BLF_buffer_col(int fontid, float r, float g, float b, float a);
 /* Draw the string into the buffer, this function draw in both buffer, float and unsigned char _BUT_
  * it's not necessary set both buffer, NULL is valid here.
  */
-void BLF_draw_buffer(int fontid, const char *str) ATTR_NONNULL();
+void BLF_draw_buffer_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) ATTR_NONNULL(2);
+void BLF_draw_buffer(int fontid, const char *str, size_t len) ATTR_NONNULL(2);
 
 /* Add a path to the font dir paths. */
 void BLF_dir_add(const char *path) ATTR_NONNULL();
@@ -210,6 +218,7 @@ void BLF_state_print(int fontid);
 #define BLF_MATRIX           (1 << 4)
 #define BLF_ASPECT           (1 << 5)
 #define BLF_HINTING          (1 << 6)
+#define BLF_WORD_WRAP        (1 << 7)
 
 #define BLF_DRAW_STR_DUMMY_MAX 1024
 
@@ -217,4 +226,18 @@ 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_WORD_WRAP is enabled (both wrapped and `\n` newline).
+	 */
+	int lines;
+	/**
+	 * The 'cursor' position on completion (ignoring character boundbox).
+	 */
+	int width;
+};
+
 #endif /* __BLF_API_H__ */
diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c
index 127826f..8a48384 100644
--- a/source/blender/blenfont/intern/blf.c
+++ b/source/blender/blenfont/intern/blf.c
@@ -53,6 +53,8 @@
 #include "BIF_gl.h"
 #include "BLF_api.h"
 
+#include "IMB_colormanagement.h"
+
 #include "blf_internal_types.h"
 #include "blf_internal.h"
 
@@ -66,6 +68,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};
 
@@ -491,7 +498,7 @@ void BLF_rotation_default(float angle)
 	}
 }
 
-static void blf_draw__start(FontBLF *font, GLint *mode, GLint *param)
+static void blf_draw_gl__start(FontBLF *font, GLint *mode, GLint *param)
 {
 	/*
 	 * The pixmap alignment hack is handle
@@ -535,7 +542,7 @@ static void blf_draw__start(FontBLF *font, GLint *mode, GLint *param)
 		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 }
 
-static void blf_draw__end(GLint mode, GLint param)
+static void blf_draw_gl__end(GLint mode, GLint param)
 {
 	/* and restore the original value. */
 	if (param != GL_MODULATE)
@@ -554,29 +561,56 @@ 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);
-		blf_font_draw(font, str, len);
-		blf_draw__end(mode, param);
+		blf_draw_gl__start(font, &mode, &param);
+		if (font->flags & BLF_WORD_WRAP) {
+			blf_font_draw__wrap(font, str, len, r_info);
+		}
+		else {
+			blf_font_draw(font, str, len, r_info);
+		}
+		blf_draw_gl__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);
-		blf_font_draw_ascii(font, str, len);
-		blf_draw__end(mode, param);
+		blf_draw_gl__start(font, &mode, &param);
+		if (font->flags & BLF_WORD_WRAP) {
+			/* 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, r_info);
+		}
+		blf_draw_gl__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)
 {
@@ -585,9 +619,9 @@ int BLF_draw_mono(int fontid, const char *str, size_t len, int cwidth)
 	int columns = 0;
 
 	if (font && font->glyph_cache) {
-		blf_draw__start(font, &mode, &param);
+		blf_draw_gl__start(font, &mode, &param);
 		columns = blf_font_draw_mono(font, str, len, cwidth);
-		blf_draw__end(mode, param);
+		blf_draw_gl__end(mode, param);
 	}
 
 	return columns;
@@ -633,21 +667,34 @@ 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_WORD_WRAP) {
+			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)
 {
 	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;
@@ -662,16 +709,24 @@ 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,
+        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);
+		return blf_font_width(font, str, len, r_info);
 	}
 
 	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)
 {
@@ -692,18 +747,26 @@ 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,
+        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);
+		return blf_font_height

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list