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
This commit is contained in:
Bastien Montagne
2019-07-29 14:10:54 +02:00
parent b83a1b62c7
commit 3b6c75dc31
8 changed files with 54 additions and 15 deletions

View File

@@ -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,

View File

@@ -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;

View File

@@ -22,6 +22,7 @@ set(INC
.
../blenkernel
../blenlib
../imbuf
../makesdna
../makesrna
../../../intern/guardedalloc

View File

@@ -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). */

View File

@@ -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)

View File

@@ -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);

View File

@@ -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);

View File

@@ -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