To use font-kit, you typically follow a workflow of selecting a font from a Source, loading it into a Font object, and then using a Canvas to rasterize specific glyphs.
Note that font-kit handles glyph-to-character mapping for simple use cases, but for complex text shaping (like ligatures or bidirectional text), you should use a dedicated shaper alongside font-kit.
use font_kit::canvas::{Canvas, Format, RasterizationOptions};
use font_kit::family_name::FamilyName;
use font_kit::hinting::HintingOptions;
use font_kit::properties::Properties;
use font_kit::source::SystemSource;
use pathfinder_geometry::transform2d::Transform2F;
use pathfinder_geometry::vector::{Vector2F, Vector2I};
// 1. Select the best matching font from the system
let font = SystemSource::new()
.select_best_match(&[FamilyName::SansSerif], &Properties::new())
.unwrap()
.load()
.unwrap();
// 2. Get a glyph ID for a character
let glyph_id = font.glyph_for_char('A').unwrap();
// 3. Prepare a canvas for rasterization
let mut canvas = Canvas::new(Vector2I::splat(32), Format::A8);
// 4. Rasterize the glyph
font.rasterize_glyph(
&mut canvas,
glyph_id,
32.0,
Transform2F::from_translation(Vector2F::new(0.0, 32.0)),
HintingOptions::None,
RasterizationOptions::GrayscaleAa
).unwrap();