From 6bf9f383dc3d0dc271dd3ffa3e22becea5675530 Mon Sep 17 00:00:00 2001 From: Diego Borghetti Date: Thu, 19 Feb 2009 16:39:36 +0000 Subject: 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. --- source/blender/blenfont/BLF_api.h | 5 ++ source/blender/blenfont/intern/blf.c | 66 ++++++++++++++++ source/blender/blenfont/intern/blf_font.c | 90 +++++++++++++++++++++- source/blender/blenfont/intern/blf_internal.h | 3 + .../blender/blenfont/intern/blf_internal_types.h | 2 +- 5 files changed, 162 insertions(+), 4 deletions(-) (limited to 'source/blender/blenfont') diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h index cec3c81ee13..decdbec8d7e 100644 --- a/source/blender/blenfont/BLF_api.h +++ b/source/blender/blenfont/BLF_api.h @@ -41,6 +41,11 @@ void BLF_position(float x, float y, float z); 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); diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c index 491d39aa301..1b29f3ebdd8 100644 --- a/source/blender/blenfont/intern/blf.c +++ b/source/blender/blenfont/intern/blf.c @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef WITH_FREETYPE2 @@ -229,9 +230,26 @@ void BLF_position(float x, float y, float z) { #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 @@ void BLF_draw(char *str) 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 @@ void BLF_draw(char *str) } #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 +} diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c index 62a64851109..825de7a62d2 100644 --- a/source/blender/blenfont/intern/blf_font.c +++ b/source/blender/blenfont/intern/blf_font.c @@ -78,9 +78,7 @@ void blf_font_fill(FontBLF *font) 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_draw(FontBLF *font, char *str) } } +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; diff --git a/source/blender/blenfont/intern/blf_internal.h b/source/blender/blenfont/intern/blf_internal.h index 6bb7578935f..6afb019c5b4 100644 --- a/source/blender/blenfont/intern/blf_internal.h +++ b/source/blender/blenfont/intern/blf_internal.h @@ -46,6 +46,9 @@ FontBLF *blf_font_new_from_mem(char *name, unsigned char *mem, int mem_size); 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); diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index f8d5159bd9b..a0198283869 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -143,7 +143,7 @@ typedef struct FontBLF { 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]; -- cgit v1.2.3