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:
@@ -223,6 +223,7 @@ void BLF_dir_free(char **dirs, int count) ATTR_NONNULL();
|
|||||||
/* blf_thumbs.c */
|
/* blf_thumbs.c */
|
||||||
void BLF_thumb_preview(const char *filename,
|
void BLF_thumb_preview(const char *filename,
|
||||||
const char **draw_str,
|
const char **draw_str,
|
||||||
|
const char **i18n_draw_str,
|
||||||
const unsigned char draw_str_lines,
|
const unsigned char draw_str_lines,
|
||||||
const float font_color[4],
|
const float font_color[4],
|
||||||
const int font_size,
|
const int font_size,
|
||||||
|
@@ -50,6 +50,7 @@
|
|||||||
*/
|
*/
|
||||||
void BLF_thumb_preview(const char *filename,
|
void BLF_thumb_preview(const char *filename,
|
||||||
const char **draw_str,
|
const char **draw_str,
|
||||||
|
const char **i18n_draw_str,
|
||||||
const unsigned char draw_str_lines,
|
const unsigned char draw_str_lines,
|
||||||
const float font_color[4],
|
const float font_color[4],
|
||||||
const int font_size,
|
const int font_size,
|
||||||
@@ -90,7 +91,7 @@ void BLF_thumb_preview(const char *filename,
|
|||||||
blf_draw_buffer__start(font);
|
blf_draw_buffer__start(font);
|
||||||
|
|
||||||
for (i = 0; i < draw_str_lines; i++) {
|
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);
|
const size_t draw_str_i18n_len = strlen(draw_str_i18n);
|
||||||
int draw_str_i18n_nbr = 0;
|
int draw_str_i18n_nbr = 0;
|
||||||
|
|
||||||
|
@@ -22,6 +22,7 @@ set(INC
|
|||||||
.
|
.
|
||||||
../blenkernel
|
../blenkernel
|
||||||
../blenlib
|
../blenlib
|
||||||
|
../imbuf
|
||||||
../makesdna
|
../makesdna
|
||||||
../makesrna
|
../makesrna
|
||||||
../../../intern/guardedalloc
|
../../../intern/guardedalloc
|
||||||
|
@@ -42,6 +42,8 @@
|
|||||||
|
|
||||||
#include "BKE_appdir.h"
|
#include "BKE_appdir.h"
|
||||||
|
|
||||||
|
#include "IMB_thumbs.h"
|
||||||
|
|
||||||
#include "DNA_userdef_types.h"
|
#include "DNA_userdef_types.h"
|
||||||
|
|
||||||
#include "MEM_guardedalloc.h"
|
#include "MEM_guardedalloc.h"
|
||||||
@@ -288,6 +290,7 @@ void BLT_lang_set(const char *str)
|
|||||||
(void)str;
|
(void)str;
|
||||||
#endif
|
#endif
|
||||||
blt_lang_check_ime_supported();
|
blt_lang_check_ime_supported();
|
||||||
|
IMB_thumb_clear_translations();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get the current locale (short code, e.g. es_ES). */
|
/* Get the current locale (short code, e.g. es_ES). */
|
||||||
|
@@ -1317,6 +1317,9 @@ static void filelist_cache_init(FileListEntryCache *cache, size_t cache_size)
|
|||||||
|
|
||||||
cache->size = cache_size;
|
cache->size = cache_size;
|
||||||
cache->flags = FLC_IS_INIT;
|
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)
|
static void filelist_cache_free(FileListEntryCache *cache)
|
||||||
|
@@ -59,27 +59,34 @@ typedef enum ThumbSource {
|
|||||||
#define THUMB_DEFAULT_HASH "00000000000000000000000000000000"
|
#define THUMB_DEFAULT_HASH "00000000000000000000000000000000"
|
||||||
|
|
||||||
/* create thumbnail for file and returns new imbuf for thumbnail */
|
/* 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 */
|
/* 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 */
|
/* delete all thumbs for the file */
|
||||||
void IMB_thumb_delete(const char *path, ThumbSize size);
|
void IMB_thumb_delete(const char *path, ThumbSize size);
|
||||||
|
|
||||||
/* return the state of the thumb, needed to determine how to manage the thumb */
|
/* 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 */
|
/* create the necessary dirs to store the thumbnails */
|
||||||
void IMB_thumb_makedirs(void);
|
void IMB_thumb_makedirs(void);
|
||||||
|
|
||||||
/* special function for loading a thumbnail embedded into a blend file */
|
/* 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);
|
void IMB_thumb_overlay_blend(unsigned int *thumb, int width, int height, float aspect);
|
||||||
|
|
||||||
/* special function for previewing fonts */
|
/* 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);
|
bool IMB_thumb_load_font_get_hash(char *r_hash);
|
||||||
|
void IMB_thumb_clear_translations(void);
|
||||||
|
void IMB_thumb_ensure_translations(void);
|
||||||
|
|
||||||
/* Threading */
|
/* Threading */
|
||||||
void IMB_thumb_locks_acquire(void);
|
void IMB_thumb_locks_acquire(void);
|
||||||
|
@@ -32,14 +32,30 @@
|
|||||||
#include "../../blenfont/BLF_api.h"
|
#include "../../blenfont/BLF_api.h"
|
||||||
#include "../../blentranslation/BLT_translation.h"
|
#include "../../blentranslation/BLT_translation.h"
|
||||||
|
|
||||||
static const char *thumb_str[] = {
|
#define THUMB_TXT_ITEMS \
|
||||||
N_("AaBbCc"),
|
N_("AaBbCc"), N_("The quick"), N_("brown fox"), N_("jumps over"), N_("the lazy dog"),
|
||||||
|
|
||||||
N_("The quick"),
|
static const char *thumb_str[] = {THUMB_TXT_ITEMS};
|
||||||
N_("brown fox"),
|
|
||||||
N_("jumps over"),
|
static const char *i18n_thumb_str[] = {THUMB_TXT_ITEMS};
|
||||||
N_("the lazy dog"),
|
|
||||||
};
|
#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)
|
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,
|
BLF_thumb_preview(filename,
|
||||||
thumb_str,
|
thumb_str,
|
||||||
|
i18n_thumb_str,
|
||||||
ARRAY_SIZE(thumb_str),
|
ARRAY_SIZE(thumb_str),
|
||||||
font_color,
|
font_color,
|
||||||
font_size,
|
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);
|
len += BLI_strncpy_rlen(str + len, THUMB_DEFAULT_HASH, sizeof(buf) - len);
|
||||||
|
|
||||||
for (i = 0; (i < draw_str_lines) && (len < sizeof(buf)); i++) {
|
for (i = 0; (i < draw_str_lines) && (len < sizeof(buf)); i++) {
|
||||||
len += BLI_strncpy_rlen(
|
len += BLI_strncpy_rlen(str + len,
|
||||||
str + len, BLT_translate_do(BLT_I18NCONTEXT_DEFAULT, thumb_str[i]), sizeof(buf) - len);
|
i18n_thumb_str[i] != NULL ? i18n_thumb_str[i] : thumb_str[i],
|
||||||
|
sizeof(buf) - len);
|
||||||
}
|
}
|
||||||
|
|
||||||
BLI_hash_md5_buffer(str, len, digest);
|
BLI_hash_md5_buffer(str, len, digest);
|
||||||
|
@@ -80,6 +80,8 @@
|
|||||||
#include "RE_engine.h"
|
#include "RE_engine.h"
|
||||||
#include "RE_pipeline.h" /* RE_ free stuff */
|
#include "RE_pipeline.h" /* RE_ free stuff */
|
||||||
|
|
||||||
|
#include "IMB_thumbs.h"
|
||||||
|
|
||||||
#ifdef WITH_PYTHON
|
#ifdef WITH_PYTHON
|
||||||
# include "BPY_extern.h"
|
# include "BPY_extern.h"
|
||||||
#endif
|
#endif
|
||||||
@@ -299,6 +301,9 @@ void WM_init(bContext *C, int argc, const char **argv)
|
|||||||
/* Call again to set from userpreferences... */
|
/* Call again to set from userpreferences... */
|
||||||
BLT_lang_set(NULL);
|
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) {
|
if (!G.background) {
|
||||||
|
|
||||||
#ifdef WITH_INPUT_NDOF
|
#ifdef WITH_INPUT_NDOF
|
||||||
|
Reference in New Issue
Block a user