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

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

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.
DrawInstance πŸ”’
Engine
Specifies the backend to use when applying hints.
Hinting
Specifies the hinting strategy for memory size calculations.
OutlineCollectionKind πŸ”’
OutlineGlyphFormat
Source format for an outline glyph.
OutlineKind πŸ”’
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.

FunctionsΒ§

with_glyf_memory πŸ”’
Invokes the callback with a memory buffer suitable for drawing the given TrueType outline.