mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
Share per-face shaping plan cache
This commit is contained in:
1 parent
70cfdb2817
commit
f25ce39493
3 files changed
+73
No files matched your search
@@ -27,6 +27,7 @@ mod fontconfig;
|
||||
mod ligature;
|
||||
mod line_break;
|
||||
mod outline;
|
||||
mod plan_cache;
|
||||
mod registry;
|
||||
mod scan;
|
||||
mod segments;
|
||||
@@ -42,6 +43,7 @@ pub use fontconfig::fontconfig_dirs;
|
||||
pub use ligature::ligature_caret_stops;
|
||||
pub use line_break::settle_line_break;
|
||||
pub use outline::{FontOutline, GlyphPen, OutlineError};
|
||||
pub use plan_cache::ShapePlanCache;
|
||||
pub use scan::{FaceInfo, LocalizedName, Source, is_font_file, style_italic, style_weight, style_width};
|
||||
pub use segments::SegmentBoundary;
|
||||
pub use shape::{RunGlyph, RunInput, ShapeRunError, shape_run};
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
//! Per-face bounded shaping plan reuse.
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
/// A cache for one font face and variation instance. Do not share it across faces or instances.
|
||||
/// Plans are created outside the lock, then checked again to avoid retaining duplicates.
|
||||
#[derive(Default)]
|
||||
pub struct ShapePlanCache {
|
||||
plans: Mutex<Vec<Arc<harfrust::ShapePlan>>>,
|
||||
}
|
||||
|
||||
impl ShapePlanCache {
|
||||
/// Maximum number of plans retained; the oldest is evicted when this is reached.
|
||||
pub const CAPACITY: usize = 32;
|
||||
|
||||
/// Returns a matching plan or builds and retains one for this face.
|
||||
pub fn get_or_create(
|
||||
&self,
|
||||
shaper: &harfrust::Shaper<'_>,
|
||||
instance: Option<&harfrust::ShaperInstance>,
|
||||
direction: harfrust::Direction,
|
||||
script: Option<harfrust::Script>,
|
||||
language: Option<&harfrust::Language>,
|
||||
features: &[harfrust::Feature],
|
||||
) -> Arc<harfrust::ShapePlan> {
|
||||
let key = harfrust::ShapePlanKey::new(script, direction).language(language).instance(instance).features(features);
|
||||
let kept = |plans: &[Arc<harfrust::ShapePlan>]| plans.iter().rev().find(|p| key.matches(p)).cloned();
|
||||
if let Some(plan) = kept(&self.plans.lock().unwrap_or_else(|e| e.into_inner())) {
|
||||
return plan;
|
||||
}
|
||||
let plan = Arc::new(harfrust::ShapePlan::new(shaper, direction, script, language, features));
|
||||
let mut plans = self.plans.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if let Some(made) = kept(&plans) {
|
||||
return made;
|
||||
}
|
||||
if plans.len() >= Self::CAPACITY {
|
||||
plans.remove(0);
|
||||
}
|
||||
plans.push(plan.clone());
|
||||
plan
|
||||
}
|
||||
|
||||
/// Number of retained plans.
|
||||
pub fn len(&self) -> usize {
|
||||
self.plans.lock().unwrap_or_else(|e| e.into_inner()).len()
|
||||
}
|
||||
|
||||
/// Whether this cache contains no plans.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
}
|
||||
@@ -568,3 +568,22 @@ fn a_read_is_capped_where_the_file_has_grown() {
|
||||
assert!(matches!(read_capped(&path.with_extension("missing"), 9), Err(FontFileError::Io(_))));
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_plan_cache_reuses_and_evicts_plans() {
|
||||
let data = font(&spec("Test", "Regular", 400));
|
||||
let face = harfrust::FontRef::from_index(&data, 0).unwrap();
|
||||
let shaper_data = harfrust::ShaperData::new(&face);
|
||||
let shaper = shaper_data.shaper(&face).build();
|
||||
let cache = ShapePlanCache::default();
|
||||
let make = |language: &harfrust::Language| cache.get_or_create(&shaper, None, harfrust::Direction::LeftToRight, None, Some(language), &[]);
|
||||
let languages: Vec<harfrust::Language> = ('a'..='z').flat_map(|a| ['a', 'b'].map(move |b| format!("{a}{b}").parse().unwrap())).collect();
|
||||
let first = make(&languages[0]);
|
||||
assert!(Arc::ptr_eq(&first, &make(&languages[0])));
|
||||
for language in languages.iter().skip(1) {
|
||||
make(language);
|
||||
}
|
||||
assert_eq!(cache.len(), ShapePlanCache::CAPACITY);
|
||||
assert!(!Arc::ptr_eq(&first, &make(&languages[0])));
|
||||
assert_eq!(cache.len(), ShapePlanCache::CAPACITY);
|
||||
}
|
||||
Reference in new issue
Block a user