diff --git a/Lib/fontTools/feaLib/ast.py b/Lib/fontTools/feaLib/ast.py index 9d0c0870c1c..af23ecd4257 100644 --- a/Lib/fontTools/feaLib/ast.py +++ b/Lib/fontTools/feaLib/ast.py @@ -1262,11 +1262,21 @@ def build(self, builder): if not self.replacement and hasattr(self.glyph, "glyphSet"): for glyph in self.glyph.glyphSet(): builder.add_multiple_subst( - self.location, prefix, glyph, suffix, self.replacement, self.forceChain + self.location, + prefix, + glyph, + suffix, + self.replacement, + self.forceChain, ) else: builder.add_multiple_subst( - self.location, prefix, self.glyph, suffix, self.replacement, self.forceChain + self.location, + prefix, + self.glyph, + suffix, + self.replacement, + self.forceChain, ) def asFea(self, indent=""): @@ -2061,18 +2071,26 @@ def asFea(self, res="", indent=""): res += indent + "}" + f" {self.name};\n" return res + class VariationBlock(Block): """A variation feature block, applicable in a given set of conditions.""" def __init__(self, name, conditionset, use_extension=False, location=None): Block.__init__(self, location) - self.name, self.conditionset, self.use_extension = name, conditionset, use_extension + self.name, self.conditionset, self.use_extension = ( + name, + conditionset, + use_extension, + ) def build(self, builder): """Call the ``start_feature`` callback on the builder object, visit all the statements in this feature, and then call ``end_feature``.""" builder.start_feature(self.location, self.name) - if self.conditionset != "NULL" and self.conditionset not in builder.conditionsets_: + if ( + self.conditionset != "NULL" + and self.conditionset not in builder.conditionsets_ + ): raise FeatureLibError( f"variation block used undefined conditionset {self.conditionset}", self.location, @@ -2084,7 +2102,9 @@ def build(self, builder): builder.features_ = {} Block.build(self, builder) for key, value in builder.features_.items(): - items = builder.feature_variations_.setdefault(key,{}).setdefault(self.conditionset,[]) + items = builder.feature_variations_.setdefault(key, {}).setdefault( + self.conditionset, [] + ) items.extend(value) if key not in features: features[key] = [] # Ensure we make a feature record @@ -2100,5 +2120,3 @@ def asFea(self, indent=""): res += Block.asFea(self, indent=indent) res += indent + "} %s;\n" % self.name.strip() return res - - diff --git a/Lib/fontTools/feaLib/parser.py b/Lib/fontTools/feaLib/parser.py index 50e3d65ae27..6020b158ff0 100644 --- a/Lib/fontTools/feaLib/parser.py +++ b/Lib/fontTools/feaLib/parser.py @@ -385,8 +385,7 @@ def parse_glyphclass_(self, accept_glyphname, accept_null=False): self.expect_symbol_("-") range_end = self.expect_cid_() self.check_glyph_name_in_glyph_set( - f"cid{range_start:05d}", - f"cid{range_end:05d}", + f"cid{range_start:05d}", f"cid{range_end:05d}", ) glyphs.add_cid_range( range_start, @@ -482,7 +481,7 @@ def parse_glyph_pattern_(self, vertical): raise FeatureLibError( "Positioning cannot be applied in the bactrack glyph sequence, " "before the marked glyph sequence.", - self.cur_token_location_ + self.cur_token_location_, ) marked_values = values[len(prefix) : len(prefix) + len(glyphs)] if any(marked_values): @@ -491,7 +490,7 @@ def parse_glyph_pattern_(self, vertical): "Positioning values are allowed only in the marked glyph " "sequence, or after the final glyph node when only one glyph " "node is marked.", - self.cur_token_location_ + self.cur_token_location_, ) values = marked_values elif values and values[-1]: @@ -500,7 +499,7 @@ def parse_glyph_pattern_(self, vertical): "Positioning values are allowed only in the marked glyph " "sequence, or after the final glyph node when only one glyph " "node is marked.", - self.cur_token_location_ + self.cur_token_location_, ) values = values[-1:] elif any(values): @@ -508,7 +507,7 @@ def parse_glyph_pattern_(self, vertical): "Positioning values are allowed only in the marked glyph " "sequence, or after the final glyph node when only one glyph " "node is marked.", - self.cur_token_location_ + self.cur_token_location_, ) return (prefix, glyphs, lookups, values, suffix, hasMarks) @@ -1010,8 +1009,8 @@ def parse_size_parameters_(self): location = self.cur_token_location_ DesignSize = self.expect_decipoint_() SubfamilyID = self.expect_number_() - RangeStart = 0. - RangeEnd = 0. + RangeStart = 0.0 + RangeEnd = 0.0 if self.next_token_type_ in (Lexer.NUMBER, Lexer.FLOAT) or SubfamilyID != 0: RangeStart = self.expect_decipoint_() RangeEnd = self.expect_decipoint_() @@ -1590,11 +1589,20 @@ def parse_device_(self): return result def is_next_value_(self): - return self.next_token_type_ is Lexer.NUMBER or self.next_token_ == "<" or self.next_token_ == "(" + return ( + self.next_token_type_ is Lexer.NUMBER + or self.next_token_ == "<" + or self.next_token_ == "(" + ) def parse_valuerecord_(self, vertical): - if (self.next_token_type_ is Lexer.SYMBOL and self.next_token_ == "(") or self.next_token_type_ is Lexer.NUMBER: - number, location = self.expect_number_(variable=True), self.cur_token_location_ + if ( + self.next_token_type_ is Lexer.SYMBOL and self.next_token_ == "(" + ) or self.next_token_type_ is Lexer.NUMBER: + number, location = ( + self.expect_number_(variable=True), + self.cur_token_location_, + ) if vertical: val = self.ast.ValueRecord( yAdvance=number, vertical=vertical, location=location @@ -1879,7 +1887,9 @@ def parse_conditionset_(self): axis = self.cur_token_ if axis in conditions: - raise FeatureLibError(f"Repeated condition for axis {axis}", self.cur_token_location_) + raise FeatureLibError( + f"Repeated condition for axis {axis}", self.cur_token_location_ + ) if self.next_token_type_ is Lexer.FLOAT: min_value = self.expect_float_() @@ -1898,9 +1908,7 @@ def parse_conditionset_(self): finalname = self.expect_name_() if finalname != name: - raise FeatureLibError( - 'Expected "%s"' % name, self.cur_token_location_ - ) + raise FeatureLibError('Expected "%s"' % name, self.cur_token_location_) return self.ast.ConditionsetStatement(name, conditions) def parse_block_( @@ -2142,7 +2150,7 @@ def expect_number_(self, variable=False): raise FeatureLibError("Expected a number", self.cur_token_location_) def expect_variable_scalar_(self): - self.advance_lexer_() # "(" + self.advance_lexer_() # "(" scalar = VariableScalar() while True: if self.cur_token_type_ == Lexer.SYMBOL and self.cur_token_ == ")": @@ -2159,15 +2167,19 @@ def expect_master_(self): axis = self.cur_token_ self.advance_lexer_() if not (self.cur_token_type_ is Lexer.SYMBOL and self.cur_token_ == "="): - raise FeatureLibError("Expected an equals sign", self.cur_token_location_) + raise FeatureLibError( + "Expected an equals sign", self.cur_token_location_ + ) value = self.expect_number_() location[axis] = value if self.next_token_type_ is Lexer.NAME and self.next_token_[0] == ":": # Lexer has just read the value as a glyph name. We'll correct it later break self.advance_lexer_() - if not(self.cur_token_type_ is Lexer.SYMBOL and self.cur_token_ == ","): - raise FeatureLibError("Expected an comma or an equals sign", self.cur_token_location_) + if not (self.cur_token_type_ is Lexer.SYMBOL and self.cur_token_ == ","): + raise FeatureLibError( + "Expected an comma or an equals sign", self.cur_token_location_ + ) self.advance_lexer_() self.advance_lexer_() value = int(self.cur_token_[1:]) diff --git a/Lib/fontTools/feaLib/variableScalar.py b/Lib/fontTools/feaLib/variableScalar.py index db19245815e..a286568eee4 100644 --- a/Lib/fontTools/feaLib/variableScalar.py +++ b/Lib/fontTools/feaLib/variableScalar.py @@ -16,10 +16,10 @@ def __init__(self, location_value={}): def __repr__(self): items = [] - for location,value in self.values.items(): - loc = ",".join(["%s=%i" % (ax,loc) for ax,loc in location]) + for location, value in self.values.items(): + loc = ",".join(["%s=%i" % (ax, loc) for ax, loc in location]) items.append("%s:%i" % (loc, value)) - return "("+(" ".join(items))+")" + return "(" + (" ".join(items)) + ")" @property def does_vary(self): @@ -29,7 +29,9 @@ def does_vary(self): @property def axes_dict(self): if not self.axes: - raise ValueError(".axes must be defined on variable scalar before interpolating") + raise ValueError( + ".axes must be defined on variable scalar before interpolating" + ) return {ax.axisTag: ax for ax in self.axes} def _normalized_location(self, location): @@ -59,7 +61,9 @@ def add_value(self, location, value): self.values[Location(location)] = value def fix_all_locations(self): - self.values = {Location(self.fix_location(l)): v for l,v in self.values.items()} + self.values = { + Location(self.fix_location(l)): v for l, v in self.values.items() + } @property def default(self): diff --git a/Tests/feaLib/builder_test.py b/Tests/feaLib/builder_test.py index 40d46f0b09c..16674fe3675 100644 --- a/Tests/feaLib/builder_test.py +++ b/Tests/feaLib/builder_test.py @@ -1,6 +1,9 @@ from fontTools.misc.loggingTools import CapturingLogHandler -from fontTools.feaLib.builder import Builder, addOpenTypeFeatures, \ - addOpenTypeFeaturesFromString +from fontTools.feaLib.builder import ( + Builder, + addOpenTypeFeatures, + addOpenTypeFeaturesFromString, +) from fontTools.feaLib.error import FeatureLibError from fontTools.ttLib import TTFont, newTable from fontTools.feaLib.parser import Parser @@ -81,7 +84,7 @@ class BuilderTest(unittest.TestCase): VARFONT_AXES = [ ("wght", 200, 200, 1000, "Weight"), - ("wdth", 100, 100, 200, "Width") + ("wdth", 100, 100, 200, "Width"), ] def __init__(self, methodName): @@ -108,8 +111,7 @@ def temp_path(self, suffix): if not self.tempdir: self.tempdir = tempfile.mkdtemp() self.num_tempfiles += 1 - return os.path.join(self.tempdir, - "tmp%d%s" % (self.num_tempfiles, suffix)) + return os.path.join(self.tempdir, "tmp%d%s" % (self.num_tempfiles, suffix)) def read_ttx(self, path): lines = [] @@ -124,8 +126,21 @@ def read_ttx(self, path): def expect_ttx(self, font, expected_ttx, replace=None): path = self.temp_path(suffix=".ttx") - font.saveXML(path, tables=['head', 'name', 'BASE', 'GDEF', 'GSUB', - 'GPOS', 'OS/2', 'STAT', 'hhea', 'vhea']) + font.saveXML( + path, + tables=[ + "head", + "name", + "BASE", + "GDEF", + "GSUB", + "GPOS", + "OS/2", + "STAT", + "hhea", + "vhea", + ], + ) actual = self.read_ttx(path) expected = self.read_ttx(expected_ttx) if replace: @@ -134,7 +149,8 @@ def expect_ttx(self, font, expected_ttx, replace=None): expected[i] = expected[i].replace(k, v) if actual != expected: for line in difflib.unified_diff( - expected, actual, fromfile=expected_ttx, tofile=path): + expected, actual, fromfile=expected_ttx, tofile=path + ): sys.stderr.write(line) self.fail("TTX output is different from expected") @@ -155,7 +171,7 @@ def check_feature_file(self, name): # Check that: # 1) tables do compile (only G* tables as long as we have a mock font) # 2) dumping after save-reload yields the same TTX dump as before - for tag in ('GDEF', 'GSUB', 'GPOS'): + for tag in ("GDEF", "GSUB", "GPOS"): if tag in font: data = font[tag].compile(font) font[tag].decompile(data, font) @@ -164,11 +180,11 @@ def check_feature_file(self, name): debugttx = self.getpath("%s-debug.ttx" % name) if os.path.exists(debugttx): addOpenTypeFeatures(font, feapath, debug=True) - self.expect_ttx(font, debugttx, replace = {"__PATH__": feapath}) + self.expect_ttx(font, debugttx, replace={"__PATH__": feapath}) def check_fea2fea_file(self, name, base=None, parser=Parser): font = makeTTFont() - fname = (name + ".fea") if '.' not in name else name + fname = (name + ".fea") if "." not in name else name p = parser(self.getpath(fname), glyphNames=font.getGlyphOrder()) doc = p.parse() actual = self.normal_fea(doc.asFea().split("\n")) @@ -178,12 +194,16 @@ def check_fea2fea_file(self, name, base=None, parser=Parser): if expected != actual: fname = name.rsplit(".", 1)[0] + ".fea" for line in difflib.unified_diff( - expected, actual, - fromfile=fname + " (expected)", - tofile=fname + " (actual)"): - sys.stderr.write(line+"\n") - self.fail("Fea2Fea output is different from expected. " - "Generated:\n{}\n".format("\n".join(actual))) + expected, + actual, + fromfile=fname + " (expected)", + tofile=fname + " (actual)", + ): + sys.stderr.write(line + "\n") + self.fail( + "Fea2Fea output is different from expected. " + "Generated:\n{}\n".format("\n".join(actual)) + ) def normal_fea(self, lines): output = [] @@ -208,13 +228,14 @@ def normal_fea(self, lines): def test_alternateSubst_multipleSubstitutionsForSameGlyph(self): self.assertRaisesRegex( FeatureLibError, - "Already defined alternates for glyph \"A\"", + 'Already defined alternates for glyph "A"', self.build, "feature test {" " sub A from [A.alt1 A.alt2];" " sub B from [B.alt1 B.alt2 B.alt3];" " sub A from [A.alt1 A.alt2];" - "} test;") + "} test;", + ) def test_singleSubst_multipleIdenticalSubstitutionsForSameGlyph_info(self): logger = logging.getLogger("fontTools.feaLib.builder") @@ -224,19 +245,23 @@ def test_singleSubst_multipleIdenticalSubstitutionsForSameGlyph_info(self): " sub A by A.sc;" " sub B by B.sc;" " sub A by A.sc;" - "} test;") - captor.assertRegex('Removing duplicate single substitution from glyph "A" to "A.sc"') + "} test;" + ) + captor.assertRegex( + 'Removing duplicate single substitution from glyph "A" to "A.sc"' + ) def test_multipleSubst_multipleSubstitutionsForSameGlyph(self): self.assertRaisesRegex( FeatureLibError, - "Already defined substitution for glyph \"f_f_i\"", + 'Already defined substitution for glyph "f_f_i"', self.build, "feature test {" " sub f_f_i by f f i;" " sub c_t by c t;" " sub f_f_i by f_f i;" - "} test;") + "} test;", + ) def test_multipleSubst_multipleIdenticalSubstitutionsForSameGlyph_info(self): logger = logging.getLogger("fontTools.feaLib.builder") @@ -246,8 +271,11 @@ def test_multipleSubst_multipleIdenticalSubstitutionsForSameGlyph_info(self): " sub f_f_i by f f i;" " sub c_t by c t;" " sub f_f_i by f f i;" - "} test;") - captor.assertRegex(r"Removing duplicate multiple substitution from glyph \"f_f_i\" to \('f', 'f', 'i'\)") + "} test;" + ) + captor.assertRegex( + r"Removing duplicate multiple substitution from glyph \"f_f_i\" to \('f', 'f', 'i'\)" + ) def test_pairPos_redefinition_warning(self): # https://github.com/fonttools/fonttools/issues/1147 @@ -261,17 +289,18 @@ def test_pairPos_redefinition_warning(self): " pos yacute semicolon -70;" " enum pos @Y_LC semicolon -80;" " pos @Y_LC @SMALL_PUNC -100;" - "} kern;") + "} kern;" + ) captor.assertRegex("Already defined position for pair yacute semicolon") # the first definition prevails: yacute semicolon -70 st = font["GPOS"].table.LookupList.Lookup[0].SubTable[0] self.assertEqual(st.Coverage.glyphs[2], "yacute") - self.assertEqual(st.PairSet[2].PairValueRecord[0].SecondGlyph, - "semicolon") - self.assertEqual(vars(st.PairSet[2].PairValueRecord[0].Value1), - {"XAdvance": -70}) + self.assertEqual(st.PairSet[2].PairValueRecord[0].SecondGlyph, "semicolon") + self.assertEqual( + vars(st.PairSet[2].PairValueRecord[0].Value1), {"XAdvance": -70} + ) def test_singleSubst_multipleSubstitutionsForSameGlyph(self): self.assertRaisesRegex( @@ -281,127 +310,153 @@ def test_singleSubst_multipleSubstitutionsForSameGlyph(self): "feature test {" " sub [a-z] by [A.sc-Z.sc];" " sub e by e.fina;" - "} test;") + "} test;", + ) def test_singlePos_redefinition(self): self.assertRaisesRegex( FeatureLibError, - "Already defined different position for glyph \"A\"", - self.build, "feature test { pos A 123; pos A 456; } test;") + 'Already defined different position for glyph "A"', + self.build, + "feature test { pos A 123; pos A 456; } test;", + ) def test_feature_outside_aalt(self): self.assertRaisesRegex( FeatureLibError, 'Feature references are only allowed inside "feature aalt"', - self.build, "feature test { feature test; } test;") + self.build, + "feature test { feature test; } test;", + ) def test_feature_undefinedReference(self): self.assertRaisesRegex( - FeatureLibError, 'Feature none has not been defined', - self.build, "feature aalt { feature none; } aalt;") + FeatureLibError, + "Feature none has not been defined", + self.build, + "feature aalt { feature none; } aalt;", + ) def test_GlyphClassDef_conflictingClasses(self): self.assertRaisesRegex( - FeatureLibError, "Glyph X was assigned to a different class", + FeatureLibError, + "Glyph X was assigned to a different class", self.build, "table GDEF {" " GlyphClassDef [a b], [X], , ;" " GlyphClassDef [a b X], , , ;" - "} GDEF;") + "} GDEF;", + ) def test_languagesystem(self): builder = Builder(makeTTFont(), (None, None)) - builder.add_language_system(None, 'latn', 'FRA') - builder.add_language_system(None, 'cyrl', 'RUS') - builder.start_feature(location=None, name='test') - self.assertEqual(builder.language_systems, - {('latn', 'FRA'), ('cyrl', 'RUS')}) + builder.add_language_system(None, "latn", "FRA") + builder.add_language_system(None, "cyrl", "RUS") + builder.start_feature(location=None, name="test") + self.assertEqual(builder.language_systems, {("latn", "FRA"), ("cyrl", "RUS")}) def test_languagesystem_duplicate(self): self.assertRaisesRegex( FeatureLibError, '"languagesystem cyrl RUS" has already been specified', - self.build, "languagesystem cyrl RUS; languagesystem cyrl RUS;") + self.build, + "languagesystem cyrl RUS; languagesystem cyrl RUS;", + ) def test_languagesystem_none_specified(self): builder = Builder(makeTTFont(), (None, None)) - builder.start_feature(location=None, name='test') - self.assertEqual(builder.language_systems, {('DFLT', 'dflt')}) + builder.start_feature(location=None, name="test") + self.assertEqual(builder.language_systems, {("DFLT", "dflt")}) def test_languagesystem_DFLT_dflt_not_first(self): self.assertRaisesRegex( FeatureLibError, - "If \"languagesystem DFLT dflt\" is present, " + 'If "languagesystem DFLT dflt" is present, ' "it must be the first of the languagesystem statements", - self.build, "languagesystem latn TRK; languagesystem DFLT dflt;") + self.build, + "languagesystem latn TRK; languagesystem DFLT dflt;", + ) def test_languagesystem_DFLT_not_preceding(self): self.assertRaisesRegex( FeatureLibError, - "languagesystems using the \"DFLT\" script tag must " + 'languagesystems using the "DFLT" script tag must ' "precede all other languagesystems", self.build, "languagesystem DFLT dflt; " "languagesystem latn dflt; " - "languagesystem DFLT fooo; " + "languagesystem DFLT fooo; ", ) def test_script(self): builder = Builder(makeTTFont(), (None, None)) - builder.start_feature(location=None, name='test') - builder.set_script(location=None, script='cyrl') - self.assertEqual(builder.language_systems, {('cyrl', 'dflt')}) + builder.start_feature(location=None, name="test") + builder.set_script(location=None, script="cyrl") + self.assertEqual(builder.language_systems, {("cyrl", "dflt")}) def test_script_in_aalt_feature(self): self.assertRaisesRegex( FeatureLibError, - "Script statements are not allowed within \"feature aalt\"", - self.build, "feature aalt { script latn; } aalt;") + 'Script statements are not allowed within "feature aalt"', + self.build, + "feature aalt { script latn; } aalt;", + ) def test_script_in_size_feature(self): self.assertRaisesRegex( FeatureLibError, - "Script statements are not allowed within \"feature size\"", - self.build, "feature size { script latn; } size;") + 'Script statements are not allowed within "feature size"', + self.build, + "feature size { script latn; } size;", + ) def test_script_in_standalone_lookup(self): self.assertRaisesRegex( FeatureLibError, "Script statements are not allowed within standalone lookup blocks", - self.build, "lookup test { script latn; } test;") + self.build, + "lookup test { script latn; } test;", + ) def test_language(self): builder = Builder(makeTTFont(), (None, None)) - builder.add_language_system(None, 'latn', 'FRA ') - builder.start_feature(location=None, name='test') - builder.set_script(location=None, script='cyrl') - builder.set_language(location=None, language='RUS ', - include_default=False, required=False) - self.assertEqual(builder.language_systems, {('cyrl', 'RUS ')}) - builder.set_language(location=None, language='BGR ', - include_default=True, required=False) - self.assertEqual(builder.language_systems, - {('cyrl', 'BGR ')}) - builder.start_feature(location=None, name='test2') - self.assertEqual(builder.language_systems, {('latn', 'FRA ')}) + builder.add_language_system(None, "latn", "FRA ") + builder.start_feature(location=None, name="test") + builder.set_script(location=None, script="cyrl") + builder.set_language( + location=None, language="RUS ", include_default=False, required=False + ) + self.assertEqual(builder.language_systems, {("cyrl", "RUS ")}) + builder.set_language( + location=None, language="BGR ", include_default=True, required=False + ) + self.assertEqual(builder.language_systems, {("cyrl", "BGR ")}) + builder.start_feature(location=None, name="test2") + self.assertEqual(builder.language_systems, {("latn", "FRA ")}) def test_language_in_aalt_feature(self): self.assertRaisesRegex( FeatureLibError, - "Language statements are not allowed within \"feature aalt\"", - self.build, "feature aalt { language FRA; } aalt;") + 'Language statements are not allowed within "feature aalt"', + self.build, + "feature aalt { language FRA; } aalt;", + ) def test_language_in_size_feature(self): self.assertRaisesRegex( FeatureLibError, - "Language statements are not allowed within \"feature size\"", - self.build, "feature size { language FRA; } size;") + 'Language statements are not allowed within "feature size"', + self.build, + "feature size { language FRA; } size;", + ) def test_language_in_standalone_lookup(self): self.assertRaisesRegex( FeatureLibError, "Language statements are not allowed within standalone lookup blocks", - self.build, "lookup test { language FRA; } test;") + self.build, + "lookup test { language FRA; } test;", + ) def test_language_required_duplicate(self): self.assertRaisesRegex( @@ -419,13 +474,16 @@ def test_language_required_duplicate(self): " script latn;" " language FRA required;" " substitute [a-z] by [A.sc-Z.sc];" - "} test;") + "} test;", + ) def test_lookup_already_defined(self): self.assertRaisesRegex( FeatureLibError, - "Lookup \"foo\" has already been defined", - self.build, "lookup foo {} foo; lookup foo {} foo;") + 'Lookup "foo" has already been defined', + self.build, + "lookup foo {} foo; lookup foo {} foo;", + ) def test_lookup_multiple_flags(self): self.assertRaisesRegex( @@ -438,7 +496,8 @@ def test_lookup_multiple_flags(self): " sub f i by f_i;" " lookupflag 2;" " sub f f i by f_f_i;" - "} foo;") + "} foo;", + ) def test_lookup_multiple_types(self): self.assertRaisesRegex( @@ -449,13 +508,16 @@ def test_lookup_multiple_types(self): "lookup foo {" " sub f f i by f_f_i;" " sub A from [A.alt1 A.alt2];" - "} foo;") + "} foo;", + ) def test_lookup_inside_feature_aalt(self): self.assertRaisesRegex( FeatureLibError, "Lookup blocks cannot be placed inside 'aalt' features", - self.build, "feature aalt {lookup L {} L;} aalt;") + self.build, + "feature aalt {lookup L {} L;} aalt;", + ) def test_chain_subst_refrences_GPOS_looup(self): self.assertRaisesRegex( @@ -465,7 +527,7 @@ def test_chain_subst_refrences_GPOS_looup(self): "lookup dummy { pos a 50; } dummy;" "feature test {" " sub a' lookup dummy b;" - "} test;" + "} test;", ) def test_chain_pos_refrences_GSUB_looup(self): @@ -476,203 +538,215 @@ def test_chain_pos_refrences_GSUB_looup(self): "lookup dummy { sub a by A; } dummy;" "feature test {" " pos a' lookup dummy b;" - "} test;" + "} test;", ) def test_STAT_elidedfallbackname_already_defined(self): self.assertRaisesRegex( FeatureLibError, - 'ElidedFallbackName is already set.', + "ElidedFallbackName is already set.", self.build, - 'table name {' + "table name {" ' nameid 256 "Roman"; ' - '} name;' - 'table STAT {' + "} name;" + "table STAT {" ' ElidedFallbackName { name "Roman"; };' - ' ElidedFallbackNameID 256;' - '} STAT;') + " ElidedFallbackNameID 256;" + "} STAT;", + ) def test_STAT_elidedfallbackname_set_twice(self): self.assertRaisesRegex( FeatureLibError, - 'ElidedFallbackName is already set.', + "ElidedFallbackName is already set.", self.build, - 'table name {' + "table name {" ' nameid 256 "Roman"; ' - '} name;' - 'table STAT {' + "} name;" + "table STAT {" ' ElidedFallbackName { name "Roman"; };' ' ElidedFallbackName { name "Italic"; };' - '} STAT;') + "} STAT;", + ) def test_STAT_elidedfallbacknameID_already_defined(self): self.assertRaisesRegex( FeatureLibError, - 'ElidedFallbackNameID is already set.', + "ElidedFallbackNameID is already set.", self.build, - 'table name {' + "table name {" ' nameid 256 "Roman"; ' - '} name;' - 'table STAT {' - ' ElidedFallbackNameID 256;' + "} name;" + "table STAT {" + " ElidedFallbackNameID 256;" ' ElidedFallbackName { name "Roman"; };' - '} STAT;') + "} STAT;", + ) def test_STAT_elidedfallbacknameID_not_in_name_table(self): self.assertRaisesRegex( FeatureLibError, - 'ElidedFallbackNameID 256 points to a nameID that does not ' + "ElidedFallbackNameID 256 points to a nameID that does not " 'exist in the "name" table', self.build, - 'table name {' + "table name {" ' nameid 257 "Roman"; ' - '} name;' - 'table STAT {' - ' ElidedFallbackNameID 256;' + "} name;" + "table STAT {" + " ElidedFallbackNameID 256;" ' DesignAxis opsz 1 { name "Optical Size"; };' - '} STAT;') + "} STAT;", + ) def test_STAT_design_axis_name(self): self.assertRaisesRegex( FeatureLibError, 'Expected "name"', self.build, - 'table name {' + "table name {" ' nameid 256 "Roman"; ' - '} name;' - 'table STAT {' + "} name;" + "table STAT {" ' ElidedFallbackName { name "Roman"; };' ' DesignAxis opsz 0 { badtag "Optical Size"; };' - '} STAT;') + "} STAT;", + ) def test_STAT_duplicate_design_axis_name(self): self.assertRaisesRegex( FeatureLibError, 'DesignAxis already defined for tag "opsz".', self.build, - 'table name {' + "table name {" ' nameid 256 "Roman"; ' - '} name;' - 'table STAT {' + "} name;" + "table STAT {" ' ElidedFallbackName { name "Roman"; };' ' DesignAxis opsz 0 { name "Optical Size"; };' ' DesignAxis opsz 1 { name "Optical Size"; };' - '} STAT;') + "} STAT;", + ) def test_STAT_design_axis_duplicate_order(self): self.assertRaisesRegex( FeatureLibError, "DesignAxis already defined for axis number 0.", self.build, - 'table name {' + "table name {" ' nameid 256 "Roman"; ' - '} name;' - 'table STAT {' + "} name;" + "table STAT {" ' ElidedFallbackName { name "Roman"; };' ' DesignAxis opsz 0 { name "Optical Size"; };' ' DesignAxis wdth 0 { name "Width"; };' - ' AxisValue {' - ' location opsz 8;' - ' location wdth 400;' + " AxisValue {" + " location opsz 8;" + " location wdth 400;" ' name "Caption";' - ' };' - '} STAT;') + " };" + "} STAT;", + ) def test_STAT_undefined_tag(self): self.assertRaisesRegex( FeatureLibError, - 'DesignAxis not defined for wdth.', + "DesignAxis not defined for wdth.", self.build, - 'table name {' + "table name {" ' nameid 256 "Roman"; ' - '} name;' - 'table STAT {' + "} name;" + "table STAT {" ' ElidedFallbackName { name "Roman"; };' ' DesignAxis opsz 0 { name "Optical Size"; };' - ' AxisValue { ' - ' location wdth 125; ' + " AxisValue { " + " location wdth 125; " ' name "Wide"; ' - ' };' - '} STAT;') + " };" + "} STAT;", + ) def test_STAT_axis_value_format4(self): self.assertRaisesRegex( FeatureLibError, - 'Axis tag wdth already defined.', + "Axis tag wdth already defined.", self.build, - 'table name {' + "table name {" ' nameid 256 "Roman"; ' - '} name;' - 'table STAT {' + "} name;" + "table STAT {" ' ElidedFallbackName { name "Roman"; };' ' DesignAxis opsz 0 { name "Optical Size"; };' ' DesignAxis wdth 1 { name "Width"; };' ' DesignAxis wght 2 { name "Weight"; };' - ' AxisValue { ' - ' location opsz 8; ' - ' location wdth 125; ' - ' location wdth 125; ' - ' location wght 500; ' + " AxisValue { " + " location opsz 8; " + " location wdth 125; " + " location wdth 125; " + " location wght 500; " ' name "Caption Medium Wide"; ' - ' };' - '} STAT;') + " };" + "} STAT;", + ) def test_STAT_duplicate_axis_value_record(self): # Test for Duplicate AxisValueRecords even when the definition order # is different. self.assertRaisesRegex( FeatureLibError, - 'An AxisValueRecord with these values is already defined.', + "An AxisValueRecord with these values is already defined.", self.build, - 'table name {' + "table name {" ' nameid 256 "Roman"; ' - '} name;' - 'table STAT {' + "} name;" + "table STAT {" ' ElidedFallbackName { name "Roman"; };' ' DesignAxis opsz 0 { name "Optical Size"; };' ' DesignAxis wdth 1 { name "Width"; };' - ' AxisValue {' - ' location opsz 8;' - ' location wdth 400;' + " AxisValue {" + " location opsz 8;" + " location wdth 400;" ' name "Caption";' - ' };' - ' AxisValue {' - ' location wdth 400;' - ' location opsz 8;' + " };" + " AxisValue {" + " location wdth 400;" + " location opsz 8;" ' name "Caption";' - ' };' - '} STAT;') + " };" + "} STAT;", + ) def test_STAT_axis_value_missing_location(self): self.assertRaisesRegex( FeatureLibError, 'Expected "Axis location"', self.build, - 'table name {' + "table name {" ' nameid 256 "Roman"; ' - '} name;' - 'table STAT {' + "} name;" + "table STAT {" ' ElidedFallbackName { name "Roman"; ' - '};' + "};" ' DesignAxis opsz 0 { name "Optical Size"; };' - ' AxisValue { ' + " AxisValue { " ' name "Wide"; ' - ' };' - '} STAT;') + " };" + "} STAT;", + ) def test_STAT_invalid_location_tag(self): self.assertRaisesRegex( FeatureLibError, - 'Tags cannot be longer than 4 characters', + "Tags cannot be longer than 4 characters", self.build, - 'table name {' + "table name {" ' nameid 256 "Roman"; ' - '} name;' - 'table STAT {' + "} name;" + "table STAT {" ' ElidedFallbackName { name "Roman"; ' ' name 3 1 0x0411 "ローマン"; }; ' ' DesignAxis width 0 { name "Width"; };' - '} STAT;') + "} STAT;", + ) def test_extensions(self): class ast_BaseClass(ast.MarkClass): @@ -690,7 +764,9 @@ def asFea(self, indent=""): for bcd in self.base.markClass.definitions: if res != "": res += "\n{}".format(indent) - res += "pos base {} {}".format(bcd.glyphs.asFea(), bcd.anchor.asFea()) + res += "pos base {} {}".format( + bcd.glyphs.asFea(), bcd.anchor.asFea() + ) for m in self.marks: res += " mark @{}".format(m.name) res += ";" @@ -703,6 +779,7 @@ def asFea(self, indent=""): class testAst(object): MarkBasePosStatement = ast_MarkBasePosStatement + def __getattr__(self, name): return getattr(ast, name) @@ -713,8 +790,9 @@ def parse_position_base_(self, enumerated, vertical): if enumerated: raise FeatureLibError( '"enumerate" is not allowed with ' - 'mark-to-base attachment positioning', - location) + "mark-to-base attachment positioning", + location, + ) base = self.parse_glyphclass_(accept_glyphname=True) if self.next_token_ == "<": marks = self.parse_anchor_marks_() @@ -725,11 +803,10 @@ def parse_position_base_(self, enumerated, vertical): m = self.expect_markClass_reference_() marks.append(m) self.expect_symbol_(";") - return self.ast.MarkBasePosStatement(base, marks, - location=location) + return self.ast.MarkBasePosStatement(base, marks, location=location) def parseBaseClass(self): - if not hasattr(self.doc_, 'baseClasses'): + if not hasattr(self.doc_, "baseClasses"): self.doc_.baseClasses = {} location = self.cur_token_location_ glyphs = self.parse_glyphclass_(accept_glyphname=True) @@ -741,37 +818,39 @@ def parseBaseClass(self): baseClass = ast_BaseClass(name) self.doc_.baseClasses[name] = baseClass self.glyphclasses_.define(name, baseClass) - bcdef = ast_BaseClassDefinition(baseClass, anchor, glyphs, - location=location) + bcdef = ast_BaseClassDefinition( + baseClass, anchor, glyphs, location=location + ) baseClass.addDefinition(bcdef) return bcdef - extensions = { - 'baseClass' : lambda s : s.parseBaseClass() - } + extensions = {"baseClass": lambda s: s.parseBaseClass()} ast = testAst() self.check_fea2fea_file( - "baseClass.feax", base="baseClass.fea", parser=testParser) + "baseClass.feax", base="baseClass.fea", parser=testParser + ) def test_markClass_same_glyph_redefined(self): self.assertRaisesRegex( FeatureLibError, "Glyph acute already defined", self.build, - "markClass [acute] @TOP_MARKS;"*2) + "markClass [acute] @TOP_MARKS;" * 2, + ) def test_markClass_same_glyph_multiple_classes(self): self.assertRaisesRegex( FeatureLibError, - 'Glyph uni0327 cannot be in both @ogonek and @cedilla', + "Glyph uni0327 cannot be in both @ogonek and @cedilla", self.build, "feature mark {" " markClass [uni0327 uni0328] @ogonek;" " pos base [a] mark @ogonek;" " markClass [uni0327] @cedilla;" " pos base [a] mark @cedilla;" - "} mark;") + "} mark;", + ) def test_build_specific_tables(self): features = "feature liga {sub f i by f_i;} liga;" @@ -793,7 +872,7 @@ def test_build_pre_parsed_ast_featurefile(self): def test_unsupported_subtable_break(self): logger = logging.getLogger("fontTools.otlLib.builder") - with CapturingLogHandler(logger, level='WARNING') as captor: + with CapturingLogHandler(logger, level="WARNING") as captor: self.build( "feature test {" " pos a 10;" @@ -824,10 +903,8 @@ def test_singlePos_multiplePositionsForSameGlyph(self): FeatureLibError, "Already defined different position for glyph", self.build, - "lookup foo {" - " pos A -45; " - " pos A 45; " - "} foo;") + "lookup foo {" " pos A -45; " " pos A 45; " "} foo;", + ) def test_pairPos_enumRuleOverridenBySinglePair_DEBUG(self): logger = logging.getLogger("fontTools.otlLib.builder") @@ -836,14 +913,14 @@ def test_pairPos_enumRuleOverridenBySinglePair_DEBUG(self): "feature test {" " enum pos A [V Y] -80;" " pos A V -75;" - "} test;") - captor.assertRegex('Already defined position for pair A V at') + "} test;" + ) + captor.assertRegex("Already defined position for pair A V at") def test_ignore_empty_lookup_block(self): # https://github.com/fonttools/fonttools/pull/2277 font = self.build( - "lookup EMPTY { ; } EMPTY;" - "feature ss01 { lookup EMPTY; } ss01;" + "lookup EMPTY { ; } EMPTY;" "feature ss01 { lookup EMPTY; } ss01;" ) assert "GPOS" not in font assert "GSUB" not in font @@ -854,8 +931,7 @@ def generate_feature_file_test(name): for name in BuilderTest.TEST_FEATURE_FILES: - setattr(BuilderTest, "test_FeatureFile_%s" % name, - generate_feature_file_test(name)) + setattr(BuilderTest, "test_FeatureFile_%s" % name, generate_feature_file_test(name)) def generate_fea2fea_file_test(name): @@ -863,8 +939,11 @@ def generate_fea2fea_file_test(name): for name in BuilderTest.TEST_FEATURE_FILES: - setattr(BuilderTest, "test_Fea2feaFile_{}".format(name), - generate_fea2fea_file_test(name)) + setattr( + BuilderTest, + "test_Fea2feaFile_{}".format(name), + generate_fea2fea_file_test(name), + ) if __name__ == "__main__": diff --git a/Tests/feaLib/parser_test.py b/Tests/feaLib/parser_test.py index 6344907c9d9..fd9dea7002e 100644 --- a/Tests/feaLib/parser_test.py +++ b/Tests/feaLib/parser_test.py @@ -14,8 +14,9 @@ def f(x): if len(x) == 1: return list(x)[0] else: - return '[%s]' % ' '.join(sorted(list(x))) - return ' '.join(f(g.glyphSet()) for g in glyphs) + return "[%s]" % " ".join(sorted(list(x))) + + return " ".join(f(g.glyphSet()) for g in glyphs) def mapping(s): @@ -30,7 +31,9 @@ def mapping(s): return dict(zip(b, c)) -GLYPHNAMES = (""" +GLYPHNAMES = ( + ( + """ .notdef space A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A.sc B.sc C.sc D.sc E.sc F.sc G.sc H.sc I.sc J.sc K.sc L.sc M.sc N.sc O.sc P.sc Q.sc R.sc S.sc T.sc U.sc V.sc W.sc X.sc Y.sc Z.sc @@ -48,7 +51,10 @@ def mapping(s): cid00111 cid00222 comma endash emdash figuredash damma hamza c_d d.alt n.end s.end f_f -""").split() + ["foo.%d" % i for i in range(1, 200)] +""" + ).split() + + ["foo.%d" % i for i in range(1, 200)] +) class ParserTest(unittest.TestCase): @@ -60,7 +66,7 @@ def __init__(self, methodName): self.assertRaisesRegex = self.assertRaisesRegexp def test_glyphMap_deprecated(self): - glyphMap = {'a': 0, 'b': 1, 'c': 2} + glyphMap = {"a": 0, "b": 1, "c": 2} with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") parser = Parser(StringIO(), glyphMap=glyphMap) @@ -68,22 +74,28 @@ def test_glyphMap_deprecated(self): self.assertEqual(len(w), 1) self.assertEqual(w[-1].category, UserWarning) self.assertIn("deprecated", str(w[-1].message)) - self.assertEqual(parser.glyphNames_, {'a', 'b', 'c'}) + self.assertEqual(parser.glyphNames_, {"a", "b", "c"}) self.assertRaisesRegex( - TypeError, "mutually exclusive", - Parser, StringIO(), ("a",), glyphMap={"a": 0}) + TypeError, + "mutually exclusive", + Parser, + StringIO(), + ("a",), + glyphMap={"a": 0}, + ) self.assertRaisesRegex( - TypeError, "unsupported keyword argument", - Parser, StringIO(), foo="bar") + TypeError, "unsupported keyword argument", Parser, StringIO(), foo="bar" + ) def test_comments(self): doc = self.parse( """ # Initial feature test { sub A by B; # simple - } test;""") + } test;""" + ) c1 = doc.statements[0] c2 = doc.statements[1].statements[1] self.assertEqual(type(c1), ast.Comment) @@ -94,9 +106,11 @@ def test_comments(self): self.assertEqual(doc.statements[1].name, "test") def test_only_comments(self): - doc = self.parse("""\ + doc = self.parse( + """\ # Initial - """) + """ + ) c1 = doc.statements[0] self.assertEqual(type(c1), ast.Comment) self.assertEqual(c1.text, "# Initial") @@ -106,7 +120,8 @@ def test_anchor_format_a(self): doc = self.parse( "feature test {" " pos cursive A ;" - "} test;") + "} test;" + ) anchor = doc.statements[0].statements[0].entryAnchor self.assertEqual(type(anchor), ast.Anchor) self.assertEqual(anchor.x, 120) @@ -119,7 +134,8 @@ def test_anchor_format_b(self): doc = self.parse( "feature test {" " pos cursive A ;" - "} test;") + "} test;" + ) anchor = doc.statements[0].statements[0].entryAnchor self.assertEqual(type(anchor), ast.Anchor) self.assertEqual(anchor.x, 120) @@ -134,7 +150,8 @@ def test_anchor_format_c(self): " pos cursive A " " >" " ;" - "} test;") + "} test;" + ) anchor = doc.statements[0].statements[0].entryAnchor self.assertEqual(type(anchor), ast.Anchor) self.assertEqual(anchor.x, 120) @@ -147,7 +164,8 @@ def test_anchor_format_d(self): doc = self.parse( "feature test {" " pos cursive A ;" - "} test;") + "} test;" + ) anchor = doc.statements[0].statements[0].exitAnchor self.assertIsNone(anchor) @@ -156,7 +174,8 @@ def test_anchor_format_e(self): "feature test {" " anchorDef 120 -20 contourpoint 7 Foo;" " pos cursive A ;" - "} test;") + "} test;" + ) anchor = doc.statements[0].statements[1].entryAnchor self.assertEqual(type(anchor), ast.Anchor) self.assertEqual(anchor.x, 120) @@ -167,18 +186,25 @@ def test_anchor_format_e(self): def test_anchor_format_e_undefined(self): self.assertRaisesRegex( - FeatureLibError, 'Unknown anchor "UnknownName"', self.parse, + FeatureLibError, + 'Unknown anchor "UnknownName"', + self.parse, "feature test {" " position cursive A ;" - "} test;") + "} test;", + ) def test_anchor_variable_scalar(self): doc = self.parse( "feature test {" " pos cursive A ;" - "} test;") + "} test;" + ) anchor = doc.statements[0].statements[0].entryAnchor - self.assertEqual(anchor.asFea(), "") + self.assertEqual( + anchor.asFea(), + "", + ) def test_anchordef(self): [foo] = self.parse("anchorDef 123 456 foo;").statements @@ -211,8 +237,11 @@ def test_anonymous(self): def test_anon_missingBrace(self): self.assertRaisesRegex( - FeatureLibError, "Expected '} TEST;' to terminate anonymous block", - self.parse, "anon TEST { \n no end in sight") + FeatureLibError, + "Expected '} TEST;' to terminate anonymous block", + self.parse, + "anon TEST { \n no end in sight", + ) def test_attach(self): doc = self.parse("table GDEF {Attach [a e] 2;} GDEF;") @@ -230,8 +259,7 @@ def test_feature_block_useExtension(self): [liga] = self.parse("feature liga useExtension {} liga;").statements self.assertEqual(liga.name, "liga") self.assertTrue(liga.use_extension) - self.assertEqual(liga.asFea(), - "feature liga useExtension {\n \n} liga;\n") + self.assertEqual(liga.asFea(), "feature liga useExtension {\n \n} liga;\n") def test_feature_comment(self): [liga] = self.parse("feature liga { # Comment\n } liga;").statements @@ -247,12 +275,16 @@ def test_feature_reference(self): def test_FeatureNames_bad(self): self.assertRaisesRegex( - FeatureLibError, 'Expected "name"', - self.parse, "feature ss01 { featureNames { feature test; } ss01;") + FeatureLibError, + 'Expected "name"', + self.parse, + "feature ss01 { featureNames { feature test; } ss01;", + ) def test_FeatureNames_comment(self): [feature] = self.parse( - "feature ss01 { featureNames { # Comment\n }; } ss01;").statements + "feature ss01 { featureNames { # Comment\n }; } ss01;" + ).statements [featureNames] = feature.statements self.assertIsInstance(featureNames, ast.NestedBlock) [comment] = featureNames.statements @@ -261,7 +293,8 @@ def test_FeatureNames_comment(self): def test_FeatureNames_emptyStatements(self): [feature] = self.parse( - "feature ss01 { featureNames { ;;; }; } ss01;").statements + "feature ss01 { featureNames { ;;; }; } ss01;" + ).statements [featureNames] = feature.statements self.assertIsInstance(featureNames, ast.NestedBlock) self.assertEqual(featureNames.statements, []) @@ -274,8 +307,11 @@ def test_FontRevision(self): def test_FontRevision_negative(self): self.assertRaisesRegex( - FeatureLibError, "Font revision numbers must be positive", - self.parse, "table head {FontRevision -17.2;} head;") + FeatureLibError, + "Font revision numbers must be positive", + self.parse, + "table head {FontRevision -17.2;} head;", + ) def test_strict_glyph_name_check(self): self.parse("@bad = [a b ccc];", glyphNames=("a", "b", "ccc")) @@ -290,14 +326,19 @@ def test_glyphclass(self): def test_glyphclass_glyphNameTooLong(self): self.assertRaisesRegex( - FeatureLibError, "must not be longer than 63 characters", - self.parse, "@GlyphClass = [%s];" % ("G" * 64)) + FeatureLibError, + "must not be longer than 63 characters", + self.parse, + "@GlyphClass = [%s];" % ("G" * 64), + ) def test_glyphclass_bad(self): self.assertRaisesRegex( FeatureLibError, "Expected glyph name, glyph range, or glyph class reference", - self.parse, "@bad = [a 123];") + self.parse, + "@bad = [a 123];", + ) def test_glyphclass_duplicate(self): # makeotf accepts this, so we should too @@ -320,9 +361,11 @@ def test_glyphclass_from_markClass(self): "markClass [acute grave] @TOP_MARKS;" "markClass cedilla @BOTTOM_MARKS;" "@MARKS = [@TOP_MARKS @BOTTOM_MARKS ogonek];" - "@ALL = @MARKS;") - self.assertEqual(doc.statements[-1].glyphSet(), - ("acute", "grave", "cedilla", "ogonek")) + "@ALL = @MARKS;" + ) + self.assertEqual( + doc.statements[-1].glyphSet(), ("acute", "grave", "cedilla", "ogonek") + ) def test_glyphclass_range_cid(self): [gc] = self.parse(r"@GlyphClass = [\999-\1001];").statements @@ -333,7 +376,9 @@ def test_glyphclass_range_cid_bad(self): self.assertRaisesRegex( FeatureLibError, "Bad range: start should be less than limit", - self.parse, r"@bad = [\998-\995];") + self.parse, + r"@bad = [\998-\995];", + ) def test_glyphclass_range_uppercase(self): [gc] = self.parse("@swashes = [X.swash-Z.swash];").statements @@ -363,7 +408,9 @@ def test_glyphclass_ambiguous_dash_no_glyph_names(self): # https://github.com/fonttools/fonttools/issues/1768 glyphNames = () with CapturingLogHandler("fontTools.feaLib.parser", level="WARNING") as caplog: - [gc] = self.parse("@class = [A-foo.sc B-foo.sc C D];", glyphNames).statements + [gc] = self.parse( + "@class = [A-foo.sc B-foo.sc C D];", glyphNames + ).statements self.assertEqual(gc.glyphSet(), ("A-foo.sc", "B-foo.sc", "C", "D")) self.assertEqual(len(caplog.records), 2) caplog.assertRegex("Ambiguous glyph name that looks like a range:") @@ -372,8 +419,7 @@ def test_glyphclass_glyph_name_should_win_over_range(self): # The OpenType Feature File Specification v1.20 makes it clear # that if a dashed name could be interpreted either as a glyph name # or as a range, then the semantics should be the single dashed name. - glyphNames = ( - "A-foo.sc-C-foo.sc A-foo.sc B-foo.sc C-foo.sc".split()) + glyphNames = "A-foo.sc-C-foo.sc A-foo.sc B-foo.sc C-foo.sc".split() [gc] = self.parse("@range = [A-foo.sc-C-foo.sc];", glyphNames).statements self.assertEqual(gc.glyphSet(), ("A-foo.sc-C-foo.sc",)) @@ -383,7 +429,10 @@ def test_glyphclass_range_dash_ambiguous(self): FeatureLibError, 'Ambiguous glyph range "A-B-C"; ' 'please use "A - B-C" or "A-B - C" to clarify what you mean', - self.parse, r"@bad = [A-B-C];", glyphNames) + self.parse, + r"@bad = [A-B-C];", + glyphNames, + ) def test_glyphclass_range_digit1(self): [gc] = self.parse("@range = [foo.2-foo.5];").statements @@ -400,36 +449,50 @@ def test_glyphclass_range_digit3(self): def test_glyphclass_range_bad(self): self.assertRaisesRegex( FeatureLibError, - "Bad range: \"a\" and \"foobar\" should have the same length", - self.parse, "@bad = [a-foobar];") + 'Bad range: "a" and "foobar" should have the same length', + self.parse, + "@bad = [a-foobar];", + ) self.assertRaisesRegex( - FeatureLibError, "Bad range: \"A.swash-z.swash\"", - self.parse, "@bad = [A.swash-z.swash];") + FeatureLibError, + 'Bad range: "A.swash-z.swash"', + self.parse, + "@bad = [A.swash-z.swash];", + ) self.assertRaisesRegex( - FeatureLibError, "Start of range must be smaller than its end", - self.parse, "@bad = [B.swash-A.swash];") + FeatureLibError, + "Start of range must be smaller than its end", + self.parse, + "@bad = [B.swash-A.swash];", + ) self.assertRaisesRegex( - FeatureLibError, "Bad range: \"foo.1234-foo.9876\"", - self.parse, "@bad = [foo.1234-foo.9876];") + FeatureLibError, + 'Bad range: "foo.1234-foo.9876"', + self.parse, + "@bad = [foo.1234-foo.9876];", + ) def test_glyphclass_range_mixed(self): [gc] = self.parse("@range = [a foo.09-foo.11 X.sc-Z.sc];").statements - self.assertEqual(gc.glyphSet(), ( - "a", "foo.09", "foo.10", "foo.11", "X.sc", "Y.sc", "Z.sc" - )) + self.assertEqual( + gc.glyphSet(), ("a", "foo.09", "foo.10", "foo.11", "X.sc", "Y.sc", "Z.sc") + ) def test_glyphclass_reference(self): [vowels_lc, vowels_uc, vowels] = self.parse( "@Vowels.lc = [a e i o u]; @Vowels.uc = [A E I O U];" - "@Vowels = [@Vowels.lc @Vowels.uc y Y];").statements + "@Vowels = [@Vowels.lc @Vowels.uc y Y];" + ).statements self.assertEqual(vowels_lc.glyphSet(), tuple("aeiou")) self.assertEqual(vowels_uc.glyphSet(), tuple("AEIOU")) self.assertEqual(vowels.glyphSet(), tuple("aeiouAEIOUyY")) - self.assertEqual(vowels.asFea(), - "@Vowels = [@Vowels.lc @Vowels.uc y Y];") + self.assertEqual(vowels.asFea(), "@Vowels = [@Vowels.lc @Vowels.uc y Y];") self.assertRaisesRegex( - FeatureLibError, "Unknown glyph class @unknown", - self.parse, "@bad = [@unknown];") + FeatureLibError, + "Unknown glyph class @unknown", + self.parse, + "@bad = [@unknown];", + ) def test_glyphclass_scoping(self): [foo, liga, smcp] = self.parse( @@ -447,8 +510,7 @@ def test_glyphclass_scoping_bug496(self): "feature F1 { lookup L { @GLYPHCLASS = [A B C];} L; } F1;" "feature F2 { sub @GLYPHCLASS by D; } F2;" ).statements - self.assertEqual(list(f2.statements[0].glyphs[0].glyphSet()), - ["A", "B", "C"]) + self.assertEqual(list(f2.statements[0].glyphs[0].glyphSet()), ["A", "B", "C"]) def test_GlyphClassDef(self): doc = self.parse("table GDEF {GlyphClassDef [b],[l],[m],[C c];} GDEF;") @@ -481,9 +543,8 @@ def test_ignore_pos(self): def test_ignore_position(self): doc = self.parse( - "feature test {" - " ignore position f [a e] d' [a u]' [e y];" - "} test;") + "feature test {" " ignore position f [a e] d' [a u]' [e y];" "} test;" + ) sub = doc.statements[0].statements[0] self.assertIsInstance(sub, ast.IgnorePosStatement) [(prefix, glyphs, suffix)] = sub.chainContexts @@ -497,7 +558,8 @@ def test_ignore_position_with_lookup(self): 'No lookups can be specified for "ignore pos"', self.parse, "lookup L { pos [A A.sc] -100; } L;" - "feature test { ignore pos f' i', A' lookup L; } test;") + "feature test { ignore pos f' i', A' lookup L; } test;", + ) def test_ignore_sub(self): doc = self.parse("feature test {ignore sub e t' c, q u' u' x;} test;") @@ -513,9 +575,8 @@ def test_ignore_sub(self): def test_ignore_substitute(self): doc = self.parse( - "feature test {" - " ignore substitute f [a e] d' [a u]' [e y];" - "} test;") + "feature test {" " ignore substitute f [a e] d' [a u]' [e y];" "} test;" + ) sub = doc.statements[0].statements[0] self.assertIsInstance(sub, ast.IgnoreSubstStatement) [(prefix, glyphs, suffix)] = sub.chainContexts @@ -529,15 +590,19 @@ def test_ignore_substitute_with_lookup(self): 'No lookups can be specified for "ignore sub"', self.parse, "lookup L { sub [A A.sc] by a; } L;" - "feature test { ignore sub f' i', A' lookup L; } test;") + "feature test { ignore sub f' i', A' lookup L; } test;", + ) def test_include_statement(self): - doc = self.parse("""\ + doc = self.parse( + """\ include(../family.fea); include # Comment (foo) ; - """, followIncludes=False) + """, + followIncludes=False, + ) s1, s2, s3 = doc.statements self.assertEqual(type(s1), ast.IncludeStatement) self.assertEqual(s1.filename, "../family.fea") @@ -549,9 +614,12 @@ def test_include_statement(self): self.assertEqual(s3.text, "# Comment") def test_include_statement_no_semicolon(self): - doc = self.parse("""\ + doc = self.parse( + """\ include(../family.fea) - """, followIncludes=False) + """, + followIncludes=False, + ) s1 = doc.statements[0] self.assertEqual(type(s1), ast.IncludeStatement) self.assertEqual(s1.filename, "../family.fea") @@ -574,9 +642,9 @@ def test_language_exclude_dflt(self): self.assertFalse(s.required) def test_language_exclude_dflt_required(self): - doc = self.parse("feature test {" - " language DEU exclude_dflt required;" - "} test;") + doc = self.parse( + "feature test {" " language DEU exclude_dflt required;" "} test;" + ) s = doc.statements[0].statements[0] self.assertEqual(type(s), ast.LanguageStatement) self.assertEqual(s.language, "DEU ") @@ -592,9 +660,9 @@ def test_language_include_dflt(self): self.assertFalse(s.required) def test_language_include_dflt_required(self): - doc = self.parse("feature test {" - " language DEU include_dflt required;" - "} test;") + doc = self.parse( + "feature test {" " language DEU include_dflt required;" "} test;" + ) s = doc.statements[0].statements[0] self.assertEqual(type(s), ast.LanguageStatement) self.assertEqual(s.language, "DEU ") @@ -605,7 +673,9 @@ def test_language_DFLT(self): self.assertRaisesRegex( FeatureLibError, '"DFLT" is not a valid language tag; use "dflt" instead', - self.parse, "feature test { language DFLT; } test;") + self.parse, + "feature test { language DFLT; } test;", + ) def test_ligatureCaretByIndex_glyphClass(self): doc = self.parse("table GDEF{LigatureCaretByIndex [c_t f_i] 2;}GDEF;") @@ -644,20 +714,21 @@ def test_lookup_block_useExtension(self): [lookup] = self.parse("lookup Foo useExtension {} Foo;").statements self.assertEqual(lookup.name, "Foo") self.assertTrue(lookup.use_extension) - self.assertEqual(lookup.asFea(), - "lookup Foo useExtension {\n \n} Foo;\n") + self.assertEqual(lookup.asFea(), "lookup Foo useExtension {\n \n} Foo;\n") def test_lookup_block_name_mismatch(self): self.assertRaisesRegex( - FeatureLibError, 'Expected "Foo"', - self.parse, "lookup Foo {} Bar;") + FeatureLibError, 'Expected "Foo"', self.parse, "lookup Foo {} Bar;" + ) def test_lookup_block_with_horizontal_valueRecordDef(self): - doc = self.parse("feature liga {" - " lookup look {" - " valueRecordDef 123 foo;" - " } look;" - "} liga;") + doc = self.parse( + "feature liga {" + " lookup look {" + " valueRecordDef 123 foo;" + " } look;" + "} liga;" + ) [liga] = doc.statements [look] = liga.statements [foo] = look.statements @@ -665,11 +736,13 @@ def test_lookup_block_with_horizontal_valueRecordDef(self): self.assertIsNone(foo.value.yAdvance) def test_lookup_block_with_vertical_valueRecordDef(self): - doc = self.parse("feature vkrn {" - " lookup look {" - " valueRecordDef 123 foo;" - " } look;" - "} vkrn;") + doc = self.parse( + "feature vkrn {" + " lookup look {" + " valueRecordDef 123 foo;" + " } look;" + "} vkrn;" + ) [vkrn] = doc.statements [look] = vkrn.statements [foo] = look.statements @@ -683,15 +756,17 @@ def test_lookup_comment(self): self.assertEqual(comment.text, "# Comment") def test_lookup_reference(self): - [foo, bar] = self.parse("lookup Foo {} Foo;" - "feature Bar {lookup Foo;} Bar;").statements + [foo, bar] = self.parse( + "lookup Foo {} Foo;" "feature Bar {lookup Foo;} Bar;" + ).statements [ref] = bar.statements self.assertEqual(type(ref), ast.LookupReferenceStatement) self.assertEqual(ref.lookup, foo) def test_lookup_reference_to_lookup_inside_feature(self): - [qux, bar] = self.parse("feature Qux {lookup Foo {} Foo;} Qux;" - "feature Bar {lookup Foo;} Bar;").statements + [qux, bar] = self.parse( + "feature Qux {lookup Foo {} Foo;} Qux;" "feature Bar {lookup Foo;} Bar;" + ).statements [foo] = qux.statements [ref] = bar.statements self.assertIsInstance(ref, ast.LookupReferenceStatement) @@ -699,8 +774,11 @@ def test_lookup_reference_to_lookup_inside_feature(self): def test_lookup_reference_unknown(self): self.assertRaisesRegex( - FeatureLibError, 'Unknown lookup "Huh"', - self.parse, "feature liga {lookup Huh;} liga;") + FeatureLibError, + 'Unknown lookup "Huh"', + self.parse, + "feature liga {lookup Huh;} liga;", + ) def parse_lookupflag_(self, s): return self.parse("lookup L {%s} L;" % s).statements[0].statements[-1] @@ -716,52 +794,59 @@ def test_lookupflag_format_A(self): def test_lookupflag_format_A_MarkAttachmentType(self): flag = self.parse_lookupflag_( "@TOP_MARKS = [acute grave macron];" - "lookupflag RightToLeft MarkAttachmentType @TOP_MARKS;") + "lookupflag RightToLeft MarkAttachmentType @TOP_MARKS;" + ) self.assertIsInstance(flag, ast.LookupFlagStatement) self.assertEqual(flag.value, 1) self.assertIsInstance(flag.markAttachment, ast.GlyphClassName) - self.assertEqual(flag.markAttachment.glyphSet(), - ("acute", "grave", "macron")) + self.assertEqual(flag.markAttachment.glyphSet(), ("acute", "grave", "macron")) self.assertIsNone(flag.markFilteringSet) - self.assertEqual(flag.asFea(), - "lookupflag RightToLeft MarkAttachmentType @TOP_MARKS;") + self.assertEqual( + flag.asFea(), "lookupflag RightToLeft MarkAttachmentType @TOP_MARKS;" + ) def test_lookupflag_format_A_MarkAttachmentType_glyphClass(self): flag = self.parse_lookupflag_( - "lookupflag RightToLeft MarkAttachmentType [acute grave macron];") + "lookupflag RightToLeft MarkAttachmentType [acute grave macron];" + ) self.assertIsInstance(flag, ast.LookupFlagStatement) self.assertEqual(flag.value, 1) self.assertIsInstance(flag.markAttachment, ast.GlyphClass) - self.assertEqual(flag.markAttachment.glyphSet(), - ("acute", "grave", "macron")) + self.assertEqual(flag.markAttachment.glyphSet(), ("acute", "grave", "macron")) self.assertIsNone(flag.markFilteringSet) - self.assertEqual(flag.asFea(), - "lookupflag RightToLeft MarkAttachmentType [acute grave macron];") + self.assertEqual( + flag.asFea(), + "lookupflag RightToLeft MarkAttachmentType [acute grave macron];", + ) def test_lookupflag_format_A_UseMarkFilteringSet(self): flag = self.parse_lookupflag_( "@BOTTOM_MARKS = [cedilla ogonek];" - "lookupflag UseMarkFilteringSet @BOTTOM_MARKS IgnoreLigatures;") + "lookupflag UseMarkFilteringSet @BOTTOM_MARKS IgnoreLigatures;" + ) self.assertIsInstance(flag, ast.LookupFlagStatement) self.assertEqual(flag.value, 4) self.assertIsNone(flag.markAttachment) self.assertIsInstance(flag.markFilteringSet, ast.GlyphClassName) - self.assertEqual(flag.markFilteringSet.glyphSet(), - ("cedilla", "ogonek")) - self.assertEqual(flag.asFea(), - "lookupflag IgnoreLigatures UseMarkFilteringSet @BOTTOM_MARKS;") + self.assertEqual(flag.markFilteringSet.glyphSet(), ("cedilla", "ogonek")) + self.assertEqual( + flag.asFea(), + "lookupflag IgnoreLigatures UseMarkFilteringSet @BOTTOM_MARKS;", + ) def test_lookupflag_format_A_UseMarkFilteringSet_glyphClass(self): flag = self.parse_lookupflag_( - "lookupflag UseMarkFilteringSet [cedilla ogonek] IgnoreLigatures;") + "lookupflag UseMarkFilteringSet [cedilla ogonek] IgnoreLigatures;" + ) self.assertIsInstance(flag, ast.LookupFlagStatement) self.assertEqual(flag.value, 4) self.assertIsNone(flag.markAttachment) self.assertIsInstance(flag.markFilteringSet, ast.GlyphClass) - self.assertEqual(flag.markFilteringSet.glyphSet(), - ("cedilla", "ogonek")) - self.assertEqual(flag.asFea(), - "lookupflag IgnoreLigatures UseMarkFilteringSet [cedilla ogonek];") + self.assertEqual(flag.markFilteringSet.glyphSet(), ("cedilla", "ogonek")) + self.assertEqual( + flag.asFea(), + "lookupflag IgnoreLigatures UseMarkFilteringSet [cedilla ogonek];", + ) def test_lookupflag_format_B(self): flag = self.parse_lookupflag_("lookupflag 7;") @@ -769,8 +854,9 @@ def test_lookupflag_format_B(self): self.assertEqual(flag.value, 7) self.assertIsNone(flag.markAttachment) self.assertIsNone(flag.markFilteringSet) - self.assertEqual(flag.asFea(), - "lookupflag RightToLeft IgnoreBaseGlyphs IgnoreLigatures;") + self.assertEqual( + flag.asFea(), "lookupflag RightToLeft IgnoreBaseGlyphs IgnoreLigatures;" + ) def test_lookupflag_format_B_zero(self): flag = self.parse_lookupflag_("lookupflag 0;") @@ -783,22 +869,26 @@ def test_lookupflag_format_B_zero(self): def test_lookupflag_no_value(self): self.assertRaisesRegex( FeatureLibError, - 'lookupflag must have a value', + "lookupflag must have a value", self.parse, - "feature test {lookupflag;} test;") + "feature test {lookupflag;} test;", + ) def test_lookupflag_repeated(self): self.assertRaisesRegex( FeatureLibError, - 'RightToLeft can be specified only once', + "RightToLeft can be specified only once", self.parse, - "feature test {lookupflag RightToLeft RightToLeft;} test;") + "feature test {lookupflag RightToLeft RightToLeft;} test;", + ) def test_lookupflag_unrecognized(self): self.assertRaisesRegex( FeatureLibError, '"IgnoreCookies" is not a recognized lookupflag', - self.parse, "feature test {lookupflag IgnoreCookies;} test;") + self.parse, + "feature test {lookupflag IgnoreCookies;} test;", + ) def test_gpos_type_1_glyph(self): doc = self.parse("feature kern {pos one <1 2 3 4>;} kern;") @@ -842,11 +932,15 @@ def test_gpos_type_1_enumerated(self): self.assertRaisesRegex( FeatureLibError, '"enumerate" is only allowed with pair positionings', - self.parse, "feature test {enum pos T 100;} test;") + self.parse, + "feature test {enum pos T 100;} test;", + ) self.assertRaisesRegex( FeatureLibError, '"enumerate" is only allowed with pair positionings', - self.parse, "feature test {enumerate pos T 100;} test;") + self.parse, + "feature test {enumerate pos T 100;} test;", + ) def test_gpos_type_1_chained(self): doc = self.parse("feature kern {pos [A B] [T Y]' 20 comma;} kern;") @@ -891,32 +985,34 @@ def test_gpos_type_1_chained_special_kern_format_valuerecord_format_b_bug2293(se def test_gpos_type_1_chained_exception1(self): with self.assertRaisesRegex(FeatureLibError, "Positioning values are allowed"): - doc = self.parse("feature kern {" - " pos [A B]' [T Y]' comma a <0 0 0 0>;" - "} kern;") + doc = self.parse( + "feature kern {" " pos [A B]' [T Y]' comma a <0 0 0 0>;" "} kern;" + ) def test_gpos_type_1_chained_exception2(self): with self.assertRaisesRegex(FeatureLibError, "Positioning values are allowed"): - doc = self.parse("feature kern {" - " pos [A B]' <0 0 0 0> [T Y]' comma a <0 0 0 0>;" - "} kern;") + doc = self.parse( + "feature kern {" + " pos [A B]' <0 0 0 0> [T Y]' comma a <0 0 0 0>;" + "} kern;" + ) def test_gpos_type_1_chained_exception3(self): with self.assertRaisesRegex(FeatureLibError, "Positioning cannot be applied"): - doc = self.parse("feature kern {" - " pos [A B] <0 0 0 0> [T Y]' comma a <0 0 0 0>;" - "} kern;") + doc = self.parse( + "feature kern {" + " pos [A B] <0 0 0 0> [T Y]' comma a <0 0 0 0>;" + "} kern;" + ) def test_gpos_type_1_chained_exception4(self): with self.assertRaisesRegex(FeatureLibError, "Positioning values are allowed"): - doc = self.parse("feature kern {" - " pos a' b c 123 d;" - "} kern;") + doc = self.parse("feature kern {" " pos a' b c 123 d;" "} kern;") def test_gpos_type_2_format_a(self): - doc = self.parse("feature kern {" - " pos [T V] -60 [a b c] <1 2 3 4>;" - "} kern;") + doc = self.parse( + "feature kern {" " pos [T V] -60 [a b c] <1 2 3 4>;" "} kern;" + ) pos = doc.statements[0].statements[0] self.assertEqual(type(pos), ast.PairPosStatement) self.assertFalse(pos.enumerated) @@ -926,9 +1022,9 @@ def test_gpos_type_2_format_a(self): self.assertEqual(pos.valuerecord2.asFea(), "<1 2 3 4>") def test_gpos_type_2_format_a_enumerated(self): - doc = self.parse("feature kern {" - " enum pos [T V] -60 [a b c] <1 2 3 4>;" - "} kern;") + doc = self.parse( + "feature kern {" " enum pos [T V] -60 [a b c] <1 2 3 4>;" "} kern;" + ) pos = doc.statements[0].statements[0] self.assertEqual(type(pos), ast.PairPosStatement) self.assertTrue(pos.enumerated) @@ -938,9 +1034,9 @@ def test_gpos_type_2_format_a_enumerated(self): self.assertEqual(pos.valuerecord2.asFea(), "<1 2 3 4>") def test_gpos_type_2_format_a_with_null_first(self): - doc = self.parse("feature kern {" - " pos [T V] [a b c] <1 2 3 4>;" - "} kern;") + doc = self.parse( + "feature kern {" " pos [T V] [a b c] <1 2 3 4>;" "} kern;" + ) pos = doc.statements[0].statements[0] self.assertEqual(type(pos), ast.PairPosStatement) self.assertFalse(pos.enumerated) @@ -952,9 +1048,9 @@ def test_gpos_type_2_format_a_with_null_first(self): self.assertEqual(pos.asFea(), "pos [T V] [a b c] <1 2 3 4>;") def test_gpos_type_2_format_a_with_null_second(self): - doc = self.parse("feature kern {" - " pos [T V] <1 2 3 4> [a b c] ;" - "} kern;") + doc = self.parse( + "feature kern {" " pos [T V] <1 2 3 4> [a b c] ;" "} kern;" + ) pos = doc.statements[0].statements[0] self.assertEqual(type(pos), ast.PairPosStatement) self.assertFalse(pos.enumerated) @@ -965,9 +1061,7 @@ def test_gpos_type_2_format_a_with_null_second(self): self.assertEqual(pos.asFea(), "pos [T V] [a b c] <1 2 3 4>;") def test_gpos_type_2_format_b(self): - doc = self.parse("feature kern {" - " pos [T V] [a b c] <1 2 3 4>;" - "} kern;") + doc = self.parse("feature kern {" " pos [T V] [a b c] <1 2 3 4>;" "} kern;") pos = doc.statements[0].statements[0] self.assertEqual(type(pos), ast.PairPosStatement) self.assertFalse(pos.enumerated) @@ -977,9 +1071,9 @@ def test_gpos_type_2_format_b(self): self.assertIsNone(pos.valuerecord2) def test_gpos_type_2_format_b_enumerated(self): - doc = self.parse("feature kern {" - " enumerate position [T V] [a b c] <1 2 3 4>;" - "} kern;") + doc = self.parse( + "feature kern {" " enumerate position [T V] [a b c] <1 2 3 4>;" "} kern;" + ) pos = doc.statements[0].statements[0] self.assertEqual(type(pos), ast.PairPosStatement) self.assertTrue(pos.enumerated) @@ -989,9 +1083,11 @@ def test_gpos_type_2_format_b_enumerated(self): self.assertIsNone(pos.valuerecord2) def test_gpos_type_3(self): - doc = self.parse("feature kern {" - " position cursive A ;" - "} kern;") + doc = self.parse( + "feature kern {" + " position cursive A ;" + "} kern;" + ) pos = doc.statements[0].statements[0] self.assertEqual(type(pos), ast.CursivePosStatement) self.assertEqual(pos.glyphclass.glyphSet(), ("A",)) @@ -1005,7 +1101,8 @@ def test_gpos_type_3_enumerated(self): self.parse, "feature kern {" " enumerate position cursive A ;" - "} kern;") + "} kern;", + ) def test_gpos_type_4(self): doc = self.parse( @@ -1016,7 +1113,8 @@ def test_gpos_type_4(self): " position base [a e o u] " " mark @TOP_MARKS " " mark @BOTTOM_MARKS;" - "} test;") + "} test;" + ) pos = doc.statements[-1].statements[0] self.assertEqual(type(pos), ast.MarkBasePosStatement) self.assertEqual(pos.base.glyphSet(), ("a", "e", "o", "u")) @@ -1027,21 +1125,24 @@ def test_gpos_type_4(self): def test_gpos_type_4_enumerated(self): self.assertRaisesRegex( FeatureLibError, - '"enumerate" is not allowed with ' - 'mark-to-base attachment positioning', + '"enumerate" is not allowed with ' "mark-to-base attachment positioning", self.parse, "feature kern {" " markClass cedilla @BOTTOM_MARKS;" " enumerate position base A mark @BOTTOM_MARKS;" - "} kern;") + "} kern;", + ) def test_gpos_type_4_not_markClass(self): self.assertRaisesRegex( - FeatureLibError, "@MARKS is not a markClass", self.parse, + FeatureLibError, + "@MARKS is not a markClass", + self.parse, "@MARKS = [acute grave];" "feature test {" " position base [a e o u] mark @MARKS;" - "} test;") + "} test;", + ) def test_gpos_type_5(self): doc = self.parse( @@ -1058,7 +1159,8 @@ def test_gpos_type_5(self): " " " ligComponent " " mark @BOTTOM_MARKS;" - "} test;") + "} test;" + ) pos = doc.statements[-1].statements[0] self.assertEqual(type(pos), ast.MarkLigPosStatement) self.assertEqual(pos.ligatures.glyphSet(), ("a_f_f_i", "o_f_f_i")) @@ -1072,29 +1174,34 @@ def test_gpos_type_5_enumerated(self): self.assertRaisesRegex( FeatureLibError, '"enumerate" is not allowed with ' - 'mark-to-ligature attachment positioning', + "mark-to-ligature attachment positioning", self.parse, "feature test {" " markClass cedilla @MARKS;" " enumerate position " " ligature f_i mark @MARKS" " ligComponent ;" - "} test;") + "} test;", + ) def test_gpos_type_5_not_markClass(self): self.assertRaisesRegex( - FeatureLibError, "@MARKS is not a markClass", self.parse, + FeatureLibError, + "@MARKS is not a markClass", + self.parse, "@MARKS = [acute grave];" "feature test {" " position ligature f_i mark @MARKS;" - "} test;") + "} test;", + ) def test_gpos_type_6(self): doc = self.parse( "markClass damma @MARK_CLASS_1;" "feature test {" " position mark hamza mark @MARK_CLASS_1;" - "} test;") + "} test;" + ) pos = doc.statements[-1].statements[0] self.assertEqual(type(pos), ast.MarkMarkPosStatement) self.assertEqual(pos.baseMarks.glyphSet(), ("hamza",)) @@ -1104,28 +1211,32 @@ def test_gpos_type_6(self): def test_gpos_type_6_enumerated(self): self.assertRaisesRegex( FeatureLibError, - '"enumerate" is not allowed with ' - 'mark-to-mark attachment positioning', + '"enumerate" is not allowed with ' "mark-to-mark attachment positioning", self.parse, "markClass damma @MARK_CLASS_1;" "feature test {" " enum pos mark hamza mark @MARK_CLASS_1;" - "} test;") + "} test;", + ) def test_gpos_type_6_not_markClass(self): self.assertRaisesRegex( - FeatureLibError, "@MARKS is not a markClass", self.parse, + FeatureLibError, + "@MARKS is not a markClass", + self.parse, "@MARKS = [acute grave];" "feature test {" " position mark cedilla mark @MARKS;" - "} test;") + "} test;", + ) def test_gpos_type_8(self): doc = self.parse( "lookup L1 {pos one 100;} L1; lookup L2 {pos two 200;} L2;" "feature test {" " pos [A a] [B b] I' lookup L1 [N n]' lookup L2 P' [Y y] [Z z];" - "} test;") + "} test;" + ) lookup1, lookup2 = doc.statements[0:2] pos = doc.statements[-1].statements[0] self.assertEqual(type(pos), ast.ChainContextPosStatement) @@ -1142,7 +1253,8 @@ def test_gpos_type_8_lookup_with_values(self): "lookup L1 {pos one 100;} L1;" "feature test {" " pos A' lookup L1 B' 20;" - "} test;") + "} test;", + ) def test_markClass(self): doc = self.parse("markClass [acute grave] @MARKS;") @@ -1153,8 +1265,7 @@ def test_markClass(self): self.assertEqual((mc.anchor.x, mc.anchor.y), (350, 3)) def test_nameid_windows_utf16(self): - doc = self.parse( - r'table name { nameid 9 "M\00fcller-Lanc\00e9"; } name;') + doc = self.parse(r'table name { nameid 9 "M\00fcller-Lanc\00e9"; } name;') name = doc.statements[0].statements[0] self.assertIsInstance(name, ast.NameRecord) self.assertEqual(name.nameID, 9) @@ -1171,8 +1282,7 @@ def test_nameid_windows_utf16_backslash(self): self.assertEqual(name.asFea(), r'nameid 9 "Back\005cslash";') def test_nameid_windows_utf16_quotation_mark(self): - doc = self.parse( - r'table name { nameid 9 "Quotation \0022Mark\0022"; } name;') + doc = self.parse(r'table name { nameid 9 "Quotation \0022Mark\0022"; } name;') name = doc.statements[0].statements[0] self.assertEqual(name.string, 'Quotation "Mark"') self.assertEqual(name.asFea(), r'nameid 9 "Quotation \0022Mark\0022";') @@ -1184,8 +1294,7 @@ def test_nameid_windows_utf16_surroates(self): self.assertEqual(name.asFea(), r'nameid 9 "Carrot \d83e\dd55";') def test_nameid_mac_roman(self): - doc = self.parse( - r'table name { nameid 9 1 "Joachim M\9fller-Lanc\8e"; } name;') + doc = self.parse(r'table name { nameid 9 1 "Joachim M\9fller-Lanc\8e"; } name;') name = doc.statements[0].statements[0] self.assertIsInstance(name, ast.NameRecord) self.assertEqual(name.nameID, 9) @@ -1193,12 +1302,10 @@ def test_nameid_mac_roman(self): self.assertEqual(name.platEncID, 0) self.assertEqual(name.langID, 0) self.assertEqual(name.string, "Joachim Müller-Lancé") - self.assertEqual(name.asFea(), - r'nameid 9 1 "Joachim M\9fller-Lanc\8e";') + self.assertEqual(name.asFea(), r'nameid 9 1 "Joachim M\9fller-Lanc\8e";') def test_nameid_mac_croatian(self): - doc = self.parse( - r'table name { nameid 9 1 0 18 "Jovica Veljovi\e6"; } name;') + doc = self.parse(r'table name { nameid 9 1 0 18 "Jovica Veljovi\e6"; } name;') name = doc.statements[0].statements[0] self.assertEqual(name.nameID, 9) self.assertEqual(name.platformID, 1) @@ -1209,12 +1316,14 @@ def test_nameid_mac_croatian(self): def test_nameid_unsupported_platform(self): self.assertRaisesRegex( - FeatureLibError, "Expected platform id 1 or 3", - self.parse, 'table name { nameid 9 666 "Foo"; } name;') + FeatureLibError, + "Expected platform id 1 or 3", + self.parse, + 'table name { nameid 9 666 "Foo"; } name;', + ) def test_nameid_hexadecimal(self): - doc = self.parse( - r'table name { nameid 0x9 0x3 0x1 0x0409 "Test"; } name;') + doc = self.parse(r'table name { nameid 0x9 0x3 0x1 0x0409 "Test"; } name;') name = doc.statements[0].statements[0] self.assertEqual(name.nameID, 9) self.assertEqual(name.platformID, 3) @@ -1222,8 +1331,7 @@ def test_nameid_hexadecimal(self): self.assertEqual(name.langID, 0x0409) def test_nameid_octal(self): - doc = self.parse( - r'table name { nameid 011 03 012 02011 "Test"; } name;') + doc = self.parse(r'table name { nameid 011 03 012 02011 "Test"; } name;') name = doc.statements[0].statements[0] self.assertEqual(name.nameID, 9) self.assertEqual(name.platformID, 3) @@ -1231,14 +1339,12 @@ def test_nameid_octal(self): self.assertEqual(name.langID, 0o2011) def test_cv_hexadecimal(self): - doc = self.parse( - r'feature cv01 { cvParameters { Character 0x5DDE; }; } cv01;') + doc = self.parse(r"feature cv01 { cvParameters { Character 0x5DDE; }; } cv01;") cv = doc.statements[0].statements[0].statements[0] self.assertEqual(cv.character, 0x5DDE) def test_cv_octal(self): - doc = self.parse( - r'feature cv01 { cvParameters { Character 056736; }; } cv01;') + doc = self.parse(r"feature cv01 { cvParameters { Character 056736; }; } cv01;") cv = doc.statements[0].statements[0].statements[0] self.assertEqual(cv.character, 0o56736) @@ -1255,8 +1361,7 @@ def test_rsub_format_a_cid(self): doc = self.parse(r"feature test {rsub \1 [\2 \3] \4' \5 by \6;} test;") rsub = doc.statements[0].statements[0] self.assertEqual(type(rsub), ast.ReverseChainSingleSubstStatement) - self.assertEqual(glyphstr(rsub.old_prefix), - "cid00001 [cid00002 cid00003]") + self.assertEqual(glyphstr(rsub.old_prefix), "cid00001 [cid00002 cid00003]") self.assertEqual(rsub.glyphs[0].glyphSet(), ("cid00004",)) self.assertEqual(rsub.replacements[0].glyphSet(), ("cid00006",)) self.assertEqual(glyphstr(rsub.old_suffix), "cid00005") @@ -1265,51 +1370,53 @@ def test_rsub_format_b(self): doc = self.parse( "feature smcp {" " reversesub A B [one.fitted one.oldstyle]' C [d D] by one;" - "} smcp;") + "} smcp;" + ) rsub = doc.statements[0].statements[0] self.assertEqual(type(rsub), ast.ReverseChainSingleSubstStatement) self.assertEqual(glyphstr(rsub.old_prefix), "A B") self.assertEqual(glyphstr(rsub.old_suffix), "C [D d]") - self.assertEqual(mapping(rsub), { - "one.fitted": "one", - "one.oldstyle": "one" - }) + self.assertEqual(mapping(rsub), {"one.fitted": "one", "one.oldstyle": "one"}) def test_rsub_format_c(self): doc = self.parse( "feature test {" " reversesub BACK TRACK [a-d]' LOOK AHEAD by [A.sc-D.sc];" - "} test;") + "} test;" + ) rsub = doc.statements[0].statements[0] self.assertEqual(type(rsub), ast.ReverseChainSingleSubstStatement) self.assertEqual(glyphstr(rsub.old_prefix), "BACK TRACK") self.assertEqual(glyphstr(rsub.old_suffix), "LOOK AHEAD") - self.assertEqual(mapping(rsub), { - "a": "A.sc", - "b": "B.sc", - "c": "C.sc", - "d": "D.sc" - }) + self.assertEqual( + mapping(rsub), {"a": "A.sc", "b": "B.sc", "c": "C.sc", "d": "D.sc"} + ) def test_rsub_from(self): self.assertRaisesRegex( FeatureLibError, 'Reverse chaining substitutions do not support "from"', - self.parse, "feature test {rsub a from [a.1 a.2 a.3];} test;") + self.parse, + "feature test {rsub a from [a.1 a.2 a.3];} test;", + ) def test_rsub_nonsingle(self): self.assertRaisesRegex( FeatureLibError, "In reverse chaining single substitutions, only a single glyph " "or glyph class can be replaced", - self.parse, "feature test {rsub c d by c_d;} test;") + self.parse, + "feature test {rsub c d by c_d;} test;", + ) def test_rsub_multiple_replacement_glyphs(self): self.assertRaisesRegex( FeatureLibError, - 'In reverse chaining single substitutions, the replacement ' + "In reverse chaining single substitutions, the replacement " r'\(after "by"\) must be a single glyph or glyph class', - self.parse, "feature test {rsub f_i by f i;} test;") + self.parse, + "feature test {rsub f_i by f i;} test;", + ) def test_script(self): doc = self.parse("feature test {script cyrl;} test;") @@ -1321,77 +1428,92 @@ def test_script_dflt(self): self.assertRaisesRegex( FeatureLibError, '"dflt" is not a valid script tag; use "DFLT" instead', - self.parse, "feature test {script dflt;} test;") + self.parse, + "feature test {script dflt;} test;", + ) def test_stat_design_axis(self): # STAT DesignAxis - doc = self.parse('table STAT { DesignAxis opsz 0 ' - '{name "Optical Size";}; } STAT;') + doc = self.parse( + "table STAT { DesignAxis opsz 0 " '{name "Optical Size";}; } STAT;' + ) da = doc.statements[0].statements[0] self.assertIsInstance(da, ast.STATDesignAxisStatement) - self.assertEqual(da.tag, 'opsz') + self.assertEqual(da.tag, "opsz") self.assertEqual(da.axisOrder, 0) - self.assertEqual(da.names[0].string, 'Optical Size') + self.assertEqual(da.names[0].string, "Optical Size") def test_stat_axis_value_format1(self): # STAT AxisValue - doc = self.parse('table STAT { DesignAxis opsz 0 ' - '{name "Optical Size";}; ' - 'AxisValue {location opsz 8; name "Caption";}; } ' - 'STAT;') + doc = self.parse( + "table STAT { DesignAxis opsz 0 " + '{name "Optical Size";}; ' + 'AxisValue {location opsz 8; name "Caption";}; } ' + "STAT;" + ) avr = doc.statements[0].statements[1] self.assertIsInstance(avr, ast.STATAxisValueStatement) - self.assertEqual(avr.locations[0].tag, 'opsz') + self.assertEqual(avr.locations[0].tag, "opsz") self.assertEqual(avr.locations[0].values[0], 8) - self.assertEqual(avr.names[0].string, 'Caption') + self.assertEqual(avr.names[0].string, "Caption") def test_stat_axis_value_format2(self): # STAT AxisValue - doc = self.parse('table STAT { DesignAxis opsz 0 ' - '{name "Optical Size";}; ' - 'AxisValue {location opsz 8 6 10; name "Caption";}; } ' - 'STAT;') + doc = self.parse( + "table STAT { DesignAxis opsz 0 " + '{name "Optical Size";}; ' + 'AxisValue {location opsz 8 6 10; name "Caption";}; } ' + "STAT;" + ) avr = doc.statements[0].statements[1] self.assertIsInstance(avr, ast.STATAxisValueStatement) - self.assertEqual(avr.locations[0].tag, 'opsz') + self.assertEqual(avr.locations[0].tag, "opsz") self.assertEqual(avr.locations[0].values, [8, 6, 10]) - self.assertEqual(avr.names[0].string, 'Caption') + self.assertEqual(avr.names[0].string, "Caption") def test_stat_axis_value_format2_bad_range(self): # STAT AxisValue self.assertRaisesRegex( FeatureLibError, - 'Default value 5 is outside of specified range 6-10.', - self.parse, 'table STAT { DesignAxis opsz 0 ' - '{name "Optical Size";}; ' - 'AxisValue {location opsz 5 6 10; name "Caption";}; } ' - 'STAT;') + "Default value 5 is outside of specified range 6-10.", + self.parse, + "table STAT { DesignAxis opsz 0 " + '{name "Optical Size";}; ' + 'AxisValue {location opsz 5 6 10; name "Caption";}; } ' + "STAT;", + ) def test_stat_axis_value_format4(self): # STAT AxisValue self.assertRaisesRegex( FeatureLibError, - 'Only one value is allowed in a Format 4 Axis Value Record, but 3 were found.', - self.parse, 'table STAT { ' - 'DesignAxis opsz 0 {name "Optical Size";}; ' - 'DesignAxis wdth 0 {name "Width";}; ' - 'AxisValue {' - 'location opsz 8 6 10; ' - 'location wdth 400; ' - 'name "Caption";}; } ' - 'STAT;') + "Only one value is allowed in a Format 4 Axis Value Record, but 3 were found.", + self.parse, + "table STAT { " + 'DesignAxis opsz 0 {name "Optical Size";}; ' + 'DesignAxis wdth 0 {name "Width";}; ' + "AxisValue {" + "location opsz 8 6 10; " + "location wdth 400; " + 'name "Caption";}; } ' + "STAT;", + ) def test_stat_elidedfallbackname(self): # STAT ElidedFallbackName - doc = self.parse('table STAT { ElidedFallbackName {name "Roman"; ' - 'name 3 1 0x0411 "ローマン"; }; ' - '} STAT;') + doc = self.parse( + 'table STAT { ElidedFallbackName {name "Roman"; ' + 'name 3 1 0x0411 "ローマン"; }; ' + "} STAT;" + ) nameRecord = doc.statements[0].statements[0] self.assertIsInstance(nameRecord, ast.ElidedFallbackName) - self.assertEqual(nameRecord.names[0].string, 'Roman') - self.assertEqual(nameRecord.names[1].string, 'ローマン') + self.assertEqual(nameRecord.names[0].string, "Roman") + self.assertEqual(nameRecord.names[1].string, "ローマン") def test_stat_elidedfallbacknameid(self): # STAT ElidedFallbackNameID - doc = self.parse('table name { nameid 278 "Roman"; } name; ' - 'table STAT { ElidedFallbackNameID 278; ' - '} STAT;') + doc = self.parse( + 'table name { nameid 278 "Roman"; } name; ' + "table STAT { ElidedFallbackNameID 278; " + "} STAT;" + ) nameRecord = doc.statements[0].statements[0] self.assertIsInstance(nameRecord, ast.NameRecord) - self.assertEqual(nameRecord.string, 'Roman') + self.assertEqual(nameRecord.string, "Roman") def test_sub_single_format_a(self): # GSUB LookupType 1 doc = self.parse("feature smcp {substitute a by a.sc;} smcp;") @@ -1421,13 +1543,11 @@ def test_sub_single_format_b(self): # GSUB LookupType 1 doc = self.parse( "feature smcp {" " substitute [one.fitted one.oldstyle] by one;" - "} smcp;") + "} smcp;" + ) sub = doc.statements[0].statements[0] self.assertIsInstance(sub, ast.SingleSubstStatement) - self.assertEqual(mapping(sub), { - "one.fitted": "one", - "one.oldstyle": "one" - }) + self.assertEqual(mapping(sub), {"one.fitted": "one", "one.oldstyle": "one"}) self.assertEqual(glyphstr(sub.prefix), "") self.assertEqual(glyphstr(sub.suffix), "") @@ -1435,45 +1555,35 @@ def test_sub_single_format_b_chained(self): # chain to GSUB LookupType 1 doc = self.parse( "feature smcp {" " substitute PRE FIX [one.fitted one.oldstyle]' SUF FIX by one;" - "} smcp;") + "} smcp;" + ) sub = doc.statements[0].statements[0] self.assertIsInstance(sub, ast.SingleSubstStatement) - self.assertEqual(mapping(sub), { - "one.fitted": "one", - "one.oldstyle": "one" - }) + self.assertEqual(mapping(sub), {"one.fitted": "one", "one.oldstyle": "one"}) self.assertEqual(glyphstr(sub.prefix), "PRE FIX") self.assertEqual(glyphstr(sub.suffix), "SUF FIX") def test_sub_single_format_c(self): # GSUB LookupType 1 doc = self.parse( - "feature smcp {" - " substitute [a-d] by [A.sc-D.sc];" - "} smcp;") + "feature smcp {" " substitute [a-d] by [A.sc-D.sc];" "} smcp;" + ) sub = doc.statements[0].statements[0] self.assertIsInstance(sub, ast.SingleSubstStatement) - self.assertEqual(mapping(sub), { - "a": "A.sc", - "b": "B.sc", - "c": "C.sc", - "d": "D.sc" - }) + self.assertEqual( + mapping(sub), {"a": "A.sc", "b": "B.sc", "c": "C.sc", "d": "D.sc"} + ) self.assertEqual(glyphstr(sub.prefix), "") self.assertEqual(glyphstr(sub.suffix), "") def test_sub_single_format_c_chained(self): # chain to GSUB LookupType 1 doc = self.parse( - "feature smcp {" - " substitute [a-d]' X Y [Z z] by [A.sc-D.sc];" - "} smcp;") + "feature smcp {" " substitute [a-d]' X Y [Z z] by [A.sc-D.sc];" "} smcp;" + ) sub = doc.statements[0].statements[0] self.assertIsInstance(sub, ast.SingleSubstStatement) - self.assertEqual(mapping(sub), { - "a": "A.sc", - "b": "B.sc", - "c": "C.sc", - "d": "D.sc" - }) + self.assertEqual( + mapping(sub), {"a": "A.sc", "b": "B.sc", "c": "C.sc", "d": "D.sc"} + ) self.assertEqual(glyphstr(sub.prefix), "") self.assertEqual(glyphstr(sub.suffix), "X Y [Z z]") @@ -1481,14 +1591,18 @@ def test_sub_single_format_c_different_num_elements(self): self.assertRaisesRegex( FeatureLibError, 'Expected a glyph class with 4 elements after "by", ' - 'but found a glyph class with 26 elements', - self.parse, "feature smcp {sub [a-d] by [A.sc-Z.sc];} smcp;") + "but found a glyph class with 26 elements", + self.parse, + "feature smcp {sub [a-d] by [A.sc-Z.sc];} smcp;", + ) def test_sub_with_values(self): self.assertRaisesRegex( FeatureLibError, "Substitution statements cannot contain values", - self.parse, "feature smcp {sub A' 20 by A.sc;} smcp;") + self.parse, + "feature smcp {sub A' 20 by A.sc;} smcp;", + ) def test_substitute_multiple(self): # GSUB LookupType 2 doc = self.parse("lookup Look {substitute f_f_i by f f i;} Look;") @@ -1518,52 +1632,59 @@ def test_substitute_multiple_by_mutliple(self): "Direct substitution of multiple glyphs by multiple glyphs " "is not supported", self.parse, - "lookup MxM {sub a b c by d e f;} MxM;") + "lookup MxM {sub a b c by d e f;} MxM;", + ) def test_split_marked_glyphs_runs(self): self.assertRaisesRegex( FeatureLibError, "Unsupported contextual target sequence", - self.parse, "feature test{" - " ignore pos a' x x A';" - "} test;") + self.parse, + "feature test{" " ignore pos a' x x A';" "} test;", + ) self.assertRaisesRegex( FeatureLibError, "Unsupported contextual target sequence", - self.parse, "lookup shift {" - " pos a <0 -10 0 0>;" - " pos A <0 10 0 0>;" - "} shift;" - "feature test {" - " sub a' lookup shift x x A' lookup shift;" - "} test;") + self.parse, + "lookup shift {" + " pos a <0 -10 0 0>;" + " pos A <0 10 0 0>;" + "} shift;" + "feature test {" + " sub a' lookup shift x x A' lookup shift;" + "} test;", + ) self.assertRaisesRegex( FeatureLibError, "Unsupported contextual target sequence", - self.parse, "feature test {" - " ignore sub a' x x A';" - "} test;") + self.parse, + "feature test {" " ignore sub a' x x A';" "} test;", + ) self.assertRaisesRegex( FeatureLibError, "Unsupported contextual target sequence", - self.parse, "lookup upper {" - " sub a by A;" - "} upper;" - "lookup lower {" - " sub A by a;" - "} lower;" - "feature test {" - " sub a' lookup upper x x A' lookup lower;" - "} test;") + self.parse, + "lookup upper {" + " sub a by A;" + "} upper;" + "lookup lower {" + " sub A by a;" + "} lower;" + "feature test {" + " sub a' lookup upper x x A' lookup lower;" + "} test;", + ) def test_substitute_mix_single_multiple(self): - doc = self.parse("lookup Look {" - " sub f_f by f f;" - " sub f by f;" - " sub f_f_i by f f i;" - " sub [a a.sc] by a;" - " sub [a a.sc] by [b b.sc];" - "} Look;") + doc = self.parse( + "lookup Look {" + " sub f_f by f f;" + " sub f by f;" + " sub f_f_i by f f i;" + " sub [a a.sc] by a;" + " sub [a a.sc] by [b b.sc];" + "} Look;" + ) statements = doc.statements[0].statements for sub in statements: self.assertIsInstance(sub, ast.MultipleSubstStatement) @@ -1579,9 +1700,9 @@ def test_substitute_mix_single_multiple(self): self.assertEqual(statements[6].replacement, ["b.sc"]) def test_substitute_from(self): # GSUB LookupType 3 - doc = self.parse("feature test {" - " substitute a from [a.1 a.2 a.3];" - "} test;") + doc = self.parse( + "feature test {" " substitute a from [a.1 a.2 a.3];" "} test;" + ) sub = doc.statements[0].statements[0] self.assertIsInstance(sub, ast.AlternateSubstStatement) self.assertEqual(glyphstr(sub.prefix), "") @@ -1590,9 +1711,9 @@ def test_substitute_from(self): # GSUB LookupType 3 self.assertEqual(glyphstr([sub.replacement]), "[a.1 a.2 a.3]") def test_substitute_from_chained(self): # chain to GSUB LookupType 3 - doc = self.parse("feature test {" - " substitute A B a' [Y y] Z from [a.1 a.2 a.3];" - "} test;") + doc = self.parse( + "feature test {" " substitute A B a' [Y y] Z from [a.1 a.2 a.3];" "} test;" + ) sub = doc.statements[0].statements[0] self.assertIsInstance(sub, ast.AlternateSubstStatement) self.assertEqual(glyphstr(sub.prefix), "A B") @@ -1601,9 +1722,9 @@ def test_substitute_from_chained(self): # chain to GSUB LookupType 3 self.assertEqual(glyphstr([sub.replacement]), "[a.1 a.2 a.3]") def test_substitute_from_cid(self): # GSUB LookupType 3 - doc = self.parse(r"feature test {" - r" substitute \7 from [\111 \222];" - r"} test;") + doc = self.parse( + r"feature test {" r" substitute \7 from [\111 \222];" r"} test;" + ) sub = doc.statements[0].statements[0] self.assertIsInstance(sub, ast.AlternateSubstStatement) self.assertEqual(glyphstr(sub.prefix), "") @@ -1612,17 +1733,18 @@ def test_substitute_from_cid(self): # GSUB LookupType 3 self.assertEqual(glyphstr([sub.replacement]), "[cid00111 cid00222]") def test_substitute_from_glyphclass(self): # GSUB LookupType 3 - doc = self.parse("feature test {" - " @Ampersands = [ampersand.1 ampersand.2];" - " substitute ampersand from @Ampersands;" - "} test;") + doc = self.parse( + "feature test {" + " @Ampersands = [ampersand.1 ampersand.2];" + " substitute ampersand from @Ampersands;" + "} test;" + ) [glyphclass, sub] = doc.statements[0].statements self.assertIsInstance(sub, ast.AlternateSubstStatement) self.assertEqual(glyphstr(sub.prefix), "") self.assertEqual(glyphstr([sub.glyph]), "ampersand") self.assertEqual(glyphstr(sub.suffix), "") - self.assertEqual(glyphstr([sub.replacement]), - "[ampersand.1 ampersand.2]") + self.assertEqual(glyphstr([sub.replacement]), "[ampersand.1 ampersand.2]") def test_substitute_ligature(self): # GSUB LookupType 4 doc = self.parse("feature liga {substitute f f i by f_f_i;} liga;") @@ -1652,13 +1774,15 @@ def test_substitute_missing_by(self): self.assertRaisesRegex( FeatureLibError, 'Expected "by", "from" or explicit lookup references', - self.parse, "feature liga {substitute f f i;} liga;") - + self.parse, + "feature liga {substitute f f i;} liga;", + ) + def test_substitute_invalid_statement(self): self.assertRaisesRegex( FeatureLibError, "Invalid substitution statement", - Parser(self.getpath("GSUB_error.fea"), GLYPHNAMES).parse + Parser(self.getpath("GSUB_error.fea"), GLYPHNAMES).parse, ) def test_subtable(self): @@ -1668,8 +1792,11 @@ def test_subtable(self): def test_table_badEnd(self): self.assertRaisesRegex( - FeatureLibError, 'Expected "GDEF"', self.parse, - "table GDEF {LigatureCaretByPos f_i 400;} ABCD;") + FeatureLibError, + 'Expected "GDEF"', + self.parse, + "table GDEF {LigatureCaretByPos f_i 400;} ABCD;", + ) def test_table_comment(self): for table in "BASE GDEF OS/2 head hhea name vhea".split(): @@ -1680,8 +1807,11 @@ def test_table_comment(self): def test_table_unsupported(self): self.assertRaisesRegex( - FeatureLibError, '"table Foo" is not supported', self.parse, - "table Foo {LigatureCaretByPos f_i 400;} Foo;") + FeatureLibError, + '"table Foo" is not supported', + self.parse, + "table Foo {LigatureCaretByPos f_i 400;} Foo;", + ) def test_valuerecord_format_a_horizontal(self): doc = self.parse("feature liga {valueRecordDef 123 foo;} liga;") @@ -1745,12 +1875,13 @@ def test_valuerecord_format_a_zero_vertical(self): def test_valuerecord_format_a_vertical_contexts_(self): for tag in "vkrn vpal vhal valt".split(): - doc = self.parse( - "feature %s {valueRecordDef 77 foo;} %s;" % (tag, tag)) + doc = self.parse("feature %s {valueRecordDef 77 foo;} %s;" % (tag, tag)) value = doc.statements[0].statements[0].value if value.yAdvance != 77: - self.fail(msg="feature %s should be a vertical context " - "for ValueRecord format A" % tag) + self.fail( + msg="feature %s should be a vertical context " + "for ValueRecord format A" % tag + ) def test_valuerecord_format_b(self): doc = self.parse("feature liga {valueRecordDef <1 2 3 4> foo;} liga;") @@ -1792,7 +1923,8 @@ def test_valuerecord_format_c(self): " " " " " > foo;" - "} liga;") + "} liga;" + ) value = doc.statements[0].statements[0].value self.assertEqual(value.xPlacement, 1) self.assertEqual(value.yPlacement, 2) @@ -1802,9 +1934,11 @@ def test_valuerecord_format_c(self): self.assertEqual(value.yPlaDevice, ((11, 111), (12, 112))) self.assertIsNone(value.xAdvDevice) self.assertEqual(value.yAdvDevice, ((33, -113), (44, -114), (55, 115))) - self.assertEqual(value.asFea(), - "<1 2 3 4 " - " >") + self.assertEqual( + value.asFea(), + "<1 2 3 4 " + " >", + ) def test_valuerecord_format_d(self): doc = self.parse("feature test {valueRecordDef foo;} test;") @@ -1813,13 +1947,20 @@ def test_valuerecord_format_d(self): self.assertEqual(value.asFea(), "") def test_valuerecord_variable_scalar(self): - doc = self.parse("feature test {valueRecordDef <0 (wght=200:-100 wght=900:-150 wdth=150,wght=900:-120) 0 0> foo;} test;") + doc = self.parse( + "feature test {valueRecordDef <0 (wght=200:-100 wght=900:-150 wdth=150,wght=900:-120) 0 0> foo;} test;" + ) value = doc.statements[0].statements[0].value - self.assertEqual(value.asFea(), "<0 (wght=200:-100 wght=900:-150 wdth=150,wght=900:-120) 0 0>") + self.assertEqual( + value.asFea(), + "<0 (wght=200:-100 wght=900:-150 wdth=150,wght=900:-120) 0 0>", + ) def test_valuerecord_named(self): - doc = self.parse("valueRecordDef <1 2 3 4> foo;" - "feature liga {valueRecordDef bar;} liga;") + doc = self.parse( + "valueRecordDef <1 2 3 4> foo;" + "feature liga {valueRecordDef bar;} liga;" + ) value = doc.statements[1].statements[0].value self.assertEqual(value.xPlacement, 1) self.assertEqual(value.yPlacement, 2) @@ -1828,8 +1969,11 @@ def test_valuerecord_named(self): def test_valuerecord_named_unknown(self): self.assertRaisesRegex( - FeatureLibError, "Unknown valueRecordDef \"unknown\"", - self.parse, "valueRecordDef foo;") + FeatureLibError, + 'Unknown valueRecordDef "unknown"', + self.parse, + "valueRecordDef foo;", + ) def test_valuerecord_scoping(self): [foo, liga, smcp] = self.parse( @@ -1843,34 +1987,45 @@ def test_valuerecord_scoping(self): def test_valuerecord_device_value_out_of_range(self): self.assertRaisesRegex( - FeatureLibError, r"Device value out of valid range \(-128..127\)", + FeatureLibError, + r"Device value out of valid range \(-128..127\)", self.parse, "valueRecordDef <1 2 3 4 " - " > foo;") + " > foo;", + ) def test_conditionset(self): doc = self.parse("conditionset heavy { wght 700 900; } heavy;") value = doc.statements[0] self.assertEqual(value.conditions["wght"], (700, 900)) - self.assertEqual(value.asFea(), "conditionset heavy {\n wght 700 900;\n} heavy;\n") + self.assertEqual( + value.asFea(), "conditionset heavy {\n wght 700 900;\n} heavy;\n" + ) doc = self.parse("conditionset heavy { wght 700 900; opsz 17 18;} heavy;") value = doc.statements[0] self.assertEqual(value.conditions["wght"], (700, 900)) self.assertEqual(value.conditions["opsz"], (17, 18)) - self.assertEqual(value.asFea(), "conditionset heavy {\n wght 700 900;\n opsz 17 18;\n} heavy;\n") + self.assertEqual( + value.asFea(), + "conditionset heavy {\n wght 700 900;\n opsz 17 18;\n} heavy;\n", + ) def test_conditionset_same_axis(self): self.assertRaisesRegex( - FeatureLibError, r"Repeated condition for axis wght", + FeatureLibError, + r"Repeated condition for axis wght", self.parse, - "conditionset heavy { wght 700 900; wght 100 200; } heavy;") + "conditionset heavy { wght 700 900; wght 100 200; } heavy;", + ) def test_conditionset_float(self): doc = self.parse("conditionset heavy { wght 700.0 900.0; } heavy;") value = doc.statements[0] self.assertEqual(value.conditions["wght"], (700.0, 900.0)) - self.assertEqual(value.asFea(), "conditionset heavy {\n wght 700.0 900.0;\n} heavy;\n") + self.assertEqual( + value.asFea(), "conditionset heavy {\n wght 700.0 900.0;\n} heavy;\n" + ) def test_variation(self): doc = self.parse("variation rvrn heavy { sub a by b; } rvrn;") @@ -1886,20 +2041,30 @@ def test_languagesystem(self): self.assertRaisesRegex( FeatureLibError, '"dflt" is not a valid script tag; use "DFLT" instead', - self.parse, "languagesystem dflt dflt;") + self.parse, + "languagesystem dflt dflt;", + ) self.assertRaisesRegex( FeatureLibError, '"DFLT" is not a valid language tag; use "dflt" instead', - self.parse, "languagesystem latn DFLT;") + self.parse, + "languagesystem latn DFLT;", + ) self.assertRaisesRegex( - FeatureLibError, "Expected ';'", - self.parse, "languagesystem latn DEU") + FeatureLibError, "Expected ';'", self.parse, "languagesystem latn DEU" + ) self.assertRaisesRegex( - FeatureLibError, "longer than 4 characters", - self.parse, "languagesystem foobar DEU;") + FeatureLibError, + "longer than 4 characters", + self.parse, + "languagesystem foobar DEU;", + ) self.assertRaisesRegex( - FeatureLibError, "longer than 4 characters", - self.parse, "languagesystem latn FOOBAR;") + FeatureLibError, + "longer than 4 characters", + self.parse, + "languagesystem latn FOOBAR;", + ) def test_empty_statement_ignored(self): doc = self.parse("feature test {;} test;") @@ -1945,4 +2110,5 @@ def test_resolve_undefined(self): if __name__ == "__main__": import sys + sys.exit(unittest.main())