[][src]Struct ttf_parser::Font

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

A font data handle.

Methods

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

pub fn glyph_index(&self, c: char) -> Option<GlyphId>[src]

Resolves Glyph ID for code point.

Returns None instead of 0 when glyph is not found.

All subtable formats except Mixed Coverage (8) are supported.

pub fn glyph_variation_index(&self, c: char, variation: char) -> Option<GlyphId>[src]

Resolves a variation of a Glyph ID from two code points.

Implemented according to Unicode Variation Sequences.

Returns None instead of 0 when glyph is not found.

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

pub fn has_glyph_classes(&self) -> bool[src]

Checks that font has Glyph Class Definition Table.

pub fn glyph_class(&self, glyph_id: GlyphId) -> Option<GlyphClass>[src]

Parses glyph's class according to Glyph Class Definition Table.

Returns None when Glyph Class Definition Table is not set or glyph class is not set or invalid.

pub fn glyph_mark_attachment_class(&self, glyph_id: GlyphId) -> Class[src]

Parses glyph's mark attachment class according to Mark Attachment Class Definition Table.

All glyphs not assigned to a class fall into Class 0.

pub fn is_mark_glyph(&self, glyph_id: GlyphId, set_index: Option<u16>) -> bool[src]

Checks that glyph is a mark according to Mark Glyph Sets Table.

set_index allows checking a specific glyph coverage set. Otherwise all sets will be checked.

Returns Ok(false) when Mark Glyph Sets Table is not set.

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

pub fn units_per_em(&self) -> Option<u16>[src]

Parses font's units per EM.

Returns None when value is not in a 16..=16384 range.

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

pub fn ascender(&self) -> i16[src]

Parses font's ascender value.

pub fn descender(&self) -> i16[src]

Parses font's descender value.

pub fn height(&self) -> i16[src]

Parses font's height.

pub fn line_gap(&self) -> i16[src]

Parses font's line gap.

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

pub fn glyph_hor_advance(&self, glyph_id: GlyphId) -> Option<u16>[src]

Parses glyph's horizontal advance using Horizontal Metrics Table.

pub fn glyph_hor_side_bearing(&self, glyph_id: GlyphId) -> Option<i16>[src]

Parses glyph's horizontal side bearing using Horizontal Metrics Table.

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

pub fn glyphs_kerning(
    &self,
    glyph_id1: GlyphId,
    glyph_id2: GlyphId
) -> Option<i16>
[src]

Parses a glyphs pair kerning.

Only horizontal kerning is supported.

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

Important traits for Names<'a>
pub fn names(&self) -> Names[src]

Returns an iterator over Name Records.

An iterator can be empty.

pub fn family_name(&self) -> Option<String>[src]

Parses font's family name.

Typographic Family is preferred over Family.

Note that font can have multiple names. You can use names() to list them all.

pub fn post_script_name(&self) -> Option<String>[src]

Parses font's PostScript name.

Note that font can have multiple names. You can use names() to list them all.

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

pub fn weight(&self) -> Weight[src]

Parses font's weight.

Returns Weight::Normal when OS/2 table is not present.

pub fn width(&self) -> Width[src]

Parses font's width.

Returns Width::Normal when OS/2 table is not present or when value is invalid.

pub fn is_regular(&self) -> bool[src]

Checks that font is marked as Regular.

Returns false when OS/2 table is not present.

pub fn is_italic(&self) -> bool[src]

Checks that font is marked as Italic.

Returns false when OS/2 table is not present.

pub fn is_bold(&self) -> bool[src]

Checks that font is marked as Bold.

Returns false when OS/2 table is not present.

pub fn is_oblique(&self) -> bool[src]

Checks that font is marked as Oblique.

Returns false when OS/2 table is not present or when its version is < 4.

pub fn x_height(&self) -> Option<i16>[src]

Parses font's X height.

Returns None when OS/2 table is not present or when its version is < 2.

pub fn strikeout_metrics(&self) -> Option<LineMetrics>[src]

Parses font's strikeout metrics.

Returns None when OS/2 table is not present.

pub fn subscript_metrics(&self) -> Option<ScriptMetrics>[src]

Parses font's subscript metrics.

Returns None when OS/2 table is not present.

pub fn superscript_metrics(&self) -> Option<ScriptMetrics>[src]

Parses font's superscript metrics.

Returns None when OS/2 table is not present.

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

pub fn underline_metrics(&self) -> Option<LineMetrics>[src]

Parses font's underline metrics.

pub fn glyph_name(&self, glyph: GlyphId) -> Option<&str>[src]

Parses glyph's name.

Uses the post table as a source.

Returns None when no name is associated with a glyph.

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

pub fn vertical_ascender(&self) -> Option<i16>[src]

Parses font's vertical ascender value.

Returns None when vhea table is not present.

pub fn vertical_descender(&self) -> Option<i16>[src]

Parses font's vertical descender value.

Returns None when vhea table is not present.

pub fn vertical_height(&self) -> Option<i16>[src]

Parses font's vertical height.

Returns None when vhea table is not present.

pub fn vertical_line_gap(&self) -> Option<i16>[src]

Parses font's vertical line gap.

Returns None when vhea table is not present.

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

pub fn glyph_ver_advance(&self, glyph_id: GlyphId) -> Option<u16>[src]

Parses glyph's vertical advance using Vertical Metrics Table.

pub fn glyph_ver_side_bearing(&self, glyph_id: GlyphId) -> Option<i16>[src]

Parses glyph's vertical side bearing using Vertical Metrics Table.

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

pub fn glyph_y_origin(&self, glyph: GlyphId) -> Option<i16>[src]

Parses a vertical origin of a glyph according to Vertical Origin Table.

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

pub fn from_data(data: &'a [u8], index: u32) -> Option<Self>[src]

Creates a Font object from a raw data.

You can set index for font collections. For simple ttf fonts set index to 0.

This method will do some parsing and sanitization, so it's a bit expensive.

Required tables: head, hhea and maxp.

If an optional table has an invalid data it will be skipped.

pub fn has_table(&self, name: TableName) -> bool[src]

Checks that font has a specified table.

Will return true only for tables that were successfully parsed.

pub fn number_of_glyphs(&self) -> u16[src]

Returns a total number of glyphs in the font.

Never zero.

The value was already parsed, so this function doesn't involve any parsing.

pub fn outline_glyph(
    &self,
    glyph_id: GlyphId,
    builder: &mut dyn OutlineBuilder
) -> Option<Rect>
[src]

Outlines a glyph and returns its tight bounding box.

Warning: since ttf-parser is a pull parser, OutlineBuilder will emit segments even when outline is partially malformed. You must check outline_glyph() result for error before using OutlineBuilder's output.

This method supports glyf, CFF and CFF2 tables.

Returns None when glyph has no outline.

Example

use std::fmt::Write;
use ttf_parser;

struct Builder(String);

impl ttf_parser::OutlineBuilder for Builder {
    fn move_to(&mut self, x: f32, y: f32) {
        write!(&mut self.0, "M {} {} ", x, y).unwrap();
    }

    fn line_to(&mut self, x: f32, y: f32) {
        write!(&mut self.0, "L {} {} ", x, y).unwrap();
    }

    fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
        write!(&mut self.0, "Q {} {} {} {} ", x1, y1, x, y).unwrap();
    }

    fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
        write!(&mut self.0, "C {} {} {} {} {} {} ", x1, y1, x2, y2, x, y).unwrap();
    }

    fn close(&mut self) {
        write!(&mut self.0, "Z ").unwrap();
    }
}

let data = std::fs::read("tests/fonts/glyphs.ttf").unwrap();
let font = ttf_parser::Font::from_data(&data, 0).unwrap();
let mut builder = Builder(String::new());
let bbox = font.outline_glyph(ttf_parser::GlyphId(0), &mut builder).unwrap();
assert_eq!(builder.0, "M 50 0 L 50 750 L 450 750 L 450 0 L 50 0 Z ");
assert_eq!(bbox, ttf_parser::Rect { x_min: 50, y_min: 0, x_max: 450, y_max: 750 });

pub fn glyph_bounding_box(&self, glyph_id: GlyphId) -> Option<Rect>[src]

Returns a tight glyph bounding box.

Note that this method's performance depends on a table type the current font is using. In case of a glyf table, it's basically free, since this table stores bounding box separately. In case of CFF and CFF2, we should actually outline a glyph and then calculate its bounding box. So if you need an outline and a bounding box and you have an OpenType font (which uses CFF/CFF2) then prefer outline_glyph() method.

Trait Implementations

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

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

Auto Trait Implementations

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

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

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

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

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

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

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

type Owned = T

The resulting type after obtaining ownership.

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.

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.