Struct Font

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

A parsed font, with access to its glyphs and stored strings

Implementations§

Source§

impl Font

Source

pub fn new(font_data: &[u8]) -> Result<Font, ParseError>

Creates a new font from the given font data

§Errors

Returns an error if the font data is invalid or cannot be parsed

Source

pub fn from_file(path: impl AsRef<Path>) -> Result<Font, ParseError>

Creates a new font from the font file at the specified path

§Errors

Returns an error if the font data is invalid or cannot be parsed

Examples found in repository?
examples/loading_fonts.rs (line 8)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() -> ParseResult<()> {
    let font = Font::from_file("examples/slick.ttf")?;
    let font_name = font.string(StringKind::FullFontName).unwrap();
    println!(
        "Font `{font_name}` contains {} glyphs: ",
        font.glyphs().len()
    );

    font.glyphs().iter().for_each(|glyph| {
        let name = glyph.name();
        let codepoint = glyph.codepoint();
        println!("- `{name}` has codepoint {codepoint:04x}");

        //
        // You can also export the glyph as an image
        let _outline = glyph.svg_outline();
    });

    Ok(())
}
Source

pub fn string(&self, kind: NameKind) -> Option<&str>

Returns the string with the specified kind, if it exists

Examples found in repository?
examples/loading_fonts.rs (line 9)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() -> ParseResult<()> {
    let font = Font::from_file("examples/slick.ttf")?;
    let font_name = font.string(StringKind::FullFontName).unwrap();
    println!(
        "Font `{font_name}` contains {} glyphs: ",
        font.glyphs().len()
    );

    font.glyphs().iter().for_each(|glyph| {
        let name = glyph.name();
        let codepoint = glyph.codepoint();
        println!("- `{name}` has codepoint {codepoint:04x}");

        //
        // You can also export the glyph as an image
        let _outline = glyph.svg_outline();
    });

    Ok(())
}
Source

pub fn strings(&self) -> &HashMap<NameKind, String>

Returns all the strings in the font

Source

pub fn glyph(&self, codepoint: u32) -> Option<&Glyph>

Returns the glyph with the specified unicode codepoint, if it exists

Source

pub fn glyph_named(&self, name: &str) -> Option<&Glyph>

Returns the glyph with the specified postscript name, if it exists

Source

pub fn glyphs(&self) -> &[Glyph]

Returns the glyphs in the font

Examples found in repository?
examples/loading_fonts.rs (line 12)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() -> ParseResult<()> {
    let font = Font::from_file("examples/slick.ttf")?;
    let font_name = font.string(StringKind::FullFontName).unwrap();
    println!(
        "Font `{font_name}` contains {} glyphs: ",
        font.glyphs().len()
    );

    font.glyphs().iter().for_each(|glyph| {
        let name = glyph.name();
        let codepoint = glyph.codepoint();
        println!("- `{name}` has codepoint {codepoint:04x}");

        //
        // You can also export the glyph as an image
        let _outline = glyph.svg_outline();
    });

    Ok(())
}

Trait Implementations§

Source§

impl Clone for Font

Source§

fn clone(&self) -> Font

Returns a copy of the value. Read more
1.0.0 · 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<(), Error>

Formats the value using the given formatter. Read more
Source§

impl FontCodegenExt for Font

Source§

fn gen_enum(&self, name: &str) -> TokenStream

Generates an enum representing the glyphs in the font
Source§

fn gen_enum_comments(&self) -> Vec<String>

Generates the comments for the enum
Source§

fn gen_enum_entry(glyph: &Glyph) -> TokenStream

Generates a single entry in the enum for a glyph
Source§

impl From<TrueTypeFont> for Font

Source§

fn from(value: TrueTypeFont) -> Font

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Font

§

impl RefUnwindSafe for Font

§

impl Send for Font

§

impl Sync for Font

§

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

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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 = 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.