# [−][src]Struct rusttype::Font

    pub struct Font<'a> { /* fields omitted */ }

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!("../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)?;

## Methods

### `impl<'a> Font<'a>`[src]

#### `pub fn from_bytes<B: Into<SharedBytes<'a>>>(bytes: B) -> Result<Font<'a>, Error>`[src]

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

#### `pub fn v_metrics(&self, scale: Scale) -> VMetrics`[src]

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.

#### `pub fn v_metrics_unscaled(&self) -> VMetrics`[src]

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

#### `pub fn units_per_em(&self) -> u16`[src]

Returns the units per EM square of this font

#### `pub fn glyph_count(&self) -> usize`[src]

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

#### `pub fn glyph<C: IntoGlyphId>(&self, id: C) -> Glyph<'a>`[src]

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. `GlyphId`s 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.

####
ⓘImportant traits for GlyphIter<'a, 'b, I>

### Important traits for GlyphIter<'a, 'b, I>

`impl<'a, 'b, I: Iterator> Iterator for GlyphIter<'a, 'b, I> where
I::Item: IntoGlyphId, type Item = Glyph<'a>;`
`pub fn glyphs_for<I: Iterator>(&self, itr: I) -> GlyphIter<'a, '_, I> where
I::Item: IntoGlyphId,` [src]

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))`.

#### `pub fn font_name_strings(&self) -> FontNameIter<SharedBytes<'a>>`[src]

Returns an iterator over the names for this font.

####
ⓘImportant traits for LayoutIter<'a, 'b>

### Important traits for LayoutIter<'a, 'b>

`impl<'a, 'b> Iterator for LayoutIter<'a, 'b> type Item = PositionedGlyph<'a>;`
`pub fn layout<'b>(
&'b self,
s: &'b str,
scale: Scale,
start: Point<f32>
) -> LayoutIter<'a, 'b>`[src]

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)
        })

#### `pub fn pair_kerning<A, B>(&self, scale: Scale, first: A, second: B) -> f32 where
A: IntoGlyphId,
B: IntoGlyphId,` [src]

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

## Trait Implementations

### `impl<'a> Clone for Font<'a>`[src]

#### `fn clone(&self) -> Font<'a>`[src]

Returns a copy of the value. Read more

#### `fn clone_from(&mut self, source: &Self)`1.0.0[src]

Performs copy-assignment from `source`. Read more

### `impl<'_> Debug for Font<'_>`[src]

#### `fn fmt(&self, f: &mut Formatter) -> Result`[src]

Formats the value using the given formatter. Read more

## Auto Trait Implementations

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

### `impl<'a> Unpin for Font<'a>`

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

### `impl<'a> UnwindSafe for Font<'a>`

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

## Blanket Implementations

### `impl<T> ToOwned for T where
T: Clone,` [src]

#### `type Owned = T`

The resulting type after obtaining ownership.

#### `fn to_owned(&self) -> T`[src]

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

#### `fn clone_into(&self, target: &mut T)`[src]

🔬 This is a nightly-only experimental API. (`toowned_clone_into`)

recently added

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

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `fn into(self) -> U`[src]

Performs the conversion.

### `impl<T> From<T> for T`[src]

#### `fn from(t: T) -> T`[src]

Performs the conversion.

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

#### `fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>`[src]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

#### `type Error = <U as TryFrom<T>>::Error`

The type returned in the event of a conversion error.

#### `fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`[src]

Performs the conversion.

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

#### `fn borrow_mut(&mut self) -> &mut T`[src]

Mutably borrows from an owned value. Read more

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

#### `fn borrow(&self) -> &T`[src]

Immutably borrows from an owned value. Read more

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `fn type_id(&self) -> TypeId`[src]

Gets the `TypeId` of `self`. Read more

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)