feaLib: Read and write OpenType feature files
Overview
fontTools’ feaLib
allows for the creation and parsing of Adobe
Font Development Kit for OpenType feature (.fea
) files. The syntax
of these files is described here.
.fea
files are primarily used for writing human-readable
definitions for the OpenType features stored in a font’s GSUB
and
GPOS
tables.
Supporting modules
feaLib contains modules for parsing and inspecting .fea
files
as well as utilities for converting .fea
rules into GSUB
and
GPOS
tables and inserting them into fonts.
The fontTools.feaLib.parser.Parser
class can be used to parse files
into an abstract syntax tree, and from there the
fontTools.feaLib.builder.Builder
class can add features to an existing
font file. You can inspect the parsed syntax tree, walk the tree and do clever
things with it, and also generate your own feature files programmatically, by
using the classes in the fontTools.feaLib.ast
module.
fontTools.feaLib.builder
- fontTools.feaLib.builder.addOpenTypeFeatures(font, featurefile, tables=None, debug=False)[source]
Add features from a file to a font. Note that this replaces any features currently present.
- Parameters:
font (feaLib.ttLib.TTFont) – The font object.
featurefile – Either a path or file object (in which case we parse it into an AST), or a pre-parsed AST instance.
tables – If passed, restrict the set of affected tables to those in the list.
debug – Whether to add source debugging information to the font in the
Debg
table
- fontTools.feaLib.builder.addOpenTypeFeaturesFromString(font, features, filename=None, tables=None, debug=False)[source]
Add features from a string to a font. Note that this replaces any features currently present.
- Parameters:
font (feaLib.ttLib.TTFont) – The font object.
features – A string containing feature code.
filename – The directory containing
filename
is used as the root of relativeinclude()
paths; ifNone
is provided, the current directory is assumed.tables – If passed, restrict the set of affected tables to those in the list.
debug – Whether to add source debugging information to the font in the
Debg
table
- class fontTools.feaLib.builder.Builder(font, featurefile)[source]
Bases:
object
- supportedTables = frozenset({'BASE', 'GDEF', 'GPOS', 'GSUB', 'OS/2', 'STAT', 'head', 'hhea', 'name', 'vhea'})
- find_lookup_builders_(lookups)[source]
Helper for building chain contextual substitutions
Given a list of lookup names, finds the LookupBuilder for each name. If an input name is None, it gets mapped to a None LookupBuilder.
fontTools.feaLib.lookupDebugInfo
- class fontTools.feaLib.lookupDebugInfo.LookupDebugInfo(location: str, name: str, feature: list)[source]
Bases:
NamedTuple
Information about where a lookup came from, to be embedded in a font
- location: str
Alias for field number 0
- name: str
Alias for field number 1
- feature: list
Alias for field number 2
fontTools.feaLib.error
- exception fontTools.feaLib.error.IncludedFeaNotFound(message, location=None)[source]
Bases:
FeatureLibError