Crate opentype

source ·
Expand description

A parser for OpenType fonts.

Example

extern crate opentype;

use std::collections::HashMap;

use opentype::Font;
use opentype::truetype::{FontHeader, HorizontalHeader};
use opentype::truetype::naming_table::{NameID, NamingTable};

macro_rules! ok(($result:expr) => ($result.unwrap()));

let path = "SourceSerifPro-Regular.otf";
let mut reader = ok!(std::fs::File::open(path));
let font = ok!(Font::read(&mut reader));

let font_header: FontHeader = ok!(ok!(font.take(&mut reader)));
assert_eq!(font_header.units_per_em, 1000);

let horizontal_header: HorizontalHeader = ok!(ok!(font.take(&mut reader)));
assert_eq!(horizontal_header.ascender, 918);

let naming_table: NamingTable = ok!(ok!(font.take(&mut reader)));
let names: Vec<_> = naming_table.collect();
let names: HashMap<_, _> = names
    .iter()
    .map(|((name_id, _), value)| (*name_id, value.as_deref()))
    .collect();
assert_eq!(ok!(names[&NameID::FullFontName]), "Source Serif Pro");
assert_eq!(ok!(names[&NameID::DesignerName]), "Frank Grießhammer");

Re-exports

pub extern crate postscript;
pub extern crate truetype;
pub use glyph_definition::GlyphDefinition;
pub use glyph_positioning::GlyphPositioning;
pub use glyph_substitution::GlyphSubstitution;

Modules

Structs

A file.
A font.

Traits

A font table.
A type that can read.
A type that can be read.
A type that can be read given a parameter.

Type Definitions

An error.
A result.