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 <CE.Mohammad.AlSaleh@gmail.com>
This commit is contained in:
Mohammad AlSaleh 2024-01-24 08:42:10 +03:00 committed by Jeremy Soller
parent 235ec02a20
commit e0ae465f91
2 changed files with 23 additions and 3 deletions

View file

@ -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<Attrs<'a>> 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 {

View file

@ -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<fontdb::ID, Option<Arc<Font>>>,
/// Cache for font matches.
font_matches_cache: HashMap<AttrsOwned, Arc<Vec<FontMatchKey>>>,
font_matches_cache: HashMap<FontMatchAttrs, Arc<Vec<FontMatchKey>>>,
/// 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();