Skip to content

Commit

Permalink
[skrifa] autohint: public GlyphStyles type (#1086)
Browse files Browse the repository at this point in the history
Exposes the glyph style map as a public type and wrapped in an `Arc` to make it shareable among multiple autohinting instances.
  • Loading branch information
dfrg authored Aug 16, 2024
1 parent 7da5d21 commit 50ce82f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
32 changes: 32 additions & 0 deletions skrifa/src/outline/autohint/instance.rs
Original file line number Diff line number Diff line change
@@ -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<GlyphStyleMap>);

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())
}
}
}
3 changes: 3 additions & 0 deletions skrifa/src/outline/autohint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
mod axis;
mod cycling;
mod hint;
mod instance;
mod latin;
mod metrics;
mod outline;
mod style;

pub use instance::GlyphStyles;
10 changes: 10 additions & 0 deletions skrifa/src/outline/autohint/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
10 changes: 10 additions & 0 deletions skrifa/src/outline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 50ce82f

Please sign in to comment.