From b5b06233dd23d814678152d4b65fa7e6703e7839 Mon Sep 17 00:00:00 2001 From: lixianjing Date: Wed, 13 Mar 2024 18:47:34 +0800 Subject: [PATCH] load different font for different locale --- docs/README.md | 1 + src/base/assets_manager.c | 42 ++++++++++++++++++++++++++++++++------- src/base/assets_manager.h | 9 +++++++++ src/base/window_manager.c | 2 ++ 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/docs/README.md b/docs/README.md index ab7719bd9..6695793e1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -101,6 +101,7 @@ * [如何自定义资源加载方式](how_to_set_custom_load_asset.md) * [如何处理将文件拖入到窗口的事件](how_to_handle_drop_file_event.md) * [如何将资源编译到应用程序并使用它们](how_to_compile_res_into_app_and_use_them.md) +* [如何为不同语言加载不同的字体文件](how_to_load_font_for_specific_language.md) ### 3. 内部原理 * [AWTK 脚本绑定原理](script_binding.md) diff --git a/src/base/assets_manager.c b/src/base/assets_manager.c index a9c1fd2a7..d7eb909c5 100644 --- a/src/base/assets_manager.c +++ b/src/base/assets_manager.c @@ -607,9 +607,7 @@ ret_t assets_manager_set_res_root(assets_manager_t* am, const char* res_root) { ret_t assets_manager_clear_all(assets_manager_t* am) { return_value_if_fail(am != NULL, RET_BAD_PARAMS); - assets_manager_clear_cache(am, ASSET_TYPE_UI); - assets_manager_clear_cache(am, ASSET_TYPE_STYLE); - assets_manager_clear_cache(am, ASSET_TYPE_FONT); + assets_manager_clear_all_cache(am); return darray_clear(&(am->assets)); } @@ -619,13 +617,19 @@ const char* assets_manager_get_theme_name(assets_manager_t* am) { return am->theme; } +ret_t assets_manager_clear_all_cache(assets_manager_t* am) { + assets_manager_clear_cache(am, ASSET_TYPE_UI); + assets_manager_clear_cache(am, ASSET_TYPE_STYLE); + assets_manager_clear_cache(am, ASSET_TYPE_FONT); + + return RET_OK; +} + ret_t assets_manager_set_theme(assets_manager_t* am, const char* theme) { return_value_if_fail(am != NULL, RET_BAD_PARAMS); am->theme = tk_str_copy(am->theme, theme); - assets_manager_clear_cache(am, ASSET_TYPE_UI); - assets_manager_clear_cache(am, ASSET_TYPE_STYLE); - assets_manager_clear_cache(am, ASSET_TYPE_FONT); + assets_manager_clear_all_cache(am); return RET_OK; } @@ -699,7 +703,31 @@ static const asset_info_t* assets_manager_ref_impl(assets_manager_t* am, asset_t const asset_info_t* info = assets_manager_find_in_cache(am, type, subtype, name); if (info == NULL) { - info = assets_manager_load_ex(am, type, subtype, name); + if (type == ASSET_TYPE_FONT) { + char name_ex[TK_NAME_LEN * 2 + 1] = {0}; + system_info_t* sinfo = assets_manager_get_system_info(am); + const char* lang = tk_object_get_prop_str(TK_OBJECT(sinfo), SYSTEM_INFO_PROP_LANGUAGE); + const char* country = tk_object_get_prop_str(TK_OBJECT(sinfo), SYSTEM_INFO_PROP_COUNTRY); + + tk_snprintf(name_ex, sizeof(name_ex) - 1, "%s.%s_%s", name, lang, country); + log_debug("try font load %s\n", name_ex); + info = assets_manager_load_ex(am, type, subtype, name_ex); + if (info == NULL) { + tk_snprintf(name_ex, sizeof(name_ex) - 1, "%s.%s", name, lang); + log_debug("try font load %s\n", name_ex); + info = assets_manager_load_ex(am, type, subtype, name_ex); + } + + if (info == NULL) { + info = assets_manager_load_ex(am, type, subtype, name); + } + + if (info != NULL) { + log_debug("load asset %s\n", asset_info_get_name(info)); + } + } else { + info = assets_manager_load_ex(am, type, subtype, name); + } } else { asset_info_ref((asset_info_t*)info); } diff --git a/src/base/assets_manager.h b/src/base/assets_manager.h index ce2f01e55..28af6f668 100644 --- a/src/base/assets_manager.h +++ b/src/base/assets_manager.h @@ -376,6 +376,15 @@ ret_t assets_manager_clear_cache(assets_manager_t* am, asset_type_t type); */ ret_t assets_manager_clear_cache_ex(assets_manager_t* am, asset_type_t type, const char* name); +/** + * @method assets_manager_clear_all_cache + * 清除缓存。 + * @param {assets_manager_t*} am asset manager对象。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t assets_manager_clear_all_cache(assets_manager_t* am); + /** * @method assets_manager_clear_all * 清除全部缓存的资源。 diff --git a/src/base/window_manager.c b/src/base/window_manager.c index c69c15029..5b98d8dfd 100644 --- a/src/base/window_manager.c +++ b/src/base/window_manager.c @@ -485,10 +485,12 @@ static ret_t wm_on_locale_changed(void* ctx, event_t* e) { widget_t* widget = WIDGET(ctx); font_manager_t* fm = widget_get_font_manager(widget); image_manager_t* imm = widget_get_image_manager(widget); + assets_manager_t* am = widget_get_assets_manager(widget); return_value_if_fail(widget != NULL, RET_BAD_PARAMS); return_value_if_fail(fm != NULL && imm != NULL, RET_BAD_PARAMS); + assets_manager_clear_all_cache(am); font_manager_unload_all(fm); image_manager_unload_all(imm);