Struct kiss2d::Font

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

A single font. This may or may not own the font data.

Lifetime

The lifetime reflects the font data lifetime. Font<'static> covers most cases ie both dynamically loaded owned data and for referenced compile time font data.

Example

let font_data: &[u8] = include_bytes!("../dev/fonts/dejavu/DejaVuSansMono.ttf");
let font: Font<'static> = Font::from_bytes(font_data)?;

let owned_font_data: Vec<u8> = font_data.to_vec();
let from_owned_font: Font<'static> = Font::from_bytes(owned_font_data)?;

Implementations

Constructs a font from an array of bytes, this is a shortcut for FontCollection::from_bytes for collections comprised of a single font.

The “vertical metrics” for this font at a given scale. These metrics are shared by all of the glyphs in the font. See VMetrics for more detail.

Get the unscaled VMetrics for this font, shared by all glyphs. See VMetrics for more detail.

Returns the units per EM square of this font

The number of glyphs present in this font. Glyph identifiers for this font will always be in the range 0..self.glyph_count()

Returns the corresponding glyph for a Unicode code point or a glyph id for this font.

If id is a GlyphId, it must be valid for this font; otherwise, this function panics. GlyphIds should always be produced by looking up some other sort of designator (like a Unicode code point) in a font, and should only be used to index the font they were produced for.

Note that code points without corresponding glyphs in this font map to the “.notdef” glyph, glyph 0.

A convenience function.

Returns an iterator that produces the glyphs corresponding to the code points or glyph ids produced by the given iterator itr.

This is equivalent in behaviour to itr.map(|c| font.glyph(c)).

Returns an iterator over the names for this font.

A convenience function for laying out glyphs for a string horizontally. It does not take control characters like line breaks into account, as treatment of these is likely to depend on the application.

Note that this function does not perform Unicode normalisation. Composite characters (such as ö constructed from two code points, ¨ and o), will not be normalised to single code points. So if a font does not contain a glyph for each separate code point, but does contain one for the normalised single code point (which is common), the desired glyph will not be produced, despite being present in the font. Deal with this by performing Unicode normalisation on the input string before passing it to layout. The crate unicode-normalization is perfect for this purpose.

Calling this function is equivalent to a longer sequence of operations involving glyphs_for, e.g.

font.layout("Hello World!", scale, start)

produces an iterator with behaviour equivalent to the following:

font.glyphs_for("Hello World!".chars())
    .scan((None, 0.0), |&mut (mut last, mut x), g| {
        let g = g.scaled(scale);
        if let Some(last) = last {
            x += font.pair_kerning(scale, last, g.id());
        }
        let w = g.h_metrics().advance_width;
        let next = g.positioned(start + vector(x, 0.0));
        last = Some(next.id());
        x += w;
        Some(next)
    })

Returns additional kerning to apply as well as that given by HMetrics for a particular pair of glyphs.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.