def test_toXML(self): writer = XMLWriter(BytesIO()) table = newTable("ltag") table.decompile(self.DATA_, ttFont=None) table.toXML(writer, ttFont=None) expected = os.linesep.join([ '<?xml version="1.0" encoding="UTF-8"?>', '<version value="1"/>', '<flags value="0"/>', '<LanguageTag tag="en"/>', '<LanguageTag tag="zh-Hant"/>', '<LanguageTag tag="zh"/>' ]) + os.linesep self.assertEqual(expected.encode("utf_8"), writer.file.getvalue())
def test_toXML(self): font = MakeFont() axis = Axis() axis.decompile(FVAR_AXIS_DATA) AddName(font, "Optical Size").nameID = 256 axis.axisNameID = 256 writer = XMLWriter(BytesIO()) axis.toXML(writer, font) self.assertEqual([ '', '<!-- Optical Size -->', '<Axis>', '<AxisTag>opsz</AxisTag>', '<MinValue>-0.5</MinValue>', '<DefaultValue>1.3</DefaultValue>', '<MaxValue>1.5</MaxValue>', '<AxisNameID>256</AxisNameID>', '</Axis>' ], xml_lines(writer))
def test_toXML2(self): writer = XMLWriter(StringIO()) table = otTables.AlternateSubst() table.alternates = {"G": ["G.alt2", "G.alt1"], "Z": ["Z.fina"]} table.toXML2(writer, self.font) self.assertEqual(writer.file.getvalue().splitlines()[1:], [ '<AlternateSet glyph="G">', ' <Alternate glyph="G.alt2"/>', ' <Alternate glyph="G.alt1"/>', '</AlternateSet>', '<AlternateSet glyph="Z">', ' <Alternate glyph="Z.fina"/>', '</AlternateSet>' ])
def test_otf(otf, tmpdir): out = str(tmpdir / basename(otf)) + ".out" options = Options(otf, out) hintFiles(options) for path in (otf, out): font = TTFont(path) assert "CFF " in font writer = XMLWriter(str(tmpdir / basename(path)) + ".xml") font["CFF "].toXML(writer, font) writer.close() assert differ([str(tmpdir / basename(otf)) + ".xml", str(tmpdir / basename(out)) + ".xml"])
def test_toXML_points(self): writer = XMLWriter(BytesIO()) g = TupleVariation(AXES, [(9, 8), None, (7, 6), (0, 0), (-1, -2), None]) g.toXML(writer, ["wdth", "wght", "opsz"]) self.assertEqual([ '<tuple>', '<coord axis="wdth" min="0.25" value="0.375" max="0.5"/>', '<coord axis="wght" value="1.0"/>', '<coord axis="opsz" value="-0.75"/>', '<delta pt="0" x="9" y="8"/>', '<delta pt="2" x="7" y="6"/>', '<delta pt="3" x="0" y="0"/>', '<delta pt="4" x="-1" y="-2"/>', '</tuple>' ], TupleVariationTest.xml_lines(writer))
def test_toXML_withoutPostScriptName(self): font = MakeFont() inst = NamedInstance() inst.flags = 0xABC inst.subfamilyNameID = AddName(font, "Light Condensed").nameID inst.coordinates = {"wght": 0.7, "wdth": 0.5} writer = XMLWriter(BytesIO()) inst.toXML(writer, font) self.assertEqual([ '', '<!-- Light Condensed -->', '<NamedInstance flags="0xABC" subfamilyNameID="%s">' % inst.subfamilyNameID, '<coord axis="wght" value="0.7"/>', '<coord axis="wdth" value="0.5"/>', '</NamedInstance>' ], xml_lines(writer))
def _newPage(self, width, height): if hasattr(self, "_svgContext"): self._svgContext.endtag("svg") self.reset() self.size(width, height) self._svgData = self._svgFileClass() self._pages.append(self._svgData) self._svgContext = XMLWriter(self._svgData, encoding="utf-8", indentwhite=self.indentation) self._svgContext.width = self.width self._svgContext.height = self.height attrs = [('width', self.width), ('height', self.height), ('viewBox', f"0 0 {self.width} {self.height}")] self._svgContext.begintag("svg", attrs + self._svgTagArguments) self._svgContext.newline() self._state.transformMatrix = self._state.transformMatrix.scale(1, -1).translate(0, -self.height)
def test_cff(cff, tmpdir): out = str(tmpdir / basename(cff)) + ".out" options = Options(cff, out) hintFiles(options) for path in (cff, out): font = CFFFontSet() writer = XMLWriter(str(tmpdir / basename(path)) + ".xml") with open(path, "rb") as fp: font.decompile(fp, None) font.toXML(writer) writer.close() assert differ([str(tmpdir / basename(cff)) + ".xml", str(tmpdir / basename(out)) + ".xml"])
def test_toXML_axes_floats(self): writer = XMLWriter(BytesIO()) axes = { "wght": (0.0, 0.2999878, 0.7000122), "wdth": (0.0, 0.4000244, 0.4000244), } g = TupleVariation(axes, [None] * 5) g.toXML(writer, ["wght", "wdth"]) self.assertEqual( [ '<coord axis="wght" min="0.0" value="0.3" max="0.7"/>', '<coord axis="wdth" value="0.4"/>', ], TupleVariationTest.xml_lines(writer)[1:3] )
def test_newlinestr(self): header = b'<?xml version="1.0" encoding="UTF-8"?>' for nls in (None, '\n', '\r\n', '\r', ''): writer = XMLWriter(BytesIO(), newlinestr=nls) writer.write("hello") writer.newline() writer.write("world") writer.newline() linesep = tobytes(os.linesep) if nls is None else tobytes(nls) self.assertEqual( header + linesep + b"hello" + linesep + b"world" + linesep, writer.file.getvalue())
def test_toXML(self): avar = table__a_v_a_r() avar.segments["opsz"] = { -1.0: -1.0, 0.0: 0.0, 0.2999878: 0.7999878, 1.0: 1.0 } writer = XMLWriter(BytesIO()) avar.toXML(writer, self.makeFont(["opsz"])) self.assertEqual([ '<segment axis="opsz">', '<mapping from="-1.0" to="-1.0"/>', '<mapping from="0.0" to="0.0"/>', '<mapping from="0.3" to="0.8"/>', '<mapping from="1.0" to="1.0"/>', '</segment>' ], self.xml_lines(writer))
def test_toXML_constants(self): writer = XMLWriter(BytesIO()) g = TupleVariation(AXES, [42, None, 23, 0, -17, None]) g.toXML(writer, ["wdth", "wght", "opsz"]) self.assertEqual([ '<tuple>', '<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="0" value="42"/>', '<delta cvt="2" value="23"/>', '<delta cvt="3" value="0"/>', '<delta cvt="4" value="-17"/>', '</tuple>' ], TupleVariationTest.xml_lines(writer))
def test_vfotf(otf, tmpdir): out = str(tmpdir / basename(otf)) + ".out" options = Options(None, [otf], [out]) options.allow_no_blues = True hintFiles(options) for path in (otf, out): font = TTFont(path) assert "CFF2" in font writer = XMLWriter(str(tmpdir / basename(path)) + ".xml") font["CFF2"].toXML(writer, font) writer.close() assert differ([ str(tmpdir / basename(otf)) + ".xml", str(tmpdir / basename(out)) + ".xml" ])
def __init__(self, metaDataPath, fontPath): self.writer = XMLWriter(metaDataPath, encoding="UTF-8") self.font = Font(fontPath) self.beginMetaData() self.uniqueId() self.vendor() self.credits() self.description() self.license() self.copyright() self.trademark() self.endMetaData() self.writer.close()
def test_toXML2(self): writer = XMLWriter(StringIO()) table = otTables.LigatureSubst() table.ligatures = { "c": self.makeLigatures("ct"), "f": self.makeLigatures("ffi ff fi") } table.toXML2(writer, self.font) self.assertEqual(writer.file.getvalue().splitlines()[1:], [ '<LigatureSet glyph="c">', ' <Ligature components="c,t" glyph="c_t"/>', '</LigatureSet>', '<LigatureSet glyph="f">', ' <Ligature components="f,f,i" glyph="f_f_i"/>', ' <Ligature components="f,f" glyph="f_f"/>', ' <Ligature components="f,i" glyph="f_i"/>', '</LigatureSet>' ])
def test_decimals_otf(tmpdir): otf = "%s/dummy/decimals.otf" % DATA_DIR out = str(tmpdir / basename(otf)) + ".out" options = Options(otf, out) options.round_coords = False hintFiles(options) for path in (otf, out): font = TTFont(path) assert "CFF " in font writer = XMLWriter(str(tmpdir / basename(path)) + ".xml") font["CFF "].toXML(writer, font) writer.close() assert differ([str(tmpdir / basename(otf)) + ".xml", str(tmpdir / basename(out)) + ".xml"])
def test_toXML(self): writer = XMLWriter(BytesIO()) axes = { "wdth": (0.3, 0.4, 0.5), "wght": (0.0, 1.0, 1.0), "opsz": (-0.7, -0.7, 0.0) } g = GlyphVariation(axes, [(9, 8), None, (7, 6), (0, 0), (-1, -2), None]) g.toXML(writer, ["wdth", "wght", "opsz"]) self.assertEqual([ '<tuple>', '<coord axis="wdth" max="0.5" min="0.3" value="0.4"/>', '<coord axis="wght" value="1.0"/>', '<coord axis="opsz" value="-0.7"/>', '<delta pt="0" x="9" y="8"/>', '<delta pt="2" x="7" y="6"/>', '<delta pt="3" x="0" y="0"/>', '<delta pt="4" x="-1" y="-2"/>', '</tuple>' ], GlyphVariationTest.xml_lines(writer))
def write_sbix_to_file(filename): basename = os.path.basename(filename) xml_filename = basename + '.xml' out_filename = os.path.join(os.getcwd(), xml_filename) if os.path.exists(out_filename): print('%s already exists. not extracting' % out_filename) return out_filename print('extracting sbix chunk to file %s' % out_filename) with open(out_filename, 'wb') as fx: mx = XMLWriter(fx) mx.begintag("root") font = TTFont(filename, fontNumber=1) bix = font['sbix'] bix.toXML(xmlWriter=mx, ttFont=font) mx.endtag("root") mx.close() return out_filename
def test_mmotf(base, tmpdir): paths = sorted(glob.glob(base + "/*.otf")) # the reference font is modified in-place, make a temp copy first reference = make_temp_copy(tmpdir, paths[0]) inpaths = paths[1:] outpaths = [str(tmpdir / basename(p)) + ".out" for p in inpaths] options = Options(reference, inpaths, outpaths) hintFiles(options) refs = [p + ".ref" for p in paths] for ref, out in zip(refs, [reference] + outpaths): for path in (ref, out): font = TTFont(path) assert "CFF " in font writer = XMLWriter(str(tmpdir / basename(path)) + ".xml") font["CFF "].toXML(writer, font) writer.close() assert differ([ str(tmpdir / basename(ref)) + ".xml", str(tmpdir / basename(out)) + ".xml" ])
def toXML(self, name): writer = XMLWriter(BytesIO()) name.toXML(writer, ttFont=None) xml = writer.file.getvalue().decode("utf_8").strip() return xml.split(writer.newlinestr.decode("utf_8"))[1:]
def test_simpletag(self): writer = XMLWriter(BytesIO()) writer.simpletag("tag", a="1", b="2") self.assertEqual(HEADER + b'<tag a="1" b="2"/>', writer.file.getvalue())
def test_encoding_UTF8(self): # https://github.com/fonttools/fonttools/issues/246 writer = XMLWriter(BytesIO(), encoding="UTF8") self.assertEqual(b'<?xml version="1.0" encoding="UTF-8"?>' + linesep, writer.file.getvalue())
def test_encoding_default(self): writer = XMLWriter(BytesIO()) self.assertEqual(b'<?xml version="1.0" encoding="UTF-8"?>' + linesep, writer.file.getvalue())
def test_comment_multiline(self): writer = XMLWriter(BytesIO()) writer.comment("Hello world\nHow are you?") self.assertEqual( HEADER + b"<!-- Hello world" + linesep + b" How are you? -->", writer.file.getvalue())
def test_carriage_return_escaped(self): writer = XMLWriter(BytesIO()) writer.write("two lines\r\nseparated by Windows line endings") self.assertEqual( HEADER + b'two lines \nseparated by Windows line endings', writer.file.getvalue())
def test_comment_escaped(self): writer = XMLWriter(BytesIO()) writer.comment("This&that are <comments>") self.assertEqual( HEADER + b"<!-- This&that are <comments> -->", writer.file.getvalue())
def _asXML(self, sub): writer = XMLWriter(BytesIO()) sub.toXML(writer, self.font) out = writer.file.getvalue().decode("utf-8") return out
def test_write(self): writer = XMLWriter(BytesIO()) writer.write("foo&bar") self.assertEqual(HEADER + b"foo&bar", writer.file.getvalue())
def test_writecdata(self): writer = XMLWriter(BytesIO()) writer.writecdata("foo&bar") self.assertEqual(HEADER + b"<![CDATA[foo&bar]]>", writer.file.getvalue())
def check_mti_file(self, name, tableTag=None): xml_expected_path = self.getpath( "%s.ttx" % name + ('.' + tableTag if tableTag is not None else '')) with open(xml_expected_path, 'rt', encoding="utf-8") as xml_expected_file: xml_expected = xml_expected_file.read() font = self.create_font() with open(self.getpath("%s.txt" % name), 'rt', encoding="utf-8") as f: table = mtiLib.build(f, font, tableTag=tableTag) if tableTag is not None: self.assertEqual(tableTag, table.tableTag) tableTag = table.tableTag # Make sure it compiles. blob = table.compile(font) # Make sure it decompiles. decompiled = table.__class__() decompiled.decompile(blob, font) # XML from built object. writer = XMLWriter(StringIO(), newlinestr='\n') writer.begintag(tableTag) writer.newline() table.toXML(writer, font) writer.endtag(tableTag) writer.newline() xml_built = writer.file.getvalue() # XML from decompiled object. writer = XMLWriter(StringIO(), newlinestr='\n') writer.begintag(tableTag) writer.newline() decompiled.toXML(writer, font) writer.endtag(tableTag) writer.newline() xml_binary = writer.file.getvalue() self.expect_ttx(xml_binary, xml_built, fromfile='decompiled', tofile='built') self.expect_ttx(xml_expected, xml_built, fromfile=xml_expected_path, tofile='built') from fontTools.misc import xmlReader f = StringIO() f.write(xml_expected) f.seek(0) font2 = TTFont() font2.setGlyphOrder(font.getGlyphOrder()) reader = xmlReader.XMLReader(f, font2) reader.read(rootless=True) # XML from object read from XML. writer = XMLWriter(StringIO(), newlinestr='\n') writer.begintag(tableTag) writer.newline() font2[tableTag].toXML(writer, font) writer.endtag(tableTag) writer.newline() xml_fromxml = writer.file.getvalue() self.expect_ttx(xml_expected, xml_fromxml, fromfile=xml_expected_path, tofile='fromxml')