diff options
author | 2024-12-14 04:09:51 +0100 | |
---|---|---|
committer | 2024-12-14 04:09:51 +0100 | |
commit | bfa722c662201a516246483a2bff84ebbb1fc7e6 (patch) | |
tree | 6a2e9a5ef06ce740656fae0d8738b6f93cabc737 /graphics | |
parent | 2cf4abf25bb5702635c19a22353399db8cef7be3 (diff) | |
download | iced-bfa722c662201a516246483a2bff84ebbb1fc7e6.tar.gz iced-bfa722c662201a516246483a2bff84ebbb1fc7e6.tar.bz2 iced-bfa722c662201a516246483a2bff84ebbb1fc7e6.zip |
Skip loading fonts that were previously loaded
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/src/text.rs | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/graphics/src/text.rs b/graphics/src/text.rs index f954e318..ca3fc6fc 100644 --- a/graphics/src/text.rs +++ b/graphics/src/text.rs @@ -16,6 +16,7 @@ use crate::core::{Color, Pixels, Point, Rectangle, Size, Transformation}; use once_cell::sync::OnceCell; use std::borrow::Cow; +use std::collections::HashSet; use std::sync::{Arc, RwLock, Weak}; /// A text primitive. @@ -169,6 +170,7 @@ pub fn font_system() -> &'static RwLock<FontSystem> { include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(), )), ]), + loaded_fonts: HashSet::new(), version: Version::default(), }) }) @@ -178,6 +180,7 @@ pub fn font_system() -> &'static RwLock<FontSystem> { #[allow(missing_debug_implementations)] pub struct FontSystem { raw: cosmic_text::FontSystem, + loaded_fonts: HashSet<usize>, version: Version, } @@ -189,6 +192,14 @@ impl FontSystem { /// Loads a font from its bytes. 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( cosmic_text::fontdb::Source::Binary(Arc::new(bytes.into_owned())), ); |