#ttf #truetype #opentype

ttf-parser

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

5 unstable releases

✓ Uses Rust 2018 edition

0.3.0 Sep 26, 2019
0.2.2 Aug 12, 2019
0.2.1 Aug 12, 2019
0.2.0 Jul 10, 2019
0.1.0 Jun 18, 2019

#26 in Parser implementations

Download history 80/week @ 2019-06-13 148/week @ 2019-06-20 66/week @ 2019-06-27 96/week @ 2019-07-04 68/week @ 2019-07-11 82/week @ 2019-07-18 40/week @ 2019-07-25 73/week @ 2019-08-01 235/week @ 2019-08-08 137/week @ 2019-08-15 67/week @ 2019-08-22 121/week @ 2019-08-29 114/week @ 2019-09-05 101/week @ 2019-09-12 120/week @ 2019-09-19

437 downloads per month
Used in 8 crates (2 directly)

MIT/Apache

185KB
4K SLoC

ttf-parser

Build Status Crates.io Documentation Rust 1.35+

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

Features

  • A high-level API.
  • Zero allocations.
  • Zero dependencies.
  • no_std compatible.
  • 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 60% more time to outline all glyphs in SourceSansPro-Regular.otf (which uses CFF) rather than in SourceSansPro-Regular.ttf.

test outline_cff  ... bench:   1,617,138 ns/iter (+/- 1,261)
test outline_glyf ... bench:     995,771 ns/iter (+/- 2,801)

Here is some methods benchmarks:

test outline_glyph_276_from_cff  ... bench:       1,203 ns/iter (+/- 4)
test outline_glyph_276_from_glyf ... bench:         796 ns/iter (+/- 3)
test outline_glyph_8_from_cff    ... bench:         497 ns/iter (+/- 3)
test from_data_otf               ... bench:         372 ns/iter (+/- 5)
test outline_glyph_8_from_glyf   ... bench:         347 ns/iter (+/- 1)
test family_name                 ... bench:         269 ns/iter (+/- 3)
test from_data_ttf               ... bench:          72 ns/iter (+/- 3)
test glyph_index_u41             ... bench:          24 ns/iter (+/- 0)
test glyph_2_hor_metrics         ... bench:           8 ns/iter (+/- 0)

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

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

test x_height            ... bench:         847 ns/iter (+/- 0)
test units_per_em        ... bench:         564 ns/iter (+/- 2)
test strikeout_metrics   ... bench:         564 ns/iter (+/- 0)
test width               ... bench:         287 ns/iter (+/- 0)
test ascender            ... bench:         279 ns/iter (+/- 1)
test subscript_metrics   ... bench:         279 ns/iter (+/- 0)

Safety

  • The library must not panic. Any panic considered as a critical bug and should be reported.
  • The library has a single unsafe block for array casting.

Alternatives

  • font-rs - Mainly a glyph outline extractor. No documentation. Has less features. Doesn't support CFF. Has a lot of magic numbers.
  • stb_truetype - Mainly a glyph outline extractor. Isn't allocation free. Has less features. Doesn't support CFF. Has a lot of magic numbers. Uses panic a lot.
  • truetype - Simply maps TrueType data to the Rust structures. Doesn't actually parses the data. Isn't allocation free. Has some unsafe. Unmaintained.
  • font - Very similar to ttf-parser, but supports less features. Still an alpha. Isn't allocation free.
  • fontdue - Parser and rasterizer. In alpha state. Allocates all the required data. Doesn't support CFF.

(revised on 2019-09-24)

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps