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]; diff --git a/source/blender/editors/space_info/space_info.c b/source/blender/editors/space_info/space_info.c index 6d7fb5a1cdf..f1968114b14 100644 --- a/source/blender/editors/space_info/space_info.c +++ b/source/blender/editors/space_info/space_info.c @@ -133,7 +133,8 @@ static void info_main_area_draw(const bContext *C, ARegion *ar) // 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 @@ static void info_main_area_draw(const bContext *C, ARegion *ar) 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 */