Skip to main content

FontDatabase

Struct FontDatabase 

Source
pub struct FontDatabase { /* private fields */ }
Expand description

Font faces loaded from bytes, addressed by FontId.

Implementations§

Source§

impl FontDb

Source

pub fn bi_level_advance(&self, id: FontId, glyph: u32, ppem: u16) -> Option<i32>

The advance of glyph of face id in whole pixels at ppem pixels per em, as bi-level rendering gives it: the linear advance where the face says that the glyph’s advance scales linearly at that size, else the advance that the glyph’s instructions hint.

Returns None for an unknown face or glyph, a face without TrueType outlines, or instructions that fail. The database keeps each answer, and a hinter for each size.

Source

pub fn device_width(&self, id: FontId, glyph: u32, ppem: u16) -> Option<i32>

The device width of glyph of face id in whole pixels at ppem pixels per em: the advance point after the glyph’s instructions in the interpreter’s device-width mode, rounded, which is the width that Word’s device steps a glyph by.

Returns None for an unknown face or glyph, a face without TrueType outlines, or instructions that fail. The database keeps each answer, and a hinter for each size.

Source§

impl FontDb

Source

pub fn add_files( &mut self, files: &[PathBuf], ) -> Result<Vec<FontId>, FileNotAdded>

Adds the fonts of the files files, in the order given, and returns the new IDs.

The faces take their IDs in the order of files, and so does the choice between two faces of equal style: the face query keeps the first (FontDb::query). The order is the caller’s, the order of its font sources, and never the files’ names. Each file must be a font file (is_font_file) that holds fonts that FontDb::add_font would take from its bytes. Of each file, only the table directories and the tables of the faces’ names, style and metrics are read here, and the bytes at the first choice of one of its faces (FontDb::load). This helper exists only in native builds.

§Errors

Returns the first file that is not added, with the cause: a file that is not a font file, one whose bytes are not fonts that the database takes, or one that cannot be read (a font file not added). The files before it stay added, and the files after it are not read.

Source§

impl FontDb

Source

pub fn glyph_id(&self, id: FontId, ch: char) -> Option<u32>

Returns the glyph that the font’s character map gives for ch.

The map comes from one cmap subtable. A Windows symbol subtable comes first, then full Unicode, then the Unicode basic plane, then Mac Roman. With a symbol subtable, a character from U+0000 to U+00FF that the subtable lacks is looked up again at U+F000 to U+F0FF. Returns None for an unknown face or an unmapped character.

Source

pub fn advance(&self, id: FontId, glyph: u32) -> Option<u16>

Returns the advance width of a glyph from hmtx, in font units and without hinting.

Returns None for an unknown face or a glyph that the font does not have.

Source

pub fn outline( &self, id: FontId, glyph: u32, ) -> Result<Vec<PathCommand>, OutlineError>

Returns the outline of a glyph in font units, without hinting.

A glyph without contours, such as a space, has an empty outline.

§Errors

Returns an OutlineError that names the cause for an unknown face, a glyph that the font does not have (the test of FontDb::advance), a font without a table of outlines, or an outline that does not read.

Source§

impl FontDb

Source

pub fn kerning_source(&self, id: FontId) -> KerningSource

Returns where the pair kerning of a face comes from: KerningSource::Gpos when its GPOS table has a kern feature for the default language of the Latin script, or of the default script, with at least one lookup, and KerningSource::KernTable otherwise.

Source

pub fn kerning(&self, id: FontId, left: u32, right: u32) -> i32

Returns the kerning between two glyphs in font units. A negative value moves the right glyph closer.

When the font’s GPOS table has a kern feature for the default language of the Latin script, or of the default script, the value is the sum of the horizontal advance adjustments that its pair lookups give the left glyph, in lookup order, where the first subtable of a lookup that covers the pair applies. Otherwise it is the sum of the horizontal subtables of the font’s kern table.

Returns 0 for an unknown face, a font without either table or a pair that the font does not list. Cross-stream and state machine subtables of the kern table, the lookup flags of GPOS and its contextual lookups are ignored.

Source

pub fn kerning_from( &self, id: FontId, left: u32, right: u32, source: KerningSource, ) -> i32

Returns the kerning between two glyphs in font units from one kerning source of the face, whatever its other source holds: the kern feature of GPOS as FontDb::kerning reads it, 0 when the face has none, or the sum of the horizontal subtables of the kern table, 0 when the face has none.

Source

pub fn has_kern_table(&self, id: FontId) -> bool

Returns whether a face has a kern table with a horizontal subtable of pairs (format 0, 2 or 3), whatever its GPOS table holds.

Source§

impl FontDb

Source

pub fn layout_values(&self, id: FontId, sizes: &[u16]) -> Option<LayoutValues>

Returns the layout values of face id, with the values in pixels at each size of sizes (pixels per em).

Each value comes from the method of the database that the layout calls for it. The pairs whose kerning it reads are those that the face’s tables list with a value; every other pair has the kerning 0. Returns None for an unknown face.

Source§

impl FontDb

Source

pub fn ligatures(&self, id: FontId, glyphs: &[u32]) -> Vec<Ligature>

Returns the standard ligatures (liga) of a sequence of glyphs from one face, in order and without overlaps. The lookups come from the default language of the Latin script, or of the default script, and apply in lookup order. Lookup flags are not read, so a mark between two glyphs keeps them apart.

Returns none for an unknown face or a font without a GSUB table.

Source§

impl FontDb

Source

pub fn math_accent(&self, id: FontId) -> Option<MathAccent>

Returns the constants of a font’s MATH table that choose and place an accent.

Returns None for an unknown face and a font without the table or its constants. The device tables of the values are ignored.

Source

pub fn top_accent_attachment(&self, id: FontId, glyph: u32) -> Option<i32>

Returns the top accent attachment of a glyph in the font’s MATH table (MathTopAccentAttachment), in font units from the glyph’s origin: where an accent over the glyph is centered.

Returns None for an unknown face, a font without the table, and a glyph that the table does not list. The device tables of the values are ignored.

Source

pub fn horizontal_construction( &self, id: FontId, glyph: u32, ) -> Option<HorizontalConstruction>

Returns how the font’s MATH table makes a glyph wider: its horizontal variants and the parts of its glyph assembly.

Returns None for an unknown face, a font without the table, and a glyph that the table does not make wider.

Source

pub fn math_radical(&self, id: FontId) -> Option<MathRadical>

Returns the constants of a font’s MATH table that place the root sign, the bar and the degree of a radical, and the room above them.

Returns None for an unknown face and a font without the table or its constants. The device tables of the values are ignored.

Source

pub fn math_bars(&self, id: FontId) -> Option<MathBars>

Returns the constants of a font’s MATH table that place an overbar and an underbar, and the room beyond them.

Returns None for an unknown face and a font without the table or its constants. The device tables of the values are ignored.

Source

pub fn math_skewed_fraction(&self, id: FontId) -> Option<MathSkewedFraction>

Returns the constants of a font’s MATH table that place the numerator and the denominator of a skewed fraction.

Returns None for an unknown face and a font without the table or its constants. The device tables of the values are ignored.

Source

pub fn math_limit_gaps(&self, id: FontId) -> Option<MathLimitGaps>

Returns the ink gap minimums of a font’s MATH table for lower and upper limits.

Returns None for an unknown face and a font without the table or its constants. The device tables of the values are ignored.

Source

pub fn math_stretch_stack(&self, id: FontId) -> Option<MathStretchStack>

Returns the constants of a font’s MATH table that place a grouping character and its base.

Returns None for an unknown face and a font without the table or its constants. The device tables of the values are ignored.

Source

pub fn math_limits(&self, id: FontId) -> Option<MathLimits>

Returns the constants of a font’s MATH table that place the limits of an n-ary operator in a display, and the room above them.

Returns None for an unknown face and a font without the table or its constants. The device tables of the values are ignored.

Source

pub fn italic_correction(&self, id: FontId, glyph: u32) -> Option<i32>

Returns the italic correction of a glyph in the font’s MATH table (MathItalicsCorrectionInfo), in font units: how far a superscript after the glyph starts right of a subscript.

Returns None for an unknown face, a font without the table, and a glyph that the table does not list. The device tables of the values are ignored.

Source

pub fn space_after_script(&self, id: FontId) -> Option<i32>

Returns the space that the font’s MATH table leaves after a subscript or a superscript (SpaceAfterScript), in font units.

Returns None for an unknown face and a font without the table or its constants. The device table of the value is ignored.

Source

pub fn math_kern( &self, id: FontId, glyph: u32, corner: MathCorner, height: i32, ) -> Option<i32>

Returns the kern of a corner of a glyph at a height in the font’s MATH table (MathKernInfo), in font units: how far a script at that height beside the corner moves toward the glyph, negative for a move away from it. height is in font units from the glyph’s baseline, up positive; a height at a band’s lower correction height takes that band’s kern.

Returns None for an unknown face, a font without the table, and a glyph whose corner the table does not list. The device tables of the values are ignored.

Source

pub fn display_variant(&self, id: FontId, glyph: u32) -> Option<u32>

Returns the glyph that a display draws for an n-ary operator: the first of the glyph’s vertical variants in the font’s MATH table whose height is at least the table’s DisplayOperatorMinHeight.

Returns None for an unknown face, a font without the table, and a glyph that has no such variant.

Source

pub fn vertical_variant( &self, id: FontId, glyph: u32, least: i32, ) -> Option<(u32, bool)>

Returns the first of the glyph’s vertical variants in the font’s MATH table whose height (its advance measurement) is at least least font units, with true; or else the tallest variant, with false. The variants start with the glyph itself in the fonts that list it, such as Cambria Math.

Returns None for an unknown face, a font without the table, and a glyph that has no vertical variants.

Source§

impl FontDb

Source

pub fn query( &self, family: &FamilyName, bold: bool, italic: bool, ) -> Option<FontId>

Finds a face of family that matches the style best: by name, as Self::query_with finds it, and else by typographic family, as Self::query_typographic_with finds it.

This never substitutes another family, because the rules for that belong to each format. A format that has stand-ins for a missing family tries them between the two steps, so that a face of another family in the same typographic family, such as Arial Narrow for Arial, comes after them.

Source

pub fn query_with( &self, family: &FamilyName, bold: bool, italic: bool, accept: impl Fn(FontId, &FaceInfo) -> bool, ) -> Option<FontId>

Finds a face whose name is family and that matches the style best, among only the faces that accept allows, given each face’s ID and names. A target’s font listing can exclude a locally installed face before style selection, so that a local bold face cannot hide a regular face that the target has, and a font choice can exclude a face whose file did not load.

A face belongs to family when one of its family names (name ID 1, in any language) is family, as FamilyName compares names; that name links the up to four styles of one family. Among those faces, one whose italic flag matches comes first. Then the face whose weight is closest to the requested one comes first, which is 700 for bold and 400 otherwise. Of faces that are still equal, one that the caller added comes before a built-in face (FaceOrigin), and then the first one added wins.

When no face belongs to family, family may be the PostScript name of a face, compared in the same way. The faces in the style group of that face (name ID 1) that have the requested style then compete in the same way, where a face is bold from weight 600. When none of them has the requested style, the face with the PostScript name is returned. Else family may be the full name of a face (name ID 4), such as “Arial Bold”, and that face is returned whatever the requested style. Of several faces with the PostScript name or the full name, the caller’s comes first. Returns None when no kind of name matches; a face whose typographic family (name ID 16) alone is family is not found here.

Source

pub fn query_typographic_with( &self, family: &FamilyName, bold: bool, italic: bool, accept: impl Fn(FontId, &FaceInfo) -> bool, ) -> Option<FontId>

Finds a face whose typographic family (name ID 16) is family and that matches the style best, among only the faces that accept allows, as Self::query_with ranks faces: the faces of other families that share a typographic family, such as Aptos Display in the typographic family Aptos. A format tries it after Self::query_with and after its stand-ins for a missing family.

Source§

impl FontDb

Source

pub fn script_glyph( &self, id: FontId, glyph: u32, level: ScriptLevel, ) -> Option<u32>

Returns the script glyph of a glyph: the glyph that the font’s ssty feature puts in its place at level. The lookups come from the default language of the math script, or of the default script, and apply in lookup order; an alternate substitution takes the first alternate for a script and the second for a script of a script.

Returns None for an unknown face, a font without the feature, and a glyph that the feature does not change.

Source

pub fn flattened_accent(&self, id: FontId, glyph: u32) -> Option<u32>

Returns the flattened form of an accent’s glyph: the glyph that the font’s flac feature puts in its place, over a base taller than the MATH table’s FlattenedAccentBaseHeight. The lookups come as for Self::script_glyph.

Returns None for an unknown face, a font without the feature, and a glyph that the feature does not change.

Source

pub fn dotless_form(&self, id: FontId, glyph: u32) -> Option<u32>

Returns the dotless form of a letter’s glyph: the glyph that the font’s dtls feature puts in its place, under an accent. The lookups come as for Self::script_glyph.

Returns None for an unknown face, a font without the feature, and a glyph that the feature does not change.

Source§

impl FontDb

Source

pub fn shape( &self, id: FontId, request: &ShapeRequest<'_>, ) -> Result<Vec<ShapedGlyph>, ShapeFailure>

Returns the glyphs of a text by the layout tables of a face, in the order of the text: the first glyph stands for the first characters, also in a text that reads from right to left. The caller orders a line.

§Errors

Returns the cause when the face is unknown or when the shaper refuses the text or the face. The caller then draws the characters with the glyphs of the font’s character map. A face without a GSUB and a GPOS table is no failure: the shaper then gives an Arabic letter the glyph of its presentation-form character, when the font has it.

Source§

impl FontDb

Source

pub fn device_heights(&self, id: FontId, pels: u16) -> Option<(i16, i16)>

Returns the largest height above the baseline and the largest depth below it, in pixels and with the signs that the font stores (yMax and yMin), that a face’s VDMX table gives at pels pixels per em on a device with square pixels.

The OpenType specification (VDMX) says how a ratio record matches a device: the record’s ratios, normalized to the device’s x resolution, must take in its y resolution, and a record of zeros matches every device. This reads the group of the first record that matches the device and that is for the Windows ANSI character set (bCharSet 1), in a table of either version. A group for all glyphs (bCharSet 0) does not count in either version.

Returns None for an unknown face, a face without such a group, or a group that does not list pels.

Source§

impl FontDb

Source

pub fn new() -> FontDb

Makes an empty database.

Source

pub fn add_font( &mut self, data: impl Into<Arc<[u8]>>, ) -> Result<Vec<FontId>, FontError>

Adds every face in an OpenType font or font collection, and returns their IDs in the order of the faces in the data.

When built with the woff2 feature, WOFF2 containers (wOF2) are transparently decompressed to OpenType before indexing. Full-table WOFF2 files preserve exact layout metrics, while web-subsetted WOFF2 files may omit glyphs or OpenType tables needed for exact Word line breaks.

This is the only way to load fonts in a WASM build. When one face fails to load, no face is added.

§Errors

Returns an error when the data is not an OpenType font or collection (or a valid WOFF2 font when woff2 is enabled), or when a face lacks the head, hhea or hmtx table.

Source

pub fn add_static( &mut self, data: &'static [u8], ) -> Result<Vec<FontId>, FontError>

Adds every face of an OpenType font or font collection compiled into the program, such as a built-in font, without a copy of its bytes, and returns their IDs in the order of the faces in the data. The faces read as the faces of FontDb::add_font do, also in a WASM build; of two faces with the same family, style and weight, FontDb::query takes the one that the caller added.

§Errors

As for FontDb::add_font; then no face is added.

Source

pub fn load(&self, id: FontId) -> Result<(), FileNotRead>

Loads the bytes of the file that holds a face, when they are not loaded yet.

Only a file that FontDb::add_files added loads here, at the first call for one of its faces, and only once: every later call for a face of the file, from any thread, gives the result of the first. The file’s length and modification time must still be the ones that add_files found. Every method that reads a face’s bytes loads them in the same way, and answers for a face whose file did not load as for an unknown face. A font choice calls this for the face that it chooses, so that such a face counts as missing and the choice searches again without it.

§Errors

Returns the file’s name in its folder and the cause when the file did not load. An unknown face has nothing to load and gives Ok.

Source

pub fn add_description(&mut self, description: Description)

Adds the description of a font family in the Compatifont format: its files with their SHA-256, and the other families that its faces can replace with the values that they share. The rules of the format crates read it to choose a family that draws a missing one, and to know a face that lays out as the target’s own.

Source

pub fn descriptions(&self) -> &[Description]

Returns the descriptions in the order they were added.

Source

pub fn len(&self) -> usize

Returns the number of faces.

Source

pub fn is_empty(&self) -> bool

Returns true when the database holds no face.

Source

pub fn faces(&self) -> impl Iterator<Item = (FontId, &FaceInfo)>

Returns every face with its ID, in the order they were added.

Source

pub fn info(&self, id: FontId) -> Option<&FaceInfo>

Returns the names and style of a face.

Source

pub fn origin(&self, id: FontId) -> Option<FaceOrigin>

Returns where a face came from.

Source

pub fn metrics(&self, id: FontId) -> Option<&FontMetrics>

Returns the font-wide metrics of a face, in font units.

Source

pub fn data(&self, id: FontId) -> Option<(&FontBytes, u32)>

Returns the bytes of the font file that holds a face, with the index of the face in it.

The bytes are the ones that FontDb::add_font or FontDb::add_static took, or that a file of a folder held, so the faces of one collection share them and differ by index. Output crates pass them on to embed the font. Returns None for an unknown face and a face whose file did not load (FontDb::load).

Trait Implementations§

Source§

impl Debug for FontDb

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for FontDb

Source§

fn default() -> FontDb

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for FontDb

§

impl !UnwindSafe for FontDb

§

impl Forget for FontDb

§

impl Freeze for FontDb

§

impl Move for FontDb

§

impl Send for FontDb
where RawVecInner: Send, &'static str: Send, String: Send, Arc<FaceSource>: Send, OnceLock<Option<ScalerFont>>: Send, OnceLock<Lookups>: Send, OnceLock<Vec<u16>>: Send, OnceLock<ScriptGlyphs>: Send, Mutex<BTreeMap<(u16, u16), Option<i32>>>: Send, Mutex<KeptHinters>: Send, NodeRef<Owned, String, String, LeafOrInternal>: Send, BoxRaw<(String, String)>: Send, NodeRef<Owned, Item, ItemResult, LeafOrInternal>: Send, BoxRaw<(Item, ItemResult)>: Send,

§

impl Sync for FontDb
where RawVecInner: Sync, String: Sync, Arc<FaceSource>: Sync, OnceLock<Option<ScalerFont>>: Sync, OnceLock<Lookups>: Sync, OnceLock<Vec<u16>>: Sync, OnceLock<ScriptGlyphs>: Sync, Mutex<BTreeMap<(u16, u16), Option<i32>>>: Sync, Mutex<KeptHinters>: Sync, NodeRef<Owned, String, String, LeafOrInternal>: Sync, BoxRaw<(String, String)>: Sync, NodeRef<Owned, Item, ItemResult, LeafOrInternal>: Sync, BoxRaw<(Item, ItemResult)>: Sync,

§

impl Unpin for FontDb
where *const u8 is !null: Unpin, &'static str: Unpin, String: Unpin, Arc<FaceSource>: Unpin, Align4<u32>: Unpin, &'static Font: Unpin, &'static (Glyf<'static>, Loca<'static>): Unpin, &'static SelectedGvar<'static>: Unpin, &'static GlyphMetricRecords<'static>: Unpin, &'static [F2Dot14]: Unpin, &'static [Fixed]: Unpin, &'static CffFontRef<'static>: Unpin, &'static Type1Font: Unpin, &'static Outlines<'static>: Unpin, Box<Font>: Unpin, Box<ScaleError>: Unpin, Box<TrueTypeHinter>: Unpin, Arc<LocatedState>: Unpin, Align1<u8>: Unpin, *const LeafNode<(u16, u16), Option<i32>> is !null: Unpin, Box<((u16, u16), Option<i32>)>: Unpin, Box<Hinter>: Unpin, *const LeafNode<String, String> is !null: Unpin, Box<(String, String)>: Unpin, *const LeafNode<Item, ItemResult> is !null: Unpin, Box<(Item, ItemResult)>: Unpin,

§

impl UnsafeUnpin for FontDb

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Finish for T

Source§

fn finish(self)

Does nothing but move self, equivalent to drop.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

Source§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
Source§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

Source§

fn to_owned_table(&self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.