#font #true-type #css #db #data-query

no-std fontdb

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

38 releases (22 breaking)

0.23.0 Oct 9, 2024
0.21.0 Aug 6, 2024
0.20.0 Jul 2, 2024
0.16.2 Feb 19, 2024
0.2.0 Jul 21, 2020

#1 in Data formats

Download history 45813/week @ 2024-08-04 47425/week @ 2024-08-11 46522/week @ 2024-08-18 48246/week @ 2024-08-25 52730/week @ 2024-09-01 51617/week @ 2024-09-08 51063/week @ 2024-09-15 59416/week @ 2024-09-22 59821/week @ 2024-09-29 60805/week @ 2024-10-06 59720/week @ 2024-10-13 60681/week @ 2024-10-20 56236/week @ 2024-10-27 62269/week @ 2024-11-03 47104/week @ 2024-11-10 45245/week @ 2024-11-17

214,198 downloads per month
Used in 277 crates (38 directly)

MIT license

110KB
898 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

~2MB
~46K SLoC