[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19041] branches/blender2.5/blender/source /blender: 4 new function, boundbox, width, height and rotation.

Diego Borghetti bdiego at gmail.com
Thu Feb 19 17:39:42 CET 2009


Revision: 19041
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19041
Author:   bdiego
Date:     2009-02-19 17:39:36 +0100 (Thu, 19 Feb 2009)

Log Message:
-----------
4 new function, boundbox, width, height and rotation.

The rotation is through glRotatef and as you can see it's ugly,
the freetype2 allow apply a transformation (2x2 mat) to the glyph
before load, so I want to try using that.
Another thing to add is the 4x4 mat to get the scale and size from
there.. but I need commit this now to continue from my home.

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/blenfont/BLF_api.h
    branches/blender2.5/blender/source/blender/blenfont/intern/blf.c
    branches/blender2.5/blender/source/blender/blenfont/intern/blf_font.c
    branches/blender2.5/blender/source/blender/blenfont/intern/blf_internal.h
    branches/blender2.5/blender/source/blender/blenfont/intern/blf_internal_types.h
    branches/blender2.5/blender/source/blender/editors/space_info/space_info.c

Modified: branches/blender2.5/blender/source/blender/blenfont/BLF_api.h
===================================================================
--- branches/blender2.5/blender/source/blender/blenfont/BLF_api.h	2009-02-19 16:22:07 UTC (rev 19040)
+++ branches/blender2.5/blender/source/blender/blenfont/BLF_api.h	2009-02-19 16:39:36 UTC (rev 19041)
@@ -41,6 +41,11 @@
 void BLF_size(int size, int dpi);
 void BLF_draw(char *str);
 
+void BLF_boundbox(char *str, rctf *box);
+float BLF_width(char *str);
+float BLF_height(char *str);
+void BLF_rotation(float angle);
+
 /* Read the .Blanguages file, return 1 on success or 0 if fails. */
 int BLF_lang_init(void);
 

Modified: branches/blender2.5/blender/source/blender/blenfont/intern/blf.c
===================================================================
--- branches/blender2.5/blender/source/blender/blenfont/intern/blf.c	2009-02-19 16:22:07 UTC (rev 19040)
+++ branches/blender2.5/blender/source/blender/blenfont/intern/blf.c	2009-02-19 16:39:36 UTC (rev 19041)
@@ -29,6 +29,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <math.h>
 
 #ifdef WITH_FREETYPE2
 
@@ -229,9 +230,26 @@
 {
 #ifdef WITH_FREETYPE2
 	FontBLF *font;
+	float remainder;
 
 	font= global_font[global_font_cur];
 	if (font) {
+		remainder= x - floor(x);
+		if (remainder > 0.4 && remainder < 0.6) {
+			if (remainder < 0.5)
+				x -= 0.1 * font->aspect;
+			else
+				x += 0.1 * font->aspect;
+		}
+
+		remainder= y - floor(y);
+		if (remainder > 0.4 && remainder < 0.6) {
+			if (remainder < 0.5)
+				y -= 0.1 * font->aspect;
+			else
+				y += 0.1 * font->aspect;
+		}
+
 		font->pos[0]= x;
 		font->pos[1]= y;
 		font->pos[2]= z;
@@ -263,6 +281,8 @@
 
 		glPushMatrix();
 		glTranslatef(font->pos[0], font->pos[1], font->pos[2]);
+		glScalef(font->aspect, font->aspect, 1.0);
+		glRotatef(font->angle, 0.0f, 0.0f, 1.0f);
 
 		blf_font_draw(font, str);
 
@@ -272,3 +292,49 @@
 	}
 #endif /* WITH_FREETYPE2 */
 }
+
+void BLF_boundbox(char *str, rctf *box)
+{
+#ifdef WITH_FREETYPE2
+	FontBLF *font;
+
+	font= global_font[global_font_cur];
+	if (font && font->glyph_cache)
+		blf_font_boundbox(font, str, box);
+#endif
+}
+
+float BLF_width(char *str)
+{
+#ifdef WITH_FREETYPE2
+	FontBLF *font;
+
+	font= global_font[global_font_cur];
+	if (font && font->glyph_cache)
+		return(blf_font_width(font, str));
+#endif
+	return(0.0f);
+}
+
+float BLF_height(char *str)
+{
+#ifdef WITH_FREETYPE2
+	FontBLF *font;
+
+	font= global_font[global_font_cur];
+	if (font && font->glyph_cache)
+		return(blf_font_height(font, str));
+#endif
+	return(0.0f);
+}
+
+void BLF_rotation(float angle)
+{
+#ifdef WITH_FREETYPE2
+	FontBLF *font;
+
+	font= global_font[global_font_cur];
+	if (font)
+		font->angle= angle;
+#endif
+}

Modified: branches/blender2.5/blender/source/blender/blenfont/intern/blf_font.c
===================================================================
--- branches/blender2.5/blender/source/blender/blenfont/intern/blf_font.c	2009-02-19 16:22:07 UTC (rev 19040)
+++ branches/blender2.5/blender/source/blender/blenfont/intern/blf_font.c	2009-02-19 16:39:36 UTC (rev 19041)
@@ -78,9 +78,7 @@
 	font->aspect= 1.0f;
 	font->pos[0]= 0.0f;
 	font->pos[1]= 0.0f;
-	font->angle[0]= 0.0f;
-	font->angle[1]= 0.0f;
-	font->angle[2]= 0.0f;
+	font->angle= 0.0f;
 	Mat4One(font->mat);
 	font->clip_rec.xmin= 0.0f;
 	font->clip_rec.xmax= 0.0f;
@@ -217,6 +215,92 @@
 	}
 }
 
+void blf_font_boundbox(FontBLF *font, char *str, rctf *box)
+{
+	unsigned int c;
+	GlyphBLF *g, *g_prev;
+	FT_Vector delta;
+	FT_UInt glyph_index;
+	rctf gbox;
+	int pen_x, pen_y;
+	int i, has_kerning;
+
+	box->xmin= 32000.0f;
+	box->xmax= -32000.0f;
+	box->ymin= 32000.0f;
+	box->ymax= -32000.0f;
+
+	i= 0;
+	pen_x= 0;
+	pen_y= 0;
+	has_kerning= FT_HAS_KERNING(font->face);
+	g_prev= NULL;
+
+	while (str[i]) {
+		c= blf_utf8_next((unsigned char *)str, &i);
+		if (c == 0)
+			break;
+
+		glyph_index= FT_Get_Char_Index(font->face, c);
+		g= blf_glyph_search(font->glyph_cache, glyph_index);
+		if (!g)
+			g= blf_glyph_add(font, glyph_index, c);
+
+		/* if we don't found a glyph, skip it. */
+		if (!g)
+			continue;
+
+		if (has_kerning && g_prev) {
+			delta.x= 0;
+			delta.y= 0;
+
+			FT_Get_Kerning(font->face, g_prev->index, glyph_index, FT_KERNING_UNFITTED, &delta);
+			pen_x += delta.x >> 6;
+		}
+
+		gbox.xmin= g->box.xmin + pen_x;
+		gbox.xmax= g->box.xmax + pen_x;
+		gbox.ymin= g->box.ymin + pen_y;
+		gbox.ymax= g->box.ymax + pen_y;
+
+		if (gbox.xmin < box->xmin)
+			box->xmin= gbox.xmin;
+		if (gbox.ymin < box->ymin)
+			box->ymin= gbox.ymin;
+
+		if (gbox.xmax > box->xmax)
+			box->xmax= gbox.xmax;
+		if (gbox.ymax > box->ymax)
+			box->ymax= gbox.ymax;
+
+		pen_x += g->advance;
+		g_prev= g;
+	}
+
+	if (box->xmin > box->xmax) {
+		box->xmin= 0.0f;
+		box->ymin= 0.0f;
+		box->xmax= 0.0f;
+		box->ymax= 0.0f;
+	}
+}
+
+float blf_font_width(FontBLF *font, char *str)
+{
+	rctf box;
+
+	blf_font_boundbox(font, str, &box);
+	return((box.xmax - box.xmin) * font->aspect);
+}
+
+float blf_font_height(FontBLF *font, char *str)
+{
+	rctf box;
+
+	blf_font_boundbox(font, str, &box);
+	return((box.ymax - box.ymin) * font->aspect);
+}
+
 void blf_font_free(FontBLF *font)
 {
 	GlyphCacheBLF *gc;

Modified: branches/blender2.5/blender/source/blender/blenfont/intern/blf_internal.h
===================================================================
--- branches/blender2.5/blender/source/blender/blenfont/intern/blf_internal.h	2009-02-19 16:22:07 UTC (rev 19040)
+++ branches/blender2.5/blender/source/blender/blenfont/intern/blf_internal.h	2009-02-19 16:39:36 UTC (rev 19041)
@@ -46,6 +46,9 @@
 void blf_font_free(FontBLF *font);
 void blf_font_size(FontBLF *font, int size, int dpi);
 void blf_font_draw(FontBLF *font, char *str);
+void blf_font_boundbox(FontBLF *font, char *str, rctf *box);
+float blf_font_width(FontBLF *font, char *str);
+float blf_font_height(FontBLF *font, char *str);
 
 GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, int size, int dpi);
 GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font);

Modified: branches/blender2.5/blender/source/blender/blenfont/intern/blf_internal_types.h
===================================================================
--- branches/blender2.5/blender/source/blender/blenfont/intern/blf_internal_types.h	2009-02-19 16:22:07 UTC (rev 19040)
+++ branches/blender2.5/blender/source/blender/blenfont/intern/blf_internal_types.h	2009-02-19 16:39:36 UTC (rev 19041)
@@ -143,7 +143,7 @@
 	float pos[3];
 
 	/* angle in degrees. */
-	float angle[3];
+	float angle;
 
 	/* this is the matrix that we load before rotate/scale/translate. */
 	float mat[4][4];

Modified: branches/blender2.5/blender/source/blender/editors/space_info/space_info.c
===================================================================
--- branches/blender2.5/blender/source/blender/editors/space_info/space_info.c	2009-02-19 16:22:07 UTC (rev 19040)
+++ branches/blender2.5/blender/source/blender/editors/space_info/space_info.c	2009-02-19 16:39:36 UTC (rev 19041)
@@ -133,7 +133,8 @@
 	// SpaceInfo *sinfo= (SpaceInfo*)CTX_wm_space_data(C);
 	View2D *v2d= &ar->v2d;
 	float col[3];
-	
+	float width, height;
+
 	/* clear and setup matrix */
 	UI_GetThemeColor3fv(TH_BACK, col);
 	glClearColor(col[0], col[1], col[2], 0.0);
@@ -148,21 +149,30 @@
 
 	BLF_size(14, 96);
 	BLF_position(5.0, 5.0, 0.0);
-	BLF_draw("Hello Blender, size 14, dpi 96");	
 
+	width= BLF_width("Hello Blender, size 14, dpi 96");
+	height= BLF_height("Hello Blender, size 14, dpi 96");
+
+	glRectf(7.0, 20.0, 7.0+width, 20.0+height);
+	glRectf(5.0+width+10.0, 3.0, 5.0+width+10.0+width, 3.0+height);
+	BLF_draw("Hello Blender, size 14, dpi 96");
+
 	glColor3f(0.0, 0.0, 1.0);
 	BLF_size(11, 96);
 	BLF_position(200.0, 50.0, 0.0);
+	BLF_rotation(45.0f);
 	BLF_draw("Another Hello Blender, size 11 and dpi 96!!");
 
 	glColor3f(0.8, 0.0, 0.7);
 	BLF_size(12, 72);
-	BLF_position(5.0, 100.0, 0.0);
+	BLF_position(200.0, 100.0, 0.0);
+	BLF_rotation(180.0f);
 	BLF_draw("Hello World, size 12, dpi 72");
 	
 	glColor3f(0.8, 0.7, 0.5);
 	BLF_size(12, 96);
 	BLF_position(5.0, 200.0, 0.0);
+	BLF_rotation(0.0f);
 	BLF_draw("And this make a new glyph cache!!");
 
 	/* reset view matrix */





More information about the Bf-blender-cvs mailing list