39 releases (19 breaking)

0.19.0 Apr 2, 2024
0.18.0 Mar 20, 2024
0.17.0 Mar 15, 2024
0.15.1 Dec 18, 2023
0.0.2 Oct 14, 2022

#123 in Parser implementations

Download history 1603/week @ 2024-01-03 1472/week @ 2024-01-10 3097/week @ 2024-01-17 2339/week @ 2024-01-24 10244/week @ 2024-01-31 9560/week @ 2024-02-07 12527/week @ 2024-02-14 14300/week @ 2024-02-21 13477/week @ 2024-02-28 12336/week @ 2024-03-06 13419/week @ 2024-03-13 13309/week @ 2024-03-20 12573/week @ 2024-03-27 11339/week @ 2024-04-03 10949/week @ 2024-04-10 10268/week @ 2024-04-17

47,186 downloads per month
Used in 102 crates (4 directly)

MIT/Apache

2MB
42K SLoC

read-fonts

This crate handles parsing and reading of OpenType fonts. It is intended to be a high performance implementation suitable for tasks such as shaping, while still providing a convenient, high-level API.

Safety

Unsafe code is forbidden by a #![forbid(unsafe_code)] attribute in the root of the library.

codegen

Much of the code in this crate is generated automatically. This generated code lives in the generated directory. Each file in this directory is included in a module in the src directory, using Rust's include! macro. This allows us to seperate the generated code from any custom implementation code, while allowing them to exist in the same module.

what we generate

With certain exceptions that require manual handling, we generate code for each table, record, enum, and flags type for each portion of the spec that we cover.

tables

All tables are aliases of the type TableRef<Marker>, where Marker is a struct that indicates the type of the table. For instance, the GDEF table is defined as TableRef<GdefMarker>. TableRef itself is a wrapper around a slice of bytes, with the marker type providing typed access to those bytes.

The marker type can only be created from a specific byte slice, and is always associated with that slice. It is created through a parse method that performs a one-time validation of the slice, ensuring that all expected fields are present. This includes bounds checking any arrays, as well as ensuring the presence of fields the existence of which may depend on the table's version.

variable lengths and version-dependent fields

n.b: the design described below has not been benchmarked agaisnt the alternatives, and may change

For fields that have variable length, or which only exist in certain table versions, the marker struct has a corresponding field where that length or offset is stored. This means that at runtime there is no need to double check a version or length.

TableRef methods

For each table, we define methods on the type TableRef<Marker> that provide access to that table's fields. These methods use the marker type to determine the byte range of a given field, and then interpret those bytes as the appropriate type.

records

Unlike tables, which are essentially a set of methods for reading into a byte slice, records are in general represented as simple packed structs containing scalar types in big-endian encodings. This means that, in general, records are zerocopy types that can be cast from raw bytes.

The exception to this is when a record has variable length; in this case the record is still a simple struct, but cannot be cast from raw bytes, and must be copied.

flags and enums

For flags, we generate a type modeled on those generated by the bitflags! macro. For enums, we generate a raw Rust enum.

Dependencies

~0.5–1MB
~22K SLoC