ttf-parser is a library for parsing TrueType/OpenType fonts.
The new version introduces full support for variable fonts.
(variable fonts) and C API, which is why I decided to promote it on the forum.
Until recently, if you needed to work with TrueType fonts, there were only two options: FreeType and stb_truetype. The first one is a huge behemoth, while the second supports a fairly limited set of functions.
ttf-parser is somewhere in between. It supports all the same TrueType tables (the TrueType format consists of many separate binary tables) as FreeType, but does not handle the rendering of the glyphs themselves.
At the same time, ttf-parser has many other significant differences:
- ttf-parser is written in Rust without using unsafe code. FreeType and stb_truetype are written in C.
- ttf-parser is the only memory-safe implementation. Reading arbitrary memory is impossible. FreeType constantly fixes vulnerabilities, and stb_truetype is not designed for reading arbitrary fonts.
- ttf-parser is the only thread-safe implementation. All parsing methods are constant. The only exception is setting coordinates for variable fonts, but this function is reentrant. FreeType is essentially single-threaded. stb_truetype is reentrant (you can use separate copies in different threads, but not one of many).
- ttf-parser is the only implementation that does not use heap allocation. This speeds up parsing and avoids problems during OOM.
- Additionally, almost all arithmetic operations and type casting are checked (including statically).
- In the worst case, the library may throw an exception. In the C API, exceptions will be caught and the function will return an error, but will not crash.
And despite all the safety guarantees, ttf-parser is also the fastest implementation. For example, parsing CFF2 is 3.5 times faster than in FreeType. Parsing glyf is 10% slower than in stb_truetype, but that’s because it does not support variable fonts, for which additional information storage is required. More details in README.
Source: linux.org.ru
