[Bf-blender-cvs] [2d88806] master: Font Preview: fallback to default english strings in case translated ones have not enough chars in current font.

Bastien Montagne noreply at git.blender.org
Mon Jun 1 19:45:18 CEST 2015


Commit: 2d8880643dad51f09370851248d3d6d80b5bf6d9
Author: Bastien Montagne
Date:   Mon Jun 1 17:12:56 2015 +0200
Branches: master
https://developer.blender.org/rB2d8880643dad51f09370851248d3d6d80b5bf6d9

Font Preview: fallback to default english strings in case translated ones have not enough chars in current font.

This avoids some ugly 'missing char' in previews - not all cases of course, but most common ones.

A complete solution would be much much more involved, and probably not worth it here.
Definitively not before a release, at least!

===================================================================

M	source/blender/blenfont/intern/blf_font.c
M	source/blender/blenfont/intern/blf_internal.h
M	source/blender/blenfont/intern/blf_thumbs.c

===================================================================

diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c
index 1391e18..8f8ee44 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 39b3e33..85410a4 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 7c0a43e..4b7a568 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);




More information about the Bf-blender-cvs mailing list