Expand description
Loading, scaling and hinting of glyph outlines.
This module provides support for retrieving (optionally scaled and hinted) glyph outlines in the form of vector paths.
Β§Drawing a glyph
Generating SVG path commands
for a character (this assumes a local variable font
of type FontRef
):
use skrifa::{
instance::{LocationRef, Size},
outline::{DrawSettings, OutlinePen},
FontRef, MetadataProvider,
};
// 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);
ModulesΒ§
- autohint π
- Runtime autohinting support.
- cff π
- Support for scaling CFF outlines.
- error
- Error types associated with outlines.
- glyf π
- Scaling support for TrueType outlines.
- hint π
- Support for applying embedded hinting instructions.
- hint_
reliant π - Name detection for fonts that require hinting to be run for correct contours (FreeType calls these βtrickyβ fonts).
- memory π
- Support for temporary memory allocation, making use of the stack for small sizes.
- metrics π
- Helper for loading (possibly variable) horizontal glyph metrics.
- path π
- TrueType style outline to path conversion.
- pen
- Types for collecting the output when drawing a glyph outline.
- unscaled π
- Compact representation of an unscaled, unhinted outline.
StructsΒ§
- Adjusted
Metrics - Information and adjusted metrics generated while drawing an outline glyph.
- Draw
Settings - Options that define how a glyph is drawn to a pen.
- Glyph
Styles - Set of derived glyph styles that are used for automatic hinting.
- Hinting
Instance - Hinting instance that uses information embedded in the font to perform grid-fitting.
- Hinting
Options - Configuration settings for a hinting instance.
- Outline
Glyph - A scalable glyph outline.
- Outline
Glyph Collection - Collection of scalable glyph outlines.
EnumsΒ§
- Draw
Error - Errors that may occur when drawing glyphs.
- Draw
Instance π - Engine
- Specifies the backend to use when applying hints.
- Hinting
- Specifies the hinting strategy for memory size calculations.
- Outline
Collection πKind - Outline
Glyph Format - Source format for an outline glyph.
- Outline
Kind π - Smooth
Mode - Mode selector for a smooth hinting target.
- Target
- Defines the target settings for hinting.
TraitsΒ§
- Outline
Pen - Interface for accepting a sequence of path commands.
FunctionsΒ§
- with_
glyf_ πmemory - Invokes the callback with a memory buffer suitable for drawing the given TrueType outline.