diff --git a/skrifa/src/outline/autohint/instance.rs b/skrifa/src/outline/autohint/instance.rs new file mode 100644 index 000000000..13f74966a --- /dev/null +++ b/skrifa/src/outline/autohint/instance.rs @@ -0,0 +1,32 @@ +//! Autohinting state for a font instance. + +use super::{super::OutlineGlyphCollection, style::GlyphStyleMap}; +use crate::MetadataProvider; +use alloc::sync::Arc; +use raw::TableProvider; + +/// Set of derived glyph styles that are used for automatic hinting. +/// +/// These are invariant per font so can be precomputed and reused for multiple +/// instances when requesting automatic hinting. +#[derive(Clone, Debug)] +pub struct GlyphStyles(Arc); + +impl GlyphStyles { + /// Precomputes the full set of glyph styles for the given outlines. + pub fn new(outlines: &OutlineGlyphCollection) -> Self { + if let Some(outlines) = outlines.common() { + let glyph_count = outlines + .font + .maxp() + .map(|maxp| maxp.num_glyphs() as u32) + .unwrap_or_default(); + Self(Arc::new(GlyphStyleMap::new( + glyph_count, + &outlines.font.charmap(), + ))) + } else { + Self(Default::default()) + } + } +} diff --git a/skrifa/src/outline/autohint/mod.rs b/skrifa/src/outline/autohint/mod.rs index f34603580..c382b5c73 100644 --- a/skrifa/src/outline/autohint/mod.rs +++ b/skrifa/src/outline/autohint/mod.rs @@ -6,7 +6,10 @@ mod axis; mod cycling; mod hint; +mod instance; mod latin; mod metrics; mod outline; mod style; + +pub use instance::GlyphStyles; diff --git a/skrifa/src/outline/autohint/style.rs b/skrifa/src/outline/autohint/style.rs index 276fbb820..312ebdba3 100644 --- a/skrifa/src/outline/autohint/style.rs +++ b/skrifa/src/outline/autohint/style.rs @@ -188,6 +188,16 @@ impl GlyphStyleMap { } } +impl Default for GlyphStyleMap { + fn default() -> Self { + Self { + styles: Default::default(), + metrics_map: [0xFF; MAX_STYLES], + metrics_count: 0, + } + } +} + /// Determines which algorithms the autohinter will use while generating /// metrics and processing a glyph outline. #[derive(Copy, Clone, PartialEq, Eq, Debug)] diff --git a/skrifa/src/outline/mod.rs b/skrifa/src/outline/mod.rs index 117441e47..6b394bbaf 100644 --- a/skrifa/src/outline/mod.rs +++ b/skrifa/src/outline/mod.rs @@ -93,6 +93,8 @@ pub mod error; pub mod pen; use common::OutlinesCommon; + +pub use autohint::GlyphStyles; pub use hint::{HintingInstance, HintingMode, LcdLayout}; use raw::FontRef; #[doc(inline)] @@ -582,6 +584,14 @@ impl<'a> OutlineGlyphCollection<'a> { Some((gid, glyph)) }) } + + pub(crate) fn common(&self) -> Option<&OutlinesCommon<'a>> { + match &self.kind { + OutlineCollectionKind::Glyf(glyf) => Some(&glyf.common), + OutlineCollectionKind::Cff(cff) => Some(&cff.common), + _ => None, + } + } } #[derive(Clone)]