diff options
-rw-r--r-- | source/blender/blenfont/intern/blf_font.c | 22 | ||||
-rw-r--r-- | source/blender/blenfont/intern/blf_internal.h | 3 | ||||
-rw-r--r-- | source/blender/blenfont/intern/blf_thumbs.c | 18 |
3 files changed, 42 insertions, 1 deletions
diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c index 1391e18748c..8f8ee445ef9 100644 --- a/source/blender/blenfont/intern/blf_font.c +++ b/source/blender/blenfont/intern/blf_font.c @@ -667,6 +667,28 @@ float blf_font_fixed_width(FontBLF *font) return g->advance; } +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; +} + void blf_font_free(FontBLF *font) { GlyphCacheBLF *gc; diff --git a/source/blender/blenfont/intern/blf_internal.h b/source/blender/blenfont/intern/blf_internal.h index 39b3e3397be..85410a4d856 100644 --- a/source/blender/blenfont/intern/blf_internal.h +++ b/source/blender/blenfont/intern/blf_internal.h @@ -62,6 +62,9 @@ void blf_font_width_and_height(struct FontBLF *font, const char *str, size_t len float blf_font_width(struct FontBLF *font, const char *str, size_t len); float blf_font_height(struct FontBLF *font, const char *str, size_t len); 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); + void blf_font_free(struct FontBLF *font); struct GlyphCacheBLF *blf_glyph_cache_find(struct FontBLF *font, unsigned int size, unsigned int dpi); diff --git a/source/blender/blenfont/intern/blf_thumbs.c b/source/blender/blenfont/intern/blf_thumbs.c index 7c0a43e09ee..4b7a568b8b0 100644 --- a/source/blender/blenfont/intern/blf_thumbs.c +++ b/source/blender/blenfont/intern/blf_thumbs.c @@ -90,6 +90,10 @@ void BLF_thumb_preview( font_size_curr = font_size; for (i = 0; i < draw_str_lines; i++) { + const char *draw_str_i18n = BLF_translate_do(BLF_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); /* decrease font size each time */ @@ -98,7 +102,19 @@ void BLF_thumb_preview( font->pos[1] -= font->glyph_cache->ascender * 1.1f; - blf_font_buffer(font, BLF_translate_do(BLF_I18NCONTEXT_DEFAULT, draw_str[i])); + /* 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_buffer(font, draw_str[i]); + } + else { + blf_font_buffer(font, draw_str_i18n); + } } blf_font_free(font); |