pub struct Database { /* private fields */ }
Expand description

A font database.

Implementations

Create a new, empty Database.

Generic font families would be set to:

  • serif - Times New Roman
  • sans-serif - Arial
  • cursive - Comic Sans MS
  • fantasy - Impact (Papyrus on macOS)
  • monospace - Courier New

Loads a font data into the Database.

Will load all font faces in case of a font collection.

Loads a font from the given source into the Database.

Will load all font faces in case of a font collection.

Loads a font file into the Database.

Will load all font faces in case of a font collection.

Loads font files from the selected directory into the Database.

This method will scan directories recursively.

Will load ttf, otf, ttc and otc fonts.

Unlike other load_* methods, this one doesn’t return an error. It will simply skip malformed fonts and will print a warning into the log for each of them.

Attempts to load system fonts.

Supports Windows, Linux and macOS.

System fonts loading is a surprisingly complicated task, mostly unsolvable without interacting with system libraries. And since fontdb tries to be small and portable, this method will simply scan some predefined directories. Which means that fonts that are not in those directories must be added manually.

Removes a font face by id from the database.

Returns false while attempting to remove a non-existing font face.

Useful when you want to ignore some specific font face(s) after loading a large directory with fonts. Or a specific face from a font.

Returns true if the Database contains no font faces.

Returns the number of font faces in the Database.

Note that Database stores font faces, not fonts. For example, if a caller will try to load a font collection (*.ttc) that contains 5 faces, then the Database will load 5 font faces and this method will return 5, not 1.

Sets the family that will be used by Family::Serif.

Sets the family that will be used by Family::SansSerif.

Sets the family that will be used by Family::Cursive.

Sets the family that will be used by Family::Fantasy.

Sets the family that will be used by Family::Monospace.

Performs a CSS-like query and returns the best matched font face.

Returns a reference to an internal storage.

This can be used for manual font matching.

Selects a FaceInfo by id.

Returns None if a face with such ID was already removed, or this ID belong to the other Database.

Returns font face storage and the face index by ID.

Executes a closure with a font’s data.

We can’t return a reference to a font binary data because of lifetimes. So instead, you can use this method to process font’s data.

The closure accepts raw font data and font face index.

In case of Source::File, the font file will be memory mapped.

Returns None when font file loading failed.

Example
let is_variable = db.with_face_data(id, |font_data, face_index| {
    let font = ttf_parser::Face::from_slice(font_data, face_index).unwrap();
    font.is_variable()
})?;

Makes the font data that backs the specified face id shared so that the application can hold a reference to it.

Safety

If the face originates from a file from disk, then the file is mapped from disk. This is unsafe as another process may make changes to the file on disk, which may become visible in this process’ mapping and possibly cause crashes.

If the underlying font provides multiple faces, then all faces are updated to participate in the data sharing. If the face was previously marked for data sharing, then this function will return a clone of the existing reference.

Transfers ownership of shared font data back to the font database. This is the reverse operation of Self::make_shared_face_data. If the font data belonging to the specified face is mapped from a file on disk, then that mapping is closed and the data becomes private to the process again.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.