parser: Lexing and parsing OpenType feature files
Overview
The primary interface for processing .fea files is
fontTools.feaLib.parser.Parser. At a lower level, the
fontTools.feaLib.lexer module implements feaLib’s lexical
analysis of the .fea language syntax, augmented by several smaller
utility modules.
Parsing
- class fontTools.feaLib.parser.Parser(featurefile, glyphNames=(), followIncludes=True, includeDir=None, **kwargs)[source]
Bases:
objectInitializes a Parser object.
Example
from fontTools.feaLib.parser import Parser parser = Parser(file, font.getReverseGlyphMap()) parsetree = parser.parse()
Note: the
glyphNamesiterable serves a double role to help distinguish glyph names from ranges in the presence of hyphens and to ensure that glyph names referenced in a feature file are actually part of a font’s glyph set. If the iterable is left empty, no glyph name in glyph set checking takes place, and all glyph tokens containing hyphens are treated as literal glyph names, not as ranges. (Adding a space around the hyphen can, in any case, help to disambiguate ranges from glyph names containing hyphens.)By default, the parser will follow
include()statements in the feature file. To turn this off, passfollowIncludes=False. Pass a directory string asincludeDirto explicitly declare a directory to search included feature files in.- extensions = {}
- ast = <module 'fontTools.feaLib.ast' from '/usr/lib/python3/dist-packages/fontTools/feaLib/ast.py'>
- SS_FEATURE_TAGS = {'ss01', 'ss02', 'ss03', 'ss04', 'ss05', 'ss06', 'ss07', 'ss08', 'ss09', 'ss10', 'ss11', 'ss12', 'ss13', 'ss14', 'ss15', 'ss16', 'ss17', 'ss18', 'ss19', 'ss20'}
- CV_FEATURE_TAGS = {'cv01', 'cv02', 'cv03', 'cv04', 'cv05', 'cv06', 'cv07', 'cv08', 'cv09', 'cv10', 'cv11', 'cv12', 'cv13', 'cv14', 'cv15', 'cv16', 'cv17', 'cv18', 'cv19', 'cv20', 'cv21', 'cv22', 'cv23', 'cv24', 'cv25', 'cv26', 'cv27', 'cv28', 'cv29', 'cv30', 'cv31', 'cv32', 'cv33', 'cv34', 'cv35', 'cv36', 'cv37', 'cv38', 'cv39', 'cv40', 'cv41', 'cv42', 'cv43', 'cv44', 'cv45', 'cv46', 'cv47', 'cv48', 'cv49', 'cv50', 'cv51', 'cv52', 'cv53', 'cv54', 'cv55', 'cv56', 'cv57', 'cv58', 'cv59', 'cv60', 'cv61', 'cv62', 'cv63', 'cv64', 'cv65', 'cv66', 'cv67', 'cv68', 'cv69', 'cv70', 'cv71', 'cv72', 'cv73', 'cv74', 'cv75', 'cv76', 'cv77', 'cv78', 'cv79', 'cv80', 'cv81', 'cv82', 'cv83', 'cv84', 'cv85', 'cv86', 'cv87', 'cv88', 'cv89', 'cv90', 'cv91', 'cv92', 'cv93', 'cv94', 'cv95', 'cv96', 'cv97', 'cv98', 'cv99'}
- parse()[source]
Parse the file, and return a
fontTools.feaLib.ast.FeatureFileobject representing the root of the abstract syntax tree containing the parsed contents of the file.
- parse_name_()[source]
Parses a name record. See section 9.e.
- parse_featureNames_(tag)[source]
Parses a
featureNamesstatement found in stylistic set features. See section 8.c.
- check_glyph_name_in_glyph_set(*names)[source]
Adds a glyph name (just start) or glyph names of a range (start and end) which are not in the glyph set to the “missing list” for future error reporting.
If no glyph set is present, does nothing.
Lexing
- class fontTools.feaLib.lexer.IncludingLexer(featurefile, *, includeDir=None)
Bases:
objectA Lexer that follows include statements.
The OpenType feature file specification states that due to historical reasons, relative imports should be resolved in this order:
If the source font is UFO format, then relative to the UFO’s font directory
relative to the top-level include file
relative to the parent include file
We only support 1 (via includeDir) and 2.
- static make_lexer_(file_or_path)
- next(self)
- scan_anonymous_block(self, tag)
- class fontTools.feaLib.lexer.Lexer(text, filename)
Bases:
object- ANONYMOUS_BLOCK = 'ANONYMOUS_BLOCK'
- CHAR_DIGIT_ = '0123456789'
- CHAR_HEXDIGIT_ = '0123456789ABCDEFabcdef'
- CHAR_LETTER_ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
- CHAR_NAME_CONTINUATION_ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.+*:^~!/-'
- CHAR_NAME_START_ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_+*:.^~!\\'
- CHAR_NEWLINE_ = '\r\n'
- CHAR_SYMBOL_ = ",;:-+'{}[]<>()="
- CHAR_WHITESPACE_ = ' \t'
- CID = 'CID'
- COMMENT = 'COMMENT'
- FILENAME = 'FILENAME'
- FLOAT = 'FLOAT'
- GLYPHCLASS = 'GLYPHCLASS'
- HEXADECIMAL = 'HEXADECIMAL'
- MODE_FILENAME_ = 'FILENAME'
- MODE_NORMAL_ = 'NORMAL'
- NAME = 'NAME'
- NEWLINE = 'NEWLINE'
- NUMBER = 'NUMBER'
- NUMBERS = ('NUMBER', 'HEXADECIMAL', 'OCTAL')
- OCTAL = 'OCTAL'
- RE_GLYPHCLASS = re.compile('^[A-Za-z_0-9.\\-]+$')
- STRING = 'STRING'
- SYMBOL = 'SYMBOL'
- location_(self)
- next(self)
- next_(self)
- scan_anonymous_block(self, tag)
- scan_over_(self, valid)
- scan_until_(self, stop_at)
- class fontTools.feaLib.lexer.NonIncludingLexer(featurefile, *, includeDir=None)
Bases:
IncludingLexerLexer that does not follow include statements, emits them as-is.
fontTools.feaLib.variableScalar
- fontTools.feaLib.variableScalar.LocationTuple
A hashable location.
alias of
tuple[tuple[str,float], …]
- fontTools.feaLib.variableScalar.Location(location: Mapping[str, float]) tuple[tuple[str, float], ...][source]
Create a hashable location from a dictionary-like location.
- class fontTools.feaLib.variableScalar.VariableScalar(location_value=None)[source]
Bases:
objectA scalar with different values at different points in the designspace.
- values: dict[tuple[tuple[str, float], ...], int]
The values across various user-locations. Must always include the default location by time of building.
- property does_vary: bool
- class fontTools.feaLib.variableScalar.VariableScalarBuilder(axis_triples: dict[str, tuple[float, float, float]], axis_mappings: dict[str, Mapping[float, float]], model_cache: dict[tuple[tuple[tuple[str, float], ...], ...], VariationModel])[source]
Bases:
objectA helper class for building variable scalars, or otherwise interrogating their variation model for interpolation or similar.
- axis_triples: dict[str, tuple[float, float, float]]
Minimum, default, and maximum for each axis in user-coordinates.
- axis_mappings: dict[str, Mapping[float, float]]
Optional mappings from normalized user-coordinates to normalized design-coordinates.
- model_cache: dict[tuple[tuple[tuple[str, float], ...], ...], VariationModel]
We often use the same exact locations (i.e. font sources) for a large number of variable scalars. Instead of creating a model for each, cache them. Cache by user-location to avoid repeated mapping computations.
- classmethod from_designspace(doc: DesignSpaceDocument) Self[source]
- default_value(scalar: VariableScalar) int[source]
Get the default value of a variable scalar.
- value_at_location(scalar: VariableScalar, location: tuple[tuple[str, float], ...]) float[source]
Interpolate the value of a scalar from a user-location.
- model(scalar: VariableScalar) VariationModel[source]
Return a variation model based on a scalar’s values.
Variable scalars with the same fully-specified user-locations will use the same cached variation model.
- get_deltas_and_supports(scalar: VariableScalar)[source]
Calculate deltas and supports from this scalar’s variation model.
- add_to_variation_store(scalar: VariableScalar, store_builder) tuple[int, int][source]
Serialize this scalar’s variation model to a store, returning the default value and variation index.