Skip to main content

Font

Struct Font 

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

Source

pub fn from_slice(data: &'a [u8]) -> Result<Self, FontError>

Parses the first face from the bytes of a font file (.ttf/.otf).

Source

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.

Source

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.

Source

pub fn ascent(&self, size: f32) -> f32

Distance from the baseline up to the font’s ascender, in pixels.

Source

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).

Source

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.

Source

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.

Source

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.

Source

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).

Source

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).

Auto Trait Implementations§

§

impl<'a> !Freeze for Font<'a>

§

impl<'a> !RefUnwindSafe for Font<'a>

§

impl<'a> !Sync for Font<'a>

§

impl<'a> Send for Font<'a>

§

impl<'a> Unpin for Font<'a>

§

impl<'a> UnsafeUnpin for Font<'a>

§

impl<'a> UnwindSafe for Font<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.