[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [22608] branches/blender2.5/blender/source /blender: Stamp info back only for float buffer.

Diego Borghetti bdiego at gmail.com
Tue Aug 18 21:26:53 CEST 2009


Revision: 22608
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=22608
Author:   bdiego
Date:     2009-08-18 21:26:53 +0200 (Tue, 18 Aug 2009)

Log Message:
-----------
Stamp info back only for float buffer. (next commit add unsigned char).

A couple of new functions:
	BLF_width_and_height	- Merge of BLF_width and BLF_height in one call to avoid freetype2 stuff.
	BLF_buffer		- Set the buffer, size and number of channel.
	BLF_buffer_col		- Set the text color (the alpha is not used right now).
	BLF_draw_buffer		- Draw the text in the current buffer.

Also tweak a little the boundbox and draw function to avoid access the freetype2 and use the cache info.
By default the font size is 12, the UI still need work to allow change the font and size.

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_glyph.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/blenkernel/intern/Makefile
    branches/blender2.5/blender/source/blender/blenkernel/intern/image.c

Modified: branches/blender2.5/blender/source/blender/blenfont/BLF_api.h
===================================================================
--- branches/blender2.5/blender/source/blender/blenfont/BLF_api.h	2009-08-18 18:06:11 UTC (rev 22607)
+++ branches/blender2.5/blender/source/blender/blenfont/BLF_api.h	2009-08-18 19:26:53 UTC (rev 22608)
@@ -70,6 +70,11 @@
 float BLF_width(char *str);
 float BLF_height(char *str);
 
+/*
+ * 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(char *str, float *width, float *height);
 
 /*
  * For fixed width fonts only, returns the width of a
@@ -117,6 +122,28 @@
 void BLF_shadow_offset(int x, int y);
 
 /*
+ * Set the buffer, size and number of channels to draw, one thing to take care is call
+ * this function with NULL pointer when we finish, for example:
+ *	BLF_buffer(my_fbuf, my_cbuf, 100, 100, 4);
+ *
+ *	... set color, position and draw ...
+ *
+ *	BLF_buffer(NULL, NULL, 0, 0, 0);
+ */
+void BLF_buffer(float *fbuf, unsigned char *cbuf, unsigned int w, unsigned int h, int nch);
+
+/*
+ * Set the color to be used for text.
+ */
+void BLF_buffer_col(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(char *str);
+
+/*
  * Search the path directory to the locale files, this try all
  * the case for Linux, Win and Mac.
  */

Modified: branches/blender2.5/blender/source/blender/blenfont/intern/blf.c
===================================================================
--- branches/blender2.5/blender/source/blender/blenfont/intern/blf.c	2009-08-18 18:06:11 UTC (rev 22607)
+++ branches/blender2.5/blender/source/blender/blenfont/intern/blf.c	2009-08-18 19:26:53 UTC (rev 22608)
@@ -371,6 +371,15 @@
 		blf_font_boundbox(font, str, box);
 }
 
+void BLF_width_and_height(char *str, float *width, float *height)
+{
+	FontBLF *font;
+
+	font= global_font[global_font_cur];
+	if (font)
+		blf_font_width_and_height(font, str, width, height);
+}
+
 float BLF_width(char *str)
 {
 	FontBLF *font;
@@ -513,3 +522,39 @@
 		font->shadow_y= y;
 	}
 }
+
+void BLF_buffer(float *fbuf, unsigned char *cbuf, unsigned int w, unsigned int h, int nch)
+{
+	FontBLF *font;
+
+	font= global_font[global_font_cur];
+	if (font) {
+		font->b_fbuf= fbuf;
+		font->b_cbuf= cbuf;
+		font->bw= w;
+		font->bh= h;
+		font->bch= nch;
+	}
+}
+
+void BLF_buffer_col(float r, float g, float b, float a)
+{
+	FontBLF *font;
+
+	font= global_font[global_font_cur];
+	if (font) {
+		font->b_col[0]= r;
+		font->b_col[1]= g;
+		font->b_col[2]= b;
+		font->b_col[3]= a;
+	}
+}
+
+void BLF_draw_buffer(char *str)
+{
+	FontBLF *font;
+
+	font= global_font[global_font_cur];
+	if (font)
+		blf_font_buffer(font, str);
+}

Modified: branches/blender2.5/blender/source/blender/blenfont/intern/blf_font.c
===================================================================
--- branches/blender2.5/blender/source/blender/blenfont/intern/blf_font.c	2009-08-18 18:06:11 UTC (rev 22607)
+++ branches/blender2.5/blender/source/blender/blenfont/intern/blf_font.c	2009-08-18 19:26:53 UTC (rev 22608)
@@ -100,7 +100,7 @@
 	unsigned int c;
 	GlyphBLF *g, *g_prev;
 	FT_Vector delta;
-	FT_UInt glyph_index, g_prev_index;
+	FT_UInt glyph_index;
 	int pen_x, pen_y;
 	int i, has_kerning, st;
 
@@ -112,17 +112,17 @@
 	pen_y= 0;
 	has_kerning= FT_HAS_KERNING(font->face);
 	g_prev= NULL;
-	g_prev_index= 0;
 
 	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, c);
-		if (!g)
+		if (!g) {
+			glyph_index= FT_Get_Char_Index(font->face, c);
 			g= blf_glyph_add(font, glyph_index, c);
+		}
 
 		/* if we don't found a glyph, skip it. */
 		if (!g)
@@ -133,9 +133,9 @@
 			delta.y= 0;
 
 			if (font->flags & BLF_KERNING_DEFAULT)
-				st= FT_Get_Kerning(font->face, g_prev_index, glyph_index, ft_kerning_default, &delta);
+				st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
 			else
-				st= FT_Get_Kerning(font->face, g_prev_index, glyph_index, FT_KERNING_UNFITTED, &delta);
+				st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
 
 			if (st == 0)
 				pen_x += delta.x >> 6;
@@ -146,16 +146,138 @@
 
 		pen_x += g->advance;
 		g_prev= g;
-		g_prev_index= glyph_index;
 	}
 }
 
+void blf_font_buffer(FontBLF *font, char *str)
+{
+	unsigned char *data;
+	unsigned int c;
+	GlyphBLF *g, *g_prev;
+	FT_Vector delta;
+	FT_UInt glyph_index;
+	float a, *fbuf;
+	int pen_x, pen_y, y, x, yb, diff;
+	int i, has_kerning, st, chx, chy;
+
+	if (!font->glyph_cache)
+		return;
+
+	i= 0;
+	pen_x= (int)font->pos[0];
+	pen_y= (int)font->pos[1];
+	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;
+
+		g= blf_glyph_search(font->glyph_cache, c);
+		if (!g) {
+			glyph_index= FT_Get_Char_Index(font->face, c);
+			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;
+
+			if (font->flags & BLF_KERNING_DEFAULT)
+				st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
+			else
+				st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
+
+			if (st == 0)
+				pen_x += delta.x >> 6;
+		}
+
+		if (font->b_fbuf) {
+			chx= pen_x + ((int)g->pos_x);
+
+			diff= g->height - ((int)g->pos_y);
+
+			if (diff > 0) {
+				if (g->pitch < 0)
+					pen_y += diff;
+				else
+					pen_y -= diff;
+			}
+			else if (diff < 0) {
+				if (g->pitch < 0)
+					pen_y -= diff;
+				else
+					pen_y += diff;
+			}
+
+
+			if (g->pitch < 0)
+				chy= pen_y - ((int)g->pos_y);
+			else
+				chy= pen_y + ((int)g->pos_y);
+
+			if (chx >= 0 && chx < font->bw && pen_y >= 0 && pen_y < font->bh) {
+				if (g->pitch < 0)
+					yb= 0;
+				else
+					yb= g->height-1;
+
+				for (y= 0; y < g->height; y++) {
+					for (x= 0; x < g->width; x++) {
+						fbuf= font->b_fbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
+						data= g->bitmap + x + (yb * g->pitch);
+						a= data[0]/255.0f;
+
+						if (a == 1.0) {
+							fbuf[0]= font->b_col[0];
+							fbuf[1]= font->b_col[1];
+							fbuf[2]= font->b_col[2];
+						}
+						else {
+							fbuf[0]= (font->b_col[0]*a) + (fbuf[0] * (1-a));
+							fbuf[1]= (font->b_col[1]*a) + (fbuf[1] * (1-a));
+							fbuf[2]= (font->b_col[2]*a) + (fbuf[2] * (1-a));
+						}
+					}
+
+					if (g->pitch < 0)
+						yb++;
+					else
+						yb--;
+				}
+			}
+
+			if (diff > 0) {
+				if (g->pitch < 0)
+					pen_x -= diff;
+				else
+					pen_y += diff;
+			}
+			else if (diff < 0) {
+				if (g->pitch < 0)
+					pen_x += diff;
+				else
+					pen_y -= diff;
+			}
+
+		}
+
+		pen_x += g->advance;
+		g_prev= g;
+	}
+}
+
 void blf_font_boundbox(FontBLF *font, char *str, rctf *box)
 {
 	unsigned int c;
 	GlyphBLF *g, *g_prev;
 	FT_Vector delta;
-	FT_UInt glyph_index, g_prev_index;
+	FT_UInt glyph_index;
 	rctf gbox;
 	int pen_x, pen_y;
 	int i, has_kerning, st;
@@ -173,17 +295,17 @@
 	pen_y= 0;
 	has_kerning= FT_HAS_KERNING(font->face);
 	g_prev= NULL;
-	g_prev_index= 0;
 
 	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, c);
-		if (!g)
+		if (!g) {
+			glyph_index= FT_Get_Char_Index(font->face, c);
 			g= blf_glyph_add(font, glyph_index, c);
+		}
 
 		/* if we don't found a glyph, skip it. */
 		if (!g)
@@ -194,9 +316,9 @@
 			delta.y= 0;
 
 			if (font->flags & BLF_KERNING_DEFAULT)
-				st= FT_Get_Kerning(font->face, g_prev_index, glyph_index, ft_kerning_default, &delta);
+				st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
 			else
-				st= FT_Get_Kerning(font->face, g_prev_index, glyph_index, FT_KERNING_UNFITTED, &delta);
+				st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
 
 			if (st == 0)
 				pen_x += delta.x >> 6;
@@ -219,7 +341,6 @@
 
 		pen_x += g->advance;
 		g_prev= g;
-		g_prev_index= glyph_index;
 	}
 
 	if (box->xmin > box->xmax) {
@@ -230,6 +351,17 @@
 	}
 }
 
+void blf_font_width_and_height(FontBLF *font, char *str, float *width, float *height)
+{
+	rctf box;
+
+	if (font->glyph_cache) {
+		blf_font_boundbox(font, str, &box);
+		*width= ((box.xmax - box.xmin) * font->aspect);
+		*height= ((box.ymax - box.ymin) * font->aspect);
+	}
+}
+
 float blf_font_width(FontBLF *font, char *str)
 {
 	rctf box;
@@ -311,6 +443,15 @@
 	font->glyph_cache= NULL;
 	font->blur= 0;
 	font->max_tex_size= -1;
+	font->b_fbuf= NULL;
+	font->b_cbuf= NULL;
+	font->bw= 0;
+	font->bh= 0;
+	font->bch= 0;
+	font->b_col[0]= 0;
+	font->b_col[1]= 0;
+	font->b_col[2]= 0;
+	font->b_col[3]= 0;
 }
 
 FontBLF *blf_font_new(char *name, char *filename)

Modified: branches/blender2.5/blender/source/blender/blenfont/intern/blf_glyph.c
===================================================================
--- branches/blender2.5/blender/source/blender/blenfont/intern/blf_glyph.c	2009-08-18 18:06:11 UTC (rev 22607)
+++ branches/blender2.5/blender/source/blender/blenfont/intern/blf_glyph.c	2009-08-18 19:26:53 UTC (rev 22608)
@@ -217,6 +217,7 @@
 	g->next= NULL;
 	g->prev= NULL;
 	g->c= c;
+	g->idx= index;
 	g->tex= 0;
 	g->build_tex= 0;
 	g->bitmap= NULL;
@@ -238,6 +239,7 @@
 	g->advance= ((float)slot->advance.x) / 64.0f;
 	g->pos_x= slot->bitmap_left;
 	g->pos_y= slot->bitmap_top;
+	g->pitch= slot->bitmap.pitch;
 
 	FT_Outline_Get_CBox(&(slot->outline), &bbox);
 	g->box.xmin= ((float)bbox.xMin) / 64.0f;

Modified: branches/blender2.5/blender/source/blender/blenfont/intern/blf_internal.h
===================================================================
--- branches/blender2.5/blender/source/blender/blenfont/intern/blf_internal.h	2009-08-18 18:06:11 UTC (rev 22607)
+++ branches/blender2.5/blender/source/blender/blenfont/intern/blf_internal.h	2009-08-18 19:26:53 UTC (rev 22608)
@@ -45,7 +45,9 @@
 
 void blf_font_size(FontBLF *font, int size, int dpi);
 void blf_font_draw(FontBLF *font, char *str);
+void blf_font_buffer(FontBLF *font, char *str);
 void blf_font_boundbox(FontBLF *font, char *str, rctf *box);
+void blf_font_width_and_height(FontBLF *font, char *str, float *width, float *height);

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list