Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[skrifa] restore outline example #771

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions skrifa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! Skrifa is a mid level library that provides access to various types
//! of [`metadata`](MetadataProvider) contained in a font as well as support
//! for [`scaling`](scale) (extraction) of glyph outlines.
//! for loading glyph [`outlines`](outline).
//!
//! It is described as "mid level" because the library is designed to sit
//! above low level font parsing (provided by [`read-fonts`](https://crates.io/crates/read-fonts))
Expand Down Expand Up @@ -32,7 +32,6 @@ mod variation;

#[doc(inline)]
pub use outline::{OutlineGlyph, OutlineGlyphCollection};

pub use variation::{Axis, AxisCollection, NamedInstance, NamedInstanceCollection};

/// Useful collection of common types suitable for glob importing.
Expand All @@ -45,7 +44,10 @@ pub mod prelude {
};
}

pub use read_fonts::types::{GlyphId, Tag};
pub use read_fonts::{
types::{GlyphId, Tag},
FontRef,
};

#[doc(inline)]
pub use provider::MetadataProvider;
Expand Down
78 changes: 78 additions & 0 deletions skrifa/src/outline/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,82 @@
//! Loading, scaling and hinting of glyph outlines.
//!
//! This module provides support for retrieving (optinally scaled and hinted)
//! glyph outlines in the form of vector paths.
//!
//! # Drawing a glyph
//!
//! Generating SVG style path data for a character (this assumes a local
//! variable `font` of type [`FontRef`](crate::FontRef)):
//!
//! ```rust
//! use skrifa::{
//! instance::{LocationRef, Size},
//! outline::{DrawSettings, OutlinePen},
//! FontRef, MetadataProvider,
//! };
//!
//! # fn wrapper(font: FontRef) {
//! // First, grab the set of outline glyphs from the font.
//! let outlines = font.outline_glyphs();
//!
//! // Find the glyph identifier for our character.
//! let glyph_id = font.charmap().map('Q').unwrap();
//!
//! // Grab the outline glyph.
//! let glyph = outlines.get(glyph_id).unwrap();
//!
//! // Define how we want the glyph to be drawn. This creates
//! // settings for an instance without hinting at a size of
//! // 16px with no variations applied.
//! let settings = DrawSettings::unhinted(Size::new(16.0), LocationRef::default());
//!
//! // Alternatively, we can apply variations like so:
//! let var_location = font.axes().location(&[("wght", 650.0), ("wdth", 100.0)]);
//! let settings = DrawSettings::unhinted(Size::new(16.0), &var_location);
//!
//! // At this point, we need a "sink" to receive the resulting path. This
//! // is done by creating an implementation of the OutlinePen trait.
//!
//! // Let's make one that generates SVG path data.
//! #[derive(Default)]
//! struct SvgPath(String);
//!
//! // Implement the OutlinePen trait for this type. This emits the appropriate
//! // SVG path commands for each element type.
//! impl OutlinePen for SvgPath {
//! fn move_to(&mut self, x: f32, y: f32) {
//! self.0.push_str(&format!("M{x:.1},{y:.1} "));
//! }
//!
//! fn line_to(&mut self, x: f32, y: f32) {
//! self.0.push_str(&format!("L{x:.1},{y:.1} "));
//! }
//!
//! fn quad_to(&mut self, cx0: f32, cy0: f32, x: f32, y: f32) {
//! self.0
//! .push_str(&format!("Q{cx0:.1},{cy0:.1} {x:.1},{y:.1} "));
//! }
//!
//! fn curve_to(&mut self, cx0: f32, cy0: f32, cx1: f32, cy1: f32, x: f32, y: f32) {
//! self.0.push_str(&format!(
//! "C{cx0:.1},{cy0:.1} {cx1:.1},{cy1:.1} {x:.1},{y:.1} "
//! ));
//! }
//!
//! fn close(&mut self) {
//! self.0.push_str("Z ");
//! }
//! }
//! // Now, construct an instance of our pen.
//! let mut svg_path = SvgPath::default();
//!
//! // And draw the glyph!
//! glyph.draw(settings, &mut svg_path).unwrap();
//!
//! // See what we've drawn.
//! println!("{}", svg_path.0);
//! # }
//! ```

mod cff;
mod embedded_hinting;
Expand Down
Loading