Cache rustybuzz shape plans.

This commit is contained in:
David Stern 2023-12-18 18:10:09 -05:00
parent 94e6cdefda
commit 73acfb0962
7 changed files with 98 additions and 17 deletions

View file

@ -6,7 +6,7 @@ use alloc::vec::Vec;
use fontdb::Family;
use unicode_script::Script;
use crate::{Font, FontSystem};
use crate::{Font, FontSystem, ShapePlanCache};
use self::platform::*;
@ -103,6 +103,10 @@ impl<'a> FontFallbackIter<'a> {
}
}
pub fn shape_plan_cache(&mut self) -> &mut ShapePlanCache {
self.font_system.shape_plan_cache()
}
fn face_contains_family(&self, id: fontdb::ID, family_name: &str) -> bool {
if let Some(face) = self.font_system.db().face(id) {
face.families.iter().any(|(name, _)| name == family_name)

View file

@ -1,17 +1,10 @@
use crate::{Attrs, AttrsOwned, Font};
use crate::{Attrs, AttrsOwned, Font, HashMap, ShapePlanCache};
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::fmt;
use core::ops::{Deref, DerefMut};
type BuildHasher = core::hash::BuildHasherDefault<rustc_hash::FxHasher>;
#[cfg(feature = "std")]
type HashMap<K, V> = std::collections::HashMap<K, V, BuildHasher>;
#[cfg(not(feature = "std"))]
type HashMap<K, V> = hashbrown::HashMap<K, V, BuildHasher>;
// re-export fontdb and rustybuzz
pub use fontdb;
pub use rustybuzz;
@ -29,6 +22,9 @@ pub struct FontSystem {
/// Cache for font matches.
font_matches_cache: HashMap<AttrsOwned, Arc<Vec<fontdb::ID>>>,
/// Cache for rustybuzz shape plans.
shape_plan_cache: ShapePlanCache,
}
impl fmt::Debug for FontSystem {
@ -74,8 +70,9 @@ impl FontSystem {
Self {
locale,
db,
font_cache: HashMap::default(),
font_matches_cache: HashMap::default(),
font_cache: Default::default(),
font_matches_cache: Default::default(),
shape_plan_cache: ShapePlanCache::default(),
}
}
@ -89,6 +86,11 @@ impl FontSystem {
&self.db
}
/// Get the shape plan cache.
pub(crate) fn shape_plan_cache(&mut self) -> &mut ShapePlanCache {
&mut self.shape_plan_cache
}
/// Get a mutable reference to the database.
pub fn db_mut(&mut self) -> &mut fontdb::Database {
self.font_matches_cache.clear();