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

52
src/shape_plan_cache.rs Normal file
View file

@ -0,0 +1,52 @@
use std::collections::hash_map::Entry;
use crate::{Font, HashMap};
/// Key for caching shape plans.
#[derive(Debug, Hash, PartialEq, Eq)]
struct ShapePlanKey {
font_id: fontdb::ID,
direction: rustybuzz::Direction,
script: rustybuzz::Script,
language: Option<rustybuzz::Language>,
}
/// A helper structure for caching rustybuzz shape plans.
#[derive(Default)]
pub struct ShapePlanCache(HashMap<ShapePlanKey, rustybuzz::ShapePlan>);
impl ShapePlanCache {
pub fn get(&mut self, font: &Font, buffer: &rustybuzz::UnicodeBuffer) -> &rustybuzz::ShapePlan {
let key = ShapePlanKey {
font_id: font.id(),
direction: buffer.direction(),
script: buffer.script(),
language: buffer.language(),
};
match self.0.entry(key) {
Entry::Occupied(occ) => occ.into_mut(),
Entry::Vacant(vac) => {
let ShapePlanKey {
direction,
script,
language,
..
} = vac.key();
let plan = rustybuzz::ShapePlan::new(
font.rustybuzz(),
*direction,
Some(*script),
language.as_ref(),
&[],
);
vac.insert(plan)
}
}
}
}
impl std::fmt::Debug for ShapePlanCache {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("ShapePlanCache").finish()
}
}