| 1 | ## ttf-parser |
| 2 |  |
| 3 | [](https://crates.io/crates/ttf-parser) |
| 4 | [](https://docs.rs/ttf-parser) |
| 5 | [](https://www.rust-lang.org) |
| 6 |  |
| 7 | |
| 8 | A high-level, safe, zero-allocation font parser for |
| 9 | [TrueType](https://docs.microsoft.com/en-us/typography/truetype/), |
| 10 | [OpenType](https://docs.microsoft.com/en-us/typography/opentype/spec/), and |
| 11 | [AAT](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6AATIntro.html). |
| 12 | |
| 13 | Can be used as a Rust or C library. |
| 14 | |
| 15 | ### Features |
| 16 | |
| 17 | - A high-level API for most common properties, hiding all parsing and data resolving logic. |
| 18 | - A low-level, but safe API to access TrueType tables data. |
| 19 | - Highly configurable. You can disable most of the features, reducing binary size. |
| 20 | You can also parse TrueType tables separately, without loading the whole font/face. |
| 21 | - Zero heap allocations. |
| 22 | - Zero unsafe. |
| 23 | - Zero dependencies. |
| 24 | - `no_std`/WASM compatible. |
| 25 | - A basic [C API](./c-api). |
| 26 | - Fast. |
| 27 | - Stateless. All parsing methods are immutable. |
| 28 | - Simple and maintainable code (no magic numbers). |
| 29 | |
| 30 | ### Safety |
| 31 | |
| 32 | - The library must not panic. Any panic considered as a critical bug and should be reported. |
| 33 | - The library forbids unsafe code. |
| 34 | - No heap allocations, so crash due to OOM is not possible. |
| 35 | - All recursive methods have a depth limit. |
| 36 | - Technically, should use less than 64KiB of stack in the worst case scenario. |
| 37 | - Most of arithmetic operations are checked. |
| 38 | - Most of numeric casts are checked. |
| 39 | |
| 40 | ### Alternatives |
| 41 | |
| 42 | It's very hard to compare different libraries, so we are using table-based comparison. |
| 43 | There are roughly three types of TrueType tables: |
| 44 | |
| 45 | - A table with a list of properties (like `head`, `OS/2`, etc.).<br/> |
| 46 | If a library tries to parse it at all then we mark it as supported. |
| 47 | - A table that contains a single type of data (`glyf`, `CFF` (kinda), `hmtx`, etc.).<br/> |
| 48 | Can only be supported or not. |
| 49 | - A table that contains multiple subtables (`cmap`, `kern`, `GPOS`, etc.).<br/> |
| 50 | Can be partially supported and we note which subtables are actually supported. |
| 51 | |
| 52 | | Feature/Library | ttf-parser | FreeType | stb_truetype | |
| 53 | | ----------------- | :--------------------: | :-----------------: | :----------------------------: | |
| 54 | | Memory safe | ✓ | | | |
| 55 | | Thread safe | ✓ | | ~ (mostly reentrant) | |
| 56 | | Zero allocation | ✓ | | | |
| 57 | | Variable fonts | ✓ | ✓ | | |
| 58 | | Rendering | -<sup>1</sup> | ✓ | ~ (very primitive) | |
| 59 | | `ankr` table | ✓ | | | |
| 60 | | `avar` table | ✓ | ✓ | | |
| 61 | | `bdat` table | ~ (no 4) | ✓ | | |
| 62 | | `bloc` table | ✓ | ✓ | | |
| 63 | | `CBDT` table | ~ (no 8, 9) | ✓ | | |
| 64 | | `CBLC` table | ✓ | ✓ | | |
| 65 | | `COLR` table | ✓ | ✓ | | |
| 66 | | `CPAL` table | ✓ | ✓ | | |
| 67 | | `CFF ` table | ✓ | ✓ | ~ (no `seac` support) | |
| 68 | | `CFF2` table | ✓ | ✓ | | |
| 69 | | `cmap` table | ~ (no 8) | ✓ | ~ (no 2,8,10,14; Unicode-only) | |
| 70 | | `EBDT` table | ~ (no 8, 9) | ✓ | | |
| 71 | | `EBLC` table | ✓ | ✓ | | |
| 72 | | `feat` table | ✓ | | | |
| 73 | | `fvar` table | ✓ | ✓ | | |
| 74 | | `gasp` table | | ✓ | | |
| 75 | | `GDEF` table | ~ | | | |
| 76 | | `glyf` table | ~<sup>2</sup> | ✓ | ~<sup>2</sup> | |
| 77 | | `GPOS` table | ✓ | | ~ (only 2) | |
| 78 | | `GSUB` table | ✓ | | | |
| 79 | | `gvar` table | ✓ | ✓ | | |
| 80 | | `head` table | ✓ | ✓ | ✓ | |
| 81 | | `hhea` table | ✓ | ✓ | ✓ | |
| 82 | | `hmtx` table | ✓ | ✓ | ✓ | |
| 83 | | `HVAR` table | ✓ | ✓ | | |
| 84 | | `kern` table | ✓ | ~ (only 0) | ~ (only 0) | |
| 85 | | `kerx` table | ✓ | | | |
| 86 | | `MATH` table | ✓ | | | |
| 87 | | `maxp` table | ✓ | ✓ | ✓ | |
| 88 | | `morx` table | ✓ | | | |
| 89 | | `MVAR` table | ✓ | ✓ | | |
| 90 | | `name` table | ✓ | ✓ | | |
| 91 | | `OS/2` table | ✓ | ✓ | | |
| 92 | | `post` table | ✓ | ✓ | | |
| 93 | | `sbix` table | ~ (PNG only) | ~ (PNG only) | | |
| 94 | | `STAT` table | ✓ | | | |
| 95 | | `SVG ` table | ✓ | ✓ | ✓ | |
| 96 | | `trak` table | ✓ | | | |
| 97 | | `vhea` table | ✓ | ✓ | | |
| 98 | | `vmtx` table | ✓ | ✓ | | |
| 99 | | `VORG` table | ✓ | ✓ | | |
| 100 | | `VVAR` table | ✓ | ✓ | | |
| 101 | | Language | Rust + C API | C | C | |
| 102 | | Tested version | 0.17.0 | 2.12.0 | 1.24 | |
| 103 | | License | MIT / Apache-2.0 | FTL / GPLv2 | public domain | |
| 104 | |
| 105 | Legend: |
| 106 | |
| 107 | - ✓ - supported |
| 108 | - ~ - partial |
| 109 | - *nothing* - not supported |
| 110 | |
| 111 | Notes: |
| 112 | |
| 113 | 1. While `ttf-parser` doesn't support rendering by itself, |
| 114 | there are multiple rendering libraries on top of it: |
| 115 | [rusttype](https://gitlab.redox-os.org/redox-os/rusttype), |
| 116 | [ab-glyph](https://github.com/alexheretic/ab-glyph) |
| 117 | and [fontdue](https://github.com/mooman219/fontdue). |
| 118 | 2. Matching points are not supported. |
| 119 | |
| 120 | ### Performance |
| 121 | |
| 122 | TrueType fonts designed for fast querying, so most of the methods are very fast. |
| 123 | The main exception is glyph outlining. Glyphs can be stored using two different methods: |
| 124 | using [Glyph Data Format](https://docs.microsoft.com/en-us/typography/opentype/spec/glyf) |
| 125 | and [Compact Font Format](http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/font/pdfs/5176.CFF.pdf) (pdf). |
| 126 | The first one is fairly simple which makes it faster to process. |
| 127 | The second one is basically a tiny language with a stack-based VM, which makes it way harder to process. |
| 128 | |
| 129 | The [benchmark](./benches/outline/) tests how long it takes to outline all glyphs in a font. |
| 130 | |
| 131 | x86 (AMD 3700X) |
| 132 | |
| 133 | | Table/Library | ttf-parser | FreeType | stb_truetype | |
| 134 | | ------------- | -------------: | ---------: | -------------: | |
| 135 | | `glyf` | `0.901 ms` | `1.171 ms` | **`0.675 ms`** | |
| 136 | | `gvar` | **`2.972 ms`** | `4.132 ms` | - | |
| 137 | | `CFF` | **`1.197 ms`** | `5.647 ms` | `2.813 ms` | |
| 138 | | `CFF2` | **`1.968 ms`** | `6.392 ms` | - | |
| 139 | |
| 140 | ARM (Apple M1) |
| 141 | |
| 142 | | Table/Library | ttf-parser | FreeType | stb_truetype | |
| 143 | | ------------- | -------------: | ---------: | -------------: | |
| 144 | | `glyf` | **`0.550 ms`** | `0.854 ms` | `0.703 ms` | |
| 145 | | `gvar` | **`2.270 ms`** | `4.594 ms` | - | |
| 146 | | `CFF` | **`1.054 ms`** | `5.223 ms` | `3.262 ms` | |
| 147 | | `CFF2` | **`1.765 ms`** | `5.995 ms` | - | |
| 148 | |
| 149 | **Note:** FreeType is surprisingly slow, so I'm worried that I've messed something up. |
| 150 | |
| 151 | And here are some methods benchmarks: |
| 152 | |
| 153 | ```text |
| 154 | test outline_glyph_276_from_cff2 ... bench: 867 ns/iter (+/- 15) |
| 155 | test from_data_otf_cff ... bench: 968 ns/iter (+/- 13) |
| 156 | test from_data_otf_cff2 ... bench: 887 ns/iter (+/- 25) |
| 157 | test outline_glyph_276_from_cff ... bench: 678 ns/iter (+/- 41) |
| 158 | test outline_glyph_276_from_glyf ... bench: 649 ns/iter (+/- 11) |
| 159 | test outline_glyph_8_from_cff2 ... bench: 534 ns/iter (+/- 14) |
| 160 | test from_data_ttf ... bench: 467 ns/iter (+/- 11) |
| 161 | test glyph_name_post_276 ... bench: 223 ns/iter (+/- 5) |
| 162 | test outline_glyph_8_from_cff ... bench: 315 ns/iter (+/- 13) |
| 163 | test outline_glyph_8_from_glyf ... bench: 291 ns/iter (+/- 5) |
| 164 | test family_name ... bench: 183 ns/iter (+/- 102) |
| 165 | test glyph_name_cff_276 ... bench: 62 ns/iter (+/- 1) |
| 166 | test glyph_index_u41 ... bench: 16 ns/iter (+/- 0) |
| 167 | test glyph_name_cff_8 ... bench: 5 ns/iter (+/- 0) |
| 168 | test glyph_name_post_8 ... bench: 2 ns/iter (+/- 0) |
| 169 | test subscript_metrics ... bench: 2 ns/iter (+/- 0) |
| 170 | test glyph_hor_advance ... bench: 2 ns/iter (+/- 0) |
| 171 | test glyph_hor_side_bearing ... bench: 2 ns/iter (+/- 0) |
| 172 | test glyph_name_8 ... bench: 1 ns/iter (+/- 0) |
| 173 | test ascender ... bench: 1 ns/iter (+/- 0) |
| 174 | test underline_metrics ... bench: 1 ns/iter (+/- 0) |
| 175 | test strikeout_metrics ... bench: 1 ns/iter (+/- 0) |
| 176 | test x_height ... bench: 1 ns/iter (+/- 0) |
| 177 | test units_per_em ... bench: 0.5 ns/iter (+/- 0) |
| 178 | test width ... bench: 0.2 ns/iter (+/- 0) |
| 179 | ``` |
| 180 | |
| 181 | ### License |
| 182 | |
| 183 | Licensed under either of |
| 184 | |
| 185 | - Apache License, Version 2.0 |
| 186 | ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 187 | - MIT license |
| 188 | ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 189 | |
| 190 | at your option. |
| 191 | |
| 192 | ### Contribution |
| 193 | |
| 194 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 195 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 196 | dual licensed as above, without any additional terms or conditions. |