pub struct Font<'a> { /* private fields */ }Expand description
A parsed TrueType/OpenType font face.
The face borrows the font’s byte buffer ('a), exactly like the underlying
ttf_parser::Face — keep the bytes alive for at least as long as the
Font. Parsing is cheap and allocation-free; metrics and outlines are read
on demand.
§Glyph outline cache
Extracting a glyph’s outline from the font (the ttf-parser outline walk) is
the dominant cost when the same text is redrawn — for example, once per
frame. Font therefore memoizes each glyph’s outline, keyed by its
GlyphId: the cached outline is stored once in
font-design units and only scaled/positioned per draw. The cache uses
interior mutability (RefCell), so a Font is single-threaded (not
Sync) — share the loaded font bytes and create one Font per thread if you
need concurrency.
Implementations§
Source§impl<'a> Font<'a>
impl<'a> Font<'a>
Sourcepub fn from_slice(data: &'a [u8]) -> Result<Self, FontError>
pub fn from_slice(data: &'a [u8]) -> Result<Self, FontError>
Parses the first face from the bytes of a font file (.ttf/.otf).
Sourcepub fn from_collection(data: &'a [u8], index: u32) -> Result<Self, FontError>
pub fn from_collection(data: &'a [u8], index: u32) -> Result<Self, FontError>
Parses the face at index inside a font collection (.ttc). Use 0 for
a plain single-face file.
Sourcepub fn units_per_em(&self) -> u16
pub fn units_per_em(&self) -> u16
The size of the design grid in font units — the denominator for scaling
outlines and metrics to a pixel size.
Sourcepub fn ascent(&self, size: f32) -> f32
pub fn ascent(&self, size: f32) -> f32
Distance from the baseline up to the font’s ascender, in pixels.
Sourcepub fn descent(&self, size: f32) -> f32
pub fn descent(&self, size: f32) -> f32
Distance from the baseline down to the font’s descender, in pixels (positive — Y grows downward in pixmap space).
Sourcepub fn line_height(&self, size: f32) -> f32
pub fn line_height(&self, size: f32) -> f32
Recommended distance between successive baselines, in pixels
(ascender - descender + line_gap). This is the step used for \n in
Font::text_path.
Sourcepub fn advance_width(&self, ch: char, size: f32) -> f32
pub fn advance_width(&self, ch: char, size: f32) -> f32
Horizontal advance of a single character, in pixels.
Characters with no glyph fall back to the advance of .notdef.
Sourcepub fn measure(&self, text: &str, size: f32) -> f32
pub fn measure(&self, text: &str, size: f32) -> f32
Width of the widest line of text, in pixels (sum of advances per line,
no kerning). \n separates lines.
Sourcepub fn glyph_path(&self, ch: char, size: f32, x: f32, y: f32) -> Option<Path>
pub fn glyph_path(&self, ch: char, size: f32, x: f32, y: f32) -> Option<Path>
Builds the filled outline of a single character with its baseline origin
at (x, y) in pixmap coordinates, scaled to em size (pixels).
Returns None when the character has no glyph or the glyph has no
contours (for example a space).
Sourcepub fn text_path(&self, text: &str, size: f32, x: f32, y: f32) -> Option<Path>
pub fn text_path(&self, text: &str, size: f32, x: f32, y: f32) -> Option<Path>
Lays a string out horizontally and returns one Path containing every
glyph’s outline, ready to be filled with FillRule::NonZero — the rule
TrueType/OpenType outlines are authored for.
(x, y) is the origin of the first baseline. Each \n resets the pen to
x and drops the baseline by one line_height.
Glyphs are advanced by their horizontal advance only (no kerning, see the
module limitations). Empty glyphs such as
spaces contribute no contours but still advance the pen.
Returns None if the result is empty (e.g. whitespace-only text).