From 553c86719ce9d57f01bcfb6442ac68ac58cd65de Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:14:57 +0000 Subject: [PATCH] Share catalog face metrics and glyph outlines --- Cargo.lock | 1 + crates/font-catalog/Cargo.toml | 1 + crates/font-catalog/src/lib.rs | 2 + crates/font-catalog/src/outline.rs | 133 +++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 crates/font-catalog/src/outline.rs diff --git a/Cargo.lock b/Cargo.lock index 9fb46057c..9424cf3fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1761,6 +1761,7 @@ name = "craft-font-catalog" version = "0.1.0" dependencies = [ "fontique", + "kurbo 0.13.1", "skrifa 0.47.0", "thiserror 2.0.21", "windows-registry", diff --git a/crates/font-catalog/Cargo.toml b/crates/font-catalog/Cargo.toml index a95e1c28f..1f0830a63 100644 --- a/crates/font-catalog/Cargo.toml +++ b/crates/font-catalog/Cargo.toml @@ -15,6 +15,7 @@ default = ["system"] system = ["dep:fontique"] [dependencies] +kurbo = { workspace = true } skrifa = { version = "0.47", default-features = false, features = ["std"] } thiserror = { workspace = true } diff --git a/crates/font-catalog/src/lib.rs b/crates/font-catalog/src/lib.rs index c787d3ece..7da4ca310 100644 --- a/crates/font-catalog/src/lib.rs +++ b/crates/font-catalog/src/lib.rs @@ -23,6 +23,7 @@ mod catalog; mod env; mod fontconfig; +mod outline; mod registry; mod scan; mod suitcase; @@ -31,6 +32,7 @@ pub mod system; pub use catalog::{Catalog, FontData, FontFileError, Limits, Lister, LoadedFace, Match, Watch}; pub use env::{FontEnv, Os, font_dirs, font_store_dir, office_font_dirs}; pub use fontconfig::fontconfig_dirs; +pub use outline::{FontOutline, OutlineError}; pub use scan::{FaceInfo, LocalizedName, Source, is_font_file, style_italic, style_weight, style_width}; #[cfg(test)] diff --git a/crates/font-catalog/src/outline.rs b/crates/font-catalog/src/outline.rs new file mode 100644 index 000000000..cd27e0c53 --- /dev/null +++ b/crates/font-catalog/src/outline.rs @@ -0,0 +1,133 @@ +//! Unhinted glyph outlines and metrics for catalog faces. + +use kurbo::BezPath; +use skrifa::instance::{Location, Size}; +use skrifa::outline::{DrawSettings, OutlinePen}; +use skrifa::{FontRef, GlyphId, MetadataProvider, Tag}; + +use crate::{FontData, LoadedFace}; + +/// A parsed face and its variation location. Coordinates use font design units, with y up. +/// The bytes can come from a catalog or any other source, including a collection face. +pub struct FontOutline<'a> { + font: FontRef<'a>, + location: Location, +} + +/// Why a font or glyph outline could not be read. +#[derive(Debug, thiserror::Error)] +pub enum OutlineError { + /// The face index or font data is invalid. + #[error("invalid font face")] + InvalidFace, + /// The glyph has no outline in this face. + #[error("glyph has no outline")] + MissingGlyph, + /// The glyph's outline is malformed. + #[error("cannot draw glyph outline: {0}")] + Draw(#[from] skrifa::outline::DrawError), +} + +impl<'a> FontOutline<'a> { + /// Parse the given face and select axis values in user units. Unknown axes are ignored and + /// values are clamped to their axis ranges by the font parser. + pub fn new(data: &'a FontData, variations: &[([u8; 4], f32)]) -> Result { + Self::from_bytes(&data.bytes, data.index, variations) + } + + /// Parse borrowed bytes and a collection face index without copying the font. + pub fn from_bytes(bytes: &'a [u8], index: u32, variations: &[([u8; 4], f32)]) -> Result { + let font = FontRef::from_index(bytes, index).map_err(|_| OutlineError::InvalidFace)?; + let location = font.axes().location(variations.iter().map(|(tag, value)| (Tag::new(tag), *value))); + Ok(Self { font, location }) + } + + /// Parse a catalog face, retaining the axis settings of a named instance. + pub fn from_loaded(face: &'a LoadedFace) -> Result { + Self::new(&face.data, &face.info.variations) + } + + /// Font-wide metrics in unscaled design units at this variation location. + pub fn metrics(&self) -> skrifa::metrics::Metrics { + self.font.metrics(Size::unscaled(), &self.location) + } + + /// Resolve a Unicode scalar to a glyph identifier. + pub fn glyph_for(&self, character: char) -> Option { + self.font.charmap().map(character).map(|id| id.to_u32()) + } + + /// Draw a glyph in design units. Empty glyphs such as spaces can produce an empty path. + pub fn outline(&self, glyph: u32) -> Result { + let outline = self.font.outline_glyphs().get(GlyphId::new(glyph)).ok_or(OutlineError::MissingGlyph)?; + let mut pen = PathPen(BezPath::new()); + outline.draw(DrawSettings::unhinted(Size::unscaled(), &self.location), &mut pen)?; + Ok(pen.0) + } +} + +struct PathPen(BezPath); +impl OutlinePen for PathPen { + fn move_to(&mut self, x: f32, y: f32) { + self.0.move_to((f64::from(x), f64::from(y))); + } + fn line_to(&mut self, x: f32, y: f32) { + self.0.line_to((f64::from(x), f64::from(y))); + } + fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) { + self.0.quad_to((f64::from(x1), f64::from(y1)), (f64::from(x), f64::from(y))); + } + fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) { + self.0.curve_to((f64::from(x1), f64::from(y1)), (f64::from(x2), f64::from(y2)), (f64::from(x), f64::from(y))); + } + fn close(&mut self) { + self.0.close_path(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use kurbo::PathEl; + use std::sync::Arc; + + #[test] + fn rejects_invalid_face_and_collection_index() { + let data = FontData { bytes: Arc::from(&b"not a font"[..]), index: 0 }; + assert!(matches!(FontOutline::new(&data, &[]), Err(OutlineError::InvalidFace))); + } + + #[test] + fn pen_preserves_geometry_and_upward_y() { + let mut pen = PathPen(BezPath::new()); + pen.move_to(1.0, 2.0); + pen.line_to(3.0, 4.0); + pen.quad_to(5.0, 6.0, 7.0, 8.0); + pen.curve_to(9.0, 10.0, 11.0, 12.0, 13.0, 14.0); + pen.close(); + assert_eq!( + pen.0.elements(), + &[ + PathEl::MoveTo((1.0, 2.0).into()), + PathEl::LineTo((3.0, 4.0).into()), + PathEl::QuadTo((5.0, 6.0).into(), (7.0, 8.0).into()), + PathEl::CurveTo((9.0, 10.0).into(), (11.0, 12.0).into(), (13.0, 14.0).into()), + PathEl::ClosePath, + ] + ); + } + + #[test] + fn reads_craft_font_when_available() { + let Ok(dir) = std::env::var("CRAFT_FONTS_DIR") else { return }; + let bytes = std::fs::read(std::path::Path::new(&dir).join("fonts/noto-sans-hebrew/NotoSansHebrew-Regular.ttf")).unwrap(); + let data = FontData { bytes: bytes.into(), index: 0 }; + let face = FontOutline::new(&data, &[]).unwrap(); + assert!(face.metrics().units_per_em > 0); + let glyph = face.glyph_for('א').unwrap(); + assert!(!face.outline(glyph).unwrap().elements().is_empty()); + assert!(matches!(face.outline(u32::MAX), Err(OutlineError::MissingGlyph))); + let invalid_index = FontData { bytes: data.bytes.clone(), index: u32::MAX }; + assert!(matches!(FontOutline::new(&invalid_index, &[]), Err(OutlineError::InvalidFace))); + } +}