Skip loading fonts that were previously loaded

This commit is contained in:
Héctor Ramón Jiménez 2024-12-14 04:09:51 +01:00
parent 2cf4abf25b
commit bfa722c662
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -16,6 +16,7 @@ use crate::core::{Color, Pixels, Point, Rectangle, Size, Transformation};
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::HashSet;
use std::sync::{Arc, RwLock, Weak}; use std::sync::{Arc, RwLock, Weak};
/// A text primitive. /// A text primitive.
@ -169,6 +170,7 @@ pub fn font_system() -> &'static RwLock<FontSystem> {
include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(), include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(),
)), )),
]), ]),
loaded_fonts: HashSet::new(),
version: Version::default(), version: Version::default(),
}) })
}) })
@ -178,6 +180,7 @@ pub fn font_system() -> &'static RwLock<FontSystem> {
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct FontSystem { pub struct FontSystem {
raw: cosmic_text::FontSystem, raw: cosmic_text::FontSystem,
loaded_fonts: HashSet<usize>,
version: Version, version: Version,
} }
@ -189,6 +192,14 @@ impl FontSystem {
/// Loads a font from its bytes. /// Loads a font from its bytes.
pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) { pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
if let Cow::Borrowed(bytes) = bytes {
let address = bytes.as_ptr() as usize;
if !self.loaded_fonts.insert(address) {
return;
}
}
let _ = self.raw.db_mut().load_font_source( let _ = self.raw.db_mut().load_font_source(
cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())), cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())),
); );