Font

Struct Font 

Source
pub struct Font<'a> { /* private fields */ }
Expand description

A loaded TrueType font

Implementations§

Source§

impl<'a> Font<'a>

Source

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

Load a font from bytes

§Arguments
  • data - The font file data (must live as long as the Font)
§Example
let font_data = include_bytes!("../tests/fonts/FiraMono-Medium.ttf");
let font = Font::from_bytes(font_data)?;
Source

pub fn glyph_by_char(&self, c: char) -> Result<Glyph<'_>>

Get a glyph by character

§Arguments
  • c - The character to look up
§Returns

The glyph for the character, or an error if not found

§Example
let glyph = font.glyph_by_char('A')?;
println!("Advance: {}", glyph.advance);
Source

pub fn glyph_by_id(&self, glyph_id: GlyphId, character: char) -> Glyph<'_>

Get a glyph by its glyph ID

This is useful when working with text shaping libraries or when you already have glyph IDs from other sources (e.g., rustybuzz, cosmic-text).

§Arguments
  • glyph_id - The glyph ID to look up
  • character - The character this glyph represents (for display purposes)
§Returns

A glyph with metrics and outline information

§Example
use ttf_parser::GlyphId;

let glyph_id = GlyphId(42);
let glyph = font.glyph_by_id(glyph_id, 'A');
Source

pub fn face(&self) -> &Face<'a>

Access the underlying ttf-parser Face for advanced operations

This allows you to use any ttf-parser functionality directly, such as accessing font tables, kerning information, or other font metadata that fontmesh doesn’t expose.

§Example
// Get kerning between two glyphs
let face = font.face();
let kern = face.kerning_subtables();

// Access font name
for name in face.names() {
    println!("{:?}: {}", name.name_id, name.to_string());
}
Source

pub fn units_per_em(&self) -> u16

Get the font’s units per em

Source

pub fn ascender(&self) -> f32

Get the font’s ascender

Source

pub fn descender(&self) -> f32

Get the font’s descender

Source

pub fn line_gap(&self) -> f32

Get the font’s line gap

Source§

impl<'a> Font<'a>

Source

pub fn glyph_to_mesh_2d(&self, character: char) -> Result<Mesh2D>

Convert a character glyph to a 2D triangle mesh

Uses default quality (20 subdivisions per curve).

§Arguments
  • character - The character to convert
§Returns

A 2D triangle mesh

§Example
let mesh = font.glyph_to_mesh_2d('A')?;
println!("Generated {} vertices", mesh.vertices.len());
Source

pub fn glyph_to_mesh_3d(&self, character: char, depth: f32) -> Result<Mesh3D>

Convert a character glyph to a 3D triangle mesh with extrusion

Uses default quality (20 subdivisions per curve).

§Arguments
  • character - The character to convert
  • depth - The extrusion depth
§Returns

A 3D triangle mesh with normals

§Example
let mesh = font.glyph_to_mesh_3d('A', 5.0)?;
println!("Generated {} triangles", mesh.triangle_count());

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<'a> Unpin 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.