def test_fromXML_badDeltaFormat(self): g = TupleVariation({}, []) with CapturingLogHandler(log, "WARNING") as captor: for name, attrs, content in parseXML('<delta a="1" b="2"/>'): g.fromXML(name, attrs, content) self.assertIn("bad delta format: a, b", [r.msg for r in captor.records])
def test_parseXML_bytes_list(self): self.assertEqual(testTools.parseXML( [b'<Foo n="1"/>' b'<Foo n="2"/>']), [ ("Foo", {"n": "1"}, []), ("Foo", {"n": "2"}, []) ])
def test_fromXML(self): cs = T2CharString() for name, attrs, content in parseXML([ '<CharString name="period">' ' 338.4 142.8 rmoveto', ' 28 0 21.9 9 15.8 18 15.8 18 7.9 20.79959 0 23.6 rrcurveto', ' endchar' '</CharString>' ]): cs.fromXML(name, attrs, content) expected_program = [ 338.3999939, 142.8000031, 'rmoveto', 28, 0, 21.8999939, 9, 15.8000031, 18, 15.8000031, 18, 7.8999939, 20.7995911, 0, 23.6000061, 'rrcurveto', 'endchar' ] self.assertEqual(len(cs.program), len(expected_program)) for arg, expected_arg in zip(cs.program, expected_program): if isinstance(arg, str): self.assertIsInstance(expected_arg, str) self.assertEqual(arg, expected_arg) else: self.assertNotIsInstance(expected_arg, str) self.assertAlmostEqual(arg, expected_arg)
def test_fromXML(self): font, gvar = self.makeFont({}) for name, attrs, content in parseXML(GVAR_XML): gvar.fromXML(name, attrs, content, ttFont=font) self.assertVariationsAlmostEqual( gvar.variations, {g: v for g, v in GVAR_VARIATIONS.items() if v})
def test_fromXML_text(self): table = table__m_e_t_a() for name, attrs, content in parseXML('<text tag="dlng">' ' Latn,Grek,Cyrl' '</text>'): table.fromXML(name, attrs, content, ttFont=None) self.assertEqual({"dlng": u"Latn,Grek,Cyrl"}, table.data)
def test_fromXML(self): font, cvar = self.makeFont() for name, attrs, content in parseXML(CVAR_XML): cvar.fromXML(name, attrs, content, ttFont=font) self.assertEqual(cvar.majorVersion, 1) self.assertEqual(cvar.minorVersion, 0) self.assertEqual(cvar.variations, CVAR_VARIATIONS)
def test_fromXML(self): table = otTables.SingleSubst() for name, attrs, content in parseXML('<Substitution in="A" out="a"/>' '<Substitution in="B" out="b"/>' '<Substitution in="C" out="c"/>'): table.fromXML(name, attrs, content, self.font) self.assertEqual(table.mapping, {"A": "a", "B": "b", "C": "c"})
def test_compile_fromXML(self): mvar = newTable('MVAR') font = TTFont() for name, attrs, content in parseXML(MVAR_XML): mvar.fromXML(name, attrs, content, font=font) data = MVAR_DATA self.assertEqual(hexStr(mvar.compile(font)), hexStr(data))
def test_parseOptions_splitTables(self): file_name = "TestTTF.ttf" font_path = self.getpath(file_name) temp_path = self.temp_font(font_path, file_name) args = ["-s", temp_path] jobs, options = ttx.parseOptions(args) ttx_file_path = jobs[0][2] temp_folder = os.path.dirname(ttx_file_path) self.assertTrue(options.splitTables) self.assertTrue(os.path.exists(ttx_file_path)) ttx.process(jobs, options) # Read the TTX file but strip the first two and the last lines: # <?xml version="1.0" encoding="UTF-8"?> # <ttFont sfntVersion="\x00\x01\x00\x00" ttLibVersion="3.22"> # ... # </ttFont> parsed_xml = parseXML(self.read_file(ttx_file_path)[2:-1]) for item in parsed_xml: if not isinstance(item, tuple): continue # the tuple looks like this: # (u'head', {u'src': u'TestTTF._h_e_a_d.ttx'}, []) table_file_name = item[1].get("src") table_file_path = os.path.join(temp_folder, table_file_name) self.assertTrue(os.path.exists(table_file_path))
def test_fromXML(self): table = table__m_e_t_a() for name, attrs, content in parseXML('<hexdata tag="TEST">' ' cafebeef' '</hexdata>'): table.fromXML(name, attrs, content, ttFont=None) self.assertEqual({"TEST": b"\xCA\xFE\xBE\xEF"}, table.data)
def test_fromXML_horiz_and_vert(self): table = self.font["trak"] for name, attrs, content in parseXML(OSAKA_TRAK_TABLE_XML): table.fromXML(name, attrs, content, self.font) self.assertEqual(table.version, 1.0) self.assertEqual(table.format, 0) self.assertEqual(table.horizData, OSAKA_HORIZ_TRACK_ENTRIES) self.assertEqual(table.vertData, OSAKA_VERT_TRACK_ENTRIES)
def testCompileFromXML(self): a = otTables.InsertionMorphAction() for name, attrs, content in parseXML(self.MORPH_ACTION_XML): a.fromXML(name, attrs, content, self.font) writer = OTTableWriter() a.compile(writer, self.font, actionIndex={('B', 'C'): 9, ('B', 'A', 'D'): 7}) self.assertEqual(hexStr(writer.getAllData()), "1234fc4300090007")
def test_fromXML_text(self): table = table__m_e_t_a() for name, attrs, content in parseXML( '<text tag="dlng">' ' Latn,Grek,Cyrl' '</text>'): table.fromXML(name, attrs, content, ttFont=None) self.assertEqual({"dlng": u"Latn,Grek,Cyrl"}, table.data)
def test_fromXML_vert(self): table = self.font['trak'] for name, attrs, content in parseXML(OSAKA_VERT_ONLY_TRAK_TABLE_XML): table.fromXML(name, attrs, content, self.font) self.assertEqual(table.version, 1.0) self.assertEqual(table.format, 0) self.assertEqual(table.horizData, TrackData()) self.assertEqual(table.vertData, OSAKA_VERT_TRACK_ENTRIES)
def test_fromXML_horiz(self): table = self.font["trak"] for name, attrs, content in parseXML(SKIA_TRAK_TABLE_XML): table.fromXML(name, attrs, content, self.font) self.assertEqual(table.version, 1.0) self.assertEqual(table.format, 0) self.assertEqual(table.horizData, SKIA_TRACK_ENTRIES) self.assertEqual(table.vertData, TrackData())
def test_fromXML(self): table = table__m_e_t_a() for name, attrs, content in parseXML( '<hexdata tag="TEST">' ' cafebeef' '</hexdata>'): table.fromXML(name, attrs, content, ttFont=None) self.assertEqual({"TEST": b"\xCA\xFE\xBE\xEF"}, table.data)
def test_fromXML(self): table = otTables.MultipleSubst() for name, attrs, content in parseXML( '<Substitution in="c_t" out="c,t"/>' '<Substitution in="f_f_i" out="f,f,i"/>'): table.fromXML(name, attrs, content, self.font) self.assertEqual(table.mapping, {'c_t': ['c', 't'], 'f_f_i': ['f', 'f', 'i']})
def test_fromXML(self): table = otTables.SingleSubst() for name, attrs, content in parseXML( '<Substitution in="A" out="a"/>' '<Substitution in="B" out="b"/>' '<Substitution in="C" out="c"/>'): table.fromXML(name, attrs, content, self.font) self.assertEqual(table.mapping, {"A": "a", "B": "b", "C": "c"})
def test_round_trip_ttx(font): table = table_S_V_G_() for name, attrs, content in parseXML(OTSVG_TTX): table.fromXML(name, attrs, content, font) compiled = table.compile(font) table = table_S_V_G_() table.decompile(compiled, font) assert getXML(table.toXML, font) == OTSVG_TTX
def test_round_trip_xml(self, font): colr = table_C_O_L_R_() for name, attrs, content in parseXML(COLR_V0_XML): colr.fromXML(name, attrs, content, font) compiled = colr.compile(font) colr = table_C_O_L_R_() colr.decompile(compiled, font) assert getXML(colr.toXML, font) == COLR_V0_XML
def test_fromXML_withoutPostScriptName(self): inst = NamedInstance() for name, attrs, content in parseXML( '<NamedInstance subfamilyNameID="345">' ' <coord axis="wght" value="0.7"/>' ' <coord axis="wdth" value="0.5"/>' '</NamedInstance>'): inst.fromXML(name, attrs, content, ttFont=MakeFont()) self.assertEqual(345, inst.subfamilyNameID) self.assertEqual({"wght": 0.7, "wdth": 0.5}, inst.coordinates)
def test_fromXML(self): table = newTable("ltag") for name, attrs, content in parseXML('<version value="1"/>' '<flags value="777"/>' '<LanguageTag tag="sr-Latn"/>' '<LanguageTag tag="fa"/>'): table.fromXML(name, attrs, content, ttFont=None) self.assertEqual(1, table.version) self.assertEqual(777, table.flags) self.assertEqual(["sr-Latn", "fa"], table.tags)
def test_fromXML_no_transform(self): comp = GlyphComponent() for name, attrs, content in parseXML( ['<component glyphName="a" x="1" y="2" flags="0x2"/>']): comp.fromXML(name, attrs, content, ttFont=None) assert comp.glyphName == "a" assert comp.flags & ARGS_ARE_XY_VALUES != 0 assert (comp.x, comp.y) == (1, 2) assert not hasattr(comp, "transform")
def test_fromXML_version_as_float(self): hhea = self.font['hhea'] with CapturingLogHandler(log, "WARNING") as captor: for name, attrs, content in parseXML(HHEA_XML_VERSION_AS_FLOAT): hhea.fromXML(name, attrs, content, self.font) self.assertTrue( len([r for r in captor.records if "Table version value is a float" in r.msg]) == 1) for key in hhea.__dict__: self.assertEqual(getattr(hhea, key), HHEA_AS_DICT[key])
def test_parseOptions_splitGlyphs(self): file_name = "TestTTF.ttf" font_path = self.getpath(file_name) temp_path = self.temp_font(font_path, file_name) args = ["-g", temp_path] jobs, options = ttx.parseOptions(args) ttx_file_path = jobs[0][2] temp_folder = os.path.dirname(ttx_file_path) self.assertTrue(options.splitGlyphs) # splitGlyphs also forces splitTables self.assertTrue(options.splitTables) self.assertTrue(os.path.exists(ttx_file_path)) ttx.process(jobs, options) # Read the TTX file but strip the first two and the last lines: # <?xml version="1.0" encoding="UTF-8"?> # <ttFont sfntVersion="\x00\x01\x00\x00" ttLibVersion="3.22"> # ... # </ttFont> for item in parseXML(self.read_file(ttx_file_path)[2:-1]): if not isinstance(item, tuple): continue # the tuple looks like this: # (u'head', {u'src': u'TestTTF._h_e_a_d.ttx'}, []) table_tag = item[0] table_file_name = item[1].get("src") table_file_path = os.path.join(temp_folder, table_file_name) self.assertTrue(os.path.exists(table_file_path)) if table_tag != "glyf": continue # also strip the enclosing 'glyf' element for item in parseXML(self.read_file(table_file_path)[4:-3]): if not isinstance(item, tuple): continue # glyphs without outline data only have 'name' attribute glyph_file_name = item[1].get("src") if glyph_file_name is not None: glyph_file_path = os.path.join(temp_folder, glyph_file_name) self.assertTrue(os.path.exists(glyph_file_path))
def test_fromXML_reference_points(self): comp = GlyphComponent() for name, attrs, content in parseXML([ '<component glyphName="a" firstPt="1" secondPt="2" flags="0x0"/>' ]): comp.fromXML(name, attrs, content, ttFont=None) assert comp.glyphName == "a" assert comp.flags == 0 assert (comp.firstPt, comp.secondPt) == (1, 2) assert not hasattr(comp, "transform")
def test_fromXML_points(self): g = TupleVariation({}, [None] * 4) for name, attrs, content in parseXML( '<coord axis="wdth" min="0.3" value="0.4" max="0.5"/>' '<coord axis="wght" value="1.0"/>' '<coord axis="opsz" value="-0.7"/>' '<delta pt="1" x="33" y="44"/>' '<delta pt="2" x="-2" y="170"/>'): g.fromXML(name, attrs, content) self.assertEqual(AXES, g.axes) self.assertEqual([None, (33, 44), (-2, 170), None], g.coordinates)
def test_fromXML(self): table = table__l_t_a_g() for name, attrs, content in parseXML( '<version value="1"/>' '<flags value="777"/>' '<LanguageTag tag="sr-Latn"/>' '<LanguageTag tag="fa"/>'): table.fromXML(name, attrs, content, ttFont=None) self.assertEqual(1, table.version) self.assertEqual(777, table.flags) self.assertEqual(["sr-Latn", "fa"], table.tags)
def test_fromXML(self): mtxTable = newTable(self.tag) for name, attrs, content in parseXML( '<mtx name="A" %s="674" %s="-11"/>' '<mtx name="B" %s="632" %s="79"/>' % ((self.tableClass.advanceName, self.tableClass.sideBearingName) * 2)): mtxTable.fromXML(name, attrs, content, ttFont=None) self.assertEqual(mtxTable.metrics, {'A': (674, -11), 'B': (632, 79)})
def test_fromXML_constants(self): g = TupleVariation({}, [None] * 4) for name, attrs, content in parseXML( '<coord axis="wdth" min="0.3" value="0.4" max="0.5"/>' '<coord axis="wght" value="1.0"/>' '<coord axis="opsz" value="-0.7"/>' '<delta cvt="1" value="42"/>' '<delta cvt="2" value="-23"/>'): g.fromXML(name, attrs, content) self.assertEqual(AXES, g.axes) self.assertEqual([None, 42, -23, None], g.coordinates)
def test_fromXML(self): inst = NamedInstance() for name, attrs, content in parseXML( '<NamedInstance nameID="345">' ' <coord axis="wght" value="0.7"/>' ' <coord axis="wdth" value="0.5"/>' "</NamedInstance>" ): inst.fromXML(name, attrs, content, ttFont=MakeFont()) self.assertEqual(345, inst.nameID) self.assertEqual({"wght": 0.7, "wdth": 0.5}, inst.coordinates)
def test_fromXML(self): avar = table__a_v_a_r() for name, attrs, content in parseXML( '<segment axis="wdth">' ' <mapping from="-1.0" to="-1.0"/>' ' <mapping from="0.0" to="0.0"/>' ' <mapping from="0.7" to="0.2"/>' ' <mapping from="1.0" to="1.0"/>' "</segment>" ): avar.fromXML(name, attrs, content, ttFont=None) self.assertEqual({"wdth": {-1: -1, 0: 0, 0.7: 0.2, 1.0: 1.0}}, avar.segments)
def test_fromXML(self): mtxTable = newTable(self.tag) for name, attrs, content in parseXML( '<mtx name="A" %s="674" %s="-11"/>' '<mtx name="B" %s="632" %s="79"/>' % ( (self.tableClass.advanceName, self.tableClass.sideBearingName) * 2)): mtxTable.fromXML(name, attrs, content, ttFont=None) self.assertEqual( mtxTable.metrics, {'A': (674, -11), 'B': (632, 79)})
def test_fromXML(self): avar = table__a_v_a_r() for name, attrs, content in parseXML( '<segment axis="wdth">' ' <mapping from="-1.0" to="-1.0"/>' ' <mapping from="0.0" to="0.0"/>' ' <mapping from="0.7" to="0.2"/>' ' <mapping from="1.0" to="1.0"/>' '</segment>'): avar.fromXML(name, attrs, content, ttFont=None) self.assertEqual({"wdth": {-1: -1, 0: 0, 0.7: 0.2, 1.0: 1.0}}, avar.segments)
def testCompileFromXML(self): a = otTables.InsertionMorphAction() for name, attrs, content in parseXML(self.MORPH_ACTION_XML): a.fromXML(name, attrs, content, self.font) writer = OTTableWriter() a.compile(writer, self.font, actionIndex={ ('B', 'C'): 9, ('B', 'A', 'D'): 7 }) self.assertEqual(hexStr(writer.getAllData()), "1234fc4300090007")
def test_fromXML_v0(self): cpal = newTable('CPAL') for name, attrs, content in parseXML( '<version value="0"/>' '<numPaletteEntries value="2"/>' '<palette index="0">' ' <color index="0" value="#12345678"/>' ' <color index="1" value="#FEDCBA98"/>' '</palette>'): cpal.fromXML(name, attrs, content, ttFont=None) self.assertEqual(cpal.version, 0) self.assertEqual(cpal.numPaletteEntries, 2) self.assertEqual(repr(cpal.palettes), '[[#12345678, #FEDCBA98]]')
def test_fromXML_withPostScriptName(self): inst = NamedInstance() for name, attrs, content in parseXML( '<NamedInstance flags="0x0" postscriptNameID="257" subfamilyNameID="345">' ' <coord axis="wght" value="0.7"/>' ' <coord axis="wdth" value="0.5"/>' '</NamedInstance>'): inst.fromXML(name, attrs, content, ttFont=MakeFont()) self.assertEqual(257, inst.postscriptNameID) self.assertEqual(345, inst.subfamilyNameID) self.assertDictAlmostEqual({ "wght": 0.6999969, "wdth": 0.5 }, inst.coordinates)
def test_fromXML(self): fvar = table__f_v_a_r() for name, attrs, content in parseXML( '<Axis>' ' <AxisTag>opsz</AxisTag>' '</Axis>' '<Axis>' ' <AxisTag>slnt</AxisTag>' '</Axis>' '<NamedInstance subfamilyNameID="765"/>' '<NamedInstance subfamilyNameID="234"/>'): fvar.fromXML(name, attrs, content, ttFont=None) self.assertEqual(["opsz", "slnt"], [a.axisTag for a in fvar.axes]) self.assertEqual([765, 234], [i.subfamilyNameID for i in fvar.instances])
def test_topDict_recalcFontBBox_empty(self): topDict = TopDict() topDict.CharStrings = CharStrings(None, None, None, PrivateDict(), None, None) topDict.CharStrings.fromXML(None, None, parseXML(""" <CharString name=".notdef"> endchar </CharString> <CharString name="space"> 123 endchar </CharString> """)) topDict.recalcFontBBox() self.assertEqual(topDict.FontBBox, [0, 0, 0, 0])
def test_fromXML(self): table = otTables.LigatureSubst() for name, attrs, content in parseXML( '<LigatureSet glyph="f">' ' <Ligature components="f,f,i" glyph="f_f_i"/>' ' <Ligature components="f,f" glyph="f_f"/>' '</LigatureSet>'): table.fromXML(name, attrs, content, self.font) self.assertEqual(set(table.ligatures.keys()), {"f"}) [ffi, ff] = table.ligatures["f"] self.assertEqual(ffi.LigGlyph, "f_f_i") self.assertEqual(ffi.Component, ["f", "f", "i"]) self.assertEqual(ff.LigGlyph, "f_f") self.assertEqual(ff.Component, ["f", "f"])
def test_fromXML(self): table = otTables.AlternateSubst() for name, attrs, content in parseXML('<AlternateSet glyph="G">' ' <Alternate glyph="G.alt2"/>' ' <Alternate glyph="G.alt1"/>' '</AlternateSet>' '<AlternateSet glyph="Z">' ' <Alternate glyph="Z.fina"/>' '</AlternateSet>'): table.fromXML(name, attrs, content, self.font) self.assertEqual(table.alternates, { "G": ["G.alt2", "G.alt1"], "Z": ["Z.fina"] })
def test_fromXML(self): table = otTables.AlternateSubst() for name, attrs, content in parseXML( '<AlternateSet glyph="G">' ' <Alternate glyph="G.alt2"/>' ' <Alternate glyph="G.alt1"/>' '</AlternateSet>' '<AlternateSet glyph="Z">' ' <Alternate glyph="Z.fina"/>' '</AlternateSet>'): table.fromXML(name, attrs, content, self.font) self.assertEqual(table.alternates, { "G": ["G.alt2", "G.alt1"], "Z": ["Z.fina"] })
def test_fromXML(self): g = GlyphVariation({}, [None] * 4) for name, attrs, content in parseXML( '<coord axis="wdth" min="0.3" value="0.4" max="0.5"/>' '<coord axis="wght" value="1.0"/>' '<coord axis="opsz" value="-0.5"/>' '<delta pt="1" x="33" y="44"/>' '<delta pt="2" x="-2" y="170"/>'): g.fromXML(name, attrs, content) self.assertEqual({ "wdth":( 0.3, 0.4, 0.5), "wght":( 0.0, 1.0, 1.0), "opsz":(-0.5, -0.5, 0.0) }, g.axes) self.assertEqual([None, (33, 44), (-2, 170), None], g.coordinates)
def test_fromXML_format_unknown(self, xml, font, version, st_length): kern = newTable("kern") for name, attrs, content in parseXML(xml): kern.fromXML(name, attrs, content, ttFont=font) assert kern.version == version assert len(kern.kernTables) == 2 st0 = kern.kernTables[0] assert st0.format == 4 assert len(st0.data) == st_length st1 = kern.kernTables[1] assert st1.format == 5 assert len(st1.data) == st_length
def test_fromXML_v0(self): cpal = newTable('CPAL') for name, attrs, content in parseXML( '<version value="0"/>' '<numPaletteEntries value="2"/>' '<palette index="0">' ' <color index="0" value="#12345678"/>' ' <color index="1" value="#FEDCBA98"/>' '</palette>'): cpal.fromXML(name, attrs, content, ttFont=None) self.assertEqual(cpal.version, 0) self.assertEqual(cpal.numPaletteEntries, 2) self.assertEqual(repr(cpal.palettes), '[[#12345678, #FEDCBA98]]') self.assertEqual(cpal.paletteLabels, [0]) self.assertEqual(cpal.paletteTypes, [0]) self.assertEqual(cpal.paletteEntryLabels, [0, 0])
def test_fromXML(self): axis = Axis() for name, attrs, content in parseXML( '<Axis>' ' <AxisTag>wght</AxisTag>' ' <MinValue>100</MinValue>' ' <DefaultValue>400</DefaultValue>' ' <MaxValue>900</MaxValue>' ' <AxisNameID>256</AxisNameID>' '</Axis>'): axis.fromXML(name, attrs, content, ttFont=None) self.assertEqual("wght", axis.axisTag) self.assertEqual(100, axis.minValue) self.assertEqual(400, axis.defaultValue) self.assertEqual(900, axis.maxValue) self.assertEqual(256, axis.axisNameID)