Skip to main content

Font

Struct Font 

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

One face of an OpenType/TrueType font file or collection. Cheap to clone (the bytes are shared); clones keep the same FontId, so they share cached glyphs.

Implementations§

Source§

impl Font

Source

pub fn new(data: impl Into<Arc<[u8]>>) -> Result<Self, FontError>

Loads a font from its file bytes (face 0 of a collection).

§Errors

FontError::Parse if the bytes are not a readable font.

Source

pub fn from_shared(data: FontBytes, index: u32) -> Result<Self, FontError>

Face index of the font file or collection in data, sharing the bytes.

Each call creates a new FontId: load a face once and clone the Font.

§Errors

FontError::Parse if the bytes are not a readable font or have no face index.

Source

pub fn bundled_mono() -> Result<Self, FontError>

The bundled JetBrains Mono (BUNDLED_MONO). Every call returns the same font (same FontId), so all users share its cached glyphs.

§Errors

Only if the bundled file were corrupt; tests guard against that.

Source

pub fn id(&self) -> FontId

This font’s identity, used to key cached glyphs.

Source

pub fn index(&self) -> u32

The face’s index within its file (0 unless the file is a collection).

Source

pub fn bytes(&self) -> &[u8] ⓘ

The whole font file.

Source

pub fn shared_bytes(&self) -> FontBytes

The shared file bytes, for handing to another library without copying.

Source

pub fn glyph_id(&self, ch: char) -> Option<GlyphId>

The glyph the font draws for ch, or None if it has no glyph for it.

Source

pub fn advance(&self, glyph: GlyphId, px: f32) -> f32

Horizontal advance of glyph at px pixels per em; 0 if unknown.

Source

pub fn glyph_extent(&self, glyph: GlyphId, px: f32) -> Option<[f32; 2]>

Width and height in pixels of glyph’s outline bounding box at px pixels per em, without rasterizing it; None if the font cannot say (then the caller must assume the worst). Some([0.0, 0.0]) for glyphs without an outline.

Source

pub fn line_metrics(&self, px: f32) -> LineMetrics

Ascent, descent and line gap at px pixels per em.

Trait Implementations§

Source§

impl Clone for Font

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Font

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Font

§

impl !UnwindSafe for Font

§

impl Freeze for Font

§

impl Send for Font

§

impl Sync for Font

§

impl Unpin for Font

§

impl UnsafeUnpin 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.