def main(args): from fontTools import configLogger # configure the library logger (for >= WARNING) configLogger() # comment this out to enable debug messages from mtiLib's logger # log.setLevel(logging.DEBUG) font = MockFont() tableTag = None if args[0].startswith('-t'): tableTag = args[0][2:] del args[0] for f in args: log.debug("Processing %s", f) table = build(open(f, 'rt', encoding="utf-8"), font, tableTag=tableTag) blob = table.compile(font) # Make sure it compiles decompiled = table.__class__() decompiled.decompile(blob, font) # Make sure it decompiles! #continue from fontTools.misc import xmlWriter tag = table.tableTag writer = xmlWriter.XMLWriter(sys.stdout) writer.begintag(tag) writer.newline() table.toXML(writer, font) #decompiled.toXML(writer, font) writer.endtag(tag) writer.newline()
def saveXML(self, fileOrPath, newlinestr=None, writeVersion=True, **kwargs): from fontTools.misc import xmlWriter writer = xmlWriter.XMLWriter(fileOrPath, newlinestr=newlinestr) if writeVersion: from fontTools import version version = ".".join(version.split('.')[:2]) writer.begintag("ttCollection", ttLibVersion=version) else: writer.begintag("ttCollection") writer.newline() writer.newline() for font in self.fonts: font._saveXML(writer, writeVersion=False, **kwargs) writer.newline() writer.endtag("ttCollection") writer.newline() writer.close()
def main(args=None, font=None): """Convert a FontDame OTL file to TTX XML. Writes XML output to stdout. Args: args: Command line arguments (``--font``, ``--table``, input files). """ import sys from fontTools import configLogger from fontTools.misc.testTools import MockFont if args is None: args = sys.argv[1:] # configure the library logger (for >= WARNING) configLogger() # comment this out to enable debug messages from mtiLib's logger # log.setLevel(logging.DEBUG) import argparse parser = argparse.ArgumentParser( "fonttools mtiLib", description=main.__doc__, ) parser.add_argument('--font', '-f', metavar='FILE', dest="font", help="Input TTF files (used for glyph classes and sorting coverage tables)") parser.add_argument('--table', '-t', metavar='TABLE', dest="tableTag", help="Table to fill (sniffed from input file if not provided)") parser.add_argument('inputs', metavar='FILE', type=str, nargs='+', help="Input FontDame .txt files") args = parser.parse_args(args) if font is None: if args.font: font = ttLib.TTFont(args.font) else: font = MockFont() for f in args.inputs: log.debug("Processing %s", f) with open(f, 'rt', encoding="utf-8") as f: table = build(f, font, tableTag=args.tableTag) blob = table.compile(font) # Make sure it compiles decompiled = table.__class__() decompiled.decompile(blob, font) # Make sure it decompiles! #continue from fontTools.misc import xmlWriter tag = table.tableTag writer = xmlWriter.XMLWriter(sys.stdout) writer.begintag(tag) writer.newline() #table.toXML(writer, font) decompiled.toXML(writer, font) writer.endtag(tag) writer.newline()
def _saveXML(self, writer, writeVersion=True, quiet=None, tables=None, skipTables=None, splitTables=False, splitGlyphs=False, disassembleInstructions=True, bitmapGlyphDataFormat='raw'): if quiet is not None: deprecateArgument("quiet", "configure logging instead") self.disassembleInstructions = disassembleInstructions self.bitmapGlyphDataFormat = bitmapGlyphDataFormat if not tables: tables = list(self.keys()) if "GlyphOrder" not in tables: tables = ["GlyphOrder"] + tables if skipTables: for tag in skipTables: if tag in tables: tables.remove(tag) numTables = len(tables) if writeVersion: from fontTools import version version = ".".join(version.split('.')[:2]) writer.begintag("ttFont", sfntVersion=repr(tostr(self.sfntVersion))[1:-1], ttLibVersion=version) else: writer.begintag("ttFont", sfntVersion=repr(tostr(self.sfntVersion))[1:-1]) writer.newline() # always splitTables if splitGlyphs is enabled splitTables = splitTables or splitGlyphs if not splitTables: writer.newline() else: path, ext = os.path.splitext(writer.filename) fileNameTemplate = path + ".%s" + ext for i in range(numTables): tag = tables[i] if splitTables: tablePath = fileNameTemplate % tagToIdentifier(tag) tableWriter = xmlWriter.XMLWriter(tablePath, newlinestr=writer.newlinestr) tableWriter.begintag("ttFont", ttLibVersion=version) tableWriter.newline() tableWriter.newline() writer.simpletag(tagToXML(tag), src=os.path.basename(tablePath)) writer.newline() else: tableWriter = writer self._tableToXML(tableWriter, tag, splitGlyphs=splitGlyphs) if splitTables: tableWriter.endtag("ttFont") tableWriter.newline() tableWriter.close() writer.endtag("ttFont") writer.newline()
def toXML(self, writer, ttFont, splitGlyphs=False): notice = ( "The xMin, yMin, xMax and yMax values\n" "will be recalculated by the compiler.") glyphNames = ttFont.getGlyphNames() if not splitGlyphs: writer.newline() writer.comment(notice) writer.newline() writer.newline() numGlyphs = len(glyphNames) if splitGlyphs: path, ext = os.path.splitext(writer.file.name) existingGlyphFiles = set() for glyphName in glyphNames: glyph = self[glyphName] if glyph.numberOfContours: if splitGlyphs: glyphPath = userNameToFileName( tounicode(glyphName, 'utf-8'), existingGlyphFiles, prefix=path + ".", suffix=ext) existingGlyphFiles.add(glyphPath.lower()) glyphWriter = xmlWriter.XMLWriter( glyphPath, idlefunc=writer.idlefunc, newlinestr=writer.newlinestr) glyphWriter.begintag("ttFont", ttLibVersion=version) glyphWriter.newline() glyphWriter.begintag("glyf") glyphWriter.newline() glyphWriter.comment(notice) glyphWriter.newline() writer.simpletag("TTGlyph", src=os.path.basename(glyphPath)) else: glyphWriter = writer glyphWriter.begintag('TTGlyph', [ ("name", glyphName), ("xMin", glyph.xMin), ("yMin", glyph.yMin), ("xMax", glyph.xMax), ("yMax", glyph.yMax), ]) glyphWriter.newline() glyph.toXML(glyphWriter, ttFont) glyphWriter.endtag('TTGlyph') glyphWriter.newline() if splitGlyphs: glyphWriter.endtag("glyf") glyphWriter.newline() glyphWriter.endtag("ttFont") glyphWriter.newline() glyphWriter.close() else: writer.simpletag('TTGlyph', name=glyphName) writer.comment("contains no outline data") if not splitGlyphs: writer.newline() writer.newline()
def font(self, font, file=sys.stdout): if not self.xml: return from fontTools.misc import xmlWriter writer = xmlWriter.XMLWriter(file) font.disassembleInstructions = False # Work around ttLib bug for tag in font.keys(): writer.begintag(tag) writer.newline() font[tag].toXML(writer, font) writer.endtag(tag) writer.newline()
def saveXML(self, fileOrPath, newlinestr="\n", **kwargs): """Export the font as TTX (an XML-based text file), or as a series of text files when splitTables is true. In the latter case, the 'fileOrPath' argument should be a path to a directory. The 'tables' argument must either be false (dump all tables) or a list of tables to dump. The 'skipTables' argument may be a list of tables to skip, but only when the 'tables' argument is false. """ writer = xmlWriter.XMLWriter(fileOrPath, newlinestr=newlinestr) self._saveXML(writer, **kwargs) writer.close()
def add_otl(font, otf, tags): namemap = {v: k for k, v in font.lib[PSNAMES_KEY].items()} otf.setGlyphOrder([namemap.get(n, n) for n in otf.getGlyphOrder()]) font.glyphOrder = otf.getGlyphOrder() for tag in tags: fp = BytesIO() writer = xmlWriter.XMLWriter(fp) writer.begintag("ttFont") writer.newline() writer.begintag(tag) writer.newline() table = otf[tag] table.toXML(writer, otf) writer.endtag(tag) writer.newline() writer.endtag("ttFont") writer.close() font.data[f"com.github.fonttools.ttx/{tag}.ttx"] = fp.getvalue()
def main(args): font = MockFont() tableTag = None if args[0].startswith('-t'): tableTag = args[0][2:] del args[0] for f in args: debug("Processing", f) table = build(open(f, 'rt'), font, tableTag=tableTag) blob = table.compile(font) decompiled = table.__class__() decompiled.decompile(blob, font) #continue from fontTools.misc import xmlWriter tag = table.tableTag writer = xmlWriter.XMLWriter(sys.stdout) writer.begintag(tag) writer.newline() #table.toXML(writer, font) decompiled.toXML(writer, font) writer.endtag(tag) writer.newline()
def saveXML(self, fileOrPath, progress=None, quiet=None, tables=None, skipTables=None, splitTables=False, disassembleInstructions=True, splitGlyphs=False, bitmapGlyphDataFormat='raw', newlinestr=None): """Export the font as TTX (an XML-based text file), or as a series of text files when splitTables is true. In the latter case, the 'fileOrPath' argument should be a path to a directory. The 'tables' argument must either be false (dump all tables) or a list of tables to dump. The 'skipTables' argument may be a list of tables to skip, but only when the 'tables' argument is false. """ from fontTools import version from fontTools.misc import xmlWriter # only write the MAJOR.MINOR version in the 'ttLibVersion' attribute of # TTX files' root element (without PATCH or .dev suffixes) version = ".".join(version.split('.')[:2]) if quiet is not None: deprecateArgument("quiet", "configure logging instead") self.disassembleInstructions = disassembleInstructions self.bitmapGlyphDataFormat = bitmapGlyphDataFormat if not tables: tables = list(self.keys()) if "GlyphOrder" not in tables: tables = ["GlyphOrder"] + tables if skipTables: for tag in skipTables: if tag in tables: tables.remove(tag) numTables = len(tables) if progress: progress.set(0, numTables) idlefunc = getattr(progress, "idle", None) else: idlefunc = None writer = xmlWriter.XMLWriter(fileOrPath, idlefunc=idlefunc, newlinestr=newlinestr) writer.begintag("ttFont", sfntVersion=repr(tostr(self.sfntVersion))[1:-1], ttLibVersion=version) writer.newline() if not splitTables: writer.newline() if splitTables or splitGlyphs: # 'fileOrPath' must now be a path path, ext = os.path.splitext(fileOrPath) fileNameTemplate = path + ".%s" + ext for i in range(numTables): if progress: progress.set(i) tag = tables[i] if splitTables or (splitGlyphs and tag == 'glyf'): tablePath = fileNameTemplate % tagToIdentifier(tag) else: tablePath = None if splitTables: tableWriter = xmlWriter.XMLWriter(tablePath, idlefunc=idlefunc, newlinestr=newlinestr) tableWriter.begintag("ttFont", ttLibVersion=version) tableWriter.newline() tableWriter.newline() writer.simpletag(tagToXML(tag), src=os.path.basename(tablePath)) writer.newline() else: tableWriter = writer self._tableToXML(tableWriter, tag, progress, splitGlyphs=splitGlyphs) if splitTables: tableWriter.endtag("ttFont") tableWriter.newline() tableWriter.close() if progress: progress.set((i + 1)) writer.endtag("ttFont") writer.newline() # close if 'fileOrPath' is a path; leave it open if it's a file. # The special string "-" means standard output so leave that open too if not hasattr(fileOrPath, "write") and fileOrPath != "-": writer.close()
def saveXML(self, fileOrPath, progress=None, quiet=False, tables=None, skipTables=None, splitTables=False, disassembleInstructions=True, bitmapGlyphDataFormat='raw'): """Export the font as TTX (an XML-based text file), or as a series of text files when splitTables is true. In the latter case, the 'fileOrPath' argument should be a path to a directory. The 'tables' argument must either be false (dump all tables) or a list of tables to dump. The 'skipTables' argument may be a list of tables to skip, but only when the 'tables' argument is false. """ from fontTools import version from fontTools.misc import xmlWriter self.disassembleInstructions = disassembleInstructions self.bitmapGlyphDataFormat = bitmapGlyphDataFormat if not tables: tables = list(self.keys()) if "GlyphOrder" not in tables: tables = ["GlyphOrder"] + tables if skipTables: for tag in skipTables: if tag in tables: tables.remove(tag) numTables = len(tables) if progress: progress.set(0, numTables) idlefunc = getattr(progress, "idle", None) else: idlefunc = None writer = xmlWriter.XMLWriter(fileOrPath, idlefunc=idlefunc) writer.begintag("ttFont", sfntVersion=repr(self.sfntVersion)[1:-1], ttLibVersion=version) writer.newline() if not splitTables: writer.newline() else: # 'fileOrPath' must now be a path path, ext = os.path.splitext(fileOrPath) fileNameTemplate = path + ".%s" + ext for i in range(numTables): if progress: progress.set(i) tag = tables[i] if splitTables: tablePath = fileNameTemplate % tagToIdentifier(tag) tableWriter = xmlWriter.XMLWriter(tablePath, idlefunc=idlefunc) tableWriter.begintag("ttFont", ttLibVersion=version) tableWriter.newline() tableWriter.newline() writer.simpletag(tagToXML(tag), src=os.path.basename(tablePath)) writer.newline() else: tableWriter = writer self._tableToXML(tableWriter, tag, progress, quiet) if splitTables: tableWriter.endtag("ttFont") tableWriter.newline() tableWriter.close() if progress: progress.set((i + 1)) writer.endtag("ttFont") writer.newline() writer.close() if self.verbose: debugmsg("Done dumping TTX")