pub struct Font {
pub sfnt_version: Tag,
pub flavor: Option<Flavor>,
pub flavor_data: Option<FlavorData>,
pub recalc_bboxes: bool,
pub recalc_timestamp: bool,
pub ignore_decompile_errors: bool,
pub disassemble_instructions: bool,
pub bitmap_glyph_data_format: String,
pub use_harfbuzz_repacker: Option<bool>,
pub write_gpos7: bool,
pub optimize_font_speed: bool,
/* private fields */
}Expand description
An OpenType font (TTFont).
Tables are decompiled on first access and recompiled on Font::write; tables
that are never accessed are copied through unchanged, as in fontTools.
Fields§
§sfnt_version: TagThe sfnt version: \0\x01\0\0 for TrueType outlines, OTTO for CFF.
flavor: Option<Flavor>The web font flavor to write (flavor).
flavor_data: Option<FlavorData>WOFF metadata and private data (flavorData).
recalc_bboxes: bool§recalc_timestamp: bool§ignore_decompile_errors: bool§disassemble_instructions: boolSet while dumping TTX (disassembleInstructions).
bitmap_glyph_data_format: StringSet while dumping TTX (bitmapGlyphDataFormat).
use_harfbuzz_repacker: Option<bool>Whether GSUB and GPOS are packed with the HarfBuzz repacker
(fontTools.ttLib.tables.otBase:USE_HARFBUZZ_REPACKER): None (the
default) uses it for tables before version 1.2, Some(true) always,
Some(false) never. fontTools does this when uharfbuzz is installed.
write_gpos7: boolWhether otlLib writes contextual positioning without context as GPOS
lookup type 7 (fontTools.otlLib.builder:WRITE_GPOS7). The default,
false, always writes type 8, for older macOS versions.
optimize_font_speed: boolWhether tables are compiled for rendering speed rather than size
(fontTools.ttLib:OPTIMIZE_FONT_SPEED): glyf coordinates use the widest
representation needed by any point of a glyph, the deltas of gvar and
VARC are not packed for size, and otlLib does not split SingleSubst
lookups to reduce their size. The default, false, optimizes for size.
Only tables that are decompiled are recompiled, as always.
Implementations§
Source§impl Font
impl Font
Sourcepub fn load_post_without_glyph_names(&mut self) -> Result<()>
pub fn load_post_without_glyph_names(&mut self) -> Result<()>
Loads post without decoding the names of a format 2 table, which becomes
format 3. This is the hack of fontTools.subset.load_font, which avoids
trouble with broken fonts when glyph names are not needed.
Sourcepub fn remap_glyphs(
&mut self,
f: &mut dyn FnMut(u32) -> Result<u32>,
new_order: Vec<String>,
) -> Result<()>
pub fn remap_glyphs( &mut self, f: &mut dyn FnMut(u32) -> Result<u32>, new_order: Vec<String>, ) -> Result<()>
Maps the glyph IDs of every loaded table with f, then sets the glyph
order to new_order (setGlyphOrder, where fontTools’ tables follow by
glyph name). Tables that were never loaded are written unchanged, as in
fontTools.
Sourcepub fn read(data: impl Into<Arc<[u8]>>, options: ReadOptions) -> Result<Self>
pub fn read(data: impl Into<Arc<[u8]>>, options: ReadOptions) -> Result<Self>
Reads a font from sfnt, TTC, WOFF or WOFF2 data (TTFont(file)).
Sourcepub fn reader(&self) -> Option<&SfntReader>
pub fn reader(&self) -> Option<&SfntReader>
The reader for the original file, if the font was read from one.
The tables in the font, sorted as the OpenType specification recommends
(keys(), without GlyphOrder).
Sourcepub fn load(&mut self, tag: Tag) -> Result<bool>
pub fn load(&mut self, tag: Tag) -> Result<bool>
Decompiles a table if it is not loaded yet. Returns whether the font has it.
Sourcepub fn family_tag<T: TableType>(&self) -> Tag
pub fn family_tag<T: TableType>(&self) -> Tag
The tag of a table type in this font: its beyond-64k companion if the font has
that ("GLYF" if "GLYF" in font else "glyf").
Sourcepub fn has<T: TableType>(&self) -> bool
pub fn has<T: TableType>(&self) -> bool
Whether the font has a table of a type, under either of its tags.
Sourcepub fn get<T: TableType>(&mut self) -> Result<Option<&mut T>>
pub fn get<T: TableType>(&mut self) -> Result<Option<&mut T>>
A table by type, decompiling it if necessary (font[tag]); None if the
font does not have it. A table with a beyond-64k companion can be either.
Sourcepub fn get_at<T: TableType>(&mut self, tag: Tag) -> Result<Option<&mut T>>
pub fn get_at<T: TableType>(&mut self, tag: Tag) -> Result<Option<&mut T>>
A table of a type under a tag.
Sourcepub fn locked_tag<T: TableType>(extended: bool) -> Tag
pub fn locked_tag<T: TableType>(extended: bool) -> Tag
The tag of a table type in the compact or the extended family, as the
locaTag and such of a table of that family.
Sourcepub fn get_locked<T: TableType>(
&mut self,
extended: bool,
) -> Result<Option<&mut T>>
pub fn get_locked<T: TableType>( &mut self, extended: bool, ) -> Result<Option<&mut T>>
A table of a type in the compact or the extended family.
Sourcepub fn require_locked<T: TableType>(&mut self, extended: bool) -> Result<&mut T>
pub fn require_locked<T: TableType>(&mut self, extended: bool) -> Result<&mut T>
Font::get_locked, failing if the font does not have it.
Sourcepub fn require<T: TableType>(&mut self) -> Result<&mut T>
pub fn require<T: TableType>(&mut self) -> Result<&mut T>
A table by type, failing if the font does not have it (font[tag]).
Sourcepub fn loaded<T: TableType>(&self) -> Option<&T>
pub fn loaded<T: TableType>(&self) -> Option<&T>
An already decompiled table by type, without decompiling.
Sourcepub fn get_dyn(&mut self, tag: Tag) -> Result<Option<&mut dyn Table>>
pub fn get_dyn(&mut self, tag: Tag) -> Result<Option<&mut dyn Table>>
A table by tag, decompiling it if necessary.
Sourcepub fn remove(&mut self, tag: Tag) -> bool
pub fn remove(&mut self, tag: Tag) -> bool
Removes a table (del font[tag]). Returns whether the font had it.
Sourcepub fn with_table<R>(
&mut self,
tag: Tag,
f: impl FnOnce(&mut dyn Table, &mut Font) -> Result<R>,
) -> Result<R>
pub fn with_table<R>( &mut self, tag: Tag, f: impl FnOnce(&mut dyn Table, &mut Font) -> Result<R>, ) -> Result<R>
Runs f with a loaded table detached from the font, so that f can also
access the rest of the font.
Sourcepub fn table_data(&mut self, tag: Tag) -> Result<Vec<u8>>
pub fn table_data(&mut self, tag: Tag) -> Result<Vec<u8>>
The binary data of a table: compiled if it is loaded, otherwise as read
(getTableData).
Sourcepub fn ensure_decompiled(&mut self) -> Result<()>
pub fn ensure_decompiled(&mut self) -> Result<()>
Decompiles every table (ensureDecompiled).
Sourcepub fn glyph_count(&mut self) -> Result<usize>
pub fn glyph_count(&mut self) -> Result<usize>
The number of glyphs, from maxp (getGlyphCount).
Sourcepub fn has_glyph_order(&self) -> bool
pub fn has_glyph_order(&self) -> bool
Whether the glyph order is set, or has been derived already.
Sourcepub fn gpos_compression_level(&self) -> u8
pub fn gpos_compression_level(&self) -> u8
How much otlLib compacts the class-based PairPos subtables it builds
(font.cfg["fontTools.otlLib.optimize.gpos:COMPRESSION_LEVEL"]), 0 to 9:
0 (the default) does not compact them.
Sourcepub fn set_gpos_compression_level(&mut self, level: u8) -> Result<()>
pub fn set_gpos_compression_level(&mut self, level: u8) -> Result<()>
Sets Font::gpos_compression_level
(font.cfg["fontTools.otlLib.optimize.gpos:COMPRESSION_LEVEL"] = level).
A level above 9 is rejected, as the validation of the config option does
(ConfigValueValidationError).
Sourcepub fn set_glyph_order(&mut self, names: Vec<String>)
pub fn set_glyph_order(&mut self, names: Vec<String>)
Sets the glyph order (setGlyphOrder).
Sourcepub fn glyph_order(&mut self) -> Result<Arc<GlyphOrder>>
pub fn glyph_order(&mut self) -> Result<Arc<GlyphOrder>>
The glyph order, deriving it from CFF , post or cmap if it has not
been set (getGlyphOrder).
Sourcepub fn get_best_cmap(
&mut self,
preferences: &[(u16, u16)],
) -> Result<Option<CharMap>>
pub fn get_best_cmap( &mut self, preferences: &[(u16, u16)], ) -> Result<Option<CharMap>>
The effective best Unicode character map of the font (getBestCmap): the
mappings of DMAP, except to glyph zero, over those of cmap. None if
neither has a Unicode subtable.
Sourcepub fn normalize_location(
&mut self,
location: &BTreeMap<String, f64>,
) -> Result<Location>
pub fn normalize_location( &mut self, location: &BTreeMap<String, f64>, ) -> Result<Location>
Normalizes a location in user coordinates (the axis ranges of fvar) to
the normalized −1…+1 space, with the mapping of avar if the font has one
(normalizeLocation). Every axis of fvar is in the result, except that a
version 2 avar drops the zeroes. Fails with “Not a variable font” if the
font has no fvar.
Sourcepub fn glyph_name(&mut self, gid: u32) -> Result<String>
pub fn glyph_name(&mut self, gid: u32) -> Result<String>
The name of a glyph (getGlyphName).
Sourcepub fn to_ttx(&mut self, options: &TtxOptions) -> Result<String>
pub fn to_ttx(&mut self, options: &TtxOptions) -> Result<String>
Dumps the font as TTX (saveXML to a stream). Fails with
TtxOptions::split_tables or TtxOptions::split_glyphs, which need
a file name: see Font::to_ttx_files.
Sourcepub fn to_ttx_files(
&mut self,
path: &str,
options: &TtxOptions,
) -> Result<Vec<TtxFile>>
pub fn to_ttx_files( &mut self, path: &str, options: &TtxOptions, ) -> Result<Vec<TtxFile>>
Dumps the font as TTX to be saved as path (saveXML(path)): returns the
file to save there, first, and, with TtxOptions::split_tables or
TtxOptions::split_glyphs, the table and glyph files to save next to
it, whose names are derived from path. Nothing is written to the file
system.
Sourcepub fn from_ttx(text: &str) -> Result<Self>
pub fn from_ttx(text: &str) -> Result<Self>
Reads a TTX document into a new font (TTFont() followed by importXML).
Sourcepub fn import_ttx(&mut self, text: &str) -> Result<()>
pub fn import_ttx(&mut self, text: &str) -> Result<()>
Merges a TTX document into the font (importXML).
Sourcepub fn import_ttx_with(
&mut self,
text: &str,
options: &TtxImportOptions,
) -> Result<()>
pub fn import_ttx_with( &mut self, text: &str, options: &TtxImportOptions, ) -> Result<()>
Font::import_ttx with options.
The files that a TTX document split into several files refers to (src
attributes) are read from the current directory, as fontTools does for a
stream without a file name.
Sourcepub fn import_ttx_file(&mut self, path: impl AsRef<Path>) -> Result<()>
pub fn import_ttx_file(&mut self, path: impl AsRef<Path>) -> Result<()>
Merges a TTX file into the font (importXML(path)), including the files
it refers to if it was split into several files.
Sourcepub fn import_ttx_split(
&mut self,
text: &str,
path: Option<&Path>,
options: &TtxImportOptions,
load: &mut TtxLoader<'_>,
) -> Result<()>
pub fn import_ttx_split( &mut self, text: &str, path: Option<&Path>, options: &TtxImportOptions, load: &mut TtxLoader<'_>, ) -> Result<()>
Merges a TTX document into the font (importXML), where path is its
file name, if any, and load reads the files it refers to (src
attributes, relative to the directory of the file that refers to them, or
to the current directory without a file name), as crate::ttx::read_ttx_file
does from the file system.
Source§impl Font
impl Font
Sourcepub fn open(
path: impl AsRef<Path>,
res_name_or_index: Option<&ResNameOrIndex>,
options: ReadOptions,
) -> Result<Font>
pub fn open( path: impl AsRef<Path>, res_name_or_index: Option<&ResNameOrIndex>, options: ReadOptions, ) -> Result<Font>
Reads a font file (TTFont(path, res_name_or_index)). With
res_name_or_index, the font is the sfnt resource of the file’s
resource fork that it names; ResNameOrIndex::Index 0 reads the first
one if there is any, else the file as an ordinary font file, as ttx does.
Without it, this is Font::read on the file’s data.
Source§impl Font
impl Font
Sourcepub fn reorder_glyphs(&mut self, new_glyph_order: Vec<String>) -> Result<()>
pub fn reorder_glyphs(&mut self, new_glyph_order: Vec<String>) -> Result<()>
Reorders the glyphs of the font (TTFont.reorderGlyphs); see
reorder_glyphs.