diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-09-18 13:48:09 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-09-18 13:48:09 +0400 |
commit | 249b41762a98b5e357cc32e0ba0cb673fc0c6225 (patch) | |
tree | 0791b0e9b437817b4495d086356c22438ce837e5 /source/blender/blenfont/intern/blf_font.c | |
parent | 7da6e0c82e2deac0bf177d8a273dc217202b3659 (diff) |
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.
Diffstat (limited to 'source/blender/blenfont/intern/blf_font.c')
-rw-r--r-- | source/blender/blenfont/intern/blf_font.c | 196 |
1 files changed, 61 insertions, 135 deletions
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; } |