Share cached glyph outline drawing

This commit is contained in:
Dylan M. Taylor
2026-11-15 16:20:29 +00:00
parent bd67a010f6
commit 4bc874ca79
3 changed files with 50 additions and 12 deletions
Generated
+12
View File
@@ -1005,6 +1005,17 @@ name = "craft-eps"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6d620e971e820b517b11a99cad040ebac670e1e2#6d620e971e820b517b11a99cad040ebac670e1e2"
[[package]]
name = "craft-font-catalog"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=14bccd4e09094820babc434d2681297be8873bd9#14bccd4e09094820babc434d2681297be8873bd9"
dependencies = [
"kurbo 0.13.1",
"skrifa 0.47.0",
"thiserror 2.0.21",
"windows-registry",
]
[[package]]
name = "craft-fonts-build"
version = "0.1.0"
@@ -6871,6 +6882,7 @@ dependencies = [
name = "vectorcraft-text"
version = "0.24.1"
dependencies = [
"craft-font-catalog",
"harfrust 0.13.3",
"icu_segmenter",
"kurbo 0.13.1",
+2
View File
@@ -9,6 +9,8 @@ description = "VectorCraft text: font database, shaping, layout, glyph outlines"
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 = "14bccd4e09094820babc434d2681297be8873bd9", default-features = false }
vectorcraft-geom = { workspace = true }
vectorcraft-doc = { workspace = true }
kurbo = { workspace = true }
+36 -12
View File
@@ -3,17 +3,14 @@ use super::PathBuf;
use super::build::{instanced, style_distance, style_italic, style_weight};
use super::cache::Catalog;
use super::face::{FALLBACK_FAMILY, FontFace, MAX_LIVE_INSTANCES, Variation, VariationAxis};
use super::glyph_pen::{FlipPen, svg_drawn};
use super::glyph_pen::svg_drawn;
#[cfg(not(target_arch = "wasm32"))]
use super::names::SYSTEM_FALLBACKS;
use super::names::{norm, norm_chars, script_families};
#[cfg(not(target_arch = "wasm32"))]
use super::scan::{enumerate_faces, font_files};
use super::traits::{Alias, FontMatch, FontTraits};
use super::{
Arc, BezPath, CANONICAL_CACHE_MAX, Color, ColourContent, DrawSettings, FontDb, GlyphId, GlyphImage, MetadataProvider, NEXT_ID, OUTLINE_CACHE_MAX,
Ordering, Size,
};
use super::{Arc, BezPath, CANONICAL_CACHE_MAX, Color, ColourContent, FontDb, GlyphImage, MetadataProvider, NEXT_ID, OUTLINE_CACHE_MAX, Ordering};
/// The most faces [`FontDb::postscript_faces`] gives, and the most families it loads.
const MAX_POSTSCRIPT_FACES: usize = 16;
@@ -566,13 +563,13 @@ impl FontDb {
if let Some(p) = self.outlines.lock().unwrap_or_else(|e| e.into_inner()).get(&key) {
return p.clone();
}
let mut pen = FlipPen(BezPath::new());
if let Some(f) = face.skrifa()
&& let Some(g) = f.outline_glyphs().get(GlyphId::new(gid))
{
let _ = g.draw(DrawSettings::unhinted(Size::unscaled(), face.location()), &mut pen);
}
let p = Arc::new(pen.0);
let path = craft_font_catalog::FontOutline::from_bytes(face.data(), face.index(), face.variations())
.and_then(|font| font.outline_recovering(gid))
// The old pen kept a partial path when drawing failed. Keep that behavior while
// the shared implementation bounds the number of elements from untrusted fonts.
.map(|(path, _status)| kurbo::Affine::scale_non_uniform(1.0, -1.0) * path)
.unwrap_or_default();
let p = Arc::new(path);
let mut cache = self.outlines.lock().unwrap_or_else(|e| e.into_inner());
if cache.len() >= OUTLINE_CACHE_MAX {
cache.clear();
@@ -634,3 +631,30 @@ impl FontDb {
content
}
}
#[cfg(test)]
mod shared_outline_tests {
use super::super::glyph_pen::FlipPen;
use super::*;
use skrifa::instance::Size;
use skrifa::outline::DrawSettings;
use skrifa::{GlyphId, MetadataProvider};
#[test]
fn shared_outlines_match_original_pen_for_variable_instances() {
let db = FontDb::with_font_dirs(vec![]);
let Some(bytes) = crate::test_fonts::variable_font() else { return };
assert!(db.add_font(bytes) > 0);
let face = db.face(crate::test_fonts::VARIABLE_FAMILY, "Bold").expect("named instance is registered");
let f = face.skrifa().expect("font parses");
for ch in ['A', 'V', ' ', 'a'] {
let Some(glyph) = f.charmap().map(ch) else { continue };
let mut old = FlipPen(BezPath::new());
if let Some(outline) = f.outline_glyphs().get(GlyphId::new(glyph.to_u32())) {
// Match the prior production behavior, including a partial path on error.
let _ = outline.draw(DrawSettings::unhinted(Size::unscaled(), face.location()), &mut old);
}
assert_eq!(*db.outline(&face, glyph.to_u32()), old.0, "glyph {ch}");
}
}
}