[][src]Crate ttf_parser

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

Features

  • A high-level API, for people who doesn't know how TrueType works internally. Basically, no direct access to font tables.
  • Zero allocations.
  • Zero unsafe.
  • Zero required dependencies. Logging is enabled by default.
  • no_std compatible.
  • Fast.
  • Stateless.
  • Simple and maintainable code (no magic numbers).

Supported TrueType features

Supported OpenType features

Error handling

ttf-parser is designed to parse well-formed fonts, so it does not have an Error enum. It doesn't mean that it will crash or panic on malformed fonts, only that the error handling will boil down to Option::None. So you will not get a detailed cause of an error. By doing so we can simplify an API quite a lot since otherwise, we will have to use Result<Option<T>, Error>.

Some methods may print warnings, when the logging feature is enabled.

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.

test outline_cff  ... bench:   1,298,871 ns/iter (+/- 11,846)
test outline_glyf ... bench:     837,958 ns/iter (+/- 6,261)

Here is some methods benchmarks:

test outline_glyph_276_from_cff  ... bench:       1,041 ns/iter (+/- 71)
test outline_glyph_276_from_glyf ... bench:         674 ns/iter (+/- 15)
test from_data_otf_cff           ... bench:         403 ns/iter (+/- 3)
test outline_glyph_8_from_cff    ... bench:         339 ns/iter (+/- 44)
test outline_glyph_8_from_glyf   ... bench:         295 ns/iter (+/- 16)
test glyph_name_276              ... bench:         214 ns/iter (+/- 1)
test from_data_ttf               ... bench:         169 ns/iter (+/- 3)
test family_name                 ... bench:         155 ns/iter (+/- 5)
test glyph_index_u41             ... bench:          16 ns/iter (+/- 0)
test glyph_name_8                ... bench:           1 ns/iter (+/- 0)
test underline_metrics           ... bench:         0.5 ns/iter (+/- 0)
test units_per_em                ... bench:         0.5 ns/iter (+/- 0)
test strikeout_metrics           ... bench:         0.5 ns/iter (+/- 0)
test x_height                    ... bench:         0.4 ns/iter (+/- 0)
test ascender                    ... bench:         0.2 ns/iter (+/- 0)
test hor_advance                 ... bench:         0.2 ns/iter (+/- 0)
test hor_side_bearing            ... bench:         0.2 ns/iter (+/- 0)
test subscript_metrics           ... bench:         0.2 ns/iter (+/- 0)
test width                       ... bench:         0.2 ns/iter (+/- 0)

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

glyph_name_8 is faster that glyph_name_276, because for glyph indexes lower than 258 we are using predefined names, so no parsing is involved.

Safety

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

Modules

name_id

A list of name ID's.

Structs

Class

A value of Class Definition Table.

Font

A font data handle.

GlyphId

A type-safe wrapper for glyph ID.

LineMetrics

A line metrics.

Name

A Name Record.

Names

An iterator over font's names.

Rect

A rectangle.

ScriptMetrics

A script metrics used by subscript and superscript.

Enums

GlyphClass

A glyph class.

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

Parses the number of fonts stored in a TrueType font collection.