[−][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
- (
cmap
) Character to glyph index mapping using glyph_index() method.
All subtable formats except Mixed Coverage (8) are supported. - (
cmap
) Character variation to glyph index mapping using glyph_variation_index() method. - (
glyf
) Glyph outlining using outline_glyph() method. - (
hmtx
) Retrieving glyph's horizontal metrics using glyph_hor_advance() and glyph_hor_side_bearing() methods. - (
vmtx
) Retrieving glyph's vertical metrics using glyph_ver_advance() and glyph_ver_side_bearing() methods. - (
kern
) Retrieving glyphs pair kerning using glyphs_kerning() method. - (
maxp
) Retrieving total number of glyphs using number_of_glyphs() method. - (
name
) Listing all name records using names() method. - (
name
) Retrieving font's family name using family_name() method. - (
name
) Retrieving font's PostScript name using post_script_name() method. - (
post
) Retrieving font's underline metrics using underline_metrics() method. - (
post
) Retrieving glyph's name using glyph_name() method. - (
head
) Retrieving font's units per EM value using units_per_em() method. - (
hhea
) Retrieving generic font info using: ascender(), descender(), height() and line_gap() methods.
Supported OpenType features
- (
CFF
) Glyph outlining using outline_glyph() method. - (
OS/2
) Retrieving font's kind using is_regular(), is_italic(), is_bold() and is_oblique() methods. - (
OS/2
) Retrieving font's weight using weight() method. - (
OS/2
) Retrieving font's width using width() method. - (
OS/2
) Retrieving font's X height using x_height() method. - (
OS/2
) Retrieving font's strikeout metrics using strikeout_metrics() method. - (
OS/2
) Retrieving font's subscript metrics using subscript_metrics() method. - (
OS/2
) Retrieving font's superscript metrics using superscript_metrics() method. - (
GDEF
) Retrieving glyph's class using glyph_class() method. - (
GDEF
) Retrieving glyph's mark attachment class using glyph_mark_attachment_class() method. - (
GDEF
) Checking that glyph is a mark using is_mark_glyph() method. - (
VORG
) Retrieving glyph's vertical origin using glyph_y_origin() method.
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. |