diff options
Diffstat (limited to 'source/blender/blenfont')
-rw-r--r-- | source/blender/blenfont/BLF_api.h | 34 | ||||
-rw-r--r-- | source/blender/blenfont/intern/blf.c | 232 | ||||
-rw-r--r-- | source/blender/blenfont/intern/blf_font.c | 49 | ||||
-rw-r--r-- | source/blender/blenfont/intern/blf_glyph.c | 148 | ||||
-rw-r--r-- | source/blender/blenfont/intern/blf_internal.h | 5 | ||||
-rw-r--r-- | source/blender/blenfont/intern/blf_internal_types.h | 8 |
6 files changed, 276 insertions, 200 deletions
diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h index f31c132c973..cd3a15f1c88 100644 --- a/source/blender/blenfont/BLF_api.h +++ b/source/blender/blenfont/BLF_api.h @@ -35,6 +35,9 @@ #include "BLI_compiler_attrs.h" #include "BLI_sys_types.h" +/* enable this only if needed (unused circa 2016) */ +#define BLF_BLUR_ENABLE 0 + struct rctf; struct ColorManagedDisplay; struct ResultBLF; @@ -43,6 +46,7 @@ int BLF_init(void); void BLF_exit(void); void BLF_default_dpi(int dpi); void BLF_default_set(int fontid); +int BLF_default(void); /* get default font ID so we can pass it to other functions */ void BLF_antialias_set(bool enabled); bool BLF_antialias_get(void); @@ -65,6 +69,17 @@ void BLF_aspect(int fontid, float x, float y, float z); void BLF_position(int fontid, float x, float y, float z); void BLF_size(int fontid, int size, int dpi); +/* goal: small but useful color API */ +void BLF_color4ubv(int fontid, const unsigned char rgba[4]); +void BLF_color3ubv(int fontid, const unsigned char rgb[3]); +void BLF_color3ubv_alpha(int fontid, const unsigned char rgb[3], unsigned char alpha); +void BLF_color3ub(int fontid, unsigned char r, unsigned char g, unsigned char b); +void BLF_color4f(int fontid, float r, float g, float b, float a); +void BLF_color4fv(int fontid, const float rgba[4]); +void BLF_color3f(int fontid, float r, float g, float b); +void BLF_color3fv_alpha(int fontid, const float rgb[3], float alpha); +/* also available: UI_FontThemeColor(fontid, colorid) */ + /* Set a 4x4 matrix to be multiplied before draw the text. * Remember that you need call BLF_enable(BLF_MATRIX) * to enable this. @@ -126,29 +141,16 @@ void BLF_width_and_height(int fontid, const char *str, size_t len, float *r_widt */ float BLF_fixed_width(int fontid) ATTR_WARN_UNUSED_RESULT; -/* and this two function return the width and height - * of the string, using the default font and both value - * are multiplied by the aspect of the font. - */ -void BLF_width_and_height_default(const char *str, size_t len, float *r_width, float *r_height) ATTR_NONNULL(); -float BLF_width_default(const char *str, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); -float BLF_height_default(const char *str, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); - -/* Set rotation for default font. */ -void BLF_rotation_default(float angle); - -/* Enable/disable options to the default font. */ -void BLF_enable_default(int option); -void BLF_disable_default(int option); - /* By default, rotation and clipping are disable and * have to be enable/disable using BLF_enable/disable. */ void BLF_rotation(int fontid, float angle); void BLF_clipping(int fontid, float xmin, float ymin, float xmax, float ymax); -void BLF_clipping_default(float xmin, float ymin, float xmax, float ymax); void BLF_wordwrap(int fontid, int wrap_width); + +#if BLF_BLUR_ENABLE void BLF_blur(int fontid, int size); +#endif void BLF_enable(int fontid, int option); void BLF_disable(int fontid, int option); diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c index b45e52f29fd..8e705616c41 100644 --- a/source/blender/blenfont/intern/blf.c +++ b/source/blender/blenfont/intern/blf.c @@ -50,13 +50,14 @@ #include "BLI_math.h" #include "BLI_threads.h" -#include "BIF_gl.h" #include "BLF_api.h" #include "IMB_colormanagement.h" #ifndef BLF_STANDALONE -#include "GPU_basic_shader.h" +#include "GPU_shader.h" +#include "GPU_matrix.h" +#include "GPU_immediate.h" #endif #include "blf_internal_types.h" @@ -176,6 +177,12 @@ void BLF_default_set(int fontid) } } +int BLF_default(void) +{ + ASSERT_DEFAULT_SET; + return global_font_default; +} + void BLF_antialias_set(bool enabled) { global_use_antialias = enabled; @@ -369,24 +376,6 @@ void BLF_disable(int fontid, int option) } } -void BLF_enable_default(int option) -{ - FontBLF *font = blf_get(global_font_default); - - if (font) { - font->flags |= option; - } -} - -void BLF_disable_default(int option) -{ - FontBLF *font = blf_get(global_font_default); - - if (font) { - font->flags &= ~option; - } -} - void BLF_aspect(int fontid, float x, float y, float z) { FontBLF *font = blf_get(fontid); @@ -465,6 +454,7 @@ void BLF_size(int fontid, int size, int dpi) } } +#if BLF_BLUR_ENABLE void BLF_blur(int fontid, int size) { FontBLF *font = blf_get(fontid); @@ -473,6 +463,77 @@ void BLF_blur(int fontid, int size) font->blur = size; } } +#endif + +void BLF_color4ubv(int fontid, const unsigned char rgba[4]) +{ + FontBLF *font = blf_get(fontid); + + if (font) { + font->color[0] = rgba[0]; + font->color[1] = rgba[1]; + font->color[2] = rgba[2]; + font->color[3] = rgba[3]; + } +} + +void BLF_color3ubv_alpha(int fontid, const unsigned char rgb[3], unsigned char alpha) +{ + FontBLF *font = blf_get(fontid); + + if (font) { + font->color[0] = rgb[0]; + font->color[1] = rgb[1]; + font->color[2] = rgb[2]; + font->color[3] = alpha; + } +} + +void BLF_color3ubv(int fontid, const unsigned char rgb[3]) +{ + BLF_color3ubv_alpha(fontid, rgb, 255); +} + +void BLF_color3ub(int fontid, unsigned char r, unsigned char g, unsigned char b) +{ + FontBLF *font = blf_get(fontid); + + if (font) { + font->color[0] = r; + font->color[1] = g; + font->color[2] = b; + font->color[3] = 255; + } +} + +void BLF_color4fv(int fontid, const float rgba[4]) +{ + FontBLF *font = blf_get(fontid); + + if (font) { + rgba_float_to_uchar(font->color, rgba); + } +} + +void BLF_color4f(int fontid, float r, float g, float b, float a) +{ + float rgba[4] = { r, g, b, a }; + BLF_color4fv(fontid, rgba); +} + +void BLF_color3fv_alpha(int fontid, const float rgb[3], float alpha) +{ + float rgba[4]; + copy_v3_v3(rgba, rgb); + rgba[3] = alpha; + BLF_color4fv(fontid, rgba); +} + +void BLF_color3f(int fontid, float r, float g, float b) +{ + float rgba[4] = { r, g, b, 1.0f }; + BLF_color4fv(fontid, rgba); +} void BLF_draw_default(float x, float y, float z, const char *str, size_t len) { @@ -493,16 +554,7 @@ void BLF_draw_default_ascii(float x, float y, float z, const char *str, size_t l BLF_draw_ascii(global_font_default, str, len); /* XXX, use real length */ } -void BLF_rotation_default(float angle) -{ - FontBLF *font = blf_get(global_font_default); - - if (font) { - font->angle = angle; - } -} - -static void blf_draw_gl__start(FontBLF *font, GLint *mode) +static void blf_draw_gl__start(FontBLF *font) { /* * The pixmap alignment hack is handle @@ -510,54 +562,48 @@ static void blf_draw_gl__start(FontBLF *font, GLint *mode) */ glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); -#ifndef BLF_STANDALONE - GPU_basic_shader_bind(GPU_SHADER_TEXTURE_2D | GPU_SHADER_USE_COLOR); -#endif + gpuPushMatrix(); - /* Save the current matrix mode. */ - glGetIntegerv(GL_MATRIX_MODE, mode); + if (font->flags & BLF_MATRIX) + gpuMultMatrix(font->m); - glMatrixMode(GL_TEXTURE); - glPushMatrix(); - glLoadIdentity(); + gpuTranslate3fv(font->pos); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); + if (font->flags & BLF_ASPECT) + gpuScale3fv(font->aspect); - if (font->flags & BLF_MATRIX) - glMultMatrixf(font->m); + if (font->flags & BLF_ROTATION) + gpuRotate2D(RAD2DEG(font->angle)); - glTranslate3fv(font->pos); +#ifndef BLF_STANDALONE + Gwn_VertFormat *format = immVertexFormat(); + unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT); + unsigned int texCoord = GWN_vertformat_attr_add(format, "texCoord", GWN_COMP_F32, 2, GWN_FETCH_FLOAT); + unsigned int color = GWN_vertformat_attr_add(format, "color", GWN_COMP_U8, 4, GWN_FETCH_INT_TO_FLOAT_UNIT); - if (font->flags & BLF_ASPECT) - glScalef(font->aspect[0], font->aspect[1], font->aspect[2]); + BLI_assert(pos == BLF_POS_ID); + BLI_assert(texCoord == BLF_COORD_ID); + BLI_assert(color == BLF_COLOR_ID); - if (font->flags & BLF_ROTATION) /* radians -> degrees */ - glRotatef(font->angle * (float)(180.0 / M_PI), 0.0f, 0.0f, 1.0f); + UNUSED_VARS_NDEBUG(pos, texCoord, color); - if (font->shadow || font->blur) - glGetFloatv(GL_CURRENT_COLOR, font->orig_col); + immBindBuiltinProgram(GPU_SHADER_TEXT); +#endif /* always bind the texture for the first glyph */ font->tex_bind_state = -1; } -static void blf_draw_gl__end(GLint mode) +static void blf_draw_gl__end(void) { - glMatrixMode(GL_TEXTURE); - glPopMatrix(); - - glMatrixMode(GL_MODELVIEW); - glPopMatrix(); - - if (mode != GL_MODELVIEW) - glMatrixMode(mode); + gpuPopMatrix(); #ifndef BLF_STANDALONE - GPU_basic_shader_bind(GPU_SHADER_USE_COLOR); + immUnbindProgram(); #endif + glDisable(GL_BLEND); } @@ -566,23 +612,26 @@ void BLF_draw_ex( struct ResultBLF *r_info) { FontBLF *font = blf_get(fontid); - GLint mode; BLF_RESULT_CHECK_INIT(r_info); if (font && font->glyph_cache) { - blf_draw_gl__start(font, &mode); + blf_draw_gl__start(font); if (font->flags & BLF_WORD_WRAP) { blf_font_draw__wrap(font, str, len, r_info); } else { blf_font_draw(font, str, len, r_info); } - blf_draw_gl__end(mode); + blf_draw_gl__end(); } } void BLF_draw(int fontid, const char *str, size_t len) { + if (len == 0 || str[0] == '\0') { + return; + } + BLF_draw_ex(fontid, str, len, NULL); } @@ -591,12 +640,11 @@ void BLF_draw_ascii_ex( struct ResultBLF *r_info) { FontBLF *font = blf_get(fontid); - GLint mode; BLF_RESULT_CHECK_INIT(r_info); if (font && font->glyph_cache) { - blf_draw_gl__start(font, &mode); + blf_draw_gl__start(font); if (font->flags & BLF_WORD_WRAP) { /* use non-ascii draw function for word-wrap */ blf_font_draw__wrap(font, str, len, r_info); @@ -604,24 +652,31 @@ void BLF_draw_ascii_ex( else { blf_font_draw_ascii(font, str, len, r_info); } - blf_draw_gl__end(mode); + blf_draw_gl__end(); } } void BLF_draw_ascii(int fontid, const char *str, size_t len) { + if (len == 0 || str[0] == '\0') { + return; + } + BLF_draw_ascii_ex(fontid, str, len, NULL); } int BLF_draw_mono(int fontid, const char *str, size_t len, int cwidth) { + if (len == 0 || str[0] == '\0') { + return 0; + } + FontBLF *font = blf_get(fontid); - GLint mode; int columns = 0; if (font && font->glyph_cache) { - blf_draw_gl__start(font, &mode); + blf_draw_gl__start(font); columns = blf_font_draw_mono(font, str, len, cwidth); - blf_draw_gl__end(mode); + blf_draw_gl__end(); } return columns; @@ -701,14 +756,6 @@ void BLF_width_and_height(int fontid, const char *str, size_t len, float *r_widt } } -void BLF_width_and_height_default(const char *str, size_t len, float *r_width, float *r_height) -{ - ASSERT_DEFAULT_SET; - - BLF_size(global_font_default, global_font_points, global_font_dpi); - BLF_width_and_height(global_font_default, str, len, r_width, r_height); -} - float BLF_width_ex( int fontid, const char *str, size_t len, struct ResultBLF *r_info) @@ -739,14 +786,6 @@ float BLF_fixed_width(int fontid) return 0.0f; } -float BLF_width_default(const char *str, size_t len) -{ - ASSERT_DEFAULT_SET; - - BLF_size(global_font_default, global_font_points, global_font_dpi); - return BLF_width(global_font_default, str, len); -} - float BLF_height_ex( int fontid, const char *str, size_t len, struct ResultBLF *r_info) @@ -810,15 +849,6 @@ float BLF_ascender(int fontid) return 0.0f; } -float BLF_height_default(const char *str, size_t len) -{ - ASSERT_DEFAULT_SET; - - BLF_size(global_font_default, global_font_points, global_font_dpi); - - return BLF_height(global_font_default, str, len); -} - void BLF_rotation(int fontid, float angle) { FontBLF *font = blf_get(fontid); @@ -840,18 +870,6 @@ void BLF_clipping(int fontid, float xmin, float ymin, float xmax, float ymax) } } -void BLF_clipping_default(float xmin, float ymin, float xmax, float ymax) -{ - FontBLF *font = blf_get(global_font_default); - - if (font) { - font->clip_rec.xmin = xmin; - font->clip_rec.ymin = ymin; - font->clip_rec.xmax = xmax; - font->clip_rec.ymax = ymax; - } -} - void BLF_wordwrap(int fontid, int wrap_width) { FontBLF *font = blf_get(fontid); @@ -867,7 +885,7 @@ void BLF_shadow(int fontid, int level, const float rgba[4]) if (font) { font->shadow = level; - copy_v4_v4(font->shadow_col, rgba); + rgba_float_to_uchar(font->shadow_color, rgba); } } diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c index 44a1d08f1fd..5846f7a29e4 100644 --- a/source/blender/blenfont/intern/blf_font.c +++ b/source/blender/blenfont/intern/blf_font.c @@ -58,6 +58,8 @@ #include "BIF_gl.h" #include "BLF_api.h" +#include "GPU_immediate.h" + #include "blf_internal_types.h" #include "blf_internal.h" @@ -172,6 +174,23 @@ static void blf_font_ensure_ascii_table(FontBLF *font) } \ } (void)0 +static unsigned int verts_needed(const FontBLF *font, const char *str, size_t len) +{ + unsigned int length = (unsigned int)((len == INT_MAX) ? strlen(str) : len); + unsigned int quad_ct = 1; + + if (font->flags & BLF_SHADOW) { + if (font->shadow == 0) + quad_ct += 1; + if (font->shadow <= 4) + quad_ct += 9; /* 3x3 kernel */ + else + quad_ct += 25; /* 5x5 kernel */ + } + + return length * quad_ct * 6; +} + static void blf_font_draw_ex( FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info, int pen_y) @@ -183,10 +202,18 @@ static void blf_font_draw_ex( size_t i = 0; GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; + if (len == 0) { + /* early output, don't do any IMM OpenGL. */ + return; + } + BLF_KERNING_VARS(font, has_kerning, kern_mode); blf_font_ensure_ascii_table(font); + immBeginAtMost(GWN_PRIM_TRIS, verts_needed(font, str, len)); + /* at most because some glyphs might be clipped & not drawn */ + while ((i < len) && str[i]) { BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); @@ -204,6 +231,8 @@ static void blf_font_draw_ex( g_prev = g; } + immEnd(); + if (r_info) { r_info->lines = 1; r_info->width = pen_x; @@ -229,6 +258,8 @@ static void blf_font_draw_ascii_ex( blf_font_ensure_ascii_table(font); + immBeginAtMost(GWN_PRIM_TRIS, verts_needed(font, str, len)); + while ((c = *(str++)) && len--) { BLI_assert(c < 128); if ((g = glyph_ascii_table[c]) == NULL) @@ -243,6 +274,8 @@ static void blf_font_draw_ascii_ex( g_prev = g; } + immEnd(); + if (r_info) { r_info->lines = 1; r_info->width = pen_x; @@ -265,6 +298,8 @@ int blf_font_draw_mono(FontBLF *font, const char *str, size_t len, int cwidth) blf_font_ensure_ascii_table(font); + immBeginAtMost(GWN_PRIM_TRIS, verts_needed(font, str, len)); + while ((i < len) && str[i]) { BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); @@ -284,6 +319,8 @@ int blf_font_draw_mono(FontBLF *font, const char *str, size_t len, int cwidth) pen_x += cwidth * col; } + immEnd(); + return columns; } @@ -901,8 +938,6 @@ void blf_font_free(FontBLF *font) static void blf_font_fill(FontBLF *font) { - unsigned int i; - font->aspect[0] = 1.0f; font->aspect[1] = 1.0f; font->aspect[2] = 1.0f; @@ -910,9 +945,15 @@ static void blf_font_fill(FontBLF *font) font->pos[1] = 0.0f; font->angle = 0.0f; - for (i = 0; i < 16; i++) + for (int i = 0; i < 16; i++) font->m[i] = 0; + /* annoying bright color so we can see where to add BLF_color calls */ + font->color[0] = 255; + font->color[1] = 255; + font->color[2] = 0; + font->color[3] = 255; + font->clip_rec.xmin = 0.0f; font->clip_rec.xmax = 0.0f; font->clip_rec.ymin = 0.0f; @@ -922,7 +963,9 @@ static void blf_font_fill(FontBLF *font) font->size = 0; BLI_listbase_clear(&font->cache); font->glyph_cache = NULL; +#if BLF_BLUR_ENABLE font->blur = 0; +#endif font->max_tex_size = -1; font->buf_info.fbuf = NULL; diff --git a/source/blender/blenfont/intern/blf_glyph.c b/source/blender/blenfont/intern/blf_glyph.c index c90e60bd6ef..1f31a2dbd0a 100644 --- a/source/blender/blenfont/intern/blf_glyph.c +++ b/source/blender/blenfont/intern/blf_glyph.c @@ -56,7 +56,7 @@ #include "BLF_api.h" #ifndef BLF_STANDALONE -# include "GPU_basic_shader.h" +# include "GPU_immediate.h" #endif #include "blf_internal_types.h" @@ -183,18 +183,7 @@ static void blf_glyph_cache_texture(FontBLF *font, GlyphCacheBLF *gc) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - -#ifndef BLF_STANDALONE - /* needed since basic shader doesn't support alpha-only textures, - * while we could add support this is only used in a few places - * (an alternative could be to have a simple shader for BLF). */ - if (GLEW_ARB_texture_swizzle && GPU_basic_shader_use_glsl_get()) { - GLint swizzle_mask[] = {GL_ONE, GL_ONE, GL_ONE, GL_ALPHA}; - glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzle_mask); - } -#endif - - glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA8, gc->p2_width, gc->p2_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, gc->p2_width, gc->p2_height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL); } GlyphBLF *blf_glyph_search(GlyphCacheBLF *gc, unsigned int c) @@ -327,73 +316,83 @@ void blf_glyph_free(GlyphBLF *g) MEM_freeN(g); } -static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2) +static void blf_texture_draw(const unsigned char color[4], float uv[2][2], float dx, float y1, float dx1, float y2) { - glBegin(GL_QUADS); - glTexCoord2f(uv[0][0], uv[0][1]); - glVertex2f(dx, y1); - - glTexCoord2f(uv[0][0], uv[1][1]); - glVertex2f(dx, y2); - - glTexCoord2f(uv[1][0], uv[1][1]); - glVertex2f(dx1, y2); - - glTexCoord2f(uv[1][0], uv[0][1]); - glVertex2f(dx1, y1); - glEnd(); + /* First triangle. */ + immAttrib2f(BLF_COORD_ID, uv[0][0], uv[0][1]); + immSkipAttrib(BLF_COLOR_ID); /* skip color of most vertices */ + immVertex2f(BLF_POS_ID, dx, y1); + + immAttrib2f(BLF_COORD_ID, uv[0][0], uv[1][1]); + immSkipAttrib(BLF_COLOR_ID); + immVertex2f(BLF_POS_ID, dx, y2); + + immAttrib2f(BLF_COORD_ID, uv[1][0], uv[1][1]); + immAttrib4ubv(BLF_COLOR_ID, color); /* set color of provoking vertex */ + immVertex2f(BLF_POS_ID, dx1, y2); + + /* Second triangle. */ + immAttrib2f(BLF_COORD_ID, uv[0][0], uv[0][1]); + immSkipAttrib(BLF_COLOR_ID); /* skip color of most vertices */ + immVertex2f(BLF_POS_ID, dx, y1); + + immAttrib2f(BLF_COORD_ID, uv[1][0], uv[1][1]); + immSkipAttrib(BLF_COLOR_ID); + immVertex2f(BLF_POS_ID, dx1, y2); + + immAttrib2f(BLF_COORD_ID, uv[1][0], uv[0][1]); + immAttrib4ubv(BLF_COLOR_ID, color); /* set color of provoking vertex */ + immVertex2f(BLF_POS_ID, dx1, y1); } -static void blf_texture5_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2) +static void blf_texture5_draw(const unsigned char color_in[4], float uv[2][2], float x1, float y1, float x2, float y2) { const float soft[25] = {1 / 60.0f, 1 / 60.0f, 2 / 60.0f, 1 / 60.0f, 1 / 60.0f, 1 / 60.0f, 3 / 60.0f, 5 / 60.0f, 3 / 60.0f, 1 / 60.0f, 2 / 60.0f, 5 / 60.0f, 8 / 60.0f, 5 / 60.0f, 2 / 60.0f, 1 / 60.0f, 3 / 60.0f, 5 / 60.0f, 3 / 60.0f, 1 / 60.0f, 1 / 60.0f, 1 / 60.0f, 2 / 60.0f, 1 / 60.0f, 1 / 60.0f}; - + const float *fp = soft; - float color[4]; + unsigned char color[4]; float dx, dy; - color[0] = shadow_col[0]; - color[1] = shadow_col[1]; - color[2] = shadow_col[2]; - + color[0] = color_in[0]; + color[1] = color_in[1]; + color[2] = color_in[2]; + + const float alpha_in = (1 / 255.0f) * color_in[3]; + for (dx = -2; dx < 3; dx++) { for (dy = -2; dy < 3; dy++, fp++) { - color[3] = *(fp) * shadow_col[3]; - glColor4fv(color); - blf_texture_draw(uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy); + color[3] = FTOCHAR(*fp * alpha_in); + blf_texture_draw(color, uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy); } } - - glColor4fv(color); } -static void blf_texture3_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2) +static void blf_texture3_draw(const unsigned char color_in[4], float uv[2][2], float x1, float y1, float x2, float y2) { const float soft[9] = {1 / 16.0f, 2 / 16.0f, 1 / 16.0f, 2 / 16.0f, 4 / 16.0f, 2 / 16.0f, 1 / 16.0f, 2 / 16.0f, 1 / 16.0f}; const float *fp = soft; - float color[4]; + unsigned char color[4]; float dx, dy; - color[0] = shadow_col[0]; - color[1] = shadow_col[1]; - color[2] = shadow_col[2]; + color[0] = color_in[0]; + color[1] = color_in[1]; + color[2] = color_in[2]; + + const float alpha_in = (1 / 255.0f) * color_in[3]; for (dx = -1; dx < 2; dx++) { for (dy = -1; dy < 2; dy++, fp++) { - color[3] = *(fp) * shadow_col[3]; - glColor4fv(color); - blf_texture_draw(uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy); + color[3] = FTOCHAR(*fp * alpha_in); + blf_texture_draw(color, uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy); } } - - glColor4fv(color); } static void blf_glyph_calc_rect(rctf *rect, GlyphBLF *g, float x, float y) @@ -411,6 +410,8 @@ void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y) if ((!g->width) || (!g->height)) return; + glActiveTexture(GL_TEXTURE0); + if (g->build_tex == 0) { GlyphCacheBLF *gc = font->glyph_cache; @@ -450,14 +451,21 @@ void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y) } - glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT); + GLint lsb_first, row_length, alignment; + glGetIntegerv(GL_UNPACK_LSB_FIRST, &lsb_first); + glGetIntegerv(GL_UNPACK_ROW_LENGTH, &row_length); + glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment); + glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE); glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glBindTexture(GL_TEXTURE_2D, g->tex); - glTexSubImage2D(GL_TEXTURE_2D, 0, g->xoff, g->yoff, g->width, g->height, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap); - glPopClientAttrib(); + glTexSubImage2D(GL_TEXTURE_2D, 0, g->xoff, g->yoff, g->width, g->height, GL_RED, GL_UNSIGNED_BYTE, g->bitmap); + + glPixelStorei(GL_UNPACK_LSB_FIRST, lsb_first); + glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length); + glPixelStorei(GL_UNPACK_ALIGNMENT, alignment); g->uv[0][0] = ((float)g->xoff) / ((float)gc->p2_width); g->uv[0][1] = ((float)g->yoff) / ((float)gc->p2_height); @@ -487,39 +495,37 @@ void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y) glBindTexture(GL_TEXTURE_2D, (font->tex_bind_state = g->tex)); } + /* TODO: blur & shadow in shader, single quad per glyph */ + if (font->flags & BLF_SHADOW) { rctf rect_ofs; blf_glyph_calc_rect(&rect_ofs, g, x + (float)font->shadow_x, y + (float)font->shadow_y); - switch (font->shadow) { - case 3: - blf_texture3_draw(font->shadow_col, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax); - break; - case 5: - blf_texture5_draw(font->shadow_col, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax); - break; - default: - glColor4fv(font->shadow_col); - blf_texture_draw(g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax); - break; + if (font->shadow == 0) { + blf_texture_draw(font->shadow_color, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax); + } + else if (font->shadow <= 4) { + blf_texture3_draw(font->shadow_color, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax); + } + else { + blf_texture5_draw(font->shadow_color, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax); } - - glColor4fv(font->orig_col); } +#if BLF_BLUR_ENABLE switch (font->blur) { case 3: - blf_texture3_draw(font->orig_col, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax); + blf_texture3_draw(font->color, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax); break; case 5: - blf_texture5_draw(font->orig_col, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax); + blf_texture5_draw(font->color, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax); break; default: - blf_texture_draw(g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax); - break; + blf_texture_draw(font->color, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax); } - - return; +#else + blf_texture_draw(font->color, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax); +#endif } diff --git a/source/blender/blenfont/intern/blf_internal.h b/source/blender/blenfont/intern/blf_internal.h index d9d758ce548..ba17e050399 100644 --- a/source/blender/blenfont/intern/blf_internal.h +++ b/source/blender/blenfont/intern/blf_internal.h @@ -37,6 +37,11 @@ struct GlyphBLF; struct GlyphCacheBLF; struct rctf; +/* vertex attribute IDs (fixed IDs so we don't have to pass them around) */ +#define BLF_POS_ID 0 +#define BLF_COORD_ID 1 +#define BLF_COLOR_ID 2 + unsigned int blf_next_p2(unsigned int x); unsigned int blf_hash(unsigned int val); diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index 0fac576a8cc..9164a02b2cc 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -175,8 +175,10 @@ typedef struct FontBLF { /* angle in radians. */ float angle; +#if 0 /* BLF_BLUR_ENABLE */ /* blur: 3 or 5 large kernel */ int blur; +#endif /* shadow level. */ int shadow; @@ -186,10 +188,10 @@ typedef struct FontBLF { int shadow_y; /* shadow color. */ - float shadow_col[4]; + unsigned char shadow_color[4]; - /* store color here when drawing shadow or blur. */ - float orig_col[4]; + /* main text color. */ + unsigned char color[4]; /* Multiplied this matrix with the current one before * draw the text! see blf_draw__start. |