[Bf-blender-cvs] [1815225] master: Blender Font (BLF): add length argument to string width/height functions

Campbell Barton noreply at git.blender.org
Mon Dec 2 11:16:26 CET 2013


Commit: 1815225faa75eb64e83fdc9f066fcd6339502d52
Author: Campbell Barton
Date:   Mon Dec 2 20:33:45 2013 +1100
http://developer.blender.org/rB1815225faa75eb64e83fdc9f066fcd6339502d52

Blender Font (BLF): add length argument to string width/height functions

This also fixes a crash editing buttons longer then UI_MAX_DRAW_STR

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

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/blenkernel/intern/image.c
M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/interface/interface.c
M	source/blender/editors/interface/interface_handlers.c
M	source/blender/editors/interface/interface_regions.c
M	source/blender/editors/interface/interface_style.c
M	source/blender/editors/interface/interface_templates.c
M	source/blender/editors/interface/interface_widgets.c
M	source/blender/editors/interface/view2d.c
M	source/blender/editors/screen/area.c
M	source/blender/editors/space_clip/clip_dopesheet_draw.c
M	source/blender/editors/space_clip/clip_draw.c
M	source/blender/editors/space_file/filesel.c
M	source/blender/editors/space_image/image_draw.c
M	source/blender/editors/space_node/drawnode.c
M	source/blender/editors/space_view3d/view3d_ruler.c
M	source/blender/editors/transform/transform.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 8f77f8c..b25166f 100644
--- a/source/blender/blenfont/BLF_api.h
+++ b/source/blender/blenfont/BLF_api.h
@@ -82,14 +82,14 @@ int BLF_draw_mono(int fontid, const char *str, size_t len, int cwidth);
 /* This function return the bounding box of the string
  * and are not multiplied by the aspect.
  */
-void BLF_boundbox(int fontid, const char *str, struct rctf *box);
+void BLF_boundbox(int fontid, const char *str, size_t len, struct rctf *box);
 
 /* 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(int fontid, const char *str);
-float BLF_height(int fontid, const char *str);
+float BLF_width(int fontid, const char *str, size_t len);
+float BLF_height(int fontid, const char *str, size_t len);
 
 /* Return dimensions of the font without any sample text. */
 float BLF_height_max(int fontid);
@@ -100,7 +100,7 @@ float BLF_ascender(int fontid);
 /* The following function return the width and height of the string, but
  * just in one call, so avoid extra freetype2 stuff.
  */
-void BLF_width_and_height(int fontid, const char *str, float *width, float *height);
+void BLF_width_and_height(int fontid, const char *str, size_t len, float *r_width, float *r_height);
 
 /* For fixed width fonts only, returns the width of a
  * character.
@@ -111,9 +111,9 @@ float BLF_fixed_width(int fontid);
  * of the string, using the default font and both value
  * are multiplied by the aspect of the font.
  */
-void  BLF_width_and_height_default(const char *str, float *width, float *height);
-float BLF_width_default(const char *str);
-float BLF_height_default(const char *str);
+void  BLF_width_and_height_default(const char *str, size_t len, float *r_width, float *r_height);
+float BLF_width_default(const char *str, size_t len);
+float BLF_height_default(const char *str, size_t len);
 
 /* Set rotation for default font. */
 void BLF_rotation_default(float angle);
diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c
index 130eaae..746ee3c 100644
--- a/source/blender/blenfont/intern/blf.c
+++ b/source/blender/blenfont/intern/blf.c
@@ -592,44 +592,44 @@ int BLF_draw_mono(int fontid, const char *str, size_t len, int cwidth)
 	return columns;
 }
 
-void BLF_boundbox(int fontid, const char *str, rctf *box)
+void BLF_boundbox(int fontid, const char *str, size_t len, rctf *box)
 {
 	FontBLF *font = blf_get(fontid);
 
 	if (font) {
-		blf_font_boundbox(font, str, box);
+		blf_font_boundbox(font, str, len, box);
 	}
 }
 
-void BLF_width_and_height(int fontid, const char *str, float *width, float *height)
+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, width, height);
+		blf_font_width_and_height(font, str, len, r_width, r_height);
 	}
 	else {
-		*width = *height = 0.0f;
+		*r_width = *r_height = 0.0f;
 	}
 }
 
-void BLF_width_and_height_default(const char *str, float *width, float *height)
+void BLF_width_and_height_default(const char *str, size_t len, float *r_width, float *r_height)
 {
 	if (!blf_global_font_init()) {
-		*width = *height = 0.0f;
+		*r_width = *r_height = 0.0f;
 		return;
 	}
 
 	BLF_size(global_font_default, global_font_points, global_font_dpi);
-	BLF_width_and_height(global_font_default, str, width, height);
+	BLF_width_and_height(global_font_default, str, len, r_width, r_height);
 }
 
-float BLF_width(int fontid, const char *str)
+float BLF_width(int fontid, const char *str, size_t len)
 {
 	FontBLF *font = blf_get(fontid);
 
 	if (font && font->glyph_cache) {
-		return blf_font_width(font, str);
+		return blf_font_width(font, str, len);
 	}
 
 	return 0.0f;
@@ -646,21 +646,21 @@ float BLF_fixed_width(int fontid)
 	return 0.0f;
 }
 
-float BLF_width_default(const char *str)
+float BLF_width_default(const char *str, size_t len)
 {
 	if (!blf_global_font_init())
 		return 0.0f;
 
 	BLF_size(global_font_default, global_font_points, global_font_dpi);
-	return BLF_width(global_font_default, str);
+	return BLF_width(global_font_default, str, len);
 }
 
-float BLF_height(int fontid, const char *str)
+float BLF_height(int fontid, const char *str, size_t len)
 {
 	FontBLF *font = blf_get(fontid);
 
 	if (font && font->glyph_cache) {
-		return blf_font_height(font, str);
+		return blf_font_height(font, str, len);
 	}
 
 	return 0.0f;
@@ -710,14 +710,14 @@ float BLF_ascender(int fontid)
 	return 0.0f;
 }
 
-float BLF_height_default(const char *str)
+float BLF_height_default(const char *str, size_t len)
 {
 	if (!blf_global_font_init())
 		return 0.0f;
 
 	BLF_size(global_font_default, global_font_points, global_font_dpi);
 
-	return BLF_height(global_font_default, str);
+	return BLF_height(global_font_default, str, len);
 }
 
 void BLF_rotation(int fontid, float angle)
diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c
index 998b415..4094322 100644
--- a/source/blender/blenfont/intern/blf_font.c
+++ b/source/blender/blenfont/intern/blf_font.c
@@ -415,7 +415,7 @@ void blf_font_buffer(FontBLF *font, const char *str)
 	}
 }
 
-void blf_font_boundbox(FontBLF *font, const char *str, rctf *box)
+void blf_font_boundbox(FontBLF *font, const char *str, size_t len, rctf *box)
 {
 	unsigned int c;
 	GlyphBLF *g, *g_prev = NULL;
@@ -435,7 +435,7 @@ void blf_font_boundbox(FontBLF *font, const char *str, rctf *box)
 
 	blf_font_ensure_ascii_table(font);
 
-	while (str[i]) {
+	while ((i < len) && str[i]) {
 		BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
 
 		if (c == BLI_UTF8_ERR)
@@ -468,7 +468,7 @@ void blf_font_boundbox(FontBLF *font, const char *str, rctf *box)
 	}
 }
 
-void blf_font_width_and_height(FontBLF *font, const char *str, float *width, float *height)
+void blf_font_width_and_height(FontBLF *font, const char *str, size_t len, float *width, float *height)
 {
 	float xa, ya;
 	rctf box;
@@ -482,12 +482,12 @@ void blf_font_width_and_height(FontBLF *font, const char *str, float *width, flo
 		ya = 1.0f;
 	}
 
-	blf_font_boundbox(font, str, &box);
+	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)
+float blf_font_width(FontBLF *font, const char *str, size_t len)
 {
 	float xa;
 	rctf box;
@@ -497,11 +497,11 @@ float blf_font_width(FontBLF *font, const char *str)
 	else
 		xa = 1.0f;
 
-	blf_font_boundbox(font, str, &box);
+	blf_font_boundbox(font, str, len, &box);
 	return BLI_rctf_size_x(&box) * xa;
 }
 
-float blf_font_height(FontBLF *font, const char *str)
+float blf_font_height(FontBLF *font, const char *str, size_t len)
 {
 	float ya;
 	rctf box;
@@ -511,7 +511,7 @@ float blf_font_height(FontBLF *font, const char *str)
 	else
 		ya = 1.0f;
 
-	blf_font_boundbox(font, str, &box);
+	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 7d4b39d..9f6bad9 100644
--- a/source/blender/blenfont/intern/blf_internal.h
+++ b/source/blender/blenfont/intern/blf_internal.h
@@ -55,10 +55,10 @@ void blf_font_draw(struct FontBLF *font, const char *str, size_t len);
 void blf_font_draw_ascii(struct FontBLF *font, const char *str, size_t len);
 int blf_font_draw_mono(struct FontBLF *font, const char *str, size_t len, int cwidth);
 void blf_font_buffer(struct FontBLF *font, const char *str);
-void blf_font_boundbox(struct FontBLF *font, const char *str, struct rctf *box);
-void blf_font_width_and_height(struct FontBLF *font, const char *str, float *width, float *height);
-float blf_font_width(struct FontBLF *font, const char *str);
-float blf_font_height(struct FontBLF *font, const char *str);
+void blf_font_boundbox(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);
 float blf_font_fixed_width(struct FontBLF *font);
 void blf_font_free(struct FontBLF *font);
 
diff --git a/source/blender/blenkernel/intern/image.c b/source/blender/blenkernel/intern/image.c
index e4278ba..d59a23c 100644
--- a/source/blender/blenkernel/intern/image.c
+++ b/source/blender/blenkernel/intern/image.c
@@ -1636,6 +1636,9 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
 	/* this could be an argument if we want to operate on non linear float imbuf's
 	 * for now though this is only used for renders which use scene settings */
 
+#define TEXT_SIZE_CHECK(str, w, h) \
+	((str[0]) && ((void)(h = h_fixed), (w = BLF_width(mono, str, sizeof(str)))))
+
 #define BUFF_MARGIN_X 2
 #define BUFF_MARGIN_Y 1
 
@@ -1665,9 +1668,8 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
 	x = 0;
 	y = height;
 
-	if (stamp_data.file[0]) {
+	if (TEXT_SIZE_CHECK(stamp_data.file, w, h)) {
 		/* Top left corner */
-		BLF_width_and_height(mono, stamp_data.file, &w, &h); h = h_fixed;
 		y -= h;
 
 		/* also a little of space to the background. */
@@ -1683,8 +1685,7 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
 	}
 
 	/* Top left corner, below File */
-	if (stamp_data.note[0]) {
-		BLF_width_and_height(mono, stamp_data.note, &w, &h); h = h_fixed;
+	if (TEXT_SIZE_CHECK(stamp_data.note, w, h)) {
 		y -= h;
 
 		/* and space for background. */
@@ -1699,8 +1700,7 @@ void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rec
 	}
 
 	/* Top left corner, below File (or Note) */
-	if (stamp_data.date[0]) {
-		BLF_width_and_height(mono, stamp_data.date, &w, &h); h = h_fixed;
+	if (TEXT_SIZE_CHECK(stamp_data.date, w, h)) {
 		y -= h;
 
 		/* and space for background. */
@@ -1715,8 +1715,7 @@ void BKE_stamp_buf(S

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list