From 3b6c75dc318223a7e559805ed9802c46c73f2f28 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Mon, 29 Jul 2019 14:10:54 +0200 Subject: Fix T67620: Font preview translations malfunction in Blender 2.8 We cannot reliably use translations API from non-main threads. Now storing translated strings in a static cache, with basic mechanism to update it on language change. Reviewers: brecht, campbellbarton Differential Revision: https://developer.blender.org/D5350 --- source/blender/blenfont/BLF_api.h | 1 + source/blender/blenfont/intern/blf_thumbs.c | 3 +- source/blender/blentranslation/CMakeLists.txt | 1 + source/blender/blentranslation/intern/blt_lang.c | 3 ++ source/blender/editors/space_file/filelist.c | 3 ++ source/blender/imbuf/IMB_thumbs.h | 17 +++++++--- source/blender/imbuf/intern/thumbs_font.c | 36 ++++++++++++++++------ source/blender/windowmanager/intern/wm_init_exit.c | 5 +++ 8 files changed, 54 insertions(+), 15 deletions(-) (limited to 'source') diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h index 448bb0d621a..bf0aa96df84 100644 --- a/source/blender/blenfont/BLF_api.h +++ b/source/blender/blenfont/BLF_api.h @@ -223,6 +223,7 @@ 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 char **i18n_draw_str, const unsigned char draw_str_lines, const float font_color[4], const int font_size, diff --git a/source/blender/blenfont/intern/blf_thumbs.c b/source/blender/blenfont/intern/blf_thumbs.c index 802f97fc5f5..d6710b91539 100644 --- a/source/blender/blenfont/intern/blf_thumbs.c +++ b/source/blender/blenfont/intern/blf_thumbs.c @@ -50,6 +50,7 @@ */ void BLF_thumb_preview(const char *filename, const char **draw_str, + const char **i18n_draw_str, const unsigned char draw_str_lines, const float font_color[4], const int font_size, @@ -90,7 +91,7 @@ void BLF_thumb_preview(const char *filename, 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 char *draw_str_i18n = i18n_draw_str[i] != NULL ? i18n_draw_str[i] : draw_str[i]; const size_t draw_str_i18n_len = strlen(draw_str_i18n); int draw_str_i18n_nbr = 0; diff --git a/source/blender/blentranslation/CMakeLists.txt b/source/blender/blentranslation/CMakeLists.txt index 34952911dce..70e68ca06d7 100644 --- a/source/blender/blentranslation/CMakeLists.txt +++ b/source/blender/blentranslation/CMakeLists.txt @@ -22,6 +22,7 @@ set(INC . ../blenkernel ../blenlib + ../imbuf ../makesdna ../makesrna ../../../intern/guardedalloc diff --git a/source/blender/blentranslation/intern/blt_lang.c b/source/blender/blentranslation/intern/blt_lang.c index 75a4681deb2..82386a17776 100644 --- a/source/blender/blentranslation/intern/blt_lang.c +++ b/source/blender/blentranslation/intern/blt_lang.c @@ -42,6 +42,8 @@ #include "BKE_appdir.h" +#include "IMB_thumbs.h" + #include "DNA_userdef_types.h" #include "MEM_guardedalloc.h" @@ -288,6 +290,7 @@ void BLT_lang_set(const char *str) (void)str; #endif blt_lang_check_ime_supported(); + IMB_thumb_clear_translations(); } /* Get the current locale (short code, e.g. es_ES). */ diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c index 9004eaa7bf6..e06ee620ea7 100644 --- a/source/blender/editors/space_file/filelist.c +++ b/source/blender/editors/space_file/filelist.c @@ -1317,6 +1317,9 @@ static void filelist_cache_init(FileListEntryCache *cache, size_t cache_size) cache->size = cache_size; cache->flags = FLC_IS_INIT; + + /* We cannot translate from non-main thread, so init translated strings once from here. */ + IMB_thumb_ensure_translations(); } static void filelist_cache_free(FileListEntryCache *cache) diff --git a/source/blender/imbuf/IMB_thumbs.h b/source/blender/imbuf/IMB_thumbs.h index bac408e869b..5c7a60bbdcf 100644 --- a/source/blender/imbuf/IMB_thumbs.h +++ b/source/blender/imbuf/IMB_thumbs.h @@ -59,27 +59,34 @@ typedef enum ThumbSource { #define THUMB_DEFAULT_HASH "00000000000000000000000000000000" /* create thumbnail for file and returns new imbuf for thumbnail */ -ImBuf *IMB_thumb_create(const char *path, ThumbSize size, ThumbSource source, ImBuf *ibuf); +struct ImBuf *IMB_thumb_create(const char *path, + ThumbSize size, + ThumbSource source, + struct ImBuf *ibuf); /* read thumbnail for file and returns new imbuf for thumbnail */ -ImBuf *IMB_thumb_read(const char *path, ThumbSize size); +struct ImBuf *IMB_thumb_read(const char *path, ThumbSize size); /* delete all thumbs for the file */ void IMB_thumb_delete(const char *path, ThumbSize size); /* return the state of the thumb, needed to determine how to manage the thumb */ -ImBuf *IMB_thumb_manage(const char *path, ThumbSize size, ThumbSource source); +struct ImBuf *IMB_thumb_manage(const char *path, ThumbSize size, ThumbSource source); /* create the necessary dirs to store the thumbnails */ void IMB_thumb_makedirs(void); /* special function for loading a thumbnail embedded into a blend file */ -ImBuf *IMB_thumb_load_blend(const char *blen_path, const char *blen_group, const char *blen_id); +struct ImBuf *IMB_thumb_load_blend(const char *blen_path, + const char *blen_group, + const char *blen_id); void IMB_thumb_overlay_blend(unsigned int *thumb, int width, int height, float aspect); /* special function for previewing fonts */ -ImBuf *IMB_thumb_load_font(const char *filename, unsigned int x, unsigned int y); +struct ImBuf *IMB_thumb_load_font(const char *filename, unsigned int x, unsigned int y); bool IMB_thumb_load_font_get_hash(char *r_hash); +void IMB_thumb_clear_translations(void); +void IMB_thumb_ensure_translations(void); /* Threading */ void IMB_thumb_locks_acquire(void); diff --git a/source/blender/imbuf/intern/thumbs_font.c b/source/blender/imbuf/intern/thumbs_font.c index 1213927d329..d9a4363d01c 100644 --- a/source/blender/imbuf/intern/thumbs_font.c +++ b/source/blender/imbuf/intern/thumbs_font.c @@ -32,14 +32,30 @@ #include "../../blenfont/BLF_api.h" #include "../../blentranslation/BLT_translation.h" -static const char *thumb_str[] = { - N_("AaBbCc"), +#define THUMB_TXT_ITEMS \ + N_("AaBbCc"), N_("The quick"), N_("brown fox"), N_("jumps over"), N_("the lazy dog"), - N_("The quick"), - N_("brown fox"), - N_("jumps over"), - N_("the lazy dog"), -}; +static const char *thumb_str[] = {THUMB_TXT_ITEMS}; + +static const char *i18n_thumb_str[] = {THUMB_TXT_ITEMS}; + +#undef THUMB_TXT_ITEMS + +void IMB_thumb_clear_translations(void) +{ + for (int i = ARRAY_SIZE(thumb_str); i-- > 0;) { + i18n_thumb_str[i] = NULL; + printf("%s: clearing i18n string %d\n", __func__, i); + } +} + +void IMB_thumb_ensure_translations(void) +{ + for (int i = ARRAY_SIZE(thumb_str); i-- > 0;) { + i18n_thumb_str[i] = BLT_translate_do(BLT_I18NCONTEXT_DEFAULT, thumb_str[i]); + printf("%s: translated %s to %s\n", __func__, thumb_str[i], i18n_thumb_str[i]); + } +} struct ImBuf *IMB_thumb_load_font(const char *filename, unsigned int x, unsigned int y) { @@ -62,6 +78,7 @@ struct ImBuf *IMB_thumb_load_font(const char *filename, unsigned int x, unsigned BLF_thumb_preview(filename, thumb_str, + i18n_thumb_str, ARRAY_SIZE(thumb_str), font_color, font_size, @@ -87,8 +104,9 @@ bool IMB_thumb_load_font_get_hash(char *r_hash) len += BLI_strncpy_rlen(str + len, THUMB_DEFAULT_HASH, sizeof(buf) - len); for (i = 0; (i < draw_str_lines) && (len < sizeof(buf)); i++) { - len += BLI_strncpy_rlen( - str + len, BLT_translate_do(BLT_I18NCONTEXT_DEFAULT, thumb_str[i]), sizeof(buf) - len); + len += BLI_strncpy_rlen(str + len, + i18n_thumb_str[i] != NULL ? i18n_thumb_str[i] : thumb_str[i], + sizeof(buf) - len); } BLI_hash_md5_buffer(str, len, digest); diff --git a/source/blender/windowmanager/intern/wm_init_exit.c b/source/blender/windowmanager/intern/wm_init_exit.c index f3c94162786..763bdbb9cf0 100644 --- a/source/blender/windowmanager/intern/wm_init_exit.c +++ b/source/blender/windowmanager/intern/wm_init_exit.c @@ -80,6 +80,8 @@ #include "RE_engine.h" #include "RE_pipeline.h" /* RE_ free stuff */ +#include "IMB_thumbs.h" + #ifdef WITH_PYTHON # include "BPY_extern.h" #endif @@ -299,6 +301,9 @@ void WM_init(bContext *C, int argc, const char **argv) /* Call again to set from userpreferences... */ BLT_lang_set(NULL); + /* That one is generated on demand, we need to be sure it's clear on init. */ + IMB_thumb_clear_translations(); + if (!G.background) { #ifdef WITH_INPUT_NDOF -- cgit v1.2.3