From e0ae465f918cd1cffca3a8239547dcf8166d3f77 Mon Sep 17 00:00:00 2001 From: Mohammad AlSaleh Date: Wed, 24 Jan 2024 08:42:10 +0300 Subject: [PATCH] Only use relevant info from `Attrs` as a key in `font_matches_cache` `Attrs` contains info like color and metadata which are not relevant to font matching. So, add a new struct `FontMatchAttrs` which only contains the relevant info, and use it as a key in `FontSystem`'s `font_matches_cache`. Signed-off-by: Mohammad AlSaleh --- src/attrs.rs | 20 ++++++++++++++++++++ src/font/system.rs | 6 +++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/attrs.rs b/src/attrs.rs index 5ef4003..1be3c46 100644 --- a/src/attrs.rs +++ b/src/attrs.rs @@ -191,6 +191,26 @@ impl<'a> Attrs<'a> { } } +/// Font-specific part of [`Attrs`] to be used for matching +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub struct FontMatchAttrs { + family: FamilyOwned, + stretch: Stretch, + style: Style, + weight: Weight, +} + +impl<'a> From> for FontMatchAttrs { + fn from(attrs: Attrs<'a>) -> Self { + Self { + family: FamilyOwned::new(attrs.family), + stretch: attrs.stretch, + style: attrs.style, + weight: attrs.weight, + } + } +} + /// An owned version of [`Attrs`] #[derive(Clone, Debug, Eq, Hash, PartialEq)] pub struct AttrsOwned { diff --git a/src/font/system.rs b/src/font/system.rs index ad85db1..82f6fd8 100644 --- a/src/font/system.rs +++ b/src/font/system.rs @@ -1,4 +1,4 @@ -use crate::{Attrs, AttrsOwned, Font, HashMap, ShapePlanCache}; +use crate::{Attrs, Font, FontMatchAttrs, HashMap, ShapePlanCache}; use alloc::string::String; use alloc::sync::Arc; use alloc::vec::Vec; @@ -27,7 +27,7 @@ pub struct FontSystem { font_cache: HashMap>>, /// Cache for font matches. - font_matches_cache: HashMap>>, + font_matches_cache: HashMap>>, /// Cache for rustybuzz shape plans. shape_plan_cache: ShapePlanCache, @@ -141,7 +141,7 @@ impl FontSystem { self.font_matches_cache //TODO: do not create AttrsOwned unless entry does not already exist - .entry(AttrsOwned::new(attrs)) + .entry(attrs.into()) .or_insert_with(|| { #[cfg(all(feature = "std", not(target_arch = "wasm32")))] let now = std::time::Instant::now();