mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 17:05:55 +01:00
Use shared shaping plan cache
This commit is contained in:
1 parent
d02b41414a
commit
624d98186e
4 files changed
+7
-25
No files matched your search
Generated
+1
-1
@@ -1008,7 +1008,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=6d620e971e820b517b11a9
|
||||
[[package]]
|
||||
name = "craft-font-catalog"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=70cfdb281775d226c2af592e36b1063e124a17a9#70cfdb281775d226c2af592e36b1063e124a17a9"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=f25ce3949366a04f63d30b93def0a43b32ffba99#f25ce3949366a04f63d30b93def0a43b32ffba99"
|
||||
dependencies = [
|
||||
"harfrust 0.13.3",
|
||||
"kurbo 0.13.1",
|
||||
|
||||
@@ -10,7 +10,7 @@ workspace = true
|
||||
|
||||
[dependencies]
|
||||
# Pending shared extraction; pin the immutable official PR head until it merges.
|
||||
craft-font-catalog = { git = "https://github.com/storytold/craft-libs", rev = "70cfdb281775d226c2af592e36b1063e124a17a9", default-features = false }
|
||||
craft-font-catalog = { git = "https://github.com/storytold/craft-libs", rev = "f25ce3949366a04f63d30b93def0a43b32ffba99", default-features = false }
|
||||
vectorcraft-geom = { workspace = true }
|
||||
vectorcraft-doc = { workspace = true }
|
||||
kurbo = { workspace = true }
|
||||
|
||||
@@ -72,7 +72,7 @@ pub(super) fn make_face(bytes: FontBytes, index: u32, spec: FaceStyle, path: Opt
|
||||
bytes,
|
||||
index,
|
||||
path,
|
||||
plans: std::sync::Mutex::new(Vec::new()),
|
||||
plans: craft_font_catalog::ShapePlanCache::default(),
|
||||
ideographic_centre: std::sync::OnceLock::new(),
|
||||
icf_margins: std::sync::OnceLock::new(),
|
||||
path_align: std::sync::OnceLock::new(),
|
||||
|
||||
@@ -98,9 +98,6 @@ pub(super) const MAX_AXES: usize = 64;
|
||||
|
||||
pub(super) const MAX_INSTANCES: usize = 512;
|
||||
|
||||
/// The most shaping plans a face keeps ([`FontFace::shape_plan`]); past it the oldest is dropped.
|
||||
const MAX_PLANS: usize = 32;
|
||||
|
||||
/// One axis of a variable font's design space, as its `fvar` table lists it: the tag (`wght`), its
|
||||
/// name in the font's name table (`Weight`; the tag when it has none) and its range and default,
|
||||
/// in the axis's own units.
|
||||
@@ -188,7 +185,7 @@ pub struct FontFace {
|
||||
/// [`Self::variations`] for the shaper (advances, kerning, feature variations).
|
||||
pub(crate) instance: Option<harfrust::ShaperInstance>,
|
||||
/// The shaping plans made for this face, newest last ([`Self::shape_plan`]).
|
||||
pub(super) plans: std::sync::Mutex<Vec<Arc<harfrust::ShapePlan>>>,
|
||||
pub(super) plans: craft_font_catalog::ShapePlanCache,
|
||||
/// [`Self::ideographic_centre`], read once: layout asks for it per glyph.
|
||||
pub(super) ideographic_centre: std::sync::OnceLock<f64>,
|
||||
/// [`Self::icf_margins`], read once.
|
||||
@@ -262,7 +259,7 @@ impl FontFace {
|
||||
}
|
||||
/// The shaping plan for a buffer of `direction`, `script` and `language` shaped with
|
||||
/// `features` by `shaper` (made from this face at [`Self::instance`]): the plan made earlier for
|
||||
/// the same properties, else a new one, which the face keeps for later (at most [`MAX_PLANS`]).
|
||||
/// the same properties, else a new one, which the face keeps for later (at most 32).
|
||||
/// A plan depends on these properties alone, so text shaped with it comes out as with a plan
|
||||
/// made for it.
|
||||
pub(crate) fn shape_plan(
|
||||
@@ -277,27 +274,12 @@ impl FontFace {
|
||||
if !crate::test_fonts::keeps_shape_plans() {
|
||||
return Arc::new(harfrust::ShapePlan::new(shaper, direction, script, language, features));
|
||||
}
|
||||
let key = harfrust::ShapePlanKey::new(script, direction).language(language).instance(self.instance.as_ref()).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;
|
||||
}
|
||||
// Made without the lock held: other threads keep shaping with this face meanwhile.
|
||||
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() >= MAX_PLANS {
|
||||
plans.remove(0);
|
||||
}
|
||||
plans.push(plan.clone());
|
||||
plan
|
||||
self.plans.get_or_create(shaper, self.instance.as_ref(), direction, script, language, features)
|
||||
}
|
||||
/// Tests: how many shaping plans the face holds.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn plans_kept(&self) -> usize {
|
||||
self.plans.lock().unwrap_or_else(|e| e.into_inner()).len()
|
||||
self.plans.len()
|
||||
}
|
||||
/// The face's index in its font file (collections hold several).
|
||||
pub(crate) fn index(&self) -> u32 {
|
||||
|
||||
Reference in new issue
Block a user