fallback fonts

This commit is contained in:
galister
2026-01-07 03:41:07 +09:00
parent 954e1c3157
commit 578165ee5b
4 changed files with 16 additions and 8 deletions

View File

@@ -1,3 +1,4 @@
use cosmic_text::PlatformFallback;
use parking_lot::Mutex;
#[derive(Default)]
@@ -13,12 +14,15 @@ pub struct WguiFontSystem {
}
impl WguiFontSystem {
pub fn new(config: &WguiFontConfig) -> Self {
pub fn new(config: &WguiFontConfig, lang: &str) -> Self {
let mut db = cosmic_text::fontdb::Database::new();
let system = if config.binaries.is_empty() {
cosmic_text::FontSystem::new()
} else {
// needed for fallback
db.load_system_fonts();
for binary in &config.binaries {
// binary data is copied and preserved here
db.load_font_data(binary.to_vec());
@@ -33,9 +37,7 @@ impl WguiFontSystem {
}
// we don't require anything special, at least for now
let locale = String::from("C");
cosmic_text::FontSystem::new_with_locale_and_db(locale, db)
cosmic_text::FontSystem::new_with_locale_and_db_and_fallback(lang.to_string(), db, PlatformFallback)
};
Self {

View File

@@ -72,10 +72,10 @@ impl WguiGlobals {
Ok(Self(Rc::new(RefCell::new(Globals {
assets_internal,
assets_builtin,
i18n_builtin,
defaults,
asset_folder,
font_system: WguiFontSystem::new(font_config),
font_system: WguiFontSystem::new(font_config, i18n_builtin.get_lang()),
i18n_builtin,
custom_glyph_cache: CustomGlyphCache::new(),
}))))
}

View File

@@ -58,6 +58,7 @@ impl Translation {
}
pub struct I18n {
lang: String,
json_root_translated: serde_json::Value, // any language
json_root_fallback: serde_json::Value, // english
}
@@ -123,11 +124,16 @@ impl I18n {
.context("Translation file not valid JSON")?;
Ok(Self {
lang,
json_root_translated,
json_root_fallback,
})
}
pub fn get_lang(&self) -> &str {
&self.lang
}
pub fn translate(&mut self, translation_key_full: &str) -> Rc<str> {
let mut sections = translation_key_full.split(';');
let translation_key = sections.next().map_or(translation_key_full, |a| a);