From e12c08e8d170b7ca40f204a5b0423c23a9fbc2c1 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 17 Apr 2019 06:17:24 +0200 Subject: ClangFormat: apply to source, most of intern Apply clang format as proposed in T53211. For details on usage and instructions for migrating branches without conflicts, see: https://wiki.blender.org/wiki/Tools/ClangFormat --- source/blender/blenfont/BLF_api.h | 96 +- source/blender/blenfont/CMakeLists.txt | 66 +- source/blender/blenfont/intern/blf.c | 1030 ++++++----- source/blender/blenfont/intern/blf_dir.c | 197 ++- source/blender/blenfont/intern/blf_font.c | 1804 ++++++++++---------- source/blender/blenfont/intern/blf_font_i18n.c | 62 +- .../blenfont/intern/blf_font_win32_compat.c | 171 +- source/blender/blenfont/intern/blf_glyph.c | 926 +++++----- source/blender/blenfont/intern/blf_internal.h | 62 +- .../blender/blenfont/intern/blf_internal_types.h | 335 ++-- source/blender/blenfont/intern/blf_thumbs.c | 143 +- source/blender/blenfont/intern/blf_util.c | 37 +- 12 files changed, 2521 insertions(+), 2408 deletions(-) (limited to 'source/blender/blenfont') diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h index ac4ffe0ed5a..bf5ecd24269 100644 --- a/source/blender/blenfont/BLF_api.h +++ b/source/blender/blenfont/BLF_api.h @@ -21,7 +21,6 @@ * \ingroup blf */ - #ifndef __BLF_API_H__ #define __BLF_API_H__ @@ -39,7 +38,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 */ +int BLF_default(void); /* get default font ID so we can pass it to other functions */ void BLF_batch_reset(void); /* call when changing opengl context. */ void BLF_cache_clear(void); @@ -99,30 +98,40 @@ void BLF_draw_default_ascii(float x, float y, float z, const char *str, size_t l int BLF_set_default(void); /* Draw the string using the current font. */ -void BLF_draw_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) ATTR_NONNULL(2); +void BLF_draw_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) + ATTR_NONNULL(2); void BLF_draw(int fontid, const char *str, size_t len) ATTR_NONNULL(2); -void BLF_draw_ascii_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) ATTR_NONNULL(2); +void BLF_draw_ascii_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) + ATTR_NONNULL(2); void BLF_draw_ascii(int fontid, const char *str, size_t len) ATTR_NONNULL(2); int BLF_draw_mono(int fontid, const char *str, size_t len, int cwidth) ATTR_NONNULL(2); /* Get the string byte offset that fits within a given width */ -size_t BLF_width_to_strlen(int fontid, const char *str, size_t len, float width, float *r_width) ATTR_NONNULL(2); +size_t BLF_width_to_strlen(int fontid, const char *str, size_t len, float width, float *r_width) + ATTR_NONNULL(2); /* Same as BLF_width_to_strlen but search from the string end */ -size_t BLF_width_to_rstrlen(int fontid, const char *str, size_t len, float width, float *r_width) ATTR_NONNULL(2); +size_t BLF_width_to_rstrlen(int fontid, const char *str, size_t len, float width, float *r_width) + ATTR_NONNULL(2); /* This function return the bounding box of the string * and are not multiplied by the aspect. */ -void BLF_boundbox_ex(int fontid, const char *str, size_t len, struct rctf *box, struct ResultBLF *r_info) ATTR_NONNULL(2); +void BLF_boundbox_ex(int fontid, + const char *str, + size_t len, + struct rctf *box, + struct ResultBLF *r_info) ATTR_NONNULL(2); void BLF_boundbox(int fontid, const char *str, size_t len, struct rctf *box) ATTR_NONNULL(); /* The next both function return the width and height * of the string, using the current font and both value * are multiplied by the aspect of the font. */ -float BLF_width_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2); +float BLF_width_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) + ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2); float BLF_width(int fontid, const char *str, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); -float BLF_height_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2); +float BLF_height_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) + ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2); float BLF_height(int fontid, const char *str, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); /* Return dimensions of the font without any sample text. */ @@ -134,7 +143,8 @@ float BLF_ascender(int fontid) ATTR_WARN_UNUSED_RESULT; /* The following function return the width and height of the string, but * just in one call, so avoid extra freetype2 stuff. */ -void BLF_width_and_height(int fontid, const char *str, size_t len, float *r_width, float *r_height) ATTR_NONNULL(); +void BLF_width_and_height(int fontid, const char *str, size_t len, float *r_width, float *r_height) + ATTR_NONNULL(); /* For fixed width fonts only, returns the width of a * character. @@ -177,7 +187,13 @@ void BLF_shadow_offset(int fontid, int x, int y); * * BLF_buffer(NULL, NULL, NULL, 0, 0, false, NULL); */ -void BLF_buffer(int fontid, float *fbuf, unsigned char *cbuf, int w, int h, int nch, struct ColorManagedDisplay *display); +void BLF_buffer(int fontid, + float *fbuf, + unsigned char *cbuf, + int w, + int h, + int nch, + struct ColorManagedDisplay *display); /* Set the color to be used for text. */ void BLF_buffer_col(int fontid, const float rgba[4]) ATTR_NONNULL(2); @@ -185,7 +201,8 @@ void BLF_buffer_col(int fontid, const float rgba[4]) ATTR_NONNULL(2); /* Draw the string into the buffer, this function draw in both buffer, float and unsigned char _BUT_ * it's not necessary set both buffer, NULL is valid here. */ -void BLF_draw_buffer_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) ATTR_NONNULL(2); +void BLF_draw_buffer_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) + ATTR_NONNULL(2); void BLF_draw_buffer(int fontid, const char *str, size_t len) ATTR_NONNULL(2); /* Add a path to the font dir paths. */ @@ -201,33 +218,38 @@ char **BLF_dir_get(int *ndir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); void BLF_dir_free(char **dirs, int count) ATTR_NONNULL(); /* blf_thumbs.c */ -void BLF_thumb_preview( - const char *filename, const char **draw_str, const unsigned char draw_str_lines, - const float font_color[4], const int font_size, - unsigned char *buf, int w, int h, int channels) ATTR_NONNULL(); +void BLF_thumb_preview(const char *filename, + const char **draw_str, + const unsigned char draw_str_lines, + const float font_color[4], + const int font_size, + unsigned char *buf, + int w, + int h, + int channels) ATTR_NONNULL(); /* blf_font_i18.c */ unsigned char *BLF_get_unifont(int *unifont_size); -void BLF_free_unifont(void); +void BLF_free_unifont(void); unsigned char *BLF_get_unifont_mono(int *unifont_size); -void BLF_free_unifont_mono(void); +void BLF_free_unifont_mono(void); #ifdef DEBUG void BLF_state_print(int fontid); #endif /* font->flags. */ -#define BLF_ROTATION (1 << 0) -#define BLF_CLIPPING (1 << 1) -#define BLF_SHADOW (1 << 2) -#define BLF_KERNING_DEFAULT (1 << 3) -#define BLF_MATRIX (1 << 4) -#define BLF_ASPECT (1 << 5) -#define BLF_WORD_WRAP (1 << 6) -#define BLF_MONOCHROME (1 << 7) /* no-AA */ -#define BLF_HINTING_NONE (1 << 8) -#define BLF_HINTING_SLIGHT (1 << 9) -#define BLF_HINTING_FULL (1 << 10) +#define BLF_ROTATION (1 << 0) +#define BLF_CLIPPING (1 << 1) +#define BLF_SHADOW (1 << 2) +#define BLF_KERNING_DEFAULT (1 << 3) +#define BLF_MATRIX (1 << 4) +#define BLF_ASPECT (1 << 5) +#define BLF_WORD_WRAP (1 << 6) +#define BLF_MONOCHROME (1 << 7) /* no-AA */ +#define BLF_HINTING_NONE (1 << 8) +#define BLF_HINTING_SLIGHT (1 << 9) +#define BLF_HINTING_FULL (1 << 10) #define BLF_DRAW_STR_DUMMY_MAX 1024 @@ -239,14 +261,14 @@ extern int blf_mono_font_render; /* don't mess drawing with render threads. */ * Result of drawing/evaluating the string */ struct ResultBLF { - /** - * Number of lines drawn when #BLF_WORD_WRAP is enabled (both wrapped and `\n` newline). - */ - int lines; - /** - * The 'cursor' position on completion (ignoring character boundbox). - */ - int width; + /** + * Number of lines drawn when #BLF_WORD_WRAP is enabled (both wrapped and `\n` newline). + */ + int lines; + /** + * The 'cursor' position on completion (ignoring character boundbox). + */ + int width; }; #endif /* __BLF_API_H__ */ diff --git a/source/blender/blenfont/CMakeLists.txt b/source/blender/blenfont/CMakeLists.txt index 78d805398ef..e16ff10c76d 100644 --- a/source/blender/blenfont/CMakeLists.txt +++ b/source/blender/blenfont/CMakeLists.txt @@ -19,58 +19,58 @@ # ***** END GPL LICENSE BLOCK ***** set(INC - . - ../blenkernel - ../blenlib - ../blentranslation - ../editors/include - ../gpu - ../makesdna - ../makesrna - ../imbuf - ../../../intern/guardedalloc - ../../../intern/glew-mx + . + ../blenkernel + ../blenlib + ../blentranslation + ../editors/include + ../gpu + ../makesdna + ../makesrna + ../imbuf + ../../../intern/guardedalloc + ../../../intern/glew-mx ) set(INC_SYS - ${GLEW_INCLUDE_PATH} - ${FREETYPE_INCLUDE_DIRS} + ${GLEW_INCLUDE_PATH} + ${FREETYPE_INCLUDE_DIRS} ) set(SRC - intern/blf.c - intern/blf_dir.c - intern/blf_font.c - intern/blf_font_i18n.c - intern/blf_glyph.c - intern/blf_thumbs.c - intern/blf_util.c + intern/blf.c + intern/blf_dir.c + intern/blf_font.c + intern/blf_font_i18n.c + intern/blf_glyph.c + intern/blf_thumbs.c + intern/blf_util.c - BLF_api.h - intern/blf_internal.h - intern/blf_internal_types.h + BLF_api.h + intern/blf_internal.h + intern/blf_internal_types.h ) set(LIB - bf_gpu - bf_intern_guardedalloc + bf_gpu + bf_intern_guardedalloc ) if(WIN32) - list(APPEND SRC - intern/blf_font_win32_compat.c - ) + list(APPEND SRC + intern/blf_font_win32_compat.c + ) endif() if(WITH_INTERNATIONAL) - add_definitions(-DWITH_INTERNATIONAL) + add_definitions(-DWITH_INTERNATIONAL) endif() if(WITH_PYTHON) - add_definitions(-DWITH_PYTHON) - list(APPEND INC - ../python - ) + add_definitions(-DWITH_PYTHON) + list(APPEND INC + ../python + ) endif() add_definitions(${GL_DEFINITIONS}) diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c index 4f6344ee506..0da96226143 100644 --- a/source/blender/blenfont/intern/blf.c +++ b/source/blender/blenfont/intern/blf.c @@ -48,9 +48,9 @@ #include "IMB_colormanagement.h" #ifndef BLF_STANDALONE -#include "GPU_shader.h" -#include "GPU_matrix.h" -#include "GPU_immediate.h" +# include "GPU_shader.h" +# include "GPU_matrix.h" +# include "GPU_immediate.h" #endif #include "blf_internal_types.h" @@ -67,9 +67,10 @@ #define ASSERT_DEFAULT_SET BLI_assert(global_font_default != -1) #define BLF_RESULT_CHECK_INIT(r_info) \ -if (r_info) { \ - memset(r_info, 0, sizeof(*(r_info))); \ -} ((void)0) + if (r_info) { \ + memset(r_info, 0, sizeof(*(r_info))); \ + } \ + ((void)0) /* Font array. */ static FontBLF *global_font[BLF_MAX_FONT] = {NULL}; @@ -85,872 +86,869 @@ int blf_mono_font_render = -1; static FontBLF *blf_get(int fontid) { - if (fontid >= 0 && fontid < BLF_MAX_FONT) - return global_font[fontid]; - return NULL; + if (fontid >= 0 && fontid < BLF_MAX_FONT) + return global_font[fontid]; + return NULL; } int BLF_init(void) { - int i; + int i; - for (i = 0; i < BLF_MAX_FONT; i++) - global_font[i] = NULL; + for (i = 0; i < BLF_MAX_FONT; i++) + global_font[i] = NULL; - global_font_points = 11; - global_font_dpi = 72; + global_font_points = 11; + global_font_dpi = 72; - return blf_font_init(); + return blf_font_init(); } void BLF_default_dpi(int dpi) { - global_font_dpi = dpi; + global_font_dpi = dpi; } void BLF_exit(void) { - FontBLF *font; - int i; + FontBLF *font; + int i; - for (i = 0; i < BLF_MAX_FONT; i++) { - font = global_font[i]; - if (font) { - blf_font_free(font); - global_font[i] = NULL; - } - } + for (i = 0; i < BLF_MAX_FONT; i++) { + font = global_font[i]; + if (font) { + blf_font_free(font); + global_font[i] = NULL; + } + } - blf_font_exit(); + blf_font_exit(); } void BLF_batch_reset(void) { - blf_batch_draw_vao_clear(); + blf_batch_draw_vao_clear(); } void BLF_cache_clear(void) { - FontBLF *font; - int i; + FontBLF *font; + int i; - for (i = 0; i < BLF_MAX_FONT; i++) { - font = global_font[i]; - if (font) { - blf_glyph_cache_clear(font); - blf_kerning_cache_clear(font); - } - } + for (i = 0; i < BLF_MAX_FONT; i++) { + font = global_font[i]; + if (font) { + blf_glyph_cache_clear(font); + blf_kerning_cache_clear(font); + } + } } static int blf_search(const char *name) { - FontBLF *font; - int i; + FontBLF *font; + int i; - for (i = 0; i < BLF_MAX_FONT; i++) { - font = global_font[i]; - if (font && (STREQ(font->name, name))) - return i; - } + for (i = 0; i < BLF_MAX_FONT; i++) { + font = global_font[i]; + if (font && (STREQ(font->name, name))) + return i; + } - return -1; + return -1; } static int blf_search_available(void) { - int i; + int i; - for (i = 0; i < BLF_MAX_FONT; i++) - if (!global_font[i]) - return i; + for (i = 0; i < BLF_MAX_FONT; i++) + if (!global_font[i]) + return i; - return -1; + return -1; } void BLF_default_set(int fontid) { - FontBLF *font = blf_get(fontid); - if (font || fontid == -1) { - global_font_default = fontid; - } + FontBLF *font = blf_get(fontid); + if (font || fontid == -1) { + global_font_default = fontid; + } } int BLF_default(void) { - ASSERT_DEFAULT_SET; - return global_font_default; + ASSERT_DEFAULT_SET; + return global_font_default; } int BLF_load(const char *name) { - FontBLF *font; - int i; + FontBLF *font; + int i; - /* check if we already load this font. */ - i = blf_search(name); - if (i >= 0) { - font = global_font[i]; - font->reference_count++; - return i; - } + /* check if we already load this font. */ + i = blf_search(name); + if (i >= 0) { + font = global_font[i]; + font->reference_count++; + return i; + } - return BLF_load_unique(name); + return BLF_load_unique(name); } int BLF_load_unique(const char *name) { - FontBLF *font; - char *filename; - int i; + FontBLF *font; + char *filename; + int i; - /* Don't search in the cache!! make a new - * object font, this is for keep fonts threads safe. - */ - i = blf_search_available(); - if (i == -1) { - printf("Too many fonts!!!\n"); - return -1; - } + /* Don't search in the cache!! make a new + * object font, this is for keep fonts threads safe. + */ + i = blf_search_available(); + if (i == -1) { + printf("Too many fonts!!!\n"); + return -1; + } - filename = blf_dir_search(name); - if (!filename) { - printf("Can't find font: %s\n", name); - return -1; - } + filename = blf_dir_search(name); + if (!filename) { + printf("Can't find font: %s\n", name); + return -1; + } - font = blf_font_new(name, filename); - MEM_freeN(filename); + font = blf_font_new(name, filename); + MEM_freeN(filename); - if (!font) { - printf("Can't load font: %s\n", name); - return -1; - } + if (!font) { + printf("Can't load font: %s\n", name); + return -1; + } - font->reference_count = 1; - global_font[i] = font; - return i; + font->reference_count = 1; + global_font[i] = font; + return i; } void BLF_metrics_attach(int fontid, unsigned char *mem, int mem_size) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - blf_font_attach_from_mem(font, mem, mem_size); - } + if (font) { + blf_font_attach_from_mem(font, mem, mem_size); + } } int BLF_load_mem(const char *name, const unsigned char *mem, int mem_size) { - int i; + int i; - i = blf_search(name); - if (i >= 0) { - /*font = global_font[i];*/ /*UNUSED*/ - return i; - } - return BLF_load_mem_unique(name, mem, mem_size); + i = blf_search(name); + if (i >= 0) { + /*font = global_font[i];*/ /*UNUSED*/ + return i; + } + return BLF_load_mem_unique(name, mem, mem_size); } int BLF_load_mem_unique(const char *name, const unsigned char *mem, int mem_size) { - FontBLF *font; - int i; + FontBLF *font; + int i; - /* - * Don't search in the cache, make a new object font! - * this is to keep the font thread safe. - */ - i = blf_search_available(); - if (i == -1) { - printf("Too many fonts!!!\n"); - return -1; - } + /* + * Don't search in the cache, make a new object font! + * this is to keep the font thread safe. + */ + i = blf_search_available(); + if (i == -1) { + printf("Too many fonts!!!\n"); + return -1; + } - if (!mem_size) { - printf("Can't load font: %s from memory!!\n", name); - return -1; - } + if (!mem_size) { + printf("Can't load font: %s from memory!!\n", name); + return -1; + } - font = blf_font_new_from_mem(name, mem, mem_size); - if (!font) { - printf("Can't load font: %s from memory!!\n", name); - return -1; - } + font = blf_font_new_from_mem(name, mem, mem_size); + if (!font) { + printf("Can't load font: %s from memory!!\n", name); + return -1; + } - font->reference_count = 1; - global_font[i] = font; - return i; + font->reference_count = 1; + global_font[i] = font; + return i; } void BLF_unload(const char *name) { - FontBLF *font; - int i; + FontBLF *font; + int i; - for (i = 0; i < BLF_MAX_FONT; i++) { - font = global_font[i]; + for (i = 0; i < BLF_MAX_FONT; i++) { + font = global_font[i]; - if (font && (STREQ(font->name, name))) { - BLI_assert(font->reference_count > 0); - font->reference_count--; + if (font && (STREQ(font->name, name))) { + BLI_assert(font->reference_count > 0); + font->reference_count--; - if (font->reference_count == 0) { - blf_font_free(font); - global_font[i] = NULL; - } - } - } + if (font->reference_count == 0) { + blf_font_free(font); + global_font[i] = NULL; + } + } + } } void BLF_unload_id(int fontid) { - FontBLF *font = blf_get(fontid); - if (font) { - BLI_assert(font->reference_count > 0); - font->reference_count--; + FontBLF *font = blf_get(fontid); + if (font) { + BLI_assert(font->reference_count > 0); + font->reference_count--; - if (font->reference_count == 0) { - blf_font_free(font); - global_font[fontid] = NULL; - } - } + if (font->reference_count == 0) { + blf_font_free(font); + global_font[fontid] = NULL; + } + } } void BLF_enable(int fontid, int option) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - font->flags |= option; - } + if (font) { + font->flags |= option; + } } void BLF_disable(int fontid, int option) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - font->flags &= ~option; - } + if (font) { + font->flags &= ~option; + } } void BLF_aspect(int fontid, float x, float y, float z) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - font->aspect[0] = x; - font->aspect[1] = y; - font->aspect[2] = z; - } + if (font) { + font->aspect[0] = x; + font->aspect[1] = y; + font->aspect[2] = z; + } } void BLF_matrix(int fontid, const float m[16]) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - memcpy(font->m, m, sizeof(font->m)); - } + if (font) { + memcpy(font->m, m, sizeof(font->m)); + } } void BLF_position(int fontid, float x, float y, float z) { - FontBLF *font = blf_get(fontid); - - if (font) { - float xa, ya, za; - float remainder; - - if (font->flags & BLF_ASPECT) { - xa = font->aspect[0]; - ya = font->aspect[1]; - za = font->aspect[2]; - } - else { - xa = 1.0f; - ya = 1.0f; - za = 1.0f; - } - - remainder = x - floorf(x); - if (remainder > 0.4f && remainder < 0.6f) { - if (remainder < 0.5f) - x -= 0.1f * xa; - else - x += 0.1f * xa; - } - - remainder = y - floorf(y); - if (remainder > 0.4f && remainder < 0.6f) { - if (remainder < 0.5f) - y -= 0.1f * ya; - else - y += 0.1f * ya; - } - - remainder = z - floorf(z); - if (remainder > 0.4f && remainder < 0.6f) { - if (remainder < 0.5f) - z -= 0.1f * za; - else - z += 0.1f * za; - } - - font->pos[0] = x; - font->pos[1] = y; - font->pos[2] = z; - } + FontBLF *font = blf_get(fontid); + + if (font) { + float xa, ya, za; + float remainder; + + if (font->flags & BLF_ASPECT) { + xa = font->aspect[0]; + ya = font->aspect[1]; + za = font->aspect[2]; + } + else { + xa = 1.0f; + ya = 1.0f; + za = 1.0f; + } + + remainder = x - floorf(x); + if (remainder > 0.4f && remainder < 0.6f) { + if (remainder < 0.5f) + x -= 0.1f * xa; + else + x += 0.1f * xa; + } + + remainder = y - floorf(y); + if (remainder > 0.4f && remainder < 0.6f) { + if (remainder < 0.5f) + y -= 0.1f * ya; + else + y += 0.1f * ya; + } + + remainder = z - floorf(z); + if (remainder > 0.4f && remainder < 0.6f) { + if (remainder < 0.5f) + z -= 0.1f * za; + else + z += 0.1f * za; + } + + font->pos[0] = x; + font->pos[1] = y; + font->pos[2] = z; + } } void BLF_size(int fontid, int size, int dpi) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - blf_font_size(font, size, dpi); - } + if (font) { + blf_font_size(font, size, dpi); + } } #if BLF_BLUR_ENABLE void BLF_blur(int fontid, int size) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - font->blur = size; - } + if (font) { + font->blur = size; + } } #endif void BLF_color4ubv(int fontid, const unsigned char rgba[4]) { - FontBLF *font = blf_get(fontid); + 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]; - } + 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); + 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; - } + 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); + 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); + FontBLF *font = blf_get(fontid); - if (font) { - font->color[0] = r; - font->color[1] = g; - font->color[2] = b; - font->color[3] = 255; - } + 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); + FontBLF *font = blf_get(fontid); - if (font) { - rgba_float_to_uchar(font->color, rgba); - } + 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); + 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); + 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); + float rgba[4] = {r, g, b, 1.0f}; + BLF_color4fv(fontid, rgba); } void BLF_batch_draw_begin(void) { - BLI_assert(g_batch.enabled == false); - g_batch.enabled = true; + BLI_assert(g_batch.enabled == false); + g_batch.enabled = true; } void BLF_batch_draw_flush(void) { - if (g_batch.enabled) { - blf_batch_draw(); - } + if (g_batch.enabled) { + blf_batch_draw(); + } } void BLF_batch_draw_end(void) { - BLI_assert(g_batch.enabled == true); - blf_batch_draw(); /* Draw remaining glyphs */ - g_batch.enabled = false; + BLI_assert(g_batch.enabled == true); + blf_batch_draw(); /* Draw remaining glyphs */ + g_batch.enabled = false; } void BLF_draw_default(float x, float y, float z, const char *str, size_t len) { - ASSERT_DEFAULT_SET; + ASSERT_DEFAULT_SET; - BLF_size(global_font_default, global_font_points, global_font_dpi); - BLF_position(global_font_default, x, y, z); - BLF_draw(global_font_default, str, len); + BLF_size(global_font_default, global_font_points, global_font_dpi); + BLF_position(global_font_default, x, y, z); + BLF_draw(global_font_default, str, len); } /* same as above but call 'BLF_draw_ascii' */ void BLF_draw_default_ascii(float x, float y, float z, const char *str, size_t len) { - ASSERT_DEFAULT_SET; + ASSERT_DEFAULT_SET; - 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, len); /* XXX, use real length */ + 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, len); /* XXX, use real length */ } int BLF_set_default(void) { - ASSERT_DEFAULT_SET; + ASSERT_DEFAULT_SET; - BLF_size(global_font_default, global_font_points, global_font_dpi); + BLF_size(global_font_default, global_font_points, global_font_dpi); - return global_font_default; + return global_font_default; } static void blf_draw_gl__start(FontBLF *font) { - /* - * The pixmap alignment hack is handle - * in BLF_position (old ui_rasterpos_safe). - */ + /* + * The pixmap alignment hack is handle + * in BLF_position (old ui_rasterpos_safe). + */ - /* always bind the texture for the first glyph */ - font->tex_bind_state = 0; + /* always bind the texture for the first glyph */ + font->tex_bind_state = 0; - if ((font->flags & (BLF_ROTATION | BLF_MATRIX | BLF_ASPECT)) == 0) - return; /* glyphs will be translated individually and batched. */ + if ((font->flags & (BLF_ROTATION | BLF_MATRIX | BLF_ASPECT)) == 0) + return; /* glyphs will be translated individually and batched. */ - GPU_matrix_push(); + GPU_matrix_push(); - if (font->flags & BLF_MATRIX) - GPU_matrix_mul(font->m); + if (font->flags & BLF_MATRIX) + GPU_matrix_mul(font->m); - GPU_matrix_translate_3fv(font->pos); + GPU_matrix_translate_3fv(font->pos); - if (font->flags & BLF_ASPECT) - GPU_matrix_scale_3fv(font->aspect); + if (font->flags & BLF_ASPECT) + GPU_matrix_scale_3fv(font->aspect); - if (font->flags & BLF_ROTATION) - GPU_matrix_rotate_2d(RAD2DEG(font->angle)); + if (font->flags & BLF_ROTATION) + GPU_matrix_rotate_2d(RAD2DEG(font->angle)); } static void blf_draw_gl__end(FontBLF *font) { - if ((font->flags & (BLF_ROTATION | BLF_MATRIX | BLF_ASPECT)) != 0) - GPU_matrix_pop(); + if ((font->flags & (BLF_ROTATION | BLF_MATRIX | BLF_ASPECT)) != 0) + GPU_matrix_pop(); } -void BLF_draw_ex( - int fontid, const char *str, size_t len, - struct ResultBLF *r_info) +void BLF_draw_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - BLF_RESULT_CHECK_INIT(r_info); + BLF_RESULT_CHECK_INIT(r_info); - if (font && font->glyph_cache) { - 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(font); - } + if (font && font->glyph_cache) { + 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(font); + } } void BLF_draw(int fontid, const char *str, size_t len) { - if (len == 0 || str[0] == '\0') { - return; - } + if (len == 0 || str[0] == '\0') { + return; + } - BLF_draw_ex(fontid, str, len, NULL); + BLF_draw_ex(fontid, str, len, NULL); } -void BLF_draw_ascii_ex( - int fontid, const char *str, size_t len, - struct ResultBLF *r_info) +void BLF_draw_ascii_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - BLF_RESULT_CHECK_INIT(r_info); + BLF_RESULT_CHECK_INIT(r_info); - if (font && font->glyph_cache) { - 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); - } - else { - blf_font_draw_ascii(font, str, len, r_info); - } - blf_draw_gl__end(font); - } + if (font && font->glyph_cache) { + 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); + } + else { + blf_font_draw_ascii(font, str, len, r_info); + } + blf_draw_gl__end(font); + } } void BLF_draw_ascii(int fontid, const char *str, size_t len) { - if (len == 0 || str[0] == '\0') { - return; - } + if (len == 0 || str[0] == '\0') { + return; + } - BLF_draw_ascii_ex(fontid, str, len, NULL); + 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; - } + if (len == 0 || str[0] == '\0') { + return 0; + } - FontBLF *font = blf_get(fontid); - int columns = 0; + FontBLF *font = blf_get(fontid); + int columns = 0; - if (font && font->glyph_cache) { - blf_draw_gl__start(font); - columns = blf_font_draw_mono(font, str, len, cwidth); - blf_draw_gl__end(font); - } + if (font && font->glyph_cache) { + blf_draw_gl__start(font); + columns = blf_font_draw_mono(font, str, len, cwidth); + blf_draw_gl__end(font); + } - return columns; + return columns; } size_t BLF_width_to_strlen(int fontid, const char *str, size_t len, float width, float *r_width) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - const float xa = (font->flags & BLF_ASPECT) ? font->aspect[0] : 1.0f; - size_t ret; - ret = blf_font_width_to_strlen(font, str, len, width / xa, r_width); - if (r_width) { - *r_width *= xa; - } - return ret; - } + if (font) { + const float xa = (font->flags & BLF_ASPECT) ? font->aspect[0] : 1.0f; + size_t ret; + ret = blf_font_width_to_strlen(font, str, len, width / xa, r_width); + if (r_width) { + *r_width *= xa; + } + return ret; + } - if (r_width) { - *r_width = 0.0f; - } - return 0; + if (r_width) { + *r_width = 0.0f; + } + return 0; } size_t BLF_width_to_rstrlen(int fontid, const char *str, size_t len, float width, float *r_width) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - const float xa = (font->flags & BLF_ASPECT) ? font->aspect[0] : 1.0f; - size_t ret; - ret = blf_font_width_to_rstrlen(font, str, len, width / xa, r_width); - if (r_width) { - *r_width *= xa; - } - return ret; - } + if (font) { + const float xa = (font->flags & BLF_ASPECT) ? font->aspect[0] : 1.0f; + size_t ret; + ret = blf_font_width_to_rstrlen(font, str, len, width / xa, r_width); + if (r_width) { + *r_width *= xa; + } + return ret; + } - if (r_width) { - *r_width = 0.0f; - } - return 0; + if (r_width) { + *r_width = 0.0f; + } + return 0; } void BLF_boundbox_ex( - int fontid, const char *str, size_t len, rctf *r_box, - struct ResultBLF *r_info) + int fontid, const char *str, size_t len, rctf *r_box, struct ResultBLF *r_info) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - BLF_RESULT_CHECK_INIT(r_info); + BLF_RESULT_CHECK_INIT(r_info); - if (font) { - if (font->flags & BLF_WORD_WRAP) { - blf_font_boundbox__wrap(font, str, len, r_box, r_info); - } - else { - blf_font_boundbox(font, str, len, r_box, r_info); - } - } + if (font) { + if (font->flags & BLF_WORD_WRAP) { + blf_font_boundbox__wrap(font, str, len, r_box, r_info); + } + else { + blf_font_boundbox(font, str, len, r_box, r_info); + } + } } void BLF_boundbox(int fontid, const char *str, size_t len, rctf *r_box) { - BLF_boundbox_ex(fontid, str, len, r_box, NULL); + BLF_boundbox_ex(fontid, str, len, r_box, NULL); } void BLF_width_and_height(int fontid, const char *str, size_t len, float *r_width, float *r_height) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font && font->glyph_cache) { - blf_font_width_and_height(font, str, len, r_width, r_height, NULL); - } - else { - *r_width = *r_height = 0.0f; - } + if (font && font->glyph_cache) { + blf_font_width_and_height(font, str, len, r_width, r_height, NULL); + } + else { + *r_width = *r_height = 0.0f; + } } -float BLF_width_ex( - int fontid, const char *str, size_t len, - struct ResultBLF *r_info) +float BLF_width_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - BLF_RESULT_CHECK_INIT(r_info); + BLF_RESULT_CHECK_INIT(r_info); - if (font && font->glyph_cache) { - return blf_font_width(font, str, len, r_info); - } + if (font && font->glyph_cache) { + return blf_font_width(font, str, len, r_info); + } - return 0.0f; + return 0.0f; } float BLF_width(int fontid, const char *str, size_t len) { - return BLF_width_ex(fontid, str, len, NULL); + return BLF_width_ex(fontid, str, len, NULL); } float BLF_fixed_width(int fontid) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font && font->glyph_cache) { - return blf_font_fixed_width(font); - } + if (font && font->glyph_cache) { + return blf_font_fixed_width(font); + } - return 0.0f; + return 0.0f; } -float BLF_height_ex( - int fontid, const char *str, size_t len, - struct ResultBLF *r_info) +float BLF_height_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - BLF_RESULT_CHECK_INIT(r_info); + BLF_RESULT_CHECK_INIT(r_info); - if (font && font->glyph_cache) { - return blf_font_height(font, str, len, r_info); - } + if (font && font->glyph_cache) { + return blf_font_height(font, str, len, r_info); + } - return 0.0f; + return 0.0f; } float BLF_height(int fontid, const char *str, size_t len) { - return BLF_height_ex(fontid, str, len, NULL); + return BLF_height_ex(fontid, str, len, NULL); } int BLF_height_max(int fontid) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font && font->glyph_cache) { - return font->glyph_cache->glyph_height_max; - } + if (font && font->glyph_cache) { + return font->glyph_cache->glyph_height_max; + } - return 0; + return 0; } float BLF_width_max(int fontid) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font && font->glyph_cache) { - return font->glyph_cache->glyph_width_max; - } + if (font && font->glyph_cache) { + return font->glyph_cache->glyph_width_max; + } - return 0.0f; + return 0.0f; } float BLF_descender(int fontid) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font && font->glyph_cache) { - return font->glyph_cache->descender; - } + if (font && font->glyph_cache) { + return font->glyph_cache->descender; + } - return 0.0f; + return 0.0f; } float BLF_ascender(int fontid) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font && font->glyph_cache) { - return font->glyph_cache->ascender; - } + if (font && font->glyph_cache) { + return font->glyph_cache->ascender; + } - return 0.0f; + return 0.0f; } void BLF_rotation(int fontid, float angle) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - font->angle = angle; - } + if (font) { + font->angle = angle; + } } void BLF_clipping(int fontid, float xmin, float ymin, float xmax, float ymax) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - font->clip_rec.xmin = xmin; - font->clip_rec.ymin = ymin; - font->clip_rec.xmax = xmax; - font->clip_rec.ymax = ymax; - } + 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); + FontBLF *font = blf_get(fontid); - if (font) { - font->wrap_width = wrap_width; - } + if (font) { + font->wrap_width = wrap_width; + } } void BLF_shadow(int fontid, int level, const float rgba[4]) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - font->shadow = level; - rgba_float_to_uchar(font->shadow_color, rgba); - } + if (font) { + font->shadow = level; + rgba_float_to_uchar(font->shadow_color, rgba); + } } void BLF_shadow_offset(int fontid, int x, int y) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - font->shadow_x = x; - font->shadow_y = y; - } + if (font) { + font->shadow_x = x; + font->shadow_y = y; + } } -void BLF_buffer(int fontid, float *fbuf, unsigned char *cbuf, int w, int h, int nch, struct ColorManagedDisplay *display) +void BLF_buffer(int fontid, + float *fbuf, + unsigned char *cbuf, + int w, + int h, + int nch, + struct ColorManagedDisplay *display) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - font->buf_info.fbuf = fbuf; - font->buf_info.cbuf = cbuf; - font->buf_info.w = w; - font->buf_info.h = h; - font->buf_info.ch = nch; - font->buf_info.display = display; - } + if (font) { + font->buf_info.fbuf = fbuf; + font->buf_info.cbuf = cbuf; + font->buf_info.w = w; + font->buf_info.h = h; + font->buf_info.ch = nch; + font->buf_info.display = display; + } } void BLF_buffer_col(int fontid, const float rgba[4]) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font) { - copy_v4_v4(font->buf_info.col_init, rgba); - } + if (font) { + copy_v4_v4(font->buf_info.col_init, rgba); + } } - void blf_draw_buffer__start(FontBLF *font) { - FontBufInfoBLF *buf_info = &font->buf_info; + FontBufInfoBLF *buf_info = &font->buf_info; - buf_info->col_char[0] = buf_info->col_init[0] * 255; - buf_info->col_char[1] = buf_info->col_init[1] * 255; - buf_info->col_char[2] = buf_info->col_init[2] * 255; - buf_info->col_char[3] = buf_info->col_init[3] * 255; + buf_info->col_char[0] = buf_info->col_init[0] * 255; + buf_info->col_char[1] = buf_info->col_init[1] * 255; + buf_info->col_char[2] = buf_info->col_init[2] * 255; + buf_info->col_char[3] = buf_info->col_init[3] * 255; - if (buf_info->display) { - copy_v4_v4(buf_info->col_float, buf_info->col_init); - IMB_colormanagement_display_to_scene_linear_v3(buf_info->col_float, buf_info->display); - } - else { - srgb_to_linearrgb_v4(buf_info->col_float, buf_info->col_init); - } + if (buf_info->display) { + copy_v4_v4(buf_info->col_float, buf_info->col_init); + IMB_colormanagement_display_to_scene_linear_v3(buf_info->col_float, buf_info->display); + } + else { + srgb_to_linearrgb_v4(buf_info->col_float, buf_info->col_init); + } +} +void blf_draw_buffer__end(void) +{ } -void blf_draw_buffer__end(void) {} -void BLF_draw_buffer_ex( - int fontid, const char *str, size_t len, - struct ResultBLF *r_info) +void BLF_draw_buffer_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info) { - FontBLF *font = blf_get(fontid); + FontBLF *font = blf_get(fontid); - if (font && font->glyph_cache && (font->buf_info.fbuf || font->buf_info.cbuf)) { - blf_draw_buffer__start(font); - if (font->flags & BLF_WORD_WRAP) { - blf_font_draw_buffer__wrap(font, str, len, r_info); - } - else { - blf_font_draw_buffer(font, str, len, r_info); - } - blf_draw_buffer__end(); - } + if (font && font->glyph_cache && (font->buf_info.fbuf || font->buf_info.cbuf)) { + blf_draw_buffer__start(font); + if (font->flags & BLF_WORD_WRAP) { + blf_font_draw_buffer__wrap(font, str, len, r_info); + } + else { + blf_font_draw_buffer(font, str, len, r_info); + } + blf_draw_buffer__end(); + } } -void BLF_draw_buffer( - int fontid, const char *str, size_t len) +void BLF_draw_buffer(int fontid, const char *str, size_t len) { - BLF_draw_buffer_ex(fontid, str, len, NULL); + BLF_draw_buffer_ex(fontid, str, len, NULL); } #ifdef DEBUG void BLF_state_print(int fontid) { - FontBLF *font = blf_get(fontid); - if (font) { - printf("fontid %d %p\n", fontid, (void *)font); - printf(" name: '%s'\n", font->name); - printf(" size: %u\n", font->size); - printf(" dpi: %u\n", font->dpi); - printf(" pos: %.6f %.6f %.6f\n", UNPACK3(font->pos)); - printf(" aspect: (%d) %.6f %.6f %.6f\n", (font->flags & BLF_ROTATION) != 0, UNPACK3(font->aspect)); - printf(" angle: (%d) %.6f\n", (font->flags & BLF_ASPECT) != 0, font->angle); - printf(" flag: %d\n", font->flags); - } - else { - printf("fontid %d (NULL)\n", fontid); - } - fflush(stdout); + FontBLF *font = blf_get(fontid); + if (font) { + printf("fontid %d %p\n", fontid, (void *)font); + printf(" name: '%s'\n", font->name); + printf(" size: %u\n", font->size); + printf(" dpi: %u\n", font->dpi); + printf(" pos: %.6f %.6f %.6f\n", UNPACK3(font->pos)); + printf(" aspect: (%d) %.6f %.6f %.6f\n", + (font->flags & BLF_ROTATION) != 0, + UNPACK3(font->aspect)); + printf(" angle: (%d) %.6f\n", (font->flags & BLF_ASPECT) != 0, font->angle); + printf(" flag: %d\n", font->flags); + } + else { + printf("fontid %d (NULL)\n", fontid); + } + fflush(stdout); } #endif diff --git a/source/blender/blenfont/intern/blf_dir.c b/source/blender/blenfont/intern/blf_dir.c index 34d0fe5bba3..89378dc6181 100644 --- a/source/blender/blenfont/intern/blf_dir.c +++ b/source/blender/blenfont/intern/blf_dir.c @@ -23,7 +23,6 @@ * Manage search paths for font files. */ - #include #include #include @@ -48,102 +47,102 @@ #include "blf_internal_types.h" #include "blf_internal.h" -static ListBase global_font_dir = { NULL, NULL }; +static ListBase global_font_dir = {NULL, NULL}; static DirBLF *blf_dir_find(const char *path) { - DirBLF *p; - - p = global_font_dir.first; - while (p) { - if (BLI_path_cmp(p->path, path) == 0) - return p; - p = p->next; - } - return NULL; + DirBLF *p; + + p = global_font_dir.first; + while (p) { + if (BLI_path_cmp(p->path, path) == 0) + return p; + p = p->next; + } + return NULL; } void BLF_dir_add(const char *path) { - DirBLF *dir; + DirBLF *dir; - dir = blf_dir_find(path); - if (dir) /* already in the list ? just return. */ - return; + dir = blf_dir_find(path); + if (dir) /* already in the list ? just return. */ + return; - dir = (DirBLF *)MEM_callocN(sizeof(DirBLF), "BLF_dir_add"); - dir->path = BLI_strdup(path); - BLI_addhead(&global_font_dir, dir); + dir = (DirBLF *)MEM_callocN(sizeof(DirBLF), "BLF_dir_add"); + dir->path = BLI_strdup(path); + BLI_addhead(&global_font_dir, dir); } void BLF_dir_rem(const char *path) { - DirBLF *dir; - - dir = blf_dir_find(path); - if (dir) { - BLI_remlink(&global_font_dir, dir); - MEM_freeN(dir->path); - MEM_freeN(dir); - } + DirBLF *dir; + + dir = blf_dir_find(path); + if (dir) { + BLI_remlink(&global_font_dir, dir); + MEM_freeN(dir->path); + MEM_freeN(dir); + } } char **BLF_dir_get(int *ndir) { - DirBLF *p; - char **dirs; - char *path; - int i, count; - - count = BLI_listbase_count(&global_font_dir); - if (!count) - return NULL; - - dirs = (char **)MEM_callocN(sizeof(char *) * count, "BLF_dir_get"); - p = global_font_dir.first; - i = 0; - while (p) { - path = BLI_strdup(p->path); - dirs[i] = path; - p = p->next; - } - *ndir = i; - return dirs; + DirBLF *p; + char **dirs; + char *path; + int i, count; + + count = BLI_listbase_count(&global_font_dir); + if (!count) + return NULL; + + dirs = (char **)MEM_callocN(sizeof(char *) * count, "BLF_dir_get"); + p = global_font_dir.first; + i = 0; + while (p) { + path = BLI_strdup(p->path); + dirs[i] = path; + p = p->next; + } + *ndir = i; + return dirs; } void BLF_dir_free(char **dirs, int count) { - char *path; - int i; - - for (i = 0; i < count; i++) { - path = dirs[i]; - MEM_freeN(path); - } - MEM_freeN(dirs); + char *path; + int i; + + for (i = 0; i < count; i++) { + path = dirs[i]; + MEM_freeN(path); + } + MEM_freeN(dirs); } char *blf_dir_search(const char *file) { - DirBLF *dir; - char full_path[FILE_MAX]; - char *s = NULL; - - for (dir = global_font_dir.first; dir; dir = dir->next) { - BLI_join_dirfile(full_path, sizeof(full_path), dir->path, file); - if (BLI_exists(full_path)) { - s = BLI_strdup(full_path); - break; - } - } - - if (!s) { - /* check the current directory, why not ? */ - if (BLI_exists(file)) - s = BLI_strdup(file); - } - - return s; + DirBLF *dir; + char full_path[FILE_MAX]; + char *s = NULL; + + for (dir = global_font_dir.first; dir; dir = dir->next) { + BLI_join_dirfile(full_path, sizeof(full_path), dir->path, file); + if (BLI_exists(full_path)) { + s = BLI_strdup(full_path); + break; + } + } + + if (!s) { + /* check the current directory, why not ? */ + if (BLI_exists(file)) + s = BLI_strdup(file); + } + + return s; } /* Some font have additional file with metrics information, @@ -151,31 +150,31 @@ char *blf_dir_search(const char *file) */ char *blf_dir_metrics_search(const char *filename) { - char *mfile; - char *s; - - mfile = BLI_strdup(filename); - s = strrchr(mfile, '.'); - if (s) { - if (BLI_strnlen(s, 4) < 4) { - MEM_freeN(mfile); - return NULL; - } - s++; - s[0] = 'a'; - s[1] = 'f'; - s[2] = 'm'; - - /* first check .afm */ - if (BLI_exists(mfile)) - return mfile; - - /* and now check .pfm */ - s[0] = 'p'; - - if (BLI_exists(mfile)) - return mfile; - } - MEM_freeN(mfile); - return NULL; + char *mfile; + char *s; + + mfile = BLI_strdup(filename); + s = strrchr(mfile, '.'); + if (s) { + if (BLI_strnlen(s, 4) < 4) { + MEM_freeN(mfile); + return NULL; + } + s++; + s[0] = 'a'; + s[1] = 'f'; + s[2] = 'm'; + + /* first check .afm */ + if (BLI_exists(mfile)) + return mfile; + + /* and now check .pfm */ + s[0] = 'p'; + + if (BLI_exists(mfile)) + return mfile; + } + MEM_freeN(mfile); + return NULL; } diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c index 06904ee36b6..ae406b103a7 100644 --- a/source/blender/blenfont/intern/blf_font.c +++ b/source/blender/blenfont/intern/blf_font.c @@ -25,7 +25,6 @@ * Also low level functions for managing \a FontBLF. */ - #include #include #include @@ -83,128 +82,130 @@ static SpinLock ft_lib_mutex; */ static void blf_batch_draw_init(void) { - GPUVertFormat format = {0}; - g_batch.pos_loc = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 4, GPU_FETCH_FLOAT); - g_batch.tex_loc = GPU_vertformat_attr_add(&format, "tex", GPU_COMP_F32, 4, GPU_FETCH_FLOAT); - g_batch.col_loc = GPU_vertformat_attr_add(&format, "col", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT); + GPUVertFormat format = {0}; + g_batch.pos_loc = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 4, GPU_FETCH_FLOAT); + g_batch.tex_loc = GPU_vertformat_attr_add(&format, "tex", GPU_COMP_F32, 4, GPU_FETCH_FLOAT); + g_batch.col_loc = GPU_vertformat_attr_add( + &format, "col", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT); - g_batch.verts = GPU_vertbuf_create_with_format_ex(&format, GPU_USAGE_STREAM); - GPU_vertbuf_data_alloc(g_batch.verts, BLF_BATCH_DRAW_LEN_MAX); + g_batch.verts = GPU_vertbuf_create_with_format_ex(&format, GPU_USAGE_STREAM); + GPU_vertbuf_data_alloc(g_batch.verts, BLF_BATCH_DRAW_LEN_MAX); - GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.pos_loc, &g_batch.pos_step); - GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.tex_loc, &g_batch.tex_step); - GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.col_loc, &g_batch.col_step); - g_batch.glyph_len = 0; + GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.pos_loc, &g_batch.pos_step); + GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.tex_loc, &g_batch.tex_step); + GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.col_loc, &g_batch.col_step); + g_batch.glyph_len = 0; - g_batch.batch = GPU_batch_create_ex(GPU_PRIM_POINTS, g_batch.verts, NULL, GPU_BATCH_OWNS_VBO); + g_batch.batch = GPU_batch_create_ex(GPU_PRIM_POINTS, g_batch.verts, NULL, GPU_BATCH_OWNS_VBO); } static void blf_batch_draw_exit(void) { - GPU_BATCH_DISCARD_SAFE(g_batch.batch); + GPU_BATCH_DISCARD_SAFE(g_batch.batch); } void blf_batch_draw_vao_clear(void) { - if (g_batch.batch) { - GPU_batch_vao_cache_clear(g_batch.batch); - } + if (g_batch.batch) { + GPU_batch_vao_cache_clear(g_batch.batch); + } } void blf_batch_draw_begin(FontBLF *font) { - if (g_batch.batch == NULL) { - blf_batch_draw_init(); - } - - const bool font_changed = (g_batch.font != font); - const bool simple_shader = ((font->flags & (BLF_ROTATION | BLF_MATRIX | BLF_ASPECT)) == 0); - const bool shader_changed = (simple_shader != g_batch.simple_shader); - - g_batch.active = g_batch.enabled && simple_shader; - - if (simple_shader) { - /* Offset is applied to each glyph. */ - g_batch.ofs[0] = floorf(font->pos[0]); - g_batch.ofs[1] = floorf(font->pos[1]); - } - else { - /* Offset is baked in modelview mat. */ - zero_v2(g_batch.ofs); - } - - if (g_batch.active) { - float gpumat[4][4]; - GPU_matrix_model_view_get(gpumat); - - bool mat_changed = (memcmp(gpumat, g_batch.mat, sizeof(g_batch.mat)) != 0); - - if (mat_changed) { - /* Modelviewmat is no longer the same. - * Flush cache but with the previous mat. */ - GPU_matrix_push(); - GPU_matrix_set(g_batch.mat); - } - - /* flush cache if config is not the same. */ - if (mat_changed || font_changed || shader_changed) { - blf_batch_draw(); - g_batch.simple_shader = simple_shader; - g_batch.font = font; - } - else { - /* Nothing changed continue batching. */ - return; - } - - if (mat_changed) { - GPU_matrix_pop(); - /* Save for next memcmp. */ - memcpy(g_batch.mat, gpumat, sizeof(g_batch.mat)); - } - } - else { - /* flush cache */ - blf_batch_draw(); - g_batch.font = font; - g_batch.simple_shader = simple_shader; - } + if (g_batch.batch == NULL) { + blf_batch_draw_init(); + } + + const bool font_changed = (g_batch.font != font); + const bool simple_shader = ((font->flags & (BLF_ROTATION | BLF_MATRIX | BLF_ASPECT)) == 0); + const bool shader_changed = (simple_shader != g_batch.simple_shader); + + g_batch.active = g_batch.enabled && simple_shader; + + if (simple_shader) { + /* Offset is applied to each glyph. */ + g_batch.ofs[0] = floorf(font->pos[0]); + g_batch.ofs[1] = floorf(font->pos[1]); + } + else { + /* Offset is baked in modelview mat. */ + zero_v2(g_batch.ofs); + } + + if (g_batch.active) { + float gpumat[4][4]; + GPU_matrix_model_view_get(gpumat); + + bool mat_changed = (memcmp(gpumat, g_batch.mat, sizeof(g_batch.mat)) != 0); + + if (mat_changed) { + /* Modelviewmat is no longer the same. + * Flush cache but with the previous mat. */ + GPU_matrix_push(); + GPU_matrix_set(g_batch.mat); + } + + /* flush cache if config is not the same. */ + if (mat_changed || font_changed || shader_changed) { + blf_batch_draw(); + g_batch.simple_shader = simple_shader; + g_batch.font = font; + } + else { + /* Nothing changed continue batching. */ + return; + } + + if (mat_changed) { + GPU_matrix_pop(); + /* Save for next memcmp. */ + memcpy(g_batch.mat, gpumat, sizeof(g_batch.mat)); + } + } + else { + /* flush cache */ + blf_batch_draw(); + g_batch.font = font; + g_batch.simple_shader = simple_shader; + } } void blf_batch_draw(void) { - if (g_batch.glyph_len == 0) - return; + if (g_batch.glyph_len == 0) + return; - GPU_blend(true); - GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA); + GPU_blend(true); + GPU_blend_set_func_separate( + GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA); - /* We need to flush widget base first to ensure correct ordering. */ - UI_widgetbase_draw_cache_flush(); + /* We need to flush widget base first to ensure correct ordering. */ + UI_widgetbase_draw_cache_flush(); - GPU_texture_bind(g_batch.tex_bind_state, 0); - GPU_vertbuf_data_len_set(g_batch.verts, g_batch.glyph_len); - GPU_vertbuf_use(g_batch.verts); /* send data */ + GPU_texture_bind(g_batch.tex_bind_state, 0); + GPU_vertbuf_data_len_set(g_batch.verts, g_batch.glyph_len); + GPU_vertbuf_use(g_batch.verts); /* send data */ - eGPUBuiltinShader shader = (g_batch.simple_shader) ? GPU_SHADER_TEXT_SIMPLE : GPU_SHADER_TEXT; - GPU_batch_program_set_builtin(g_batch.batch, shader); - GPU_batch_uniform_1i(g_batch.batch, "glyph", 0); - GPU_batch_draw(g_batch.batch); + eGPUBuiltinShader shader = (g_batch.simple_shader) ? GPU_SHADER_TEXT_SIMPLE : GPU_SHADER_TEXT; + GPU_batch_program_set_builtin(g_batch.batch, shader); + GPU_batch_uniform_1i(g_batch.batch, "glyph", 0); + GPU_batch_draw(g_batch.batch); - GPU_blend(false); + GPU_blend(false); - /* restart to 1st vertex data pointers */ - GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.pos_loc, &g_batch.pos_step); - GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.tex_loc, &g_batch.tex_step); - GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.col_loc, &g_batch.col_step); - g_batch.glyph_len = 0; + /* restart to 1st vertex data pointers */ + GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.pos_loc, &g_batch.pos_step); + GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.tex_loc, &g_batch.tex_step); + GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.col_loc, &g_batch.col_step); + g_batch.glyph_len = 0; } static void blf_batch_draw_end(void) { - if (!g_batch.active) { - blf_batch_draw(); - } + if (!g_batch.active) { + blf_batch_draw(); + } } /** \} */ @@ -213,81 +214,81 @@ static void blf_batch_draw_end(void) int blf_font_init(void) { - memset(&g_batch, 0, sizeof(g_batch)); - BLI_spin_init(&ft_lib_mutex); - return FT_Init_FreeType(&ft_lib); + memset(&g_batch, 0, sizeof(g_batch)); + BLI_spin_init(&ft_lib_mutex); + return FT_Init_FreeType(&ft_lib); } void blf_font_exit(void) { - FT_Done_FreeType(ft_lib); - BLI_spin_end(&ft_lib_mutex); - blf_batch_draw_exit(); + FT_Done_FreeType(ft_lib); + BLI_spin_end(&ft_lib_mutex); + blf_batch_draw_exit(); } void blf_font_size(FontBLF *font, unsigned int size, unsigned int dpi) { - GlyphCacheBLF *gc; - FT_Error err; - - gc = blf_glyph_cache_find(font, size, dpi); - if (gc) { - font->glyph_cache = gc; - /* Optimization: do not call FT_Set_Char_Size if size did not change. */ - if (font->size == size && font->dpi == dpi) - return; - } - - err = FT_Set_Char_Size(font->face, 0, (FT_F26Dot6)(size * 64), dpi, dpi); - if (err) { - /* FIXME: here we can go through the fixed size and choice a close one */ - printf("The current font don't support the size, %u and dpi, %u\n", size, dpi); - return; - } - - font->size = size; - font->dpi = dpi; - - if (!gc) { - gc = blf_glyph_cache_new(font); - if (gc) - font->glyph_cache = gc; - else - font->glyph_cache = NULL; - } + GlyphCacheBLF *gc; + FT_Error err; + + gc = blf_glyph_cache_find(font, size, dpi); + if (gc) { + font->glyph_cache = gc; + /* Optimization: do not call FT_Set_Char_Size if size did not change. */ + if (font->size == size && font->dpi == dpi) + return; + } + + err = FT_Set_Char_Size(font->face, 0, (FT_F26Dot6)(size * 64), dpi, dpi); + if (err) { + /* FIXME: here we can go through the fixed size and choice a close one */ + printf("The current font don't support the size, %u and dpi, %u\n", size, dpi); + return; + } + + font->size = size; + font->dpi = dpi; + + if (!gc) { + gc = blf_glyph_cache_new(font); + if (gc) + font->glyph_cache = gc; + else + font->glyph_cache = NULL; + } } static void blf_font_ensure_ascii_table(FontBLF *font) { - GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; - - /* build ascii on demand */ - if (glyph_ascii_table['0'] == NULL) { - GlyphBLF *g; - unsigned int i; - for (i = 0; i < 256; i++) { - g = blf_glyph_search(font->glyph_cache, i); - if (!g) { - FT_UInt glyph_index = FT_Get_Char_Index(font->face, i); - g = blf_glyph_add(font, glyph_index, i); - } - glyph_ascii_table[i] = g; - } - } + GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; + + /* build ascii on demand */ + if (glyph_ascii_table['0'] == NULL) { + GlyphBLF *g; + unsigned int i; + for (i = 0; i < 256; i++) { + g = blf_glyph_search(font->glyph_cache, i); + if (!g) { + FT_UInt glyph_index = FT_Get_Char_Index(font->face, i); + g = blf_glyph_add(font, glyph_index, i); + } + glyph_ascii_table[i] = g; + } + } } static void blf_font_ensure_ascii_kerning(FontBLF *font, const FT_UInt kern_mode) { - KerningCacheBLF *kc = font->kerning_cache; + KerningCacheBLF *kc = font->kerning_cache; - font->kerning_mode = kern_mode; + font->kerning_mode = kern_mode; - if (!kc || kc->mode != kern_mode) { - font->kerning_cache = kc = blf_kerning_cache_find(font); - if (!kc) { - font->kerning_cache = kc = blf_kerning_cache_new(font); - } - } + if (!kc || kc->mode != kern_mode) { + font->kerning_cache = kc = blf_kerning_cache_find(font); + if (!kc) { + font->kerning_cache = kc = blf_kerning_cache_new(font); + } + } } /* Fast path for runs of ASCII characters. Given that common UTF-8 @@ -298,528 +299,518 @@ static void blf_font_ensure_ascii_kerning(FontBLF *font, const FT_UInt kern_mode /* Note, * blf_font_ensure_ascii_table(font); must be called before this macro */ -#define BLF_UTF8_NEXT_FAST(_font, _g, _str, _i, _c, _glyph_ascii_table) \ - if (((_c) = (_str)[_i]) < 0x80) { \ - _g = (_glyph_ascii_table)[_c]; \ - _i++; \ - } \ - else if ((_c = BLI_str_utf8_as_unicode_step(_str, &(_i))) != BLI_UTF8_ERR) { \ - if ((_g = blf_glyph_search((_font)->glyph_cache, _c)) == NULL) { \ - _g = blf_glyph_add(_font, \ - FT_Get_Char_Index((_font)->face, _c), _c); \ - } \ - } (void)0 - - -#define BLF_KERNING_VARS(_font, _has_kerning, _kern_mode) \ - const bool _has_kerning = FT_HAS_KERNING((_font)->face) != 0; \ - const FT_UInt _kern_mode = (_has_kerning == 0) ? 0 : \ - (((_font)->flags & BLF_KERNING_DEFAULT) ? \ - ft_kerning_default : (FT_UInt)FT_KERNING_UNFITTED) \ +#define BLF_UTF8_NEXT_FAST(_font, _g, _str, _i, _c, _glyph_ascii_table) \ + if (((_c) = (_str)[_i]) < 0x80) { \ + _g = (_glyph_ascii_table)[_c]; \ + _i++; \ + } \ + else if ((_c = BLI_str_utf8_as_unicode_step(_str, &(_i))) != BLI_UTF8_ERR) { \ + if ((_g = blf_glyph_search((_font)->glyph_cache, _c)) == NULL) { \ + _g = blf_glyph_add(_font, FT_Get_Char_Index((_font)->face, _c), _c); \ + } \ + } \ + (void)0 + +#define BLF_KERNING_VARS(_font, _has_kerning, _kern_mode) \ + const bool _has_kerning = FT_HAS_KERNING((_font)->face) != 0; \ + const FT_UInt _kern_mode = (_has_kerning == 0) ? 0 : \ + (((_font)->flags & BLF_KERNING_DEFAULT) ? \ + ft_kerning_default : \ + (FT_UInt)FT_KERNING_UNFITTED) /* Note, * blf_font_ensure_ascii_kerning(font, kern_mode); must be called before this macro */ #define BLF_KERNING_STEP_FAST(_font, _kern_mode, _g_prev, _g, _c_prev, _c, _pen_x) \ -{ \ - if (_g_prev) { \ - FT_Vector _delta; \ - if (_c_prev < 0x80 && _c < 0x80) { \ - _pen_x += (_font)->kerning_cache->table[_c][_c_prev]; \ - } \ - else if (FT_Get_Kerning((_font)->face, \ - (_g_prev)->idx, \ - (_g)->idx, \ - _kern_mode, \ - &(_delta)) == 0) \ - { \ - _pen_x += (int)_delta.x >> 6; \ - } \ - } \ -} (void)0 - -#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 += (int)_delta.x >> 6; \ - } \ - } \ -} (void)0 + { \ + if (_g_prev) { \ + FT_Vector _delta; \ + if (_c_prev < 0x80 && _c < 0x80) { \ + _pen_x += (_font)->kerning_cache->table[_c][_c_prev]; \ + } \ + else if (FT_Get_Kerning((_font)->face, (_g_prev)->idx, (_g)->idx, _kern_mode, &(_delta)) == \ + 0) { \ + _pen_x += (int)_delta.x >> 6; \ + } \ + } \ + } \ + (void)0 + +#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 += (int)_delta.x >> 6; \ + } \ + } \ + } \ + (void)0 static void blf_font_draw_ex( - FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info, - int pen_y) + FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info, int pen_y) { - unsigned int c, c_prev = BLI_UTF8_ERR; - GlyphBLF *g, *g_prev = NULL; - int pen_x = 0; - size_t i = 0; - GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; + unsigned int c, c_prev = BLI_UTF8_ERR; + GlyphBLF *g, *g_prev = NULL; + int pen_x = 0; + 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; - } + if (len == 0) { + /* early output, don't do any IMM OpenGL. */ + return; + } - BLF_KERNING_VARS(font, has_kerning, kern_mode); + BLF_KERNING_VARS(font, has_kerning, kern_mode); - blf_font_ensure_ascii_table(font); - blf_font_ensure_ascii_kerning(font, kern_mode); + blf_font_ensure_ascii_table(font); + blf_font_ensure_ascii_kerning(font, kern_mode); - blf_batch_draw_begin(font); + blf_batch_draw_begin(font); - while ((i < len) && str[i]) { - BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); + while ((i < len) && str[i]) { + BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); - if (UNLIKELY(c == BLI_UTF8_ERR)) - break; - if (UNLIKELY(g == NULL)) - continue; - if (has_kerning) - BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, pen_x); + if (UNLIKELY(c == BLI_UTF8_ERR)) + break; + if (UNLIKELY(g == NULL)) + continue; + if (has_kerning) + BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, 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); + /* 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_i; - g_prev = g; - c_prev = c; - } + pen_x += g->advance_i; + g_prev = g; + c_prev = c; + } - blf_batch_draw_end(); + blf_batch_draw_end(); - if (r_info) { - r_info->lines = 1; - r_info->width = pen_x; - } + if (r_info) { + r_info->lines = 1; + r_info->width = pen_x; + } } void blf_font_draw(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info) { - blf_font_draw_ex(font, str, len, r_info, 0); + blf_font_draw_ex(font, str, len, r_info, 0); } /* faster version of blf_font_draw, ascii only for view dimensions */ static void blf_font_draw_ascii_ex( - FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info, - int pen_y) + FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info, int pen_y) { - unsigned int c, c_prev = BLI_UTF8_ERR; - GlyphBLF *g, *g_prev = NULL; - int pen_x = 0; - GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; + unsigned int c, c_prev = BLI_UTF8_ERR; + GlyphBLF *g, *g_prev = NULL; + int pen_x = 0; + GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; - BLF_KERNING_VARS(font, has_kerning, kern_mode); + BLF_KERNING_VARS(font, has_kerning, kern_mode); - blf_font_ensure_ascii_table(font); - blf_font_ensure_ascii_kerning(font, kern_mode); + blf_font_ensure_ascii_table(font); + blf_font_ensure_ascii_kerning(font, kern_mode); - blf_batch_draw_begin(font); + blf_batch_draw_begin(font); - while ((c = *(str++)) && len--) { - BLI_assert(c < 128); - if ((g = glyph_ascii_table[c]) == NULL) - continue; - if (has_kerning) - BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, pen_x); + while ((c = *(str++)) && len--) { + BLI_assert(c < 128); + if ((g = glyph_ascii_table[c]) == NULL) + continue; + if (has_kerning) + BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, 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); + /* 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_i; - g_prev = g; - c_prev = c; - } + pen_x += g->advance_i; + g_prev = g; + c_prev = c; + } - blf_batch_draw_end(); + blf_batch_draw_end(); - if (r_info) { - r_info->lines = 1; - r_info->width = pen_x; - } + if (r_info) { + r_info->lines = 1; + r_info->width = pen_x; + } } void blf_font_draw_ascii(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info) { - blf_font_draw_ascii_ex(font, str, len, r_info, 0); + blf_font_draw_ascii_ex(font, str, len, r_info, 0); } /* use fixed column width, but an utf8 character may occupy multiple columns */ int blf_font_draw_mono(FontBLF *font, const char *str, size_t len, int cwidth) { - unsigned int c; - GlyphBLF *g; - int col, columns = 0; - int pen_x = 0, pen_y = 0; - size_t i = 0; - GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; + unsigned int c; + GlyphBLF *g; + int col, columns = 0; + int pen_x = 0, pen_y = 0; + size_t i = 0; + GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; - blf_font_ensure_ascii_table(font); + blf_font_ensure_ascii_table(font); - blf_batch_draw_begin(font); + blf_batch_draw_begin(font); - while ((i < len) && str[i]) { - BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); + while ((i < len) && str[i]) { + BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); - if (UNLIKELY(c == BLI_UTF8_ERR)) - break; - if (UNLIKELY(g == NULL)) - continue; + if (UNLIKELY(c == BLI_UTF8_ERR)) + break; + if (UNLIKELY(g == NULL)) + continue; - /* do not return this loop if clipped, we want every character tested */ - blf_glyph_render(font, g, (float)pen_x, (float)pen_y); + /* do not return this loop if clipped, we want every character tested */ + blf_glyph_render(font, g, (float)pen_x, (float)pen_y); - col = BLI_wcwidth((wchar_t)c); - if (col < 0) - col = 1; + col = BLI_wcwidth((wchar_t)c); + if (col < 0) + col = 1; - columns += col; - pen_x += cwidth * col; - } + columns += col; + pen_x += cwidth * col; + } - blf_batch_draw_end(); + blf_batch_draw_end(); - return columns; + return columns; } /* Sanity checks are done by BLF_draw_buffer() */ static void blf_font_draw_buffer_ex( - FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info, - int pen_y) + FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info, int pen_y) { - unsigned int c, c_prev = BLI_UTF8_ERR; - GlyphBLF *g, *g_prev = NULL; - int pen_x = (int)font->pos[0]; - int pen_y_basis = (int)font->pos[1] + pen_y; - size_t i = 0; - GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; - - /* buffer specific vars */ - FontBufInfoBLF *buf_info = &font->buf_info; - const float *b_col_float = buf_info->col_float; - const unsigned char *b_col_char = buf_info->col_char; - int chx, chy; - int y, x; - - BLF_KERNING_VARS(font, has_kerning, kern_mode); - - blf_font_ensure_ascii_table(font); - blf_font_ensure_ascii_kerning(font, kern_mode); - - /* another buffer specific call for color conversion */ - - while ((i < len) && str[i]) { - BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); - - if (UNLIKELY(c == BLI_UTF8_ERR)) - break; - if (UNLIKELY(g == NULL)) - continue; - if (has_kerning) - BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, pen_x); - - chx = pen_x + ((int)g->pos_x); - chy = pen_y_basis + g->height; - - if (g->pitch < 0) { - pen_y = pen_y_basis + (g->height - (int)g->pos_y); - } - else { - pen_y = pen_y_basis - (g->height - (int)g->pos_y); - } - - if ((chx + g->width) >= 0 && chx < buf_info->w && (pen_y + g->height) >= 0 && pen_y < buf_info->h) { - /* don't draw beyond the buffer bounds */ - int width_clip = g->width; - int height_clip = g->height; - int yb_start = g->pitch < 0 ? 0 : g->height - 1; - - if (width_clip + chx > buf_info->w) - width_clip -= chx + width_clip - buf_info->w; - if (height_clip + pen_y > buf_info->h) - height_clip -= pen_y + height_clip - buf_info->h; - - /* drawing below the image? */ - if (pen_y < 0) { - yb_start += (g->pitch < 0) ? -pen_y : pen_y; - height_clip += pen_y; - pen_y = 0; - } - - if (buf_info->fbuf) { - int yb = yb_start; - for (y = ((chy >= 0) ? 0 : -chy); y < height_clip; y++) { - for (x = ((chx >= 0) ? 0 : -chx); x < width_clip; x++) { - const char a_byte = *(g->bitmap + x + (yb * g->pitch)); - if (a_byte) { - const float a = (a_byte / 255.0f) * b_col_float[3]; - const size_t buf_ofs = ( - ((size_t)(chx + x) + ((size_t)(pen_y + y) * (size_t)buf_info->w)) * - (size_t)buf_info->ch); - float *fbuf = buf_info->fbuf + buf_ofs; - - if (a >= 1.0f) { - fbuf[0] = b_col_float[0]; - fbuf[1] = b_col_float[1]; - fbuf[2] = b_col_float[2]; - fbuf[3] = 1.0f; - } - else { - fbuf[0] = (b_col_float[0] * a) + (fbuf[0] * (1.0f - a)); - fbuf[1] = (b_col_float[1] * a) + (fbuf[1] * (1.0f - a)); - fbuf[2] = (b_col_float[2] * a) + (fbuf[2] * (1.0f - a)); - fbuf[3] = MIN2(fbuf[3] + a, 1.0f); /* clamp to 1.0 */ - } - } - } - - if (g->pitch < 0) - yb++; - else - yb--; - } - } - - if (buf_info->cbuf) { - int yb = yb_start; - for (y = ((chy >= 0) ? 0 : -chy); y < height_clip; y++) { - for (x = ((chx >= 0) ? 0 : -chx); x < width_clip; x++) { - const char a_byte = *(g->bitmap + x + (yb * g->pitch)); - - if (a_byte) { - const float a = (a_byte / 255.0f) * b_col_float[3]; - const size_t buf_ofs = ( - ((size_t)(chx + x) + ((size_t)(pen_y + y) * (size_t)buf_info->w)) * - (size_t)buf_info->ch); - unsigned char *cbuf = buf_info->cbuf + buf_ofs; - - if (a >= 1.0f) { - cbuf[0] = b_col_char[0]; - cbuf[1] = b_col_char[1]; - cbuf[2] = b_col_char[2]; - cbuf[3] = 255; - } - else { - cbuf[0] = (unsigned char)((b_col_char[0] * a) + (cbuf[0] * (1.0f - a))); - cbuf[1] = (unsigned char)((b_col_char[1] * a) + (cbuf[1] * (1.0f - a))); - cbuf[2] = (unsigned char)((b_col_char[2] * a) + (cbuf[2] * (1.0f - a))); - cbuf[3] = (unsigned char)MIN2((int)cbuf[3] + (int)(a * 255), 255); /* clamp to 255 */ - } - } - } - - if (g->pitch < 0) - yb++; - else - yb--; - } - } - } - - pen_x += g->advance_i; - g_prev = g; - c_prev = c; - } - - if (r_info) { - r_info->lines = 1; - r_info->width = pen_x; - } + unsigned int c, c_prev = BLI_UTF8_ERR; + GlyphBLF *g, *g_prev = NULL; + int pen_x = (int)font->pos[0]; + int pen_y_basis = (int)font->pos[1] + pen_y; + size_t i = 0; + GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; + + /* buffer specific vars */ + FontBufInfoBLF *buf_info = &font->buf_info; + const float *b_col_float = buf_info->col_float; + const unsigned char *b_col_char = buf_info->col_char; + int chx, chy; + int y, x; + + BLF_KERNING_VARS(font, has_kerning, kern_mode); + + blf_font_ensure_ascii_table(font); + blf_font_ensure_ascii_kerning(font, kern_mode); + + /* another buffer specific call for color conversion */ + + while ((i < len) && str[i]) { + BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); + + if (UNLIKELY(c == BLI_UTF8_ERR)) + break; + if (UNLIKELY(g == NULL)) + continue; + if (has_kerning) + BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, pen_x); + + chx = pen_x + ((int)g->pos_x); + chy = pen_y_basis + g->height; + + if (g->pitch < 0) { + pen_y = pen_y_basis + (g->height - (int)g->pos_y); + } + else { + pen_y = pen_y_basis - (g->height - (int)g->pos_y); + } + + if ((chx + g->width) >= 0 && chx < buf_info->w && (pen_y + g->height) >= 0 && + pen_y < buf_info->h) { + /* don't draw beyond the buffer bounds */ + int width_clip = g->width; + int height_clip = g->height; + int yb_start = g->pitch < 0 ? 0 : g->height - 1; + + if (width_clip + chx > buf_info->w) + width_clip -= chx + width_clip - buf_info->w; + if (height_clip + pen_y > buf_info->h) + height_clip -= pen_y + height_clip - buf_info->h; + + /* drawing below the image? */ + if (pen_y < 0) { + yb_start += (g->pitch < 0) ? -pen_y : pen_y; + height_clip += pen_y; + pen_y = 0; + } + + if (buf_info->fbuf) { + int yb = yb_start; + for (y = ((chy >= 0) ? 0 : -chy); y < height_clip; y++) { + for (x = ((chx >= 0) ? 0 : -chx); x < width_clip; x++) { + const char a_byte = *(g->bitmap + x + (yb * g->pitch)); + if (a_byte) { + const float a = (a_byte / 255.0f) * b_col_float[3]; + const size_t buf_ofs = (((size_t)(chx + x) + + ((size_t)(pen_y + y) * (size_t)buf_info->w)) * + (size_t)buf_info->ch); + float *fbuf = buf_info->fbuf + buf_ofs; + + if (a >= 1.0f) { + fbuf[0] = b_col_float[0]; + fbuf[1] = b_col_float[1]; + fbuf[2] = b_col_float[2]; + fbuf[3] = 1.0f; + } + else { + fbuf[0] = (b_col_float[0] * a) + (fbuf[0] * (1.0f - a)); + fbuf[1] = (b_col_float[1] * a) + (fbuf[1] * (1.0f - a)); + fbuf[2] = (b_col_float[2] * a) + (fbuf[2] * (1.0f - a)); + fbuf[3] = MIN2(fbuf[3] + a, 1.0f); /* clamp to 1.0 */ + } + } + } + + if (g->pitch < 0) + yb++; + else + yb--; + } + } + + if (buf_info->cbuf) { + int yb = yb_start; + for (y = ((chy >= 0) ? 0 : -chy); y < height_clip; y++) { + for (x = ((chx >= 0) ? 0 : -chx); x < width_clip; x++) { + const char a_byte = *(g->bitmap + x + (yb * g->pitch)); + + if (a_byte) { + const float a = (a_byte / 255.0f) * b_col_float[3]; + const size_t buf_ofs = (((size_t)(chx + x) + + ((size_t)(pen_y + y) * (size_t)buf_info->w)) * + (size_t)buf_info->ch); + unsigned char *cbuf = buf_info->cbuf + buf_ofs; + + if (a >= 1.0f) { + cbuf[0] = b_col_char[0]; + cbuf[1] = b_col_char[1]; + cbuf[2] = b_col_char[2]; + cbuf[3] = 255; + } + else { + cbuf[0] = (unsigned char)((b_col_char[0] * a) + (cbuf[0] * (1.0f - a))); + cbuf[1] = (unsigned char)((b_col_char[1] * a) + (cbuf[1] * (1.0f - a))); + cbuf[2] = (unsigned char)((b_col_char[2] * a) + (cbuf[2] * (1.0f - a))); + cbuf[3] = (unsigned char)MIN2((int)cbuf[3] + (int)(a * 255), + 255); /* clamp to 255 */ + } + } + } + + if (g->pitch < 0) + yb++; + else + yb--; + } + } + } + + pen_x += g->advance_i; + g_prev = g; + c_prev = c; + } + + if (r_info) { + r_info->lines = 1; + r_info->width = pen_x; + } } -void blf_font_draw_buffer( - FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info) +void blf_font_draw_buffer(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info) { - blf_font_draw_buffer_ex(font, str, len, r_info, 0); + blf_font_draw_buffer_ex(font, str, len, r_info, 0); } -static bool blf_font_width_to_strlen_glyph_process( - FontBLF *font, const bool has_kerning, const FT_UInt kern_mode, - const uint c_prev, const uint c, GlyphBLF *g_prev, GlyphBLF *g, - int *pen_x, const int width_i) +static bool blf_font_width_to_strlen_glyph_process(FontBLF *font, + const bool has_kerning, + const FT_UInt kern_mode, + const uint c_prev, + const uint c, + GlyphBLF *g_prev, + GlyphBLF *g, + int *pen_x, + const int width_i) { - if (UNLIKELY(c == BLI_UTF8_ERR)) { - return true; /* break the calling loop. */ - } - if (UNLIKELY(g == NULL)) { - return false; /* continue the calling loop. */ - } - if (has_kerning) { - BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, *pen_x); - } - - *pen_x += g->advance_i; - - return (*pen_x >= width_i); + if (UNLIKELY(c == BLI_UTF8_ERR)) { + return true; /* break the calling loop. */ + } + if (UNLIKELY(g == NULL)) { + return false; /* continue the calling loop. */ + } + if (has_kerning) { + BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, *pen_x); + } + + *pen_x += g->advance_i; + + return (*pen_x >= width_i); } -size_t blf_font_width_to_strlen(FontBLF *font, const char *str, size_t len, float width, float *r_width) +size_t blf_font_width_to_strlen( + FontBLF *font, const char *str, size_t len, float width, float *r_width) { - unsigned int c, c_prev; - GlyphBLF *g, *g_prev; - int pen_x, width_new; - size_t i, i_prev; - GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; - const int width_i = (int)width; - - BLF_KERNING_VARS(font, has_kerning, kern_mode); - - blf_font_ensure_ascii_table(font); - if (has_kerning) { - blf_font_ensure_ascii_kerning(font, kern_mode); - } - - for (i_prev = i = 0, width_new = pen_x = 0, g_prev = NULL, c_prev = 0; - (i < len) && str[i]; - i_prev = i, width_new = pen_x, c_prev = c, g_prev = g) - { - BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); - - if (blf_font_width_to_strlen_glyph_process( - font, has_kerning, kern_mode, - c_prev, c, g_prev, g, - &pen_x, width_i)) - { - break; - } - } - - if (r_width) { - *r_width = (float)width_new; - } - - return i_prev; + unsigned int c, c_prev; + GlyphBLF *g, *g_prev; + int pen_x, width_new; + size_t i, i_prev; + GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; + const int width_i = (int)width; + + BLF_KERNING_VARS(font, has_kerning, kern_mode); + + blf_font_ensure_ascii_table(font); + if (has_kerning) { + blf_font_ensure_ascii_kerning(font, kern_mode); + } + + for (i_prev = i = 0, width_new = pen_x = 0, g_prev = NULL, c_prev = 0; (i < len) && str[i]; + i_prev = i, width_new = pen_x, c_prev = c, g_prev = g) { + BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); + + if (blf_font_width_to_strlen_glyph_process( + font, has_kerning, kern_mode, c_prev, c, g_prev, g, &pen_x, width_i)) { + break; + } + } + + if (r_width) { + *r_width = (float)width_new; + } + + return i_prev; } -size_t blf_font_width_to_rstrlen(FontBLF *font, const char *str, size_t len, float width, float *r_width) +size_t blf_font_width_to_rstrlen( + FontBLF *font, const char *str, size_t len, float width, float *r_width) { - unsigned int c, c_prev; - GlyphBLF *g, *g_prev; - int pen_x, width_new; - size_t i, i_prev, i_tmp; - char *s, *s_prev; - GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; - const int width_i = (int)width; - - BLF_KERNING_VARS(font, has_kerning, kern_mode); - - blf_font_ensure_ascii_table(font); - if (has_kerning) { - blf_font_ensure_ascii_kerning(font, kern_mode); - } - - i = BLI_strnlen(str, len); - s = BLI_str_find_prev_char_utf8(str, &str[i]); - i = (size_t)((s != NULL) ? s - str : 0); - s_prev = BLI_str_find_prev_char_utf8(str, s); - i_prev = (size_t)((s_prev != NULL) ? s_prev - str : 0); - - i_tmp = i; - BLF_UTF8_NEXT_FAST(font, g, str, i_tmp, c, glyph_ascii_table); - for (width_new = pen_x = 0; - (s != NULL); - i = i_prev, s = s_prev, c = c_prev, g = g_prev, g_prev = NULL, width_new = pen_x) - { - s_prev = BLI_str_find_prev_char_utf8(str, s); - i_prev = (size_t)((s_prev != NULL) ? s_prev - str : 0); - - if (s_prev != NULL) { - i_tmp = i_prev; - BLF_UTF8_NEXT_FAST(font, g_prev, str, i_tmp, c_prev, glyph_ascii_table); - BLI_assert(i_tmp == i); - } - - if (blf_font_width_to_strlen_glyph_process( - font, has_kerning, kern_mode, - c_prev, c, g_prev, g, - &pen_x, width_i)) - { - break; - } - } - - if (r_width) { - *r_width = (float)width_new; - } - - return i; + unsigned int c, c_prev; + GlyphBLF *g, *g_prev; + int pen_x, width_new; + size_t i, i_prev, i_tmp; + char *s, *s_prev; + GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; + const int width_i = (int)width; + + BLF_KERNING_VARS(font, has_kerning, kern_mode); + + blf_font_ensure_ascii_table(font); + if (has_kerning) { + blf_font_ensure_ascii_kerning(font, kern_mode); + } + + i = BLI_strnlen(str, len); + s = BLI_str_find_prev_char_utf8(str, &str[i]); + i = (size_t)((s != NULL) ? s - str : 0); + s_prev = BLI_str_find_prev_char_utf8(str, s); + i_prev = (size_t)((s_prev != NULL) ? s_prev - str : 0); + + i_tmp = i; + BLF_UTF8_NEXT_FAST(font, g, str, i_tmp, c, glyph_ascii_table); + for (width_new = pen_x = 0; (s != NULL); + i = i_prev, s = s_prev, c = c_prev, g = g_prev, g_prev = NULL, width_new = pen_x) { + s_prev = BLI_str_find_prev_char_utf8(str, s); + i_prev = (size_t)((s_prev != NULL) ? s_prev - str : 0); + + if (s_prev != NULL) { + i_tmp = i_prev; + BLF_UTF8_NEXT_FAST(font, g_prev, str, i_tmp, c_prev, glyph_ascii_table); + BLI_assert(i_tmp == i); + } + + if (blf_font_width_to_strlen_glyph_process( + font, has_kerning, kern_mode, c_prev, c, g_prev, g, &pen_x, width_i)) { + break; + } + } + + if (r_width) { + *r_width = (float)width_new; + } + + return i; } static void blf_font_boundbox_ex( - FontBLF *font, const char *str, size_t len, rctf *box, struct ResultBLF *r_info, - int pen_y) + FontBLF *font, const char *str, size_t len, rctf *box, struct ResultBLF *r_info, int pen_y) { - unsigned int c, c_prev = BLI_UTF8_ERR; - GlyphBLF *g, *g_prev = NULL; - int pen_x = 0; - size_t i = 0; - GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; - - rctf gbox; - - BLF_KERNING_VARS(font, has_kerning, kern_mode); - - box->xmin = 32000.0f; - box->xmax = -32000.0f; - box->ymin = 32000.0f; - box->ymax = -32000.0f; - - blf_font_ensure_ascii_table(font); - blf_font_ensure_ascii_kerning(font, kern_mode); - - while ((i < len) && str[i]) { - BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); - - if (UNLIKELY(c == BLI_UTF8_ERR)) - break; - if (UNLIKELY(g == NULL)) - continue; - if (has_kerning) - BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, pen_x); - - gbox.xmin = (float)pen_x; - gbox.xmax = (float)pen_x + g->advance; - gbox.ymin = g->box.ymin + (float)pen_y; - gbox.ymax = g->box.ymax + (float)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_i; - g_prev = g; - c_prev = c; - } - - if (box->xmin > box->xmax) { - box->xmin = 0.0f; - box->ymin = 0.0f; - box->xmax = 0.0f; - box->ymax = 0.0f; - } - - if (r_info) { - r_info->lines = 1; - r_info->width = pen_x; - } + unsigned int c, c_prev = BLI_UTF8_ERR; + GlyphBLF *g, *g_prev = NULL; + int pen_x = 0; + size_t i = 0; + GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; + + rctf gbox; + + BLF_KERNING_VARS(font, has_kerning, kern_mode); + + box->xmin = 32000.0f; + box->xmax = -32000.0f; + box->ymin = 32000.0f; + box->ymax = -32000.0f; + + blf_font_ensure_ascii_table(font); + blf_font_ensure_ascii_kerning(font, kern_mode); + + while ((i < len) && str[i]) { + BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); + + if (UNLIKELY(c == BLI_UTF8_ERR)) + break; + if (UNLIKELY(g == NULL)) + continue; + if (has_kerning) + BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, pen_x); + + gbox.xmin = (float)pen_x; + gbox.xmax = (float)pen_x + g->advance; + gbox.ymin = g->box.ymin + (float)pen_y; + gbox.ymax = g->box.ymax + (float)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_i; + g_prev = g; + c_prev = c; + } + + if (box->xmin > box->xmax) { + box->xmin = 0.0f; + box->ymin = 0.0f; + box->xmax = 0.0f; + box->ymax = 0.0f; + } + + if (r_info) { + r_info->lines = 1; + r_info->width = pen_x; + } } -void blf_font_boundbox(FontBLF *font, const char *str, size_t len, rctf *r_box, struct ResultBLF *r_info) +void blf_font_boundbox( + FontBLF *font, const char *str, size_t len, rctf *r_box, struct ResultBLF *r_info) { - blf_font_boundbox_ex(font, str, len, r_box, r_info, 0); + blf_font_boundbox_ex(font, str, len, r_box, r_info, 0); } - /* -------------------------------------------------------------------- */ /** \name Word-Wrap Support * \{ */ - /** * Generic function to add word-wrap support for other existing functions. * @@ -830,377 +821,392 @@ void blf_font_boundbox(FontBLF *font, const char *str, size_t len, rctf *r_box, * (color, bold, switching fonts... etc). */ static void blf_font_wrap_apply( - FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info, - void (*callback)(FontBLF *font, const char *str, size_t len, int pen_y, void *userdata), - void *userdata) + FontBLF *font, + const char *str, + size_t len, + struct ResultBLF *r_info, + void (*callback)(FontBLF *font, const char *str, size_t len, int pen_y, void *userdata), + void *userdata) { - unsigned int c; - GlyphBLF *g, *g_prev = NULL; - FT_Vector delta; - int pen_x = 0, pen_y = 0; - size_t i = 0; - GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; - int lines = 0; - int pen_x_next = 0; - - BLF_KERNING_VARS(font, has_kerning, kern_mode); - - struct WordWrapVars { - int wrap_width; - size_t start, last[2]; - } wrap = {font->wrap_width != -1 ? font->wrap_width : INT_MAX, 0, {0, 0}}; - - blf_font_ensure_ascii_table(font); - // printf("%s wrapping (%d, %d) `%s`:\n", __func__, len, strlen(str), str); - while ((i < len) && str[i]) { - - /* wrap vars */ - size_t i_curr = i; - bool do_draw = false; - - BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); - - if (UNLIKELY(c == BLI_UTF8_ERR)) - break; - if (UNLIKELY(g == NULL)) - continue; - if (has_kerning) - BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x); - - /** - * Implementation Detail (utf8). - * - * Take care with single byte offsets here, - * since this is utf8 we can't be sure a single byte is a single character. - * - * This is _only_ done when we know for sure the character is ascii (newline or a space). - */ - pen_x_next = pen_x + g->advance_i; - if (UNLIKELY((pen_x_next >= wrap.wrap_width) && (wrap.start != wrap.last[0]))) { - do_draw = true; - } - else if (UNLIKELY(((i < len) && str[i]) == 0)) { - /* need check here for trailing newline, else we draw it */ - wrap.last[0] = i + ((g->c != '\n') ? 1 : 0); - wrap.last[1] = i; - do_draw = true; - } - else if (UNLIKELY(g->c == '\n')) { - wrap.last[0] = i_curr + 1; - wrap.last[1] = i; - do_draw = true; - } - else if (UNLIKELY(g->c != ' ' && (g_prev ? g_prev->c == ' ' : false))) { - wrap.last[0] = i_curr; - wrap.last[1] = i_curr; - } - - if (UNLIKELY(do_draw)) { - // printf("(%03d..%03d) `%.*s`\n", - // wrap.start, wrap.last[0], (wrap.last[0] - wrap.start) - 1, &str[wrap.start]); - - callback(font, &str[wrap.start], (wrap.last[0] - wrap.start) - 1, pen_y, userdata); - wrap.start = wrap.last[0]; - i = wrap.last[1]; - pen_x = 0; - pen_y -= font->glyph_cache->glyph_height_max; - g_prev = NULL; - lines += 1; - continue; - } - - pen_x = pen_x_next; - g_prev = g; - } - - // printf("done! lines: %d, width, %d\n", lines, pen_x_next); - - if (r_info) { - r_info->lines = lines; - /* width of last line only (with wrapped lines) */ - r_info->width = pen_x_next; - } + unsigned int c; + GlyphBLF *g, *g_prev = NULL; + FT_Vector delta; + int pen_x = 0, pen_y = 0; + size_t i = 0; + GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table; + int lines = 0; + int pen_x_next = 0; + + BLF_KERNING_VARS(font, has_kerning, kern_mode); + + struct WordWrapVars { + int wrap_width; + size_t start, last[2]; + } wrap = {font->wrap_width != -1 ? font->wrap_width : INT_MAX, 0, {0, 0}}; + + blf_font_ensure_ascii_table(font); + // printf("%s wrapping (%d, %d) `%s`:\n", __func__, len, strlen(str), str); + while ((i < len) && str[i]) { + + /* wrap vars */ + size_t i_curr = i; + bool do_draw = false; + + BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); + + if (UNLIKELY(c == BLI_UTF8_ERR)) + break; + if (UNLIKELY(g == NULL)) + continue; + if (has_kerning) + BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x); + + /** + * Implementation Detail (utf8). + * + * Take care with single byte offsets here, + * since this is utf8 we can't be sure a single byte is a single character. + * + * This is _only_ done when we know for sure the character is ascii (newline or a space). + */ + pen_x_next = pen_x + g->advance_i; + if (UNLIKELY((pen_x_next >= wrap.wrap_width) && (wrap.start != wrap.last[0]))) { + do_draw = true; + } + else if (UNLIKELY(((i < len) && str[i]) == 0)) { + /* need check here for trailing newline, else we draw it */ + wrap.last[0] = i + ((g->c != '\n') ? 1 : 0); + wrap.last[1] = i; + do_draw = true; + } + else if (UNLIKELY(g->c == '\n')) { + wrap.last[0] = i_curr + 1; + wrap.last[1] = i; + do_draw = true; + } + else if (UNLIKELY(g->c != ' ' && (g_prev ? g_prev->c == ' ' : false))) { + wrap.last[0] = i_curr; + wrap.last[1] = i_curr; + } + + if (UNLIKELY(do_draw)) { + // printf("(%03d..%03d) `%.*s`\n", + // wrap.start, wrap.last[0], (wrap.last[0] - wrap.start) - 1, &str[wrap.start]); + + callback(font, &str[wrap.start], (wrap.last[0] - wrap.start) - 1, pen_y, userdata); + wrap.start = wrap.last[0]; + i = wrap.last[1]; + pen_x = 0; + pen_y -= font->glyph_cache->glyph_height_max; + g_prev = NULL; + lines += 1; + continue; + } + + pen_x = pen_x_next; + g_prev = g; + } + + // printf("done! lines: %d, width, %d\n", lines, pen_x_next); + + if (r_info) { + r_info->lines = lines; + /* width of last line only (with wrapped lines) */ + r_info->width = pen_x_next; + } } /* blf_font_draw__wrap */ -static void blf_font_draw__wrap_cb(FontBLF *font, const char *str, size_t len, int pen_y, void *UNUSED(userdata)) +static void blf_font_draw__wrap_cb( + FontBLF *font, const char *str, size_t len, int pen_y, void *UNUSED(userdata)) { - blf_font_draw_ex(font, str, len, NULL, pen_y); + blf_font_draw_ex(font, str, len, NULL, pen_y); } void blf_font_draw__wrap(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info) { - blf_font_wrap_apply(font, str, len, r_info, blf_font_draw__wrap_cb, NULL); + blf_font_wrap_apply(font, str, len, r_info, blf_font_draw__wrap_cb, NULL); } /* blf_font_boundbox__wrap */ -static void blf_font_boundbox_wrap_cb(FontBLF *font, const char *str, size_t len, int pen_y, void *userdata) +static void blf_font_boundbox_wrap_cb( + FontBLF *font, const char *str, size_t len, int pen_y, void *userdata) { - rctf *box = userdata; - rctf box_single; + rctf *box = userdata; + rctf box_single; - blf_font_boundbox_ex(font, str, len, &box_single, NULL, pen_y); - BLI_rctf_union(box, &box_single); + blf_font_boundbox_ex(font, str, len, &box_single, NULL, pen_y); + BLI_rctf_union(box, &box_single); } -void blf_font_boundbox__wrap(FontBLF *font, const char *str, size_t len, rctf *box, struct ResultBLF *r_info) +void blf_font_boundbox__wrap( + FontBLF *font, const char *str, size_t len, rctf *box, struct ResultBLF *r_info) { - box->xmin = 32000.0f; - box->xmax = -32000.0f; - box->ymin = 32000.0f; - box->ymax = -32000.0f; + box->xmin = 32000.0f; + box->xmax = -32000.0f; + box->ymin = 32000.0f; + box->ymax = -32000.0f; - blf_font_wrap_apply(font, str, len, r_info, blf_font_boundbox_wrap_cb, box); + blf_font_wrap_apply(font, str, len, r_info, blf_font_boundbox_wrap_cb, box); } /* blf_font_draw_buffer__wrap */ -static void blf_font_draw_buffer__wrap_cb(FontBLF *font, const char *str, size_t len, int pen_y, void *UNUSED(userdata)) +static void blf_font_draw_buffer__wrap_cb( + FontBLF *font, const char *str, size_t len, int pen_y, void *UNUSED(userdata)) { - blf_font_draw_buffer_ex(font, str, len, NULL, pen_y); + blf_font_draw_buffer_ex(font, str, len, NULL, pen_y); } -void blf_font_draw_buffer__wrap(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info) +void blf_font_draw_buffer__wrap(FontBLF *font, + const char *str, + size_t len, + struct ResultBLF *r_info) { - blf_font_wrap_apply(font, str, len, r_info, blf_font_draw_buffer__wrap_cb, NULL); + blf_font_wrap_apply(font, str, len, r_info, blf_font_draw_buffer__wrap_cb, NULL); } /** \} */ - -void blf_font_width_and_height( - FontBLF *font, const char *str, size_t len, - float *r_width, float *r_height, struct ResultBLF *r_info) +void blf_font_width_and_height(FontBLF *font, + const char *str, + size_t len, + float *r_width, + float *r_height, + struct ResultBLF *r_info) { - float xa, ya; - rctf box; - - if (font->flags & BLF_ASPECT) { - xa = font->aspect[0]; - ya = font->aspect[1]; - } - else { - xa = 1.0f; - ya = 1.0f; - } - - if (font->flags & BLF_WORD_WRAP) { - blf_font_boundbox__wrap(font, str, len, &box, r_info); - } - else { - blf_font_boundbox(font, str, len, &box, r_info); - } - *r_width = (BLI_rctf_size_x(&box) * xa); - *r_height = (BLI_rctf_size_y(&box) * ya); + float xa, ya; + rctf box; + + if (font->flags & BLF_ASPECT) { + xa = font->aspect[0]; + ya = font->aspect[1]; + } + else { + xa = 1.0f; + ya = 1.0f; + } + + if (font->flags & BLF_WORD_WRAP) { + blf_font_boundbox__wrap(font, str, len, &box, r_info); + } + else { + blf_font_boundbox(font, str, len, &box, r_info); + } + *r_width = (BLI_rctf_size_x(&box) * xa); + *r_height = (BLI_rctf_size_y(&box) * ya); } float blf_font_width(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info) { - float xa; - rctf box; - - if (font->flags & BLF_ASPECT) - xa = font->aspect[0]; - else - xa = 1.0f; - - if (font->flags & BLF_WORD_WRAP) { - blf_font_boundbox__wrap(font, str, len, &box, r_info); - } - else { - blf_font_boundbox(font, str, len, &box, r_info); - } - return BLI_rctf_size_x(&box) * xa; + float xa; + rctf box; + + if (font->flags & BLF_ASPECT) + xa = font->aspect[0]; + else + xa = 1.0f; + + if (font->flags & BLF_WORD_WRAP) { + blf_font_boundbox__wrap(font, str, len, &box, r_info); + } + else { + blf_font_boundbox(font, str, len, &box, r_info); + } + return BLI_rctf_size_x(&box) * xa; } float blf_font_height(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info) { - float ya; - rctf box; - - if (font->flags & BLF_ASPECT) - ya = font->aspect[1]; - else - ya = 1.0f; - - if (font->flags & BLF_WORD_WRAP) { - blf_font_boundbox__wrap(font, str, len, &box, r_info); - } - else { - blf_font_boundbox(font, str, len, &box, r_info); - } - return BLI_rctf_size_y(&box) * ya; + float ya; + rctf box; + + if (font->flags & BLF_ASPECT) + ya = font->aspect[1]; + else + ya = 1.0f; + + if (font->flags & BLF_WORD_WRAP) { + blf_font_boundbox__wrap(font, str, len, &box, r_info); + } + else { + blf_font_boundbox(font, str, len, &box, r_info); + } + return BLI_rctf_size_y(&box) * ya; } float blf_font_fixed_width(FontBLF *font) { - 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; - } - } - - return g->advance; + 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; + } + } + + return g->advance; } -int blf_font_count_missing_chars(FontBLF *font, const char *str, const size_t len, int *r_tot_chars) +int blf_font_count_missing_chars(FontBLF *font, + const char *str, + const size_t len, + int *r_tot_chars) { - int missing = 0; - size_t i = 0; - - *r_tot_chars = 0; - while (i < len) { - unsigned int c; - - if ((c = str[i]) < 0x80) { - i++; - } - else if ((c = BLI_str_utf8_as_unicode_step(str, &i)) != BLI_UTF8_ERR) { - if (FT_Get_Char_Index((font)->face, c) == 0) { - missing++; - } - } - (*r_tot_chars)++; - } - return missing; + int missing = 0; + size_t i = 0; + + *r_tot_chars = 0; + while (i < len) { + unsigned int c; + + if ((c = str[i]) < 0x80) { + i++; + } + else if ((c = BLI_str_utf8_as_unicode_step(str, &i)) != BLI_UTF8_ERR) { + if (FT_Get_Char_Index((font)->face, c) == 0) { + missing++; + } + } + (*r_tot_chars)++; + } + return missing; } void blf_font_free(FontBLF *font) { - GlyphCacheBLF *gc; + GlyphCacheBLF *gc; - font->glyph_cache = NULL; - while ((gc = BLI_pophead(&font->cache))) { - blf_glyph_cache_free(gc); - } + font->glyph_cache = NULL; + while ((gc = BLI_pophead(&font->cache))) { + blf_glyph_cache_free(gc); + } - blf_kerning_cache_clear(font); + blf_kerning_cache_clear(font); - FT_Done_Face(font->face); - if (font->filename) - MEM_freeN(font->filename); - if (font->name) - MEM_freeN(font->name); - MEM_freeN(font); + FT_Done_Face(font->face); + if (font->filename) + MEM_freeN(font->filename); + if (font->name) + MEM_freeN(font->name); + MEM_freeN(font); } static void blf_font_fill(FontBLF *font) { - font->aspect[0] = 1.0f; - font->aspect[1] = 1.0f; - font->aspect[2] = 1.0f; - font->pos[0] = 0.0f; - font->pos[1] = 0.0f; - font->angle = 0.0f; - - 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; - font->clip_rec.ymax = 0.0f; - font->flags = 0; - font->dpi = 0; - font->size = 0; - BLI_listbase_clear(&font->cache); - BLI_listbase_clear(&font->kerning_caches); - font->glyph_cache = NULL; - font->kerning_cache = NULL; + font->aspect[0] = 1.0f; + font->aspect[1] = 1.0f; + font->aspect[2] = 1.0f; + font->pos[0] = 0.0f; + font->pos[1] = 0.0f; + font->angle = 0.0f; + + 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; + font->clip_rec.ymax = 0.0f; + font->flags = 0; + font->dpi = 0; + font->size = 0; + BLI_listbase_clear(&font->cache); + BLI_listbase_clear(&font->kerning_caches); + font->glyph_cache = NULL; + font->kerning_cache = NULL; #if BLF_BLUR_ENABLE - font->blur = 0; + font->blur = 0; #endif - font->tex_size_max = -1; - - font->buf_info.fbuf = NULL; - font->buf_info.cbuf = NULL; - font->buf_info.w = 0; - font->buf_info.h = 0; - font->buf_info.ch = 0; - font->buf_info.col_init[0] = 0; - font->buf_info.col_init[1] = 0; - font->buf_info.col_init[2] = 0; - font->buf_info.col_init[3] = 0; - - font->ft_lib = ft_lib; - font->ft_lib_mutex = &ft_lib_mutex; + font->tex_size_max = -1; + + font->buf_info.fbuf = NULL; + font->buf_info.cbuf = NULL; + font->buf_info.w = 0; + font->buf_info.h = 0; + font->buf_info.ch = 0; + font->buf_info.col_init[0] = 0; + font->buf_info.col_init[1] = 0; + font->buf_info.col_init[2] = 0; + font->buf_info.col_init[3] = 0; + + font->ft_lib = ft_lib; + font->ft_lib_mutex = &ft_lib_mutex; } FontBLF *blf_font_new(const char *name, const char *filename) { - FontBLF *font; - FT_Error err; - char *mfile; - - font = (FontBLF *)MEM_callocN(sizeof(FontBLF), "blf_font_new"); - err = FT_New_Face(ft_lib, filename, 0, &font->face); - if (err) { - MEM_freeN(font); - return NULL; - } - - err = FT_Select_Charmap(font->face, ft_encoding_unicode); - if (err) { - printf("Can't set the unicode character map!\n"); - FT_Done_Face(font->face); - MEM_freeN(font); - return NULL; - } - - mfile = blf_dir_metrics_search(filename); - if (mfile) { - err = FT_Attach_File(font->face, mfile); - if (err) { - fprintf(stderr, "FT_Attach_File failed to load '%s' with error %d\n", filename, (int)err); - } - MEM_freeN(mfile); - } - - font->name = BLI_strdup(name); - font->filename = BLI_strdup(filename); - blf_font_fill(font); - return font; + FontBLF *font; + FT_Error err; + char *mfile; + + font = (FontBLF *)MEM_callocN(sizeof(FontBLF), "blf_font_new"); + err = FT_New_Face(ft_lib, filename, 0, &font->face); + if (err) { + MEM_freeN(font); + return NULL; + } + + err = FT_Select_Charmap(font->face, ft_encoding_unicode); + if (err) { + printf("Can't set the unicode character map!\n"); + FT_Done_Face(font->face); + MEM_freeN(font); + return NULL; + } + + mfile = blf_dir_metrics_search(filename); + if (mfile) { + err = FT_Attach_File(font->face, mfile); + if (err) { + fprintf(stderr, "FT_Attach_File failed to load '%s' with error %d\n", filename, (int)err); + } + MEM_freeN(mfile); + } + + font->name = BLI_strdup(name); + font->filename = BLI_strdup(filename); + blf_font_fill(font); + return font; } void blf_font_attach_from_mem(FontBLF *font, const unsigned char *mem, int mem_size) { - FT_Open_Args open; + FT_Open_Args open; - open.flags = FT_OPEN_MEMORY; - open.memory_base = (const FT_Byte *)mem; - open.memory_size = mem_size; - FT_Attach_Stream(font->face, &open); + open.flags = FT_OPEN_MEMORY; + open.memory_base = (const FT_Byte *)mem; + open.memory_size = mem_size; + FT_Attach_Stream(font->face, &open); } FontBLF *blf_font_new_from_mem(const char *name, const unsigned char *mem, int mem_size) { - FontBLF *font; - FT_Error err; - - font = (FontBLF *)MEM_callocN(sizeof(FontBLF), "blf_font_new_from_mem"); - err = FT_New_Memory_Face(ft_lib, mem, mem_size, 0, &font->face); - if (err) { - MEM_freeN(font); - return NULL; - } - - err = FT_Select_Charmap(font->face, ft_encoding_unicode); - if (err) { - printf("Can't set the unicode character map!\n"); - FT_Done_Face(font->face); - MEM_freeN(font); - return NULL; - } - - font->name = BLI_strdup(name); - font->filename = NULL; - blf_font_fill(font); - return font; + FontBLF *font; + FT_Error err; + + font = (FontBLF *)MEM_callocN(sizeof(FontBLF), "blf_font_new_from_mem"); + err = FT_New_Memory_Face(ft_lib, mem, mem_size, 0, &font->face); + if (err) { + MEM_freeN(font); + return NULL; + } + + err = FT_Select_Charmap(font->face, ft_encoding_unicode); + if (err) { + printf("Can't set the unicode character map!\n"); + FT_Done_Face(font->face); + MEM_freeN(font); + return NULL; + } + + font->name = BLI_strdup(name); + font->filename = NULL; + blf_font_fill(font); + return font; } diff --git a/source/blender/blenfont/intern/blf_font_i18n.c b/source/blender/blenfont/intern/blf_font_i18n.c index 2a69d714dc6..74113ae4ce1 100644 --- a/source/blender/blenfont/intern/blf_font_i18n.c +++ b/source/blender/blenfont/intern/blf_font_i18n.c @@ -36,80 +36,78 @@ #ifdef WITH_INTERNATIONAL -#include "BLI_fileops.h" -#include "BLI_string.h" +# include "BLI_fileops.h" +# include "BLI_string.h" struct FontBuf { - const char *filename; - uchar *data; - int data_len; + const char *filename; + uchar *data; + int data_len; }; -static struct FontBuf unifont_ttf = {"droidsans.ttf.gz"}; +static struct FontBuf unifont_ttf = {"droidsans.ttf.gz"}; static struct FontBuf unifont_mono_ttf = {"bmonofont-i18n.ttf.gz"}; static void fontbuf_load(struct FontBuf *fb) { - const char *fontpath = BKE_appdir_folder_id(BLENDER_DATAFILES, "fonts"); - if (fontpath) { - char unifont_path[1024]; - BLI_snprintf(unifont_path, sizeof(unifont_path), "%s/%s", fontpath, fb->filename); - fb->data = (uchar *)BLI_file_ungzip_to_mem(unifont_path, &fb->data_len); - - } - else { - printf("%s: 'fonts' data path not found for '%s', continuing\n", __func__, fb->filename); - } + const char *fontpath = BKE_appdir_folder_id(BLENDER_DATAFILES, "fonts"); + if (fontpath) { + char unifont_path[1024]; + BLI_snprintf(unifont_path, sizeof(unifont_path), "%s/%s", fontpath, fb->filename); + fb->data = (uchar *)BLI_file_ungzip_to_mem(unifont_path, &fb->data_len); + } + else { + printf("%s: 'fonts' data path not found for '%s', continuing\n", __func__, fb->filename); + } } static void fontbuf_free(struct FontBuf *fb) { - MEM_SAFE_FREE(fb->data); - fb->data_len = 0; + MEM_SAFE_FREE(fb->data); + fb->data_len = 0; } static uchar *fontbuf_get_mem(struct FontBuf *fb, int *r_size) { - if (fb->data == NULL) { - fontbuf_load(fb); - } - *r_size = fb->data_len; - return fb->data; + if (fb->data == NULL) { + fontbuf_load(fb); + } + *r_size = fb->data_len; + return fb->data; } #endif /* WITH_INTERNATIONAL */ - uchar *BLF_get_unifont(int *r_unifont_size) { #ifdef WITH_INTERNATIONAL - return fontbuf_get_mem(&unifont_ttf, r_unifont_size); + return fontbuf_get_mem(&unifont_ttf, r_unifont_size); #else - UNUSED_VARS(r_unifont_size); - return NULL; + UNUSED_VARS(r_unifont_size); + return NULL; #endif } uchar *BLF_get_unifont_mono(int *r_unifont_size) { #ifdef WITH_INTERNATIONAL - return fontbuf_get_mem(&unifont_mono_ttf, r_unifont_size); + return fontbuf_get_mem(&unifont_mono_ttf, r_unifont_size); #else - UNUSED_VARS(r_unifont_size); - return NULL; + UNUSED_VARS(r_unifont_size); + return NULL; #endif } void BLF_free_unifont(void) { #ifdef WITH_INTERNATIONAL - fontbuf_free(&unifont_ttf); + fontbuf_free(&unifont_ttf); #endif } void BLF_free_unifont_mono(void) { #ifdef WITH_INTERNATIONAL - fontbuf_free(&unifont_mono_ttf); + fontbuf_free(&unifont_mono_ttf); #endif } diff --git a/source/blender/blenfont/intern/blf_font_win32_compat.c b/source/blender/blenfont/intern/blf_font_win32_compat.c index 70a5e3a9ce3..c609d67ece1 100644 --- a/source/blender/blenfont/intern/blf_font_win32_compat.c +++ b/source/blender/blenfont/intern/blf_font_win32_compat.c @@ -24,118 +24,117 @@ #ifdef WIN32 -#include +# include -#include -#include FT_FREETYPE_H +# include +# include FT_FREETYPE_H -#include "MEM_guardedalloc.h" +# include "MEM_guardedalloc.h" -#include "BLI_utildefines.h" -#include "BLI_fileops.h" +# include "BLI_utildefines.h" +# include "BLI_fileops.h" -#include "blf_internal.h" +# include "blf_internal.h" /* internal freetype defines */ -#define STREAM_FILE(stream) ((FILE *)stream->descriptor.pointer) -#define FT_THROW(e) -1 +# define STREAM_FILE(stream) ((FILE *)stream->descriptor.pointer) +# define FT_THROW(e) -1 -static void ft_ansi_stream_close( - FT_Stream stream) +static void ft_ansi_stream_close(FT_Stream stream) { - fclose(STREAM_FILE(stream)); + fclose(STREAM_FILE(stream)); - stream->descriptor.pointer = NULL; - stream->size = 0; - stream->base = 0; + stream->descriptor.pointer = NULL; + stream->size = 0; + stream->base = 0; - /* WARNING: this works but be careful! - * Checked freetype sources, there isn't any access after closing. */ - MEM_freeN(stream); + /* WARNING: this works but be careful! + * Checked freetype sources, there isn't any access after closing. */ + MEM_freeN(stream); } -static unsigned long ft_ansi_stream_io( - FT_Stream stream, - unsigned long offset, - unsigned char *buffer, - unsigned long count) +static unsigned long ft_ansi_stream_io(FT_Stream stream, + unsigned long offset, + unsigned char *buffer, + unsigned long count) { - FILE *file; - if (!count && offset > stream->size) - return 1; + FILE *file; + if (!count && offset > stream->size) + return 1; - file = STREAM_FILE(stream); + file = STREAM_FILE(stream); - if (stream->pos != offset) - fseek(file, offset, SEEK_SET); + if (stream->pos != offset) + fseek(file, offset, SEEK_SET); - return fread(buffer, 1, count, file); + return fread(buffer, 1, count, file); } static FT_Error FT_Stream_Open__win32_compat(FT_Stream stream, const char *filepathname) { - FILE *file; - BLI_assert(stream); - - stream->descriptor.pointer = NULL; - stream->pathname.pointer = (char *)filepathname; - stream->base = 0; - stream->pos = 0; - stream->read = NULL; - stream->close = NULL; - - file = BLI_fopen(filepathname, "rb"); - if (!file) { - fprintf(stderr, - "FT_Stream_Open: " - "could not open `%s'\n", filepathname); - return FT_THROW(Cannot_Open_Resource); - } - - fseek(file, 0, SEEK_END); - stream->size = ftell(file); - if (!stream->size) { - fprintf(stderr, - "FT_Stream_Open: " - "opened `%s' but zero-sized\n", filepathname); - fclose(file); - return FT_THROW(Cannot_Open_Stream); - } - - fseek(file, 0, SEEK_SET); - - stream->descriptor.pointer = file; - stream->read = ft_ansi_stream_io; - stream->close = ft_ansi_stream_close; - - return FT_Err_Ok; + FILE *file; + BLI_assert(stream); + + stream->descriptor.pointer = NULL; + stream->pathname.pointer = (char *)filepathname; + stream->base = 0; + stream->pos = 0; + stream->read = NULL; + stream->close = NULL; + + file = BLI_fopen(filepathname, "rb"); + if (!file) { + fprintf(stderr, + "FT_Stream_Open: " + "could not open `%s'\n", + filepathname); + return FT_THROW(Cannot_Open_Resource); + } + + fseek(file, 0, SEEK_END); + stream->size = ftell(file); + if (!stream->size) { + fprintf(stderr, + "FT_Stream_Open: " + "opened `%s' but zero-sized\n", + filepathname); + fclose(file); + return FT_THROW(Cannot_Open_Stream); + } + + fseek(file, 0, SEEK_SET); + + stream->descriptor.pointer = file; + stream->read = ft_ansi_stream_io; + stream->close = ft_ansi_stream_close; + + return FT_Err_Ok; } -FT_Error FT_New_Face__win32_compat( - FT_Library library, - const char *pathname, - FT_Long face_index, - FT_Face *aface) +FT_Error FT_New_Face__win32_compat(FT_Library library, + const char *pathname, + FT_Long face_index, + FT_Face *aface) { - FT_Error err; - FT_Open_Args open; - FT_Stream stream = NULL; - stream = MEM_callocN(sizeof(*stream), __func__); + FT_Error err; + FT_Open_Args open; + FT_Stream stream = NULL; + stream = MEM_callocN(sizeof(*stream), __func__); - open.flags = FT_OPEN_STREAM; - open.stream = stream; - stream->pathname.pointer = (char *)pathname; + open.flags = FT_OPEN_STREAM; + open.stream = stream; + stream->pathname.pointer = (char *)pathname; - err = FT_Stream_Open__win32_compat(stream, pathname); - if (err) { - MEM_freeN(stream); - return err; - } + err = FT_Stream_Open__win32_compat(stream, pathname); + if (err) { + MEM_freeN(stream); + return err; + } - err = FT_Open_Face(library, &open, face_index, aface); - /* no need to free 'stream', its handled by FT_Open_Face if an error occurs */ + err = FT_Open_Face(library, &open, face_index, aface); + /* no need to free 'stream', its handled by FT_Open_Face if an error occurs */ - return err; + return err; } -#endif /* WIN32 */ +#endif /* WIN32 */ diff --git a/source/blender/blenfont/intern/blf_glyph.c b/source/blender/blenfont/intern/blf_glyph.c index 20cd6a51a47..8f9db604434 100644 --- a/source/blender/blenfont/intern/blf_glyph.c +++ b/source/blender/blenfont/intern/blf_glyph.c @@ -23,7 +23,6 @@ * Glyph rendering, texturing and caching. Wraps Freetype and OpenGL functions. */ - #include #include #include @@ -60,510 +59,567 @@ KerningCacheBLF *blf_kerning_cache_find(FontBLF *font) { - KerningCacheBLF *p; - - p = (KerningCacheBLF *)font->kerning_caches.first; - while (p) { - if (p->mode == font->kerning_mode) - return p; - p = p->next; - } - return NULL; + KerningCacheBLF *p; + + p = (KerningCacheBLF *)font->kerning_caches.first; + while (p) { + if (p->mode == font->kerning_mode) + return p; + p = p->next; + } + return NULL; } /* Create a new glyph cache for the current kerning mode. */ KerningCacheBLF *blf_kerning_cache_new(FontBLF *font) { - KerningCacheBLF *kc; - - kc = (KerningCacheBLF *)MEM_callocN(sizeof(KerningCacheBLF), "blf_kerning_cache_new"); - kc->next = NULL; - kc->prev = NULL; - kc->mode = font->kerning_mode; - - unsigned int i, j; - for (i = 0; i < 0x80; i++) { - for (j = 0; j < 0x80; j++) { - GlyphBLF *g = blf_glyph_search(font->glyph_cache, i); - if (!g) { - FT_UInt glyph_index = FT_Get_Char_Index(font->face, i); - g = blf_glyph_add(font, glyph_index, i); - } - /* Can fail on certain fonts */ - GlyphBLF *g_prev = blf_glyph_search(font->glyph_cache, j); - - FT_Vector delta = { .x = 0, .y = 0, }; - if (g_prev && FT_Get_Kerning(font->face, g_prev->idx, g->idx, kc->mode, &delta) == 0) { - kc->table[i][j] = (int)delta.x >> 6; - } - else { - kc->table[i][j] = 0; - } - } - } - - BLI_addhead(&font->kerning_caches, kc); - return kc; + KerningCacheBLF *kc; + + kc = (KerningCacheBLF *)MEM_callocN(sizeof(KerningCacheBLF), "blf_kerning_cache_new"); + kc->next = NULL; + kc->prev = NULL; + kc->mode = font->kerning_mode; + + unsigned int i, j; + for (i = 0; i < 0x80; i++) { + for (j = 0; j < 0x80; j++) { + GlyphBLF *g = blf_glyph_search(font->glyph_cache, i); + if (!g) { + FT_UInt glyph_index = FT_Get_Char_Index(font->face, i); + g = blf_glyph_add(font, glyph_index, i); + } + /* Can fail on certain fonts */ + GlyphBLF *g_prev = blf_glyph_search(font->glyph_cache, j); + + FT_Vector delta = { + .x = 0, + .y = 0, + }; + if (g_prev && FT_Get_Kerning(font->face, g_prev->idx, g->idx, kc->mode, &delta) == 0) { + kc->table[i][j] = (int)delta.x >> 6; + } + else { + kc->table[i][j] = 0; + } + } + } + + BLI_addhead(&font->kerning_caches, kc); + return kc; } void blf_kerning_cache_clear(FontBLF *font) { - font->kerning_cache = NULL; - BLI_freelistN(&font->kerning_caches); + font->kerning_cache = NULL; + BLI_freelistN(&font->kerning_caches); } GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, unsigned int size, unsigned int dpi) { - GlyphCacheBLF *p; - - p = (GlyphCacheBLF *)font->cache.first; - while (p) { - if (p->size == size && p->dpi == dpi) - return p; - p = p->next; - } - return NULL; + GlyphCacheBLF *p; + + p = (GlyphCacheBLF *)font->cache.first; + while (p) { + if (p->size == size && p->dpi == dpi) + return p; + p = p->next; + } + return NULL; } /* Create a new glyph cache for the current size and dpi. */ GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font) { - GlyphCacheBLF *gc; - - gc = (GlyphCacheBLF *)MEM_callocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new"); - gc->next = NULL; - gc->prev = NULL; - gc->size = font->size; - gc->dpi = font->dpi; - - memset(gc->glyph_ascii_table, 0, sizeof(gc->glyph_ascii_table)); - memset(gc->bucket, 0, sizeof(gc->bucket)); - - gc->textures = (GPUTexture **)MEM_callocN(sizeof(GPUTexture *) * 256, __func__); - gc->textures_len = 256; - gc->texture_current = BLF_TEXTURE_UNSET; - gc->offset_x = 3; /* enough padding for blur */ - gc->offset_y = 3; /* enough padding for blur */ - gc->pad = 6; - - gc->glyphs_len_max = (int)font->face->num_glyphs; - gc->glyphs_len_free = (int)font->face->num_glyphs; - gc->ascender = ((float)font->face->size->metrics.ascender) / 64.0f; - gc->descender = ((float)font->face->size->metrics.descender) / 64.0f; - - if (FT_IS_SCALABLE(font->face)) { - gc->glyph_width_max = (int)((float)(font->face->bbox.xMax - font->face->bbox.xMin) * - (((float)font->face->size->metrics.x_ppem) / - ((float)font->face->units_per_EM))); - - gc->glyph_height_max = (int)((float)(font->face->bbox.yMax - font->face->bbox.yMin) * - (((float)font->face->size->metrics.y_ppem) / - ((float)font->face->units_per_EM))); - } - else { - gc->glyph_width_max = (int)(((float)font->face->size->metrics.max_advance) / 64.0f); - gc->glyph_height_max = (int)(((float)font->face->size->metrics.height) / 64.0f); - } - - /* can happen with size 1 fonts */ - CLAMP_MIN(gc->glyph_width_max, 1); - CLAMP_MIN(gc->glyph_height_max, 1); - - gc->p2_width = 0; - gc->p2_height = 0; - - BLI_addhead(&font->cache, gc); - return gc; + GlyphCacheBLF *gc; + + gc = (GlyphCacheBLF *)MEM_callocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new"); + gc->next = NULL; + gc->prev = NULL; + gc->size = font->size; + gc->dpi = font->dpi; + + memset(gc->glyph_ascii_table, 0, sizeof(gc->glyph_ascii_table)); + memset(gc->bucket, 0, sizeof(gc->bucket)); + + gc->textures = (GPUTexture **)MEM_callocN(sizeof(GPUTexture *) * 256, __func__); + gc->textures_len = 256; + gc->texture_current = BLF_TEXTURE_UNSET; + gc->offset_x = 3; /* enough padding for blur */ + gc->offset_y = 3; /* enough padding for blur */ + gc->pad = 6; + + gc->glyphs_len_max = (int)font->face->num_glyphs; + gc->glyphs_len_free = (int)font->face->num_glyphs; + gc->ascender = ((float)font->face->size->metrics.ascender) / 64.0f; + gc->descender = ((float)font->face->size->metrics.descender) / 64.0f; + + if (FT_IS_SCALABLE(font->face)) { + gc->glyph_width_max = (int)((float)(font->face->bbox.xMax - font->face->bbox.xMin) * + (((float)font->face->size->metrics.x_ppem) / + ((float)font->face->units_per_EM))); + + gc->glyph_height_max = (int)((float)(font->face->bbox.yMax - font->face->bbox.yMin) * + (((float)font->face->size->metrics.y_ppem) / + ((float)font->face->units_per_EM))); + } + else { + gc->glyph_width_max = (int)(((float)font->face->size->metrics.max_advance) / 64.0f); + gc->glyph_height_max = (int)(((float)font->face->size->metrics.height) / 64.0f); + } + + /* can happen with size 1 fonts */ + CLAMP_MIN(gc->glyph_width_max, 1); + CLAMP_MIN(gc->glyph_height_max, 1); + + gc->p2_width = 0; + gc->p2_height = 0; + + BLI_addhead(&font->cache, gc); + return gc; } void blf_glyph_cache_clear(FontBLF *font) { - GlyphCacheBLF *gc; + GlyphCacheBLF *gc; - while ((gc = BLI_pophead(&font->cache))) { - blf_glyph_cache_free(gc); - } - font->glyph_cache = NULL; + while ((gc = BLI_pophead(&font->cache))) { + blf_glyph_cache_free(gc); + } + font->glyph_cache = NULL; } void blf_glyph_cache_free(GlyphCacheBLF *gc) { - GlyphBLF *g; - unsigned int i; - - for (i = 0; i < 257; i++) { - while ((g = BLI_pophead(&gc->bucket[i]))) { - blf_glyph_free(g); - } - } - for (i = 0; i < gc->textures_len; i++) { - if (gc->textures[i]) { - GPU_texture_free(gc->textures[i]); - } - } - MEM_freeN(gc->textures); - MEM_freeN(gc); + GlyphBLF *g; + unsigned int i; + + for (i = 0; i < 257; i++) { + while ((g = BLI_pophead(&gc->bucket[i]))) { + blf_glyph_free(g); + } + } + for (i = 0; i < gc->textures_len; i++) { + if (gc->textures[i]) { + GPU_texture_free(gc->textures[i]); + } + } + MEM_freeN(gc->textures); + MEM_freeN(gc); } static void blf_glyph_cache_texture(FontBLF *font, GlyphCacheBLF *gc) { - int i; - char error[256]; - - /* move the index. */ - gc->texture_current++; - - if (UNLIKELY(gc->texture_current >= gc->textures_len)) { - gc->textures_len *= 2; - gc->textures = MEM_recallocN((void *)gc->textures, sizeof(GPUTexture *) * gc->textures_len); - } - - gc->p2_width = (int)blf_next_p2((unsigned int)((gc->glyphs_len_free * gc->glyph_width_max) + (gc->pad * 2))); - if (gc->p2_width > font->tex_size_max) { - gc->p2_width = font->tex_size_max; - } - - i = (int)((gc->p2_width - (gc->pad * 2)) / gc->glyph_width_max); - gc->p2_height = (int)blf_next_p2((unsigned int)(((gc->glyphs_len_max / i) + 1) * gc->glyph_height_max + (gc->pad * 2))); - - if (gc->p2_height > font->tex_size_max) { - gc->p2_height = font->tex_size_max; - } - - unsigned char *pixels = MEM_callocN((size_t)gc->p2_width * (size_t)gc->p2_height, "BLF texture init"); - GPUTexture *tex = GPU_texture_create_nD(gc->p2_width, gc->p2_height, 0, 2, pixels, GPU_R8, GPU_DATA_UNSIGNED_BYTE, 0, false, error); - MEM_freeN(pixels); - gc->textures[gc->texture_current] = tex; - GPU_texture_bind(tex, 0); - GPU_texture_wrap_mode(tex, false); - GPU_texture_filters(tex, GPU_NEAREST, GPU_LINEAR); - GPU_texture_unbind(tex); + int i; + char error[256]; + + /* move the index. */ + gc->texture_current++; + + if (UNLIKELY(gc->texture_current >= gc->textures_len)) { + gc->textures_len *= 2; + gc->textures = MEM_recallocN((void *)gc->textures, sizeof(GPUTexture *) * gc->textures_len); + } + + gc->p2_width = (int)blf_next_p2( + (unsigned int)((gc->glyphs_len_free * gc->glyph_width_max) + (gc->pad * 2))); + if (gc->p2_width > font->tex_size_max) { + gc->p2_width = font->tex_size_max; + } + + i = (int)((gc->p2_width - (gc->pad * 2)) / gc->glyph_width_max); + gc->p2_height = (int)blf_next_p2( + (unsigned int)(((gc->glyphs_len_max / i) + 1) * gc->glyph_height_max + (gc->pad * 2))); + + if (gc->p2_height > font->tex_size_max) { + gc->p2_height = font->tex_size_max; + } + + unsigned char *pixels = MEM_callocN((size_t)gc->p2_width * (size_t)gc->p2_height, + "BLF texture init"); + GPUTexture *tex = GPU_texture_create_nD( + gc->p2_width, gc->p2_height, 0, 2, pixels, GPU_R8, GPU_DATA_UNSIGNED_BYTE, 0, false, error); + MEM_freeN(pixels); + gc->textures[gc->texture_current] = tex; + GPU_texture_bind(tex, 0); + GPU_texture_wrap_mode(tex, false); + GPU_texture_filters(tex, GPU_NEAREST, GPU_LINEAR); + GPU_texture_unbind(tex); } GlyphBLF *blf_glyph_search(GlyphCacheBLF *gc, unsigned int c) { - GlyphBLF *p; - unsigned int key; - - key = blf_hash(c); - p = gc->bucket[key].first; - while (p) { - if (p->c == c) - return p; - p = p->next; - } - return NULL; + GlyphBLF *p; + unsigned int key; + + key = blf_hash(c); + p = gc->bucket[key].first; + while (p) { + if (p->c == c) + return p; + p = p->next; + } + return NULL; } GlyphBLF *blf_glyph_add(FontBLF *font, unsigned int index, unsigned int c) { - FT_GlyphSlot slot; - GlyphBLF *g; - FT_Error err; - FT_Bitmap bitmap, tempbitmap; - FT_BBox bbox; - unsigned int key; - - g = blf_glyph_search(font->glyph_cache, c); - if (g) - return g; - - /* glyphs are dynamically created as needed by font rendering. this means that - * to make font rendering thread safe we have to do locking here. note that this - * must be a lock for the whole library and not just per font, because the font - * renderer uses a shared buffer internally */ - BLI_spin_lock(font->ft_lib_mutex); - - /* search again after locking */ - g = blf_glyph_search(font->glyph_cache, c); - if (g) { - BLI_spin_unlock(font->ft_lib_mutex); - return g; - } - - if (font->flags & BLF_MONOCHROME) { - err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_MONO); - } - else { - int flags = FT_LOAD_NO_BITMAP; - - if (font->flags & BLF_HINTING_NONE) { - flags |= FT_LOAD_TARGET_NORMAL | FT_LOAD_NO_HINTING; - } - else if (font->flags & BLF_HINTING_SLIGHT) { - flags |= FT_LOAD_TARGET_LIGHT; - } - else if (font->flags & BLF_HINTING_FULL) { - flags |= FT_LOAD_TARGET_NORMAL; - } - else { - /* Default, hinting disabled until FreeType has been upgraded - * to give good results on all platforms. */ - flags |= FT_LOAD_TARGET_NORMAL | FT_LOAD_NO_HINTING; - } - - err = FT_Load_Glyph(font->face, (FT_UInt)index, flags); - } - - if (err) { - BLI_spin_unlock(font->ft_lib_mutex); - return NULL; - } - - /* get the glyph. */ - slot = font->face->glyph; - - if (font->flags & BLF_MONOCHROME) { - err = FT_Render_Glyph(slot, FT_RENDER_MODE_MONO); - - /* Convert result from 1 bit per pixel to 8 bit per pixel */ - /* Accum errors for later, fine if not interested beyond "ok vs any error" */ - FT_Bitmap_New(&tempbitmap); - err += FT_Bitmap_Convert(font->ft_lib, &slot->bitmap, &tempbitmap, 1); /* Does Blender use Pitch 1 always? It works so far */ - err += FT_Bitmap_Copy(font->ft_lib, &tempbitmap, &slot->bitmap); - err += FT_Bitmap_Done(font->ft_lib, &tempbitmap); - } - else { - err = FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL); - } - - if (err || slot->format != FT_GLYPH_FORMAT_BITMAP) { - BLI_spin_unlock(font->ft_lib_mutex); - return NULL; - } - - g = (GlyphBLF *)MEM_callocN(sizeof(GlyphBLF), "blf_glyph_add"); - g->c = c; - g->idx = (FT_UInt)index; - g->offset_x = -1; - g->offset_y = -1; - bitmap = slot->bitmap; - g->width = (int)bitmap.width; - g->height = (int)bitmap.rows; - - if (g->width && g->height) { - if (font->flags & BLF_MONOCHROME) { - /* Font buffer uses only 0 or 1 values, Blender expects full 0..255 range */ - int i; - for (i = 0; i < (g->width * g->height); i++) { - bitmap.buffer[i] = bitmap.buffer[i] ? 255 : 0; - } - } - - g->bitmap = (unsigned char *)MEM_mallocN((size_t)g->width * (size_t)g->height, "glyph bitmap"); - memcpy((void *)g->bitmap, (void *)bitmap.buffer, (size_t)g->width * (size_t)g->height); - } - - g->advance = ((float)slot->advance.x) / 64.0f; - g->advance_i = (int)g->advance; - g->pos_x = (float)slot->bitmap_left; - g->pos_y = (float)slot->bitmap_top; - g->pitch = slot->bitmap.pitch; - - FT_Outline_Get_CBox(&(slot->outline), &bbox); - g->box.xmin = ((float)bbox.xMin) / 64.0f; - g->box.xmax = ((float)bbox.xMax) / 64.0f; - g->box.ymin = ((float)bbox.yMin) / 64.0f; - g->box.ymax = ((float)bbox.yMax) / 64.0f; - - key = blf_hash(g->c); - BLI_addhead(&(font->glyph_cache->bucket[key]), g); - - BLI_spin_unlock(font->ft_lib_mutex); - - return g; + FT_GlyphSlot slot; + GlyphBLF *g; + FT_Error err; + FT_Bitmap bitmap, tempbitmap; + FT_BBox bbox; + unsigned int key; + + g = blf_glyph_search(font->glyph_cache, c); + if (g) + return g; + + /* glyphs are dynamically created as needed by font rendering. this means that + * to make font rendering thread safe we have to do locking here. note that this + * must be a lock for the whole library and not just per font, because the font + * renderer uses a shared buffer internally */ + BLI_spin_lock(font->ft_lib_mutex); + + /* search again after locking */ + g = blf_glyph_search(font->glyph_cache, c); + if (g) { + BLI_spin_unlock(font->ft_lib_mutex); + return g; + } + + if (font->flags & BLF_MONOCHROME) { + err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_MONO); + } + else { + int flags = FT_LOAD_NO_BITMAP; + + if (font->flags & BLF_HINTING_NONE) { + flags |= FT_LOAD_TARGET_NORMAL | FT_LOAD_NO_HINTING; + } + else if (font->flags & BLF_HINTING_SLIGHT) { + flags |= FT_LOAD_TARGET_LIGHT; + } + else if (font->flags & BLF_HINTING_FULL) { + flags |= FT_LOAD_TARGET_NORMAL; + } + else { + /* Default, hinting disabled until FreeType has been upgraded + * to give good results on all platforms. */ + flags |= FT_LOAD_TARGET_NORMAL | FT_LOAD_NO_HINTING; + } + + err = FT_Load_Glyph(font->face, (FT_UInt)index, flags); + } + + if (err) { + BLI_spin_unlock(font->ft_lib_mutex); + return NULL; + } + + /* get the glyph. */ + slot = font->face->glyph; + + if (font->flags & BLF_MONOCHROME) { + err = FT_Render_Glyph(slot, FT_RENDER_MODE_MONO); + + /* Convert result from 1 bit per pixel to 8 bit per pixel */ + /* Accum errors for later, fine if not interested beyond "ok vs any error" */ + FT_Bitmap_New(&tempbitmap); + err += FT_Bitmap_Convert(font->ft_lib, + &slot->bitmap, + &tempbitmap, + 1); /* Does Blender use Pitch 1 always? It works so far */ + err += FT_Bitmap_Copy(font->ft_lib, &tempbitmap, &slot->bitmap); + err += FT_Bitmap_Done(font->ft_lib, &tempbitmap); + } + else { + err = FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL); + } + + if (err || slot->format != FT_GLYPH_FORMAT_BITMAP) { + BLI_spin_unlock(font->ft_lib_mutex); + return NULL; + } + + g = (GlyphBLF *)MEM_callocN(sizeof(GlyphBLF), "blf_glyph_add"); + g->c = c; + g->idx = (FT_UInt)index; + g->offset_x = -1; + g->offset_y = -1; + bitmap = slot->bitmap; + g->width = (int)bitmap.width; + g->height = (int)bitmap.rows; + + if (g->width && g->height) { + if (font->flags & BLF_MONOCHROME) { + /* Font buffer uses only 0 or 1 values, Blender expects full 0..255 range */ + int i; + for (i = 0; i < (g->width * g->height); i++) { + bitmap.buffer[i] = bitmap.buffer[i] ? 255 : 0; + } + } + + g->bitmap = (unsigned char *)MEM_mallocN((size_t)g->width * (size_t)g->height, "glyph bitmap"); + memcpy((void *)g->bitmap, (void *)bitmap.buffer, (size_t)g->width * (size_t)g->height); + } + + g->advance = ((float)slot->advance.x) / 64.0f; + g->advance_i = (int)g->advance; + g->pos_x = (float)slot->bitmap_left; + g->pos_y = (float)slot->bitmap_top; + g->pitch = slot->bitmap.pitch; + + FT_Outline_Get_CBox(&(slot->outline), &bbox); + g->box.xmin = ((float)bbox.xMin) / 64.0f; + g->box.xmax = ((float)bbox.xMax) / 64.0f; + g->box.ymin = ((float)bbox.yMin) / 64.0f; + g->box.ymax = ((float)bbox.yMax) / 64.0f; + + key = blf_hash(g->c); + BLI_addhead(&(font->glyph_cache->bucket[key]), g); + + BLI_spin_unlock(font->ft_lib_mutex); + + return g; } void blf_glyph_free(GlyphBLF *g) { - /* don't need free the texture, the GlyphCache already - * have a list of all the texture and free it. - */ - if (g->bitmap) - MEM_freeN(g->bitmap); - MEM_freeN(g); + /* don't need free the texture, the GlyphCache already + * have a list of all the texture and free it. + */ + if (g->bitmap) + MEM_freeN(g->bitmap); + MEM_freeN(g); } -static void blf_texture_draw(const unsigned char color[4], const float uv[2][2], float x1, float y1, float x2, float y2) +static void blf_texture_draw( + const unsigned char color[4], const float uv[2][2], float x1, float y1, float x2, float y2) { - /* Only one vertex per glyph, geometry shader expand it into a quad. */ - /* TODO Get rid of Geom Shader because it's not optimal AT ALL for the GPU */ - copy_v4_fl4(GPU_vertbuf_raw_step(&g_batch.pos_step), x1 + g_batch.ofs[0], y1 + g_batch.ofs[1], - x2 + g_batch.ofs[0], y2 + g_batch.ofs[1]); - copy_v4_v4(GPU_vertbuf_raw_step(&g_batch.tex_step), (float *)uv); - copy_v4_v4_uchar(GPU_vertbuf_raw_step(&g_batch.col_step), color); - g_batch.glyph_len++; - /* Flush cache if it's full. */ - if (g_batch.glyph_len == BLF_BATCH_DRAW_LEN_MAX) { - blf_batch_draw(); - } + /* Only one vertex per glyph, geometry shader expand it into a quad. */ + /* TODO Get rid of Geom Shader because it's not optimal AT ALL for the GPU */ + copy_v4_fl4(GPU_vertbuf_raw_step(&g_batch.pos_step), + x1 + g_batch.ofs[0], + y1 + g_batch.ofs[1], + x2 + g_batch.ofs[0], + y2 + g_batch.ofs[1]); + copy_v4_v4(GPU_vertbuf_raw_step(&g_batch.tex_step), (float *)uv); + copy_v4_v4_uchar(GPU_vertbuf_raw_step(&g_batch.col_step), color); + g_batch.glyph_len++; + /* Flush cache if it's full. */ + if (g_batch.glyph_len == BLF_BATCH_DRAW_LEN_MAX) { + blf_batch_draw(); + } } -static void blf_texture5_draw(const unsigned char color_in[4], int tex_w, int tex_h, const float uv[2][2], - float x1, float y1, float x2, float y2) +static void blf_texture5_draw(const unsigned char color_in[4], + int tex_w, + int tex_h, + const float uv[2][2], + float x1, + float y1, + float x2, + float y2) { - float ofs[2] = { 2 / (float)tex_w, 2 / (float)tex_h }; - float uv_flag[2][2]; - copy_v4_v4((float *)uv_flag, (float *)uv); - /* flag the x and y component signs for 5x5 blurring */ - uv_flag[0][0] = -(uv_flag[0][0] - ofs[0]); - uv_flag[0][1] = -(uv_flag[0][1] - ofs[1]); - uv_flag[1][0] = -(uv_flag[1][0] + ofs[0]); - uv_flag[1][1] = -(uv_flag[1][1] + ofs[1]); - - blf_texture_draw(color_in, uv_flag, x1 - 2, y1 + 2, x2 + 2, y2 - 2); + float ofs[2] = {2 / (float)tex_w, 2 / (float)tex_h}; + float uv_flag[2][2]; + copy_v4_v4((float *)uv_flag, (float *)uv); + /* flag the x and y component signs for 5x5 blurring */ + uv_flag[0][0] = -(uv_flag[0][0] - ofs[0]); + uv_flag[0][1] = -(uv_flag[0][1] - ofs[1]); + uv_flag[1][0] = -(uv_flag[1][0] + ofs[0]); + uv_flag[1][1] = -(uv_flag[1][1] + ofs[1]); + + blf_texture_draw(color_in, uv_flag, x1 - 2, y1 + 2, x2 + 2, y2 - 2); } -static void blf_texture3_draw(const unsigned char color_in[4], int tex_w, int tex_h, const float uv[2][2], - float x1, float y1, float x2, float y2) +static void blf_texture3_draw(const unsigned char color_in[4], + int tex_w, + int tex_h, + const float uv[2][2], + float x1, + float y1, + float x2, + float y2) { - float ofs[2] = { 1 / (float)tex_w, 1 / (float)tex_h }; - float uv_flag[2][2]; - copy_v4_v4((float *)uv_flag, (float *)uv); - /* flag the x component sign for 3x3 blurring */ - uv_flag[0][0] = -(uv_flag[0][0] - ofs[0]); - uv_flag[0][1] = (uv_flag[0][1] - ofs[1]); - uv_flag[1][0] = -(uv_flag[1][0] + ofs[0]); - uv_flag[1][1] = (uv_flag[1][1] + ofs[1]); - - blf_texture_draw(color_in, uv_flag, x1 - 1, y1 + 1, x2 + 1, y2 - 1); + float ofs[2] = {1 / (float)tex_w, 1 / (float)tex_h}; + float uv_flag[2][2]; + copy_v4_v4((float *)uv_flag, (float *)uv); + /* flag the x component sign for 3x3 blurring */ + uv_flag[0][0] = -(uv_flag[0][0] - ofs[0]); + uv_flag[0][1] = (uv_flag[0][1] - ofs[1]); + uv_flag[1][0] = -(uv_flag[1][0] + ofs[0]); + uv_flag[1][1] = (uv_flag[1][1] + ofs[1]); + + blf_texture_draw(color_in, uv_flag, x1 - 1, y1 + 1, x2 + 1, y2 - 1); } static void blf_glyph_calc_rect(rctf *rect, GlyphBLF *g, float x, float y) { - rect->xmin = floorf(x + g->pos_x); - rect->xmax = rect->xmin + (float)g->width; - rect->ymin = floorf(y + g->pos_y); - rect->ymax = rect->ymin - (float)g->height; + rect->xmin = floorf(x + g->pos_x); + rect->xmax = rect->xmin + (float)g->width; + rect->ymin = floorf(y + g->pos_y); + rect->ymax = rect->ymin - (float)g->height; } static void blf_glyph_calc_rect_test(rctf *rect, GlyphBLF *g, float x, float y) { - /* Intentionally check with g->advance, because this is the - * width used by BLF_width. This allows that the text slightly - * overlaps the clipping border to achieve better alignment. */ - rect->xmin = floorf(x); - rect->xmax = rect->xmin + MIN2(g->advance, (float)g->width); - rect->ymin = floorf(y); - rect->ymax = rect->ymin - (float)g->height; + /* Intentionally check with g->advance, because this is the + * width used by BLF_width. This allows that the text slightly + * overlaps the clipping border to achieve better alignment. */ + rect->xmin = floorf(x); + rect->xmax = rect->xmin + MIN2(g->advance, (float)g->width); + rect->ymin = floorf(y); + rect->ymax = rect->ymin - (float)g->height; } static void blf_glyph_calc_rect_shadow(rctf *rect, GlyphBLF *g, float x, float y, FontBLF *font) { - blf_glyph_calc_rect(rect, g, - x + (float)font->shadow_x, - y + (float)font->shadow_y); + blf_glyph_calc_rect(rect, g, x + (float)font->shadow_x, y + (float)font->shadow_y); } void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y) { - if ((!g->width) || (!g->height)) - return; - - if (g->build_tex == 0) { - GlyphCacheBLF *gc = font->glyph_cache; - - if (font->tex_size_max == -1) - font->tex_size_max = GPU_max_texture_size(); - - if (gc->texture_current == BLF_TEXTURE_UNSET) { - blf_glyph_cache_texture(font, gc); - gc->offset_x = gc->pad; - gc->offset_y = 3; /* enough padding for blur */ - } - - if (gc->offset_x > (gc->p2_width - gc->glyph_width_max)) { - gc->offset_x = gc->pad; - gc->offset_y += gc->glyph_height_max; - - if (gc->offset_y > (gc->p2_height - gc->glyph_height_max)) { - gc->offset_y = 3; /* enough padding for blur */ - blf_glyph_cache_texture(font, gc); - } - } - - g->tex = gc->textures[gc->texture_current]; - g->offset_x = gc->offset_x; - g->offset_y = gc->offset_y; - - /* prevent glTexSubImage2D from failing if the character - * asks for pixels out of bounds, this tends only to happen - * with very small sizes (5px high or less) */ - if (UNLIKELY((g->offset_x + g->width) > gc->p2_width)) { - g->width -= (g->offset_x + g->width) - gc->p2_width; - BLI_assert(g->width > 0); - } - if (UNLIKELY((g->offset_y + g->height) > gc->p2_height)) { - g->height -= (g->offset_y + g->height) - gc->p2_height; - BLI_assert(g->height > 0); - } - - GPU_texture_update_sub(g->tex, GPU_DATA_UNSIGNED_BYTE, g->bitmap, g->offset_x, g->offset_y, 0, g->width, g->height, 0); - - g->uv[0][0] = ((float)g->offset_x) / ((float)gc->p2_width); - g->uv[0][1] = ((float)g->offset_y) / ((float)gc->p2_height); - g->uv[1][0] = ((float)(g->offset_x + g->width)) / ((float)gc->p2_width); - g->uv[1][1] = ((float)(g->offset_y + g->height)) / ((float)gc->p2_height); - - /* update the x offset for the next glyph. */ - gc->offset_x += (int)BLI_rctf_size_x(&g->box) + gc->pad; - - gc->glyphs_len_free--; - g->build_tex = 1; - } - - if (font->flags & BLF_CLIPPING) { - rctf rect_test; - blf_glyph_calc_rect_test(&rect_test, g, x, y); - BLI_rctf_translate(&rect_test, font->pos[0], font->pos[1]); - - if (!BLI_rctf_inside_rctf(&font->clip_rec, &rect_test)) { - return; - } - } - - if (font->tex_bind_state != g->tex) { - blf_batch_draw(); - font->tex_bind_state = g->tex; - GPU_texture_bind(font->tex_bind_state, 0); - } - - g_batch.tex_bind_state = g->tex; - - if (font->flags & BLF_SHADOW) { - rctf rect_ofs; - blf_glyph_calc_rect_shadow(&rect_ofs, g, x, y, font); - - 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, font->glyph_cache->p2_width, font->glyph_cache->p2_height, g->uv, - rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax); - } - else { - blf_texture5_draw(font->shadow_color, font->glyph_cache->p2_width, font->glyph_cache->p2_height, g->uv, - rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax); - } - } - - rctf rect; - blf_glyph_calc_rect(&rect, g, x, y); + if ((!g->width) || (!g->height)) + return; + + if (g->build_tex == 0) { + GlyphCacheBLF *gc = font->glyph_cache; + + if (font->tex_size_max == -1) + font->tex_size_max = GPU_max_texture_size(); + + if (gc->texture_current == BLF_TEXTURE_UNSET) { + blf_glyph_cache_texture(font, gc); + gc->offset_x = gc->pad; + gc->offset_y = 3; /* enough padding for blur */ + } + + if (gc->offset_x > (gc->p2_width - gc->glyph_width_max)) { + gc->offset_x = gc->pad; + gc->offset_y += gc->glyph_height_max; + + if (gc->offset_y > (gc->p2_height - gc->glyph_height_max)) { + gc->offset_y = 3; /* enough padding for blur */ + blf_glyph_cache_texture(font, gc); + } + } + + g->tex = gc->textures[gc->texture_current]; + g->offset_x = gc->offset_x; + g->offset_y = gc->offset_y; + + /* prevent glTexSubImage2D from failing if the character + * asks for pixels out of bounds, this tends only to happen + * with very small sizes (5px high or less) */ + if (UNLIKELY((g->offset_x + g->width) > gc->p2_width)) { + g->width -= (g->offset_x + g->width) - gc->p2_width; + BLI_assert(g->width > 0); + } + if (UNLIKELY((g->offset_y + g->height) > gc->p2_height)) { + g->height -= (g->offset_y + g->height) - gc->p2_height; + BLI_assert(g->height > 0); + } + + GPU_texture_update_sub(g->tex, + GPU_DATA_UNSIGNED_BYTE, + g->bitmap, + g->offset_x, + g->offset_y, + 0, + g->width, + g->height, + 0); + + g->uv[0][0] = ((float)g->offset_x) / ((float)gc->p2_width); + g->uv[0][1] = ((float)g->offset_y) / ((float)gc->p2_height); + g->uv[1][0] = ((float)(g->offset_x + g->width)) / ((float)gc->p2_width); + g->uv[1][1] = ((float)(g->offset_y + g->height)) / ((float)gc->p2_height); + + /* update the x offset for the next glyph. */ + gc->offset_x += (int)BLI_rctf_size_x(&g->box) + gc->pad; + + gc->glyphs_len_free--; + g->build_tex = 1; + } + + if (font->flags & BLF_CLIPPING) { + rctf rect_test; + blf_glyph_calc_rect_test(&rect_test, g, x, y); + BLI_rctf_translate(&rect_test, font->pos[0], font->pos[1]); + + if (!BLI_rctf_inside_rctf(&font->clip_rec, &rect_test)) { + return; + } + } + + if (font->tex_bind_state != g->tex) { + blf_batch_draw(); + font->tex_bind_state = g->tex; + GPU_texture_bind(font->tex_bind_state, 0); + } + + g_batch.tex_bind_state = g->tex; + + if (font->flags & BLF_SHADOW) { + rctf rect_ofs; + blf_glyph_calc_rect_shadow(&rect_ofs, g, x, y, font); + + 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, + font->glyph_cache->p2_width, + font->glyph_cache->p2_height, + g->uv, + rect_ofs.xmin, + rect_ofs.ymin, + rect_ofs.xmax, + rect_ofs.ymax); + } + else { + blf_texture5_draw(font->shadow_color, + font->glyph_cache->p2_width, + font->glyph_cache->p2_height, + g->uv, + rect_ofs.xmin, + rect_ofs.ymin, + rect_ofs.xmax, + rect_ofs.ymax); + } + } + + rctf rect; + blf_glyph_calc_rect(&rect, g, x, y); #if BLF_BLUR_ENABLE - switch (font->blur) { - case 3: - blf_texture3_draw(font->color, font->glyph_cache->p2_width, font->glyph_cache->p2_height, g->uv, - rect.xmin, rect.ymin, rect.xmax, rect.ymax); - break; - case 5: - blf_texture5_draw(font->color, font->glyph_cache->p2_width, font->glyph_cache->p2_height, g->uv, - rect.xmin, rect.ymin, rect.xmax, rect.ymax); - break; - default: - blf_texture_draw(font->color, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax); - } + switch (font->blur) { + case 3: + blf_texture3_draw(font->color, + font->glyph_cache->p2_width, + font->glyph_cache->p2_height, + g->uv, + rect.xmin, + rect.ymin, + rect.xmax, + rect.ymax); + break; + case 5: + blf_texture5_draw(font->color, + font->glyph_cache->p2_width, + font->glyph_cache->p2_height, + g->uv, + rect.xmin, + rect.ymin, + rect.xmax, + rect.ymax); + break; + default: + blf_texture_draw(font->color, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax); + } #else - blf_texture_draw(font->color, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax); + 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 f7eec01470a..cfc1d245a11 100644 --- a/source/blender/blenfont/intern/blf_internal.h +++ b/source/blender/blenfont/intern/blf_internal.h @@ -21,7 +21,6 @@ * \ingroup blf */ - #ifndef __BLF_INTERNAL_H__ #define __BLF_INTERNAL_H__ @@ -40,7 +39,7 @@ unsigned int blf_hash(unsigned int val); char *blf_dir_search(const char *file); char *blf_dir_metrics_search(const char *filename); -/* int blf_dir_split(const char *str, char *file, int *size); *//* UNUSED */ +/* int blf_dir_split(const char *str, char *file, int *size); */ /* UNUSED */ int blf_font_init(void); void blf_font_exit(void); @@ -54,21 +53,51 @@ void blf_font_attach_from_mem(struct FontBLF *font, const unsigned char *mem, in void blf_font_size(struct FontBLF *font, unsigned int size, unsigned int dpi); void blf_font_draw(struct FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info); -void blf_font_draw__wrap(struct FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info); -void blf_font_draw_ascii(struct FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info); +void blf_font_draw__wrap(struct FontBLF *font, + const char *str, + size_t len, + struct ResultBLF *r_info); +void blf_font_draw_ascii(struct FontBLF *font, + const char *str, + size_t len, + struct ResultBLF *r_info); int blf_font_draw_mono(struct FontBLF *font, const char *str, size_t len, int cwidth); -void blf_font_draw_buffer(struct FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info); -void blf_font_draw_buffer__wrap(struct FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info); -size_t blf_font_width_to_strlen(struct FontBLF *font, const char *str, size_t len, float width, float *r_width); -size_t blf_font_width_to_rstrlen(struct FontBLF *font, const char *str, size_t len, float width, float *r_width); -void blf_font_boundbox(struct FontBLF *font, const char *str, size_t len, struct rctf *r_box, struct ResultBLF *r_info); -void blf_font_boundbox__wrap(struct FontBLF *font, const char *str, size_t len, struct rctf *r_box, struct ResultBLF *r_info); -void blf_font_width_and_height(struct FontBLF *font, const char *str, size_t len, float *r_width, float *r_height, struct ResultBLF *r_info); +void blf_font_draw_buffer(struct FontBLF *font, + const char *str, + size_t len, + struct ResultBLF *r_info); +void blf_font_draw_buffer__wrap(struct FontBLF *font, + const char *str, + size_t len, + struct ResultBLF *r_info); +size_t blf_font_width_to_strlen( + struct FontBLF *font, const char *str, size_t len, float width, float *r_width); +size_t blf_font_width_to_rstrlen( + struct FontBLF *font, const char *str, size_t len, float width, float *r_width); +void blf_font_boundbox(struct FontBLF *font, + const char *str, + size_t len, + struct rctf *r_box, + struct ResultBLF *r_info); +void blf_font_boundbox__wrap(struct FontBLF *font, + const char *str, + size_t len, + struct rctf *r_box, + struct ResultBLF *r_info); +void blf_font_width_and_height(struct FontBLF *font, + const char *str, + size_t len, + float *r_width, + float *r_height, + struct ResultBLF *r_info); float blf_font_width(struct FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info); float blf_font_height(struct FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info); float blf_font_fixed_width(struct FontBLF *font); -int blf_font_count_missing_chars(struct FontBLF *font, const char *str, const size_t len, int *r_tot_chars); +int blf_font_count_missing_chars(struct FontBLF *font, + const char *str, + const size_t len, + int *r_tot_chars); void blf_font_free(struct FontBLF *font); @@ -76,7 +105,9 @@ struct KerningCacheBLF *blf_kerning_cache_find(struct FontBLF *font); struct KerningCacheBLF *blf_kerning_cache_new(struct FontBLF *font); void blf_kerning_cache_clear(struct FontBLF *font); -struct GlyphCacheBLF *blf_glyph_cache_find(struct FontBLF *font, unsigned int size, unsigned int dpi); +struct GlyphCacheBLF *blf_glyph_cache_find(struct FontBLF *font, + unsigned int size, + unsigned int dpi); struct GlyphCacheBLF *blf_glyph_cache_new(struct FontBLF *font); void blf_glyph_cache_clear(struct FontBLF *font); void blf_glyph_cache_free(struct GlyphCacheBLF *gc); @@ -90,7 +121,10 @@ void blf_glyph_render(struct FontBLF *font, struct GlyphBLF *g, float x, float y #ifdef WIN32 /* blf_font_win32_compat.c */ # ifdef FT_FREETYPE_H -extern FT_Error FT_New_Face__win32_compat(FT_Library library, const char *pathname, FT_Long face_index, FT_Face *aface); +extern FT_Error FT_New_Face__win32_compat(FT_Library library, + const char *pathname, + FT_Long face_index, + FT_Face *aface); # endif #endif diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index 013127b6a47..22950c26b6b 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -21,7 +21,6 @@ * \ingroup blf */ - #ifndef __BLF_INTERNAL_TYPES_H__ #define __BLF_INTERNAL_TYPES_H__ @@ -31,256 +30,256 @@ #define BLF_BATCH_DRAW_LEN_MAX 2048 /* in glyph */ typedef struct BatchBLF { - struct FontBLF *font; /* can only batch glyph from the same font */ - struct GPUBatch *batch; - struct GPUVertBuf *verts; - struct GPUVertBufRaw pos_step, tex_step, col_step; - unsigned int pos_loc, tex_loc, col_loc; - unsigned int glyph_len; - float ofs[2]; /* copy of font->pos */ - float mat[4][4]; /* previous call modelmatrix. */ - bool enabled, active, simple_shader; - GPUTexture *tex_bind_state; + struct FontBLF *font; /* can only batch glyph from the same font */ + struct GPUBatch *batch; + struct GPUVertBuf *verts; + struct GPUVertBufRaw pos_step, tex_step, col_step; + unsigned int pos_loc, tex_loc, col_loc; + unsigned int glyph_len; + float ofs[2]; /* copy of font->pos */ + float mat[4][4]; /* previous call modelmatrix. */ + bool enabled, active, simple_shader; + GPUTexture *tex_bind_state; } BatchBLF; extern BatchBLF g_batch; typedef struct KerningCacheBLF { - struct KerningCacheBLF *next, *prev; + struct KerningCacheBLF *next, *prev; - /* kerning mode. */ - FT_UInt mode; + /* kerning mode. */ + FT_UInt mode; - /* only cache a ascii glyph pairs. Only store the x - * offset we are interested in, instead of the full FT_Vector. */ - int table[0x80][0x80]; + /* only cache a ascii glyph pairs. Only store the x + * offset we are interested in, instead of the full FT_Vector. */ + int table[0x80][0x80]; } KerningCacheBLF; typedef struct GlyphCacheBLF { - struct GlyphCacheBLF *next; - struct GlyphCacheBLF *prev; + struct GlyphCacheBLF *next; + struct GlyphCacheBLF *prev; - /* font size. */ - unsigned int size; + /* font size. */ + unsigned int size; - /* and dpi. */ - unsigned int dpi; + /* and dpi. */ + unsigned int dpi; - /* and the glyphs. */ - ListBase bucket[257]; + /* and the glyphs. */ + ListBase bucket[257]; - /* fast ascii lookup */ - struct GlyphBLF *glyph_ascii_table[256]; + /* fast ascii lookup */ + struct GlyphBLF *glyph_ascii_table[256]; - /* texture array, to draw the glyphs. */ - GPUTexture **textures; + /* texture array, to draw the glyphs. */ + GPUTexture **textures; - /* size of the array. */ - unsigned int textures_len; + /* size of the array. */ + unsigned int textures_len; - /* and the last texture, aka. the current texture. */ - unsigned int texture_current; + /* and the last texture, aka. the current texture. */ + unsigned int texture_current; - /* like bftgl, we draw every glyph in a big texture, so this is the - * current position inside the texture. - */ - int offset_x; - int offset_y; + /* like bftgl, we draw every glyph in a big texture, so this is the + * current position inside the texture. + */ + int offset_x; + int offset_y; - /* and the space from one to other. */ - int pad; + /* and the space from one to other. */ + int pad; - /* and the bigger glyph in the font. */ - int glyph_width_max; - int glyph_height_max; + /* and the bigger glyph in the font. */ + int glyph_width_max; + int glyph_height_max; - /* next two integer power of two, to build the texture. */ - int p2_width; - int p2_height; + /* next two integer power of two, to build the texture. */ + int p2_width; + int p2_height; - /* number of glyphs in the font. */ - int glyphs_len_max; + /* number of glyphs in the font. */ + int glyphs_len_max; - /* number of glyphs not yet loaded (decreases every glyph loaded). */ - int glyphs_len_free; + /* number of glyphs not yet loaded (decreases every glyph loaded). */ + int glyphs_len_free; - /* ascender and descender value. */ - float ascender; - float descender; + /* ascender and descender value. */ + float ascender; + float descender; } GlyphCacheBLF; typedef struct GlyphBLF { - struct GlyphBLF *next; - struct GlyphBLF *prev; + struct GlyphBLF *next; + struct GlyphBLF *prev; - /* and the character, as UTF8 */ - unsigned int c; + /* and the character, as UTF8 */ + unsigned int c; - /* freetype2 index, to speed-up the search. */ - FT_UInt idx; + /* freetype2 index, to speed-up the search. */ + FT_UInt idx; - /* glyph box. */ - rctf box; + /* glyph box. */ + rctf box; - /* advance size. */ - float advance; - /* avoid conversion to int while drawing */ - int advance_i; + /* advance size. */ + float advance; + /* avoid conversion to int while drawing */ + int advance_i; - /* texture id where this glyph is store. */ - GPUTexture *tex; + /* texture id where this glyph is store. */ + GPUTexture *tex; - /* position inside the texture where this glyph is store. */ - int offset_x; - int offset_y; + /* position inside the texture where this glyph is store. */ + int offset_x; + int offset_y; - /* Bitmap data, from freetype. Take care that this - * can be NULL. - */ - unsigned char *bitmap; + /* Bitmap data, from freetype. Take care that this + * can be NULL. + */ + unsigned char *bitmap; - /* glyph width and height. */ - int width; - int height; - int pitch; + /* glyph width and height. */ + int width; + int height; + int pitch; - /* uv coords. */ - float uv[2][2]; + /* uv coords. */ + float uv[2][2]; - /* X and Y bearing of the glyph. - * The X bearing is from the origin to the glyph left bbox edge. - * The Y bearing is from the baseline to the top of the glyph edge. - */ - float pos_x; - float pos_y; + /* X and Y bearing of the glyph. + * The X bearing is from the origin to the glyph left bbox edge. + * The Y bearing is from the baseline to the top of the glyph edge. + */ + float pos_x; + float pos_y; - /* with value of zero mean that we need build the texture. */ - char build_tex; + /* with value of zero mean that we need build the texture. */ + char build_tex; } GlyphBLF; typedef struct FontBufInfoBLF { - /* for draw to buffer, always set this to NULL after finish! */ - float *fbuf; + /* for draw to buffer, always set this to NULL after finish! */ + float *fbuf; - /* the same but unsigned char */ - unsigned char *cbuf; + /* the same but unsigned char */ + unsigned char *cbuf; - /* buffer size, keep signed so comparisons with negative values work */ - int w; - int h; + /* buffer size, keep signed so comparisons with negative values work */ + int w; + int h; - /* number of channels. */ - int ch; + /* number of channels. */ + int ch; - /* display device used for color management */ - struct ColorManagedDisplay *display; + /* display device used for color management */ + struct ColorManagedDisplay *display; - /* and the color, the alphas is get from the glyph! - * color is srgb space */ - float col_init[4]; - /* cached conversion from 'col_init' */ - unsigned char col_char[4]; - float col_float[4]; + /* and the color, the alphas is get from the glyph! + * color is srgb space */ + float col_init[4]; + /* cached conversion from 'col_init' */ + unsigned char col_char[4]; + float col_float[4]; } FontBufInfoBLF; typedef struct FontBLF { - /* font name. */ - char *name; + /* font name. */ + char *name; - /* # of times this font was loaded */ - unsigned int reference_count; + /* # of times this font was loaded */ + unsigned int reference_count; - /* filename or NULL. */ - char *filename; + /* filename or NULL. */ + char *filename; - /* aspect ratio or scale. */ - float aspect[3]; + /* aspect ratio or scale. */ + float aspect[3]; - /* initial position for draw the text. */ - float pos[3]; + /* initial position for draw the text. */ + float pos[3]; - /* angle in radians. */ - float angle; + /* angle in radians. */ + float angle; #if 0 /* BLF_BLUR_ENABLE */ - /* blur: 3 or 5 large kernel */ - int blur; + /* blur: 3 or 5 large kernel */ + int blur; #endif - /* shadow level. */ - int shadow; + /* shadow level. */ + int shadow; - /* and shadow offset. */ - int shadow_x; - int shadow_y; + /* and shadow offset. */ + int shadow_x; + int shadow_y; - /* shadow color. */ - unsigned char shadow_color[4]; + /* shadow color. */ + unsigned char shadow_color[4]; - /* main text color. */ - unsigned char color[4]; + /* main text color. */ + unsigned char color[4]; - /* Multiplied this matrix with the current one before - * draw the text! see blf_draw__start. - */ - float m[16]; + /* Multiplied this matrix with the current one before + * draw the text! see blf_draw__start. + */ + float m[16]; - /* clipping rectangle. */ - rctf clip_rec; + /* clipping rectangle. */ + rctf clip_rec; - /* the width to wrap the text, see BLF_WORD_WRAP */ - int wrap_width; + /* the width to wrap the text, see BLF_WORD_WRAP */ + int wrap_width; - /* font dpi (default 72). */ - unsigned int dpi; + /* font dpi (default 72). */ + unsigned int dpi; - /* font size. */ - unsigned int size; + /* font size. */ + unsigned int size; - /* max texture size. */ - int tex_size_max; + /* max texture size. */ + int tex_size_max; - /* cache current OpenGL texture to save calls into the API */ - GPUTexture *tex_bind_state; + /* cache current OpenGL texture to save calls into the API */ + GPUTexture *tex_bind_state; - /* font options. */ - int flags; + /* font options. */ + int flags; - /* list of glyph cache for this font. */ - ListBase cache; + /* list of glyph cache for this font. */ + ListBase cache; - /* current glyph cache, size and dpi. */ - GlyphCacheBLF *glyph_cache; + /* current glyph cache, size and dpi. */ + GlyphCacheBLF *glyph_cache; - /* list of kerning cache for this font. */ - ListBase kerning_caches; + /* list of kerning cache for this font. */ + ListBase kerning_caches; - /* current kerning cache for this font and kerning mode. */ - KerningCacheBLF *kerning_cache; + /* current kerning cache for this font and kerning mode. */ + KerningCacheBLF *kerning_cache; - /* freetype2 lib handle. */ - FT_Library ft_lib; + /* freetype2 lib handle. */ + FT_Library ft_lib; - /* Mutex lock for library */ - SpinLock *ft_lib_mutex; + /* Mutex lock for library */ + SpinLock *ft_lib_mutex; - /* freetype2 face. */ - FT_Face face; + /* freetype2 face. */ + FT_Face face; - /* freetype kerning */ - FT_UInt kerning_mode; + /* freetype kerning */ + FT_UInt kerning_mode; - /* data for buffer usage (drawing into a texture buffer) */ - FontBufInfoBLF buf_info; + /* data for buffer usage (drawing into a texture buffer) */ + FontBufInfoBLF buf_info; } FontBLF; typedef struct DirBLF { - struct DirBLF *next; - struct DirBLF *prev; + struct DirBLF *next; + struct DirBLF *prev; - /* full path where search fonts. */ - char *path; + /* full path where search fonts. */ + char *path; } DirBLF; #define BLF_TEXTURE_UNSET ((unsigned int)-1) diff --git a/source/blender/blenfont/intern/blf_thumbs.c b/source/blender/blenfont/intern/blf_thumbs.c index bbab5110e0e..f2036d2e4e9 100644 --- a/source/blender/blenfont/intern/blf_thumbs.c +++ b/source/blender/blenfont/intern/blf_thumbs.c @@ -48,75 +48,78 @@ * * \note called from a thread, so it bypasses the normal BLF_* api (which isn't thread-safe). */ -void BLF_thumb_preview( - const char *filename, - const char **draw_str, const unsigned char draw_str_lines, - const float font_color[4], const int font_size, - unsigned char *buf, int w, int h, int channels) +void BLF_thumb_preview(const char *filename, + const char **draw_str, + const unsigned char draw_str_lines, + const float font_color[4], + const int font_size, + unsigned char *buf, + int w, + int h, + int channels) { - const unsigned int dpi = 72; - const int font_size_min = 6; - int font_size_curr; - /* shrink 1/th each line */ - int font_shrink = 4; - - FontBLF *font; - int i; - - /* Create a new blender font obj and fill it with default values */ - font = blf_font_new("thumb_font", filename); - if (!font) { - printf("Info: Can't load font '%s', no preview possible\n", filename); - return; - } - - /* Would be done via the BLF API, but we're not using a fontid here */ - font->buf_info.cbuf = buf; - font->buf_info.ch = channels; - font->buf_info.w = w; - font->buf_info.h = h; - - /* Always create the image with a white font, - * the caller can theme how it likes */ - memcpy(font->buf_info.col_init, font_color, sizeof(font->buf_info.col_init)); - font->pos[1] = (float)h; - - font_size_curr = font_size; - - blf_draw_buffer__start(font); - - for (i = 0; i < draw_str_lines; i++) { - const char *draw_str_i18n = BLT_translate_do(BLT_I18NCONTEXT_DEFAULT, draw_str[i]); - const size_t draw_str_i18n_len = strlen(draw_str_i18n); - int draw_str_i18n_nbr = 0; - - blf_font_size(font, (unsigned int)MAX2(font_size_min, font_size_curr), dpi); - - /* font->glyph_cache remains NULL if blf_font_size() failed to set font size */ - if (!font->glyph_cache) - break; - - /* decrease font size each time */ - font_size_curr -= (font_size_curr / font_shrink); - font_shrink += 1; - - font->pos[1] -= font->glyph_cache->ascender * 1.1f; - - /* We fallback to default english strings in case not enough chars are available in current font for given - * translated string (useful in non-latin i18n context, like chinese, since many fonts will then show - * nothing but ugly 'missing char' in their preview). - * Does not handle all cases, but much better than nothing. - */ - if (blf_font_count_missing_chars( - font, draw_str_i18n, draw_str_i18n_len, &draw_str_i18n_nbr) > (draw_str_i18n_nbr / 2)) - { - blf_font_draw_buffer(font, draw_str[i], strlen(draw_str[i]), NULL); - } - else { - blf_font_draw_buffer(font, draw_str_i18n, draw_str_i18n_len, NULL); - } - } - - blf_draw_buffer__end(); - blf_font_free(font); + const unsigned int dpi = 72; + const int font_size_min = 6; + int font_size_curr; + /* shrink 1/th each line */ + int font_shrink = 4; + + FontBLF *font; + int i; + + /* Create a new blender font obj and fill it with default values */ + font = blf_font_new("thumb_font", filename); + if (!font) { + printf("Info: Can't load font '%s', no preview possible\n", filename); + return; + } + + /* Would be done via the BLF API, but we're not using a fontid here */ + font->buf_info.cbuf = buf; + font->buf_info.ch = channels; + font->buf_info.w = w; + font->buf_info.h = h; + + /* Always create the image with a white font, + * the caller can theme how it likes */ + memcpy(font->buf_info.col_init, font_color, sizeof(font->buf_info.col_init)); + font->pos[1] = (float)h; + + font_size_curr = font_size; + + blf_draw_buffer__start(font); + + for (i = 0; i < draw_str_lines; i++) { + const char *draw_str_i18n = BLT_translate_do(BLT_I18NCONTEXT_DEFAULT, draw_str[i]); + const size_t draw_str_i18n_len = strlen(draw_str_i18n); + int draw_str_i18n_nbr = 0; + + blf_font_size(font, (unsigned int)MAX2(font_size_min, font_size_curr), dpi); + + /* font->glyph_cache remains NULL if blf_font_size() failed to set font size */ + if (!font->glyph_cache) + break; + + /* decrease font size each time */ + font_size_curr -= (font_size_curr / font_shrink); + font_shrink += 1; + + font->pos[1] -= font->glyph_cache->ascender * 1.1f; + + /* We fallback to default english strings in case not enough chars are available in current font for given + * translated string (useful in non-latin i18n context, like chinese, since many fonts will then show + * nothing but ugly 'missing char' in their preview). + * Does not handle all cases, but much better than nothing. + */ + if (blf_font_count_missing_chars(font, draw_str_i18n, draw_str_i18n_len, &draw_str_i18n_nbr) > + (draw_str_i18n_nbr / 2)) { + blf_font_draw_buffer(font, draw_str[i], strlen(draw_str[i]), NULL); + } + else { + blf_font_draw_buffer(font, draw_str_i18n, draw_str_i18n_len, NULL); + } + } + + blf_draw_buffer__end(); + blf_font_free(font); } diff --git a/source/blender/blenfont/intern/blf_util.c b/source/blender/blenfont/intern/blf_util.c index d479861b897..e55759199a2 100644 --- a/source/blender/blenfont/intern/blf_util.c +++ b/source/blender/blenfont/intern/blf_util.c @@ -23,7 +23,6 @@ * Internal utility API for BLF. */ - #include #include #include @@ -34,26 +33,26 @@ unsigned int blf_next_p2(unsigned int x) { - x -= 1; - x |= (x >> 16); - x |= (x >> 8); - x |= (x >> 4); - x |= (x >> 2); - x |= (x >> 1); - x += 1; - return x; + x -= 1; + x |= (x >> 16); + x |= (x >> 8); + x |= (x >> 4); + x |= (x >> 2); + x |= (x >> 1); + x += 1; + return x; } unsigned int blf_hash(unsigned int val) { - unsigned int key; - - key = val; - key += ~(key << 16); - key ^= (key >> 5); - key += (key << 3); - key ^= (key >> 13); - key += ~(key << 9); - key ^= (key >> 17); - return key % 257; + unsigned int key; + + key = val; + key += ~(key << 16); + key ^= (key >> 5); + key += (key << 3); + key ^= (key >> 13); + key += ~(key << 9); + key ^= (key >> 17); + return key % 257; } -- cgit v1.2.3