🎉 Implement font fallback to support multiple languages

This commit is contained in:
Elena Torro 2025-06-03 08:21:15 +02:00
parent 9733c41ae4
commit c40de5fb87
7 changed files with 179 additions and 38 deletions

View file

@ -1,4 +1,5 @@
use skia_safe::{self as skia, textlayout, Font, FontMgr};
use std::collections::HashSet;
use crate::shapes::{FontFamily, FontStyle};
use crate::uuid::Uuid;
@ -21,6 +22,7 @@ pub struct FontStore {
font_provider: textlayout::TypefaceFontProvider,
font_collection: textlayout::FontCollection,
debug_font: Font,
fallback_fonts: HashSet<String>,
}
impl FontStore {
@ -41,6 +43,7 @@ impl FontStore {
font_provider,
font_collection,
debug_font,
fallback_fonts: HashSet::new(),
}
}
@ -61,6 +64,7 @@ impl FontStore {
family: FontFamily,
font_data: &[u8],
is_emoji: bool,
is_fallback: bool,
) -> Result<(), String> {
if self.has_family(&family) {
return Ok(());
@ -80,6 +84,11 @@ impl FontStore {
self.font_provider.register_typeface(typeface, font_name);
self.font_collection.clear_caches();
if is_fallback {
self.fallback_fonts.insert(alias);
}
Ok(())
}
@ -87,6 +96,10 @@ impl FontStore {
let serialized = format!("{}", family);
self.font_provider.family_names().any(|x| x == serialized)
}
pub fn get_fallback(&self) -> &HashSet<String> {
&self.fallback_fonts
}
}
fn load_default_provider(font_mgr: &FontMgr) -> skia::textlayout::TypefaceFontProvider {