From 6572909ab5b004176f6d261b67b4caa99f1f54bb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 12 Dec 2024 03:14:40 +0100 Subject: Embed and use Fira Sans as default font when testing --- graphics/src/text.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'graphics') diff --git a/graphics/src/text.rs b/graphics/src/text.rs index feb9932a..e6f06b61 100644 --- a/graphics/src/text.rs +++ b/graphics/src/text.rs @@ -149,8 +149,8 @@ impl Text { /// It is loaded as part of the default fonts in Wasm builds. /// /// [Fira Sans]: https://mozilla.github.io/Fira/ -#[cfg(all(target_arch = "wasm32", feature = "fira-sans"))] -pub const FIRA_SANS_REGULAR: &'static [u8] = +#[cfg(feature = "fira-sans")] +pub const FIRA_SANS_REGULAR: &[u8] = include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(); /// Returns the global [`FontSystem`]. @@ -163,7 +163,7 @@ pub fn font_system() -> &'static RwLock { cosmic_text::fontdb::Source::Binary(Arc::new( include_bytes!("../fonts/Iced-Icons.ttf").as_slice(), )), - #[cfg(all(target_arch = "wasm32", feature = "fira-sans"))] + #[cfg(feature = "fira-sans")] cosmic_text::fontdb::Source::Binary(Arc::new( include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(), )), -- cgit From 2cf4abf25bb5702635c19a22353399db8cef7be3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Dec 2024 03:49:24 +0100 Subject: Support custom renderers in `iced_test` through `renderer::Headless` trait --- graphics/src/text.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/text.rs b/graphics/src/text.rs index e6f06b61..f954e318 100644 --- a/graphics/src/text.rs +++ b/graphics/src/text.rs @@ -146,7 +146,8 @@ impl Text { /// The regular variant of the [Fira Sans] font. /// -/// It is loaded as part of the default fonts in Wasm builds. +/// It is loaded as part of the default fonts when the `fira-sans` +/// feature is enabled. /// /// [Fira Sans]: https://mozilla.github.io/Fira/ #[cfg(feature = "fira-sans")] -- cgit From bfa722c662201a516246483a2bff84ebbb1fc7e6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Dec 2024 04:09:51 +0100 Subject: Skip loading fonts that were previously loaded --- graphics/src/text.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'graphics') 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 { 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 { #[allow(missing_debug_implementations)] pub struct FontSystem { raw: cosmic_text::FontSystem, + loaded_fonts: HashSet, 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())), ); -- cgit