Skip to main content

Crate skrifa

Crate skrifa 

Source
Expand description

Drawing glyphs from fonts at a size, with hinting.

A Font is a font at a location in its variation space. A ScalerFont draws the font’s glyph outlines at a size in pixels per em, hinted or not.

use skrifa::{
    font::{glyph::outline::SvgSink, Font},
    DrawMemory, HintOptions, RasterTarget, ScalerFont,
};

// Load the first font in the file and create a bold instance.
let font = Font::new(data, 0).unwrap();
let bold = font.instance_builder().variations([("wght", 700.0)]).build();

// Find the glyph ID for the character 'A'.
let glyph_id = bold.charmap().map_unicode('A').unwrap_or_default();

// Configure a scaler font for hinted drawing at 16 pixels per em, using
// the interpreter with a horizontal subpixel target.
let options = HintOptions::interpreter().with_target(RasterTarget::Lcd);
let scaler = ScalerFont::hinted(bold, 16.0, options);

if let Some(outline) = scaler.outline(glyph_id) {
    let mut path = SvgSink::new();
    // Draw into the path and store the resulting metrics.
    let metrics = outline.draw(DrawMemory::Scratch, &mut path)?;
    println!("{path} advances by {:?}", metrics.h_advance);
}

§Drawing

ScalerFont::outline returns a glyph’s outline as a ScalerOutline, which draws into any PathSink. Drawing needs working memory. By default it uses short-lived scratch memory, and a caller drawing many glyphs can supply a reusable buffer instead.

A scaler draws from the first outline table the font contains, in GlyphSourceOrder::DEFAULT: VARC composites over glyf, CFF2, CFF or Type 1 charstrings. To choose other tables, pass a ScalerSource in place of the font. The scaler reports the tables it draws from.

A scaler with no size draws in font units.

§Hinting

Hinting fits an outline to the pixel grid at the scaler’s size. HintOptions configures it:

  • Engine. Run the font’s own hints, either TrueType instructions or PostScript hints; run the automatic hinter, which derives hints from the outline; or pick based on the font, using its own hints where they are worth running. A TrueType font that needs its instructions to draw correct contours always runs them.
  • Target. Monochrome, grayscale, light grayscale, or horizontal or vertical subpixel rendering. Each target hints to suit the way its pixels are drawn.
  • Linear metrics. Hint only vertically, keeping the unhinted horizontal shape and spacing.
  • ClearType compatibility. Choose between the current ClearType behavior of the TrueType interpreter, which follows the font’s gasp table, and the classic behavior, which grid fits every glyph.
  • Strictness. Fail a draw when a glyph’s instructions fail, instead of keeping the outline as they left it.

The scaler reports the engine that runs, and the rasterizer settings the font requests at the size: whether to smooth along both axes, and the dropout control a glyph asks of a monochrome rasterizer.

§Metrics

A scaler returns the font’s line metrics and heights, its style metrics such as the underline and italic angle, and the advance and bounds of each glyph, all scaled linearly to its size. Hinting can change a glyph’s advance, so hinted glyph metrics come from drawing the outline.

§The font model

The font module holds everything else about a font: its character map, names, variation axes and instances, and color and bitmap glyphs.

§Need shaping?

HarfRust turns text into positioned glyphs. It reads fonts through the same font model, so a Font can be shaped with HarfRust and drawn with a ScalerFont.

Re-exports§

pub extern crate read_fonts as raw;

Modules§

font
Font types supplied by read-fonts.

Structs§

DrawnMetrics
What drawing an outline reports about it.
Font
A font at one location in its design space.
GlyphId
A 32-bit glyph identifier.
GlyphSourceOrder
An ordered list of glyph sources, most preferred first.
HintOptions
Hinting configuration for a ScalerFont.
RasterPolicy
The smoothing and grid fitting a font requests at a given size.
ScalerFont
Draws glyphs from a Font at a size, with optional hinting.
ScalerOutline
An outline from a ScalerFont, resolved for drawing.
ScalerSource
A font and the order in which its outline sources are tried.
Tag
An OpenType tag.

Enums§

DrawMemory
Working memory for drawing an outline.
DropoutControl
The dropout control mode for a bilevel rasterizer.
GlyphSource
A table that can provide a glyph.
HintEngine
The hinting engine a scaler runs.
OutlineSource
A table that provides outlines.
RasterTarget
The rasterization target for a hinted outline.
ScaleError
An error from drawing a glyph with a ScalerFont.

Traits§

PathSink
Receives the path commands for an outline.