♻️ Refactor Custom Fonts allocations (#6146)

* ♻️ Refactor Custom Fonts allocations

* 💄 Remove commented code

---------

Co-authored-by: Belén Albeza <belen@hey.com>
This commit is contained in:
Aitor Moreno 2025-03-25 15:34:11 +01:00 committed by GitHub
parent 065b50f5a2
commit 6eb686c06b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,7 +8,7 @@ pub static DEFAULT_FONT: &'static str = "robotomono-regular";
pub static DEFAULT_EMOJI_FONT: &'static str = "noto-color-emoji";
pub struct FontStore {
// TODO: we should probably have just one of those
font_mgr: FontMgr,
font_provider: textlayout::TypefaceFontProvider,
font_collection: textlayout::FontCollection,
debug_font: Font,
@ -16,23 +16,24 @@ pub struct FontStore {
impl FontStore {
pub fn new() -> Self {
let font_mgr = FontMgr::new();
let mut font_provider = skia::textlayout::TypefaceFontProvider::new();
let default_font = skia::FontMgr::default()
let default_font = font_mgr
.new_from_data(DEFAULT_FONT_BYTES, None)
.expect("Failed to load font");
font_provider.register_typeface(default_font, DEFAULT_FONT);
let emoji_font = skia::FontMgr::default()
let emoji_font = font_mgr
.new_from_data(EMOJI_FONT_BYTES, None)
.expect("Failed to load font");
font_provider.register_typeface(emoji_font, DEFAULT_EMOJI_FONT);
let mut font_collection = skia::textlayout::FontCollection::new();
font_collection.set_default_font_manager(FontMgr::default(), None);
font_collection.set_dynamic_font_manager(FontMgr::from(font_provider.clone()));
font_collection.set_default_font_manager(FontMgr::from(font_provider.clone()), None);
let debug_typeface = font_provider
.match_family_style("robotomono-regular", skia::FontStyle::default())
@ -41,6 +42,7 @@ impl FontStore {
let debug_font = skia::Font::new(debug_typeface, 10.0);
Self {
font_mgr,
font_provider,
font_collection,
debug_font,
@ -65,14 +67,15 @@ impl FontStore {
}
let alias = format!("{}", family);
let typeface = skia::FontMgr::default()
let typeface = self
.font_mgr
.new_from_data(font_data, None)
.ok_or("Failed to create typeface")?;
self.font_provider
.register_typeface(typeface, alias.as_str());
self.refresh_font_collection();
self.font_collection.clear_caches();
Ok(())
}
@ -81,12 +84,4 @@ impl FontStore {
let serialized = format!("{}", family);
self.font_provider.family_names().any(|x| x == serialized)
}
fn refresh_font_collection(&mut self) {
self.font_collection = skia::textlayout::FontCollection::new();
self.font_collection
.set_default_font_manager(FontMgr::default(), None);
self.font_collection
.set_dynamic_font_manager(FontMgr::from(self.font_provider.clone()));
}
}