Skip to main content

Font

Struct Font 

Source
pub struct Font { /* private fields */ }
Expand description

A loaded font, ready for shaping and rasterization.

Constructed from raw TTF/OTF bytes via Font::from_bytes. The data is reference-counted so fonts can be cheaply shared and saved across frames.

An optional fallback font can be chained via Font::with_fallback; when a glyph is missing from the primary font (glyph_id == 0 after shaping), the fallback is consulted for both the glyph outline and advance width.

Implementations§

Source§

impl Font

Source

pub fn from_bytes(data: Vec<u8>) -> Result<Self, &'static str>

Parse a font from raw TTF/OTF bytes.

Returns Err if the data is not a valid font.

Source

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

Parse a font from a borrowed byte slice (data is copied).

Source

pub fn with_fallback(self, fallback: Arc<Font>) -> Self

Chain a fallback font consulted when this font lacks a glyph.

Returns self so it can be used as a builder method:

let font = Font::from_slice(MAIN_BYTES)?.with_fallback(Arc::new(emoji_font));
Source

pub fn units_per_em(&self) -> u16

Source

pub fn ascender_px(&self, size: f64) -> f64

Ascender height in pixels at the given font size.

Source

pub fn descender_px(&self, size: f64) -> f64

Descender depth in pixels at the given font size (positive value).

Source

pub fn line_height_px(&self, size: f64) -> f64

Recommended line height in pixels at the given font size.

Auto Trait Implementations§

§

impl Freeze for Font

§

impl RefUnwindSafe for Font

§

impl Send for Font

§

impl Sync for Font

§

impl Unpin for Font

§

impl UnsafeUnpin for Font

§

impl UnwindSafe for Font

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.