Module outline

Source
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§

error
Error types associated with outlines.
pen
Types for collecting the output when drawing a glyph outline.

Structs§

AdjustedMetrics
Information and adjusted metrics generated while drawing an outline glyph.
DrawSettings
Options that define how a glyph is drawn to a pen.
GlyphStyles
Set of derived glyph styles that are used for automatic hinting.
HintingInstance
Hinting instance that uses information embedded in the font to perform grid-fitting.
HintingOptions
Configuration settings for a hinting instance.
OutlineGlyph
A scalable glyph outline.
OutlineGlyphCollection
Collection of scalable glyph outlines.

Enums§

DrawError
Errors that may occur when drawing glyphs.
Engine
Specifies the backend to use when applying hints.
Hinting
Specifies the hinting strategy for memory size calculations.
OutlineGlyphFormat
Source format for an outline glyph.
SmoothMode
Mode selector for a smooth hinting target.
Target
Defines the target settings for hinting.

Traits§

OutlinePen
Interface for accepting a sequence of path commands.