logo
pub struct Font<T: FontTableProvider> {
    pub font_table_provider: T,
    pub maxp_table: MaxpTable,
    pub hhea_table: HheaTable,
    pub cmap_subtable_encoding: Encoding,
    pub glyph_table_flags: GlyphTableFlags,
    /* private fields */
}
Expand description

Core type for loading a font in order to perform glyph mapping and font shaping.

Fields

font_table_provider: Tmaxp_table: MaxpTablehhea_table: HheaTablecmap_subtable_encoding: Encodingglyph_table_flags: GlyphTableFlags

Implementations

Construct a new instance from a type that can supply font tables.

Returns None if the font was able to be read but no supported cmap sub-table was able to be found. The lack of such a table prevents glyph mapping.

Returns the number of glyphs in the font.

Set the embedded image table filter.

When determining if a font contains embedded images, as well as retrieving images this value it used to set which tables are consulted. By default it is set to only consult tables that can contain colour images (CBDT/CBLC, sbix, and SVG). You can change the value to exclude certain tables or opt into tables that can only contain B&W images (EBDT/EBLC).

Look up the glyph index for the supplied character in the font.

Convenience method to shape the supplied glyphs.

The method maps applies glyph substitution (gsub) and glyph positioning (gpos). Use map_glyphs to turn text into glyphs that can be accepted by this method.

Arguments:

  • glyphs: the glyphs to be shaped in logical order.
  • script_tag: the OpenType script tag of the text.
  • opt_lang_tag: the OpenType language tag of the text.
  • features: the OpenType features to enable.
  • kerning: when applying gpos if this argument is true the kern OpenType feature is enabled for non-complex scripts. If it is false then the kern feature is not enabled for non-complex scripts.

Error Handling:

This method will continue on in the face of errors, applying what it can. If no errors are encountered this method returns Ok(Vec<Info>). If one or more errors are encountered Err((ShapingError, Vec<Info>)) is returned. The first error encountered is returned as the first item of the tuple. Vec<Info> is returned as the second item of the tuple, allowing consumers of the shaping output to proceed even if errors are encountered. However, in the error case the glyphs might not have had GPOS or GSUB applied.

Example
use allsorts::binary::read::ReadScope;
use allsorts::font::MatchingPresentation;
use allsorts::font_data::FontData;
use allsorts::gsub::{self, Features, FeatureMask};
use allsorts::DOTTED_CIRCLE;
use allsorts::{tag, Font};

let script = tag::LATN;
let lang = tag::DFLT;
let buffer = std::fs::read("tests/fonts/opentype/Klei.otf")
    .expect("unable to read Klei.otf");
let scope = ReadScope::new(&buffer);
let font_file = scope.read::<FontData<'_>>().expect("unable to parse font");
// Use a different index to access other fonts in a font collection (E.g. TTC)
let provider = font_file
    .table_provider(0)
    .expect("unable to create table provider");
let mut font = Font::new(provider)
    .expect("unable to load font tables")
    .expect("unable to find suitable cmap sub-table");

// Klei ligates ff
let glyphs = font.map_glyphs("Shaping in a jiffy.", script, MatchingPresentation::NotRequired);
let glyph_infos = font
    .shape(
        glyphs,
        script,
        Some(lang),
        &Features::Mask(FeatureMask::default()),
        true,
    )
    .expect("error shaping text");
// We expect ff to be ligated so the number of glyphs (18) should be one less than the
// number of input characters (19).
assert_eq!(glyph_infos.len(), 18);

Map text to glyphs.

This method maps text into glyphs, which can then be passed to shape. The text is processed in logical order, there is no need to reorder text before calling this method.

The match_presentation argument controls glyph mapping in the presence of emoji/text variation selectors. If MatchingPresentation::NotRequired is passed then glyph mapping will succeed if the font contains a mapping for a given character, regardless of whether it has the tables necessary to support the requested presentation. If MatchingPresentation::Required is passed then a character with emoji presentation, either by default or requested via variation selector will only map to a glyph if the font has mapping for the character, and it has the necessary tables for color emoji.

Find an image matching the supplied criteria.

  • glyph_index is the glyph to lookup.
  • target_ppem is the desired size. If an exact match can’t be found the nearest one will be returned, favouring being oversize vs. undersized.
  • max_bit_depth is the maximum accepted bit depth of the bitmap to return. If you accept all bit depths then use BitDepth::ThirtyTwo.

Returns true if the font contains embedded images in supported tables.

Allsorts supports extracting images from CBDT/CBLC, sbix, and SVG tables. If any of these tables are present and parsable then this method returns true.

Returns the horizontal advance of the supplied glyph index.

Will return None if there are errors encountered reading the hmtx table or there is no entry for the glyph index.

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 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.