load different font for different locale

This commit is contained in:
lixianjing 2024-03-13 18:47:34 +08:00
parent d43524370b
commit b5b06233dd
4 changed files with 47 additions and 7 deletions

View File

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

View File

@ -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;
}
@ -698,8 +702,32 @@ static const asset_info_t* assets_manager_ref_impl(assets_manager_t* am, asset_t
uint16_t subtype, const char* name) {
const asset_info_t* info = assets_manager_find_in_cache(am, type, subtype, name);
if (info == NULL) {
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);
}

View File

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

View File

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