From 249b41762a98b5e357cc32e0ba0cb673fc0c6225 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 18 Sep 2011 09:48:09 +0000 Subject: blf code - no functional changes. - remove saniy checks from blf_font.c, the callers now check instead. - move duplicate code into defines (may move into static functions). - move kerning checks into const values set at the start of the function, rather then checking on every character. --- source/blender/blenfont/intern/blf.c | 29 ++--- source/blender/blenfont/intern/blf_font.c | 196 ++++++++++-------------------- source/blender/blenfont/intern/blf_lang.c | 5 - 3 files changed, 74 insertions(+), 156 deletions(-) diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c index 577697de594..07291fbb3e3 100644 --- a/source/blender/blenfont/intern/blf.c +++ b/source/blender/blenfont/intern/blf.c @@ -505,7 +505,7 @@ static void blf_draw__end(void) void BLF_draw(int fontid, const char *str, size_t len) { FontBLF *font= BLF_get(fontid); - if (font) { + if (font && font->glyph_cache) { blf_draw__start(font); blf_font_draw(font, str, len); blf_draw__end(); @@ -515,7 +515,7 @@ void BLF_draw(int fontid, const char *str, size_t len) void BLF_draw_ascii(int fontid, const char *str, size_t len) { FontBLF *font= BLF_get(fontid); - if (font) { + if (font && font->glyph_cache) { blf_draw__start(font); blf_font_draw_ascii(font, str, len); blf_draw__end(); @@ -536,7 +536,7 @@ void BLF_width_and_height(int fontid, const char *str, float *width, float *heig FontBLF *font; font= BLF_get(fontid); - if (font) + if (font && font->glyph_cache) blf_font_width_and_height(font, str, width, height); } @@ -545,7 +545,7 @@ float BLF_width(int fontid, const char *str) FontBLF *font; font= BLF_get(fontid); - if (font) + if (font && font->glyph_cache) return(blf_font_width(font, str)); return(0.0f); } @@ -555,9 +555,9 @@ float BLF_fixed_width(int fontid) FontBLF *font; font= BLF_get(fontid); - if (font) - return(blf_font_fixed_width(font)); - return(0.0f); + if (font && font->glyph_cache) + return blf_font_fixed_width(font); + return 0.0f; } float BLF_width_default(const char *str) @@ -582,7 +582,7 @@ float BLF_height(int fontid, const char *str) FontBLF *font; font= BLF_get(fontid); - if (font) + if (font && font->glyph_cache) return(blf_font_height(font, str)); return(0.0f); } @@ -592,10 +592,8 @@ float BLF_height_max(int fontid) FontBLF *font; font= BLF_get(fontid); - if (font) { - if(font->glyph_cache) - return(font->glyph_cache->max_glyph_height); - } + if (font && font->glyph_cache) + return(font->glyph_cache->max_glyph_height); return(0.0f); } @@ -741,9 +739,8 @@ void BLF_buffer_col(int fontid, float r, float g, float b, float a) void BLF_draw_buffer(int fontid, const char *str) { - FontBLF *font; - - font= BLF_get(fontid); - if (font) + FontBLF *font= BLF_get(fontid); + if (font && font->glyph_cache && (font->b_fbuf || font->b_cbuf)) { blf_font_buffer(font, str); + } } diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c index 3bec7dd2626..c1674052fb1 100644 --- a/source/blender/blenfont/intern/blf_font.c +++ b/source/blender/blenfont/intern/blf_font.c @@ -136,6 +136,22 @@ static void blf_font_ensure_ascii_table(FontBLF *font) } \ +#define BLF_KERNING_VARS(_font, _has_kerning, _kern_mode) \ + const short has_kerning= FT_HAS_KERNING((_font)->face); \ + const FT_UInt kern_mode= (has_kerning == 0) ? 0 : \ + (((_font)->flags & BLF_KERNING_DEFAULT) ? \ + ft_kerning_default : FT_KERNING_UNFITTED) \ + \ + + +#define BLF_KERNING_STEP(_font, kern_mode, g_prev, g, delta, pen_x) \ +{ \ + if (g_prev) { \ + delta.x= delta.y= 0; \ + if (FT_Get_Kerning((_font)->face, g_prev->idx, g->idx, kern_mode, &delta) == 0) \ + pen_x += delta.x >> 6; \ + } \ +} \ void blf_font_draw(FontBLF *font, const char *str, unsigned int len) { @@ -143,18 +159,14 @@ void blf_font_draw(FontBLF *font, const char *str, unsigned int len) GlyphBLF *g, *g_prev; FT_Vector delta; int pen_x, pen_y; - int has_kerning, st; unsigned int i; - GlyphBLF **glyph_ascii_table; + GlyphBLF **glyph_ascii_table= font->glyph_cache->glyph_ascii_table; - if (!font->glyph_cache) - return; - glyph_ascii_table= font->glyph_cache->glyph_ascii_table; + BLF_KERNING_VARS(font, has_kerning, kern_mode); i= 0; pen_x= 0; pen_y= 0; - has_kerning= FT_HAS_KERNING(font->face); g_prev= NULL; blf_font_ensure_ascii_table(font); @@ -163,25 +175,9 @@ void blf_font_draw(FontBLF *font, const char *str, unsigned int len) BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); - if (c == 0) - break; - - /* 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 (c == 0) break; + if (g == NULL) continue; + if (has_kerning) BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x); /* do not return this loop if clipped, we want every character tested */ blf_glyph_render(font, g, (float)pen_x, (float)pen_y); @@ -198,39 +194,19 @@ void blf_font_draw_ascii(FontBLF *font, const char *str, unsigned int len) GlyphBLF *g, *g_prev; FT_Vector delta; int pen_x, pen_y; - int has_kerning, st; - GlyphBLF **glyph_ascii_table; + GlyphBLF **glyph_ascii_table= font->glyph_cache->glyph_ascii_table; - if (!font->glyph_cache) - return; - glyph_ascii_table= font->glyph_cache->glyph_ascii_table; + BLF_KERNING_VARS(font, has_kerning, kern_mode); pen_x= 0; pen_y= 0; - has_kerning= FT_HAS_KERNING(font->face); g_prev= NULL; blf_font_ensure_ascii_table(font); while ((c= *(str++)) && len--) { - g= glyph_ascii_table[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 ((g= glyph_ascii_table[c]) == NULL) continue; + if (has_kerning) BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x); /* do not return this loop if clipped, we want every character tested */ blf_glyph_render(font, g, (float)pen_x, (float)pen_y); @@ -240,6 +216,7 @@ void blf_font_draw_ascii(FontBLF *font, const char *str, unsigned int len) } } +/* Sanity checks are done by BLF_draw_buffer() */ void blf_font_buffer(FontBLF *font, const char *str) { unsigned char *cbuf; @@ -248,18 +225,15 @@ void blf_font_buffer(FontBLF *font, const char *str) GlyphBLF *g, *g_prev; FT_Vector delta; float a, *fbuf; - int pen_x, y, x; - int has_kerning, st, chx, chy; + int pen_x, pen_y, y, x; + int chx, chy; unsigned int i; - GlyphBLF **glyph_ascii_table; + GlyphBLF **glyph_ascii_table= font->glyph_cache->glyph_ascii_table; + + BLF_KERNING_VARS(font, has_kerning, kern_mode); - if (!font->glyph_cache || (!font->b_fbuf && !font->b_cbuf)) - return; - glyph_ascii_table= font->glyph_cache->glyph_ascii_table; - i= 0; pen_x= (int)font->pos[0]; - has_kerning= FT_HAS_KERNING(font->face); g_prev= NULL; b_col_char[0]= font->b_col[0] * 255; @@ -270,29 +244,12 @@ void blf_font_buffer(FontBLF *font, const char *str) blf_font_ensure_ascii_table(font); while (str[i]) { - int pen_y; BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); - if (c == 0) - break; - - /* 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 (c == 0) break; + if (g == NULL) continue; + if (has_kerning) BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x); chx= pen_x + ((int)g->pos_x); chy= (int)font->pos[1] + g->height; @@ -394,14 +351,13 @@ void blf_font_boundbox(FontBLF *font, const char *str, rctf *box) unsigned int c; GlyphBLF *g, *g_prev; FT_Vector delta; - rctf gbox; int pen_x, pen_y; - int has_kerning, st; unsigned int i; GlyphBLF **glyph_ascii_table; - if (!font->glyph_cache) - return; + rctf gbox; + + BLF_KERNING_VARS(font, has_kerning, kern_mode); box->xmin= 32000.0f; box->xmax= -32000.0f; @@ -411,7 +367,6 @@ void blf_font_boundbox(FontBLF *font, const char *str, rctf *box) i= 0; pen_x= 0; pen_y= 0; - has_kerning= FT_HAS_KERNING(font->face); g_prev= NULL; blf_font_ensure_ascii_table(font); @@ -421,25 +376,9 @@ void blf_font_boundbox(FontBLF *font, const char *str, rctf *box) BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); - if (c == 0) - break; - - /* 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 (c == 0) break; + if (g == NULL) continue; + if (has_kerning) BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x); gbox.xmin= pen_x; gbox.xmax= pen_x + g->advance; @@ -473,20 +412,18 @@ void blf_font_width_and_height(FontBLF *font, const char *str, float *width, flo float xa, ya; rctf box; - if (font->glyph_cache) { - if (font->flags & BLF_ASPECT) { - xa= font->aspect[0]; - ya= font->aspect[1]; - } - else { - xa= 1.0f; - ya= 1.0f; - } - - blf_font_boundbox(font, str, &box); - *width= ((box.xmax - box.xmin) * xa); - *height= ((box.ymax - box.ymin) * ya); + if (font->flags & BLF_ASPECT) { + xa= font->aspect[0]; + ya= font->aspect[1]; } + else { + xa= 1.0f; + ya= 1.0f; + } + + blf_font_boundbox(font, str, &box); + *width= ((box.xmax - box.xmin) * xa); + *height= ((box.ymax - box.ymin) * ya); } float blf_font_width(FontBLF *font, const char *str) @@ -494,9 +431,6 @@ float blf_font_width(FontBLF *font, const char *str) float xa; rctf box; - if (!font->glyph_cache) - return(0.0f); - if (font->flags & BLF_ASPECT) xa= font->aspect[0]; else @@ -511,9 +445,6 @@ float blf_font_height(FontBLF *font, const char *str) float ya; rctf box; - if (!font->glyph_cache) - return(0.0f); - if (font->flags & BLF_ASPECT) ya= font->aspect[1]; else @@ -525,22 +456,17 @@ float blf_font_height(FontBLF *font, const char *str) float blf_font_fixed_width(FontBLF *font) { - GlyphBLF *g; - FT_UInt glyph_index; - unsigned int c = ' '; - - if (!font->glyph_cache) - return 0.0f; - - glyph_index= FT_Get_Char_Index(font->face, c); - g= blf_glyph_search(font->glyph_cache, c); - if (!g) - g= blf_glyph_add(font, glyph_index, c); + const unsigned int c = ' '; + GlyphBLF *g= blf_glyph_search(font->glyph_cache, c); + if (!g) { + g= blf_glyph_add(font, FT_Get_Char_Index(font->face, c), c); + + /* if we don't find the glyph. */ + if (!g) { + return 0.0f; + } + } - /* if we don't find the glyph. */ - if (!g) - return 0.0f; - return g->advance; } diff --git a/source/blender/blenfont/intern/blf_lang.c b/source/blender/blenfont/intern/blf_lang.c index e7f9d1746ad..80951899040 100644 --- a/source/blender/blenfont/intern/blf_lang.c +++ b/source/blender/blenfont/intern/blf_lang.c @@ -51,11 +51,6 @@ #include "BLI_string.h" #include "BLI_path_util.h" - -#ifdef __APPLE__ - -#endif - #define DOMAIN_NAME "blender" #define SYSTEM_ENCODING_DEFAULT "UTF-8" #define FONT_SIZE_DEFAULT 12 -- cgit v1.2.3