[][src]Crate ttf_parser

A high-level, safe, zero-allocation TrueType font parser.

Features

  • A high-level API.
  • Zero allocations.
  • Zero unsafe.
  • Zero dependencies.
  • Fast.
  • Stateless.
  • Simple and maintainable code (no magic numbers).

Supported TrueType features

Supported OpenType features

Methods' computational complexity

TrueType fonts designed for fast querying, so most of the methods are very fast. The main exception is glyph outlining. Glyphs can be stored using two different methods: using Glyph Data format and Compact Font Format (pdf). The first one is fairly simple which makes it faster to process. The second one is basically a tiny language with a stack-based VM, which makes it way harder to process. Currently, it takes 40% more time to outline all glyphs in SourceSansPro-Regular.otf (which uses CFF) rather than in SourceSansPro-Regular.ttf.

test outline_cff  ... bench:   1,651,557 ns/iter (+/- 2,751)
test outline_glyf ... bench:     977,046 ns/iter (+/- 4,973)

Here is some methods benchmarks:

test outline_glyph_276_from_cff  ... bench:       1,247 ns/iter (+/- 2)
test outline_glyph_276_from_glyf ... bench:         817 ns/iter (+/- 15)
test outline_glyph_8_from_cff    ... bench:         521 ns/iter (+/- 2)
test family_name                 ... bench:         445 ns/iter (+/- 4)
test from_data_otf               ... bench:         435 ns/iter (+/- 1)
test outline_glyph_8_from_glyf   ... bench:         360 ns/iter (+/- 7)
test from_data_ttf               ... bench:         133 ns/iter (+/- 0)

Some methods are too fast, so we execute them 1000 times to get better measurements.

test glyph_index_u41     ... bench:      24,648 ns/iter (+/- 256)
test glyph_2_hor_metrics ... bench:       8,421 ns/iter (+/- 18)
test units_per_em        ... bench:         564 ns/iter (+/- 2)
test x_height            ... bench:         568 ns/iter (+/- 1)
test strikeout_metrics   ... bench:         564 ns/iter (+/- 0)
test width               ... bench:         422 ns/iter (+/- 0)
test ascender            ... bench:         279 ns/iter (+/- 1)
test subscript_metrics   ... bench:         279 ns/iter (+/- 0)

family_name is expensive, because it allocates a String and the original data is stored as UTF-16 BE.

Safety

  • The library must not panic. Any panic considered as a critical bug and should be reported.
  • The library forbids unsafe code.

Structs

Font

A font data handle.

GlyphId

A type-safe wrapper for glyph ID.

HorizontalMetrics

A horizontal metrics of a glyph.

LineMetrics

A line metrics.

Name

A Name Record.

Names

An iterator over font's names.

Rect

Rectangle.

ScriptMetrics

A script metrics used by subscript and superscript.

VerticalMetrics

A vertical metrics of a glyph.

Enums

CFFError

A list of errors that can occur during a CFF table parsing.

Error

A font parsing error.

NameId

A name ID.

PlatformId

A platform ID.

TableName

A table name.

Weight

A font weight.

Width

A font width.

Traits

OutlineBuilder

A trait for glyph outline construction.

Functions

fonts_in_collection

Returns a number of fonts stored in a TrueType font collection.