From b349f7c99d770673cfd27b3ce7de311db33d6b3f Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 30 Oct 2010 23:02:38 +0000 Subject: Minor speedups for 3D view text drawing ~10-15% improved frame-rate with particle display. - ascii text drawing functions, slightly faster since they dont have to do hash lookups & utf8 conversions for each char. - used ascii drawing functions for the view3d's number display. - each text item was using fixed 128 chars, now only allocate the string length needed. --- source/blender/blenfont/BLF_api.h | 6 +- source/blender/blenfont/intern/blf.c | 69 ++++++++++++++++------ source/blender/blenfont/intern/blf_font.c | 63 +++++++++++++++++++- source/blender/blenfont/intern/blf_internal.h | 3 +- .../blender/blenfont/intern/blf_internal_types.h | 3 + 5 files changed, 123 insertions(+), 21 deletions(-) (limited to 'source/blender/blenfont') diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h index 933d287bcff..c01886be65e 100644 --- a/source/blender/blenfont/BLF_api.h +++ b/source/blender/blenfont/BLF_api.h @@ -48,10 +48,12 @@ void BLF_position(int fontid, float x, float y, float z); void BLF_size(int fontid, int size, int dpi); /* Draw the string using the default font, size and dpi. */ -void BLF_draw_default(float x, float y, float z, char *str); +void BLF_draw_default(float x, float y, float z, const char *str); +void BLF_draw_default_ascii(float x, float y, float z, const char *str); /* Draw the string using the current font. */ -void BLF_draw(int fontid, char *str); +void BLF_draw(int fontid, const char *str); +void BLF_draw_ascii(int fontid, const char *str); /* * This function return the bounding box of the string diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c index f6b7c5f71e6..59189abf1e3 100644 --- a/source/blender/blenfont/intern/blf.c +++ b/source/blender/blenfont/intern/blf.c @@ -361,7 +361,7 @@ void BLF_blur(int fontid, int size) font->blur= size; } -void BLF_draw_default(float x, float y, float z, char *str) +void BLF_draw_default(float x, float y, float z, const char *str) { if (!str) return; @@ -378,6 +378,24 @@ void BLF_draw_default(float x, float y, float z, char *str) BLF_position(global_font_default, x, y, z); BLF_draw(global_font_default, str); } +/* same as above but call 'BLF_draw_ascii' */ +void BLF_draw_default_ascii(float x, float y, float z, const char *str) +{ + if (!str) + return; + + if (global_font_default == -1) + global_font_default= blf_search("default"); + + if (global_font_default == -1) { + printf("Warning: Can't found default font!!\n"); + return; + } + + BLF_size(global_font_default, global_font_points, global_font_dpi); + BLF_position(global_font_default, x, y, z); + BLF_draw_ascii(global_font_default, str); +} void BLF_rotation_default(float angle) { @@ -388,32 +406,49 @@ void BLF_rotation_default(float angle) font->angle= angle; } -void BLF_draw(int fontid, char *str) -{ - FontBLF *font; +static void blf_draw__start(FontBLF *font) +{ /* * The pixmap alignment hack is handle * in BLF_position (old ui_rasterpos_safe). */ - font= BLF_get(fontid); - if (font) { - glEnable(GL_BLEND); - glEnable(GL_TEXTURE_2D); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glPushMatrix(); - glTranslatef(font->pos[0], font->pos[1], font->pos[2]); - glScalef(font->aspect, font->aspect, 1.0); + glEnable(GL_BLEND); + glEnable(GL_TEXTURE_2D); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - if (font->flags & BLF_ROTATION) - glRotatef(font->angle, 0.0f, 0.0f, 1.0f); + glPushMatrix(); + glTranslatef(font->pos[0], font->pos[1], font->pos[2]); + glScalef(font->aspect, font->aspect, 1.0); + if (font->flags & BLF_ROTATION) + glRotatef(font->angle, 0.0f, 0.0f, 1.0f); +} +static void blf_draw__end(void) +{ + glPopMatrix(); + glDisable(GL_BLEND); + glDisable(GL_TEXTURE_2D); +} + +void BLF_draw(int fontid, const char *str) +{ + FontBLF *font= BLF_get(fontid); + if (font) { + blf_draw__start(font); blf_font_draw(font, str); + blf_draw__end(); + } +} - glPopMatrix(); - glDisable(GL_BLEND); - glDisable(GL_TEXTURE_2D); +void BLF_draw_ascii(int fontid, const char *str) +{ + FontBLF *font= BLF_get(fontid); + if (font) { + blf_draw__start(font); + blf_font_draw_ascii(font, str); + blf_draw__end(); } } diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c index 04f40ac825b..9fb40f0206d 100644 --- a/source/blender/blenfont/intern/blf_font.c +++ b/source/blender/blenfont/intern/blf_font.c @@ -92,7 +92,7 @@ void blf_font_size(FontBLF *font, int size, int dpi) } } -void blf_font_draw(FontBLF *font, char *str) +void blf_font_draw(FontBLF *font, const char *str) { unsigned int c; GlyphBLF *g, *g_prev; @@ -146,6 +146,65 @@ void blf_font_draw(FontBLF *font, char *str) } } +/* faster version of blf_font_draw, ascii only for view dimensions */ +void blf_font_draw_ascii(FontBLF *font, const char *str) +{ + char c; + GlyphBLF *g, *g_prev; + FT_Vector delta; + FT_UInt glyph_index; + int pen_x, pen_y; + int i, has_kerning, st; + + if (!font->glyph_cache) + return; + + i= 0; + pen_x= 0; + pen_y= 0; + has_kerning= FT_HAS_KERNING(font->face); + g_prev= NULL; + + /* build ascii on demand */ + if(font->glyph_ascii_table['0']==NULL) { + for(i=0; i<256; i++) { + g= blf_glyph_search(font->glyph_cache, i); + if (!g) { + glyph_index= FT_Get_Char_Index(font->face, i); + g= blf_glyph_add(font, glyph_index, i); + } + font->glyph_ascii_table[i]= g; + } + } + + while ((c= *(str++))) { + g= font->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; + } + + /* do not return this loop if clipped, we want every character tested */ + blf_glyph_render(font, g, (float)pen_x, (float)pen_y); + + pen_x += g->advance; + g_prev= g; + } +} + void blf_font_buffer(FontBLF *font, char *str) { unsigned char *cbuf; @@ -460,6 +519,8 @@ static void blf_font_fill(FontBLF *font) font->b_col[1]= 0; font->b_col[2]= 0; font->b_col[3]= 0; + + memset(font->glyph_ascii_table, 0, sizeof(font->glyph_ascii_table)); } FontBLF *blf_font_new(char *name, char *filename) diff --git a/source/blender/blenfont/intern/blf_internal.h b/source/blender/blenfont/intern/blf_internal.h index 844a7a3c3de..c7a3cd54740 100644 --- a/source/blender/blenfont/intern/blf_internal.h +++ b/source/blender/blenfont/intern/blf_internal.h @@ -44,7 +44,8 @@ FontBLF *blf_font_new_from_mem(char *name, unsigned char *mem, int mem_size); void blf_font_attach_from_mem(FontBLF *font, const unsigned char *mem, int mem_size); void blf_font_size(FontBLF *font, int size, int dpi); -void blf_font_draw(FontBLF *font, char *str); +void blf_font_draw(FontBLF *font, const char *str); +void blf_font_draw_ascii(FontBLF *font, const 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 --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index 368eb8751de..70318c51823 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -177,6 +177,9 @@ typedef struct FontBLF { /* current glyph cache, size and dpi. */ GlyphCacheBLF *glyph_cache; + + /* fast ascii lookip */ + GlyphBLF *glyph_ascii_table[256]; /* freetype2 face. */ FT_Face face; -- cgit v1.2.3