#font #true-type #css #db #unique-id

no-std fontdb

A simple, in-memory font database with CSS-like queries

31 unstable releases

0.16.2 Feb 19, 2024
0.16.0 Oct 31, 2023
0.14.1 May 10, 2023
0.13.0 Feb 21, 2023
0.2.0 Jul 21, 2020

#2 in Data formats

Download history 13233/week @ 2023-12-23 16769/week @ 2023-12-30 19364/week @ 2024-01-06 27620/week @ 2024-01-13 26985/week @ 2024-01-20 27845/week @ 2024-01-27 25670/week @ 2024-02-03 28271/week @ 2024-02-10 36411/week @ 2024-02-17 29841/week @ 2024-02-24 28195/week @ 2024-03-02 31114/week @ 2024-03-09 28950/week @ 2024-03-16 29500/week @ 2024-03-23 25007/week @ 2024-03-30 19626/week @ 2024-04-06

107,331 downloads per month
Used in 183 crates (24 directly)

MIT license

110KB
823 lines

fontdb

Build Status Crates.io Documentation Rust 1.60+

fontdb is a simple, in-memory font database with CSS-like queries.

Features

  • The database can load fonts from files, directories and raw data (Vec<u8>).
  • The database can match a font using CSS-like queries. See Database::query.
  • The database can try to load system fonts. Currently, this is implemented by scanning predefined directories. The library does not interact with the system API.
  • Provides a unique ID for each font face.

Non-goals

  • Advanced font properties querying.
    The database provides only storage and matching capabilities. For font properties querying you can use ttf-parser.

  • A font fallback mechanism.
    This library can be used to implement a font fallback mechanism, but it doesn't implement one.

  • Application's global database.
    The database doesn't use static, therefore it's up to the caller where it should be stored.

  • Font types support other than TrueType.

Font vs Face

A font is a collection of font faces. Therefore, a font face is a subset of a font. A simple font (*.ttf/*.otf) usually contains a single font face, but a font collection (*.ttc) can contain multiple font faces.

fontdb stores and matches font faces, not fonts. Therefore, after loading a font collection with 5 faces (for example), the database will be populated with 5 FaceInfo objects, all of which will be pointing to the same file or binary data.

Performance

The database performance is largely limited by the storage itself. We are using ttf-parser, so the parsing should not be a bottleneck.

For example, on Mac Book Pro 14 with M1 Pro, it takes just ~24ms to load 1361 font faces.

Safety

The library relies on memory-mapped files, which is inherently unsafe. But since we do not keep the files open it should be perfectly safe.

If you would like to use a persistent memory mapping of the font files, then you can use the unsafe Database::make_shared_face_data function.

License

MIT

Dependencies

~1.5MB
~34K SLoC