Skip to main content

Font

Struct Font 

Source
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: Tag

The 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: bool

Set while dumping TTX (disassembleInstructions).

§bitmap_glyph_data_format: String

Set 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: bool

Whether 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: bool

Whether 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

Source

pub fn new() -> Self

An empty TrueType font (TTFont()).

Source

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.

Source

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.

Source

pub fn read(data: impl Into<Arc<[u8]>>, options: ReadOptions) -> Result<Self>

Reads a font from sfnt, TTC, WOFF or WOFF2 data (TTFont(file)).

Source

pub fn reader(&self) -> Option<&SfntReader>

The reader for the original file, if the font was read from one.

Source

pub fn contains(&self, tag: Tag) -> bool

Whether the font has a table (tag in font).

Source

pub fn is_loaded(&self, tag: Tag) -> bool

Whether a table has been decompiled (isLoaded).

Source

pub fn tags(&self) -> Vec<Tag>

The tables in the font, sorted as the OpenType specification recommends (keys(), without GlyphOrder).

Source

pub fn load(&mut self, tag: Tag) -> Result<bool>

Decompiles a table if it is not loaded yet. Returns whether the font has it.

Source

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

Source

pub fn has<T: TableType>(&self) -> bool

Whether the font has a table of a type, under either of its tags.

Source

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.

Source

pub fn get_at<T: TableType>(&mut self, tag: Tag) -> Result<Option<&mut T>>

A table of a type under a tag.

Source

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.

Source

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.

Source

pub fn require_locked<T: TableType>(&mut self, extended: bool) -> Result<&mut T>

Font::get_locked, failing if the font does not have it.

Source

pub fn require<T: TableType>(&mut self) -> Result<&mut T>

A table by type, failing if the font does not have it (font[tag]).

Source

pub fn loaded<T: TableType>(&self) -> Option<&T>

An already decompiled table by type, without decompiling.

Source

pub fn get_dyn(&mut self, tag: Tag) -> Result<Option<&mut dyn Table>>

A table by tag, decompiling it if necessary.

Source

pub fn insert(&mut self, table: Box<dyn Table>)

Adds or replaces a table (font[tag] = table).

Source

pub fn remove(&mut self, tag: Tag) -> bool

Removes a table (del font[tag]). Returns whether the font had it.

Source

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.

Source

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

Source

pub fn ensure_decompiled(&mut self) -> Result<()>

Decompiles every table (ensureDecompiled).

Source

pub fn glyph_count(&mut self) -> Result<usize>

The number of glyphs, from maxp (getGlyphCount).

Source

pub fn has_glyph_order(&self) -> bool

Whether the glyph order is set, or has been derived already.

Source

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.

Source

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

Source

pub fn set_glyph_order(&mut self, names: Vec<String>)

Sets the glyph order (setGlyphOrder).

Source

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

Source

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.

Source

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.

Source

pub fn glyph_name(&mut self, gid: u32) -> Result<String>

The name of a glyph (getGlyphName).

Source

pub fn glyph_id(&mut self, name: &str) -> Result<u32>

The glyph ID for a name (getGlyphID).

Source

pub fn write(&mut self, options: &WriteOptions) -> Result<Vec<u8>>

Compiles the font (save).

Source

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.

Source

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.

Source

pub fn from_ttx(text: &str) -> Result<Self>

Reads a TTX document into a new font (TTFont() followed by importXML).

Source

pub fn import_ttx(&mut self, text: &str) -> Result<()>

Merges a TTX document into the font (importXML).

Source

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.

Source

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.

Source

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

Source

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

Source

pub fn reorder_glyphs(&mut self, new_glyph_order: Vec<String>) -> Result<()>

Reorders the glyphs of the font (TTFont.reorderGlyphs); see reorder_glyphs.

Trait Implementations§

Source§

impl Clone for Font

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Font

Source§

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

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

impl Default for Font

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl !RefUnwindSafe for Font

§

impl !UnwindSafe for Font

§

impl Forget for Font

§

impl Freeze for Font
where *const ArcInner<[u8]> is !null: Freeze, PhantomData<ArcInner<[u8]>>: Freeze, *const u8 is !null: Freeze, PhantomData<u8>: Freeze, PhantomData<Tag>: Freeze, PhantomData<DirectoryEntry>: Freeze, *const ArcInner<Woff2Data> is !null: Freeze, PhantomData<ArcInner<Woff2Data>>: Freeze, *const LeafNode<Tag, Slot> is !null: Freeze, PhantomData<(Owned, LeafOrInternal)>: Freeze, PhantomData<Box<(Tag, Slot)>>: Freeze, *const ArcInner<GlyphOrder> is !null: Freeze, PhantomData<ArcInner<GlyphOrder>>: Freeze, PhantomData<Option<String>>: Freeze,

§

impl Move for Font

§

impl Send for Font
where Arc<[u8]>: Send, RawVecInner: Send, Arc<Woff2Data>: Send, NodeRef<Owned, Tag, Slot, LeafOrInternal>: Send, BoxRaw<(Tag, Slot)>: Send, Arc<GlyphOrder>: Send,

§

impl Sync for Font
where Arc<[u8]>: Sync, RawVecInner: Sync, Arc<Woff2Data>: Sync, NodeRef<Owned, Tag, Slot, LeafOrInternal>: Sync, BoxRaw<(Tag, Slot)>: Sync, Arc<GlyphOrder>: Sync,

§

impl Unpin for Font
where Arc<[u8]>: Unpin, *const u8 is !null: Unpin, Arc<Woff2Data>: Unpin, *const LeafNode<Tag, Slot> is !null: Unpin, Box<(Tag, Slot)>: Unpin, Arc<GlyphOrder>: Unpin,

§

impl UnsafeUnpin for Font

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> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

self as &dyn Any.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

self as &mut dyn Any.
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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.