#! /usr/bin/env python """usage: ttroundtrip [options] font1 ... fontN Dump each TT/OT font as a TTX file, compile again to TTF or OTF and dump again. Then do a diff on the two TTX files. Append problems and diffs to a file called "report.txt" in the current directory. This is only for testing FontTools/TTX, the resulting files are deleted afterwards. This tool supports some of ttdump's command line options (-i, -t and -x) and they will in fact be passed to ttdump. Specifying -t or -x implies ttcompile -i on the way back. Normally all output from ttdump and ttcompile is suppressed, -v (verbose) causes it to be shown. """ import sys import os import tempfile import getopt class Error(Exception): pass def usage(): print __doc__ sys.exit(2) def doCommand(cmd, verbose=1, okresult=None): print "--> ", cmd output = os.popen(cmd, "r", 1) lines = [] while 1: line = output.readline() if not line: break if verbose: sys.stdout.write(line) lines.append(line) result = os.wait()[1] >> 8 if result and result <> okresult: raise Error, result return "".join(lines) try: options, args = getopt.getopt(sys.argv[1:], "it:x:v") except getopt.GetoptError: usage() if not args: usage() dumpOptions = [] compileWithMinusI = 0 verbose = 0 for option, value in options: if option in ("-t", "-x"): compileWithMinusI = 1 dumpOptions.extend((option, value)) elif option == "-v": verbose = 1 else: dumpOptions.extend((option, value)) dumpOptions = " ".join(dumpOptions) if dumpOptions: dumpOptions = dumpOptions + " " report = open("report.txt", "a+") for ttFile in args: print "--- roundtripping %s ---" % ttFile fn = os.path.basename(ttFile) xmlFile1 = tempfile.mktemp(".%s.ttx1" % fn) ttFile2 = tempfile.mktemp(".%s" % fn) xmlFile2 = tempfile.mktemp(".%s.ttx2" % fn) try: cmd = 'ttdump -f %s"%s" "%s"' % (dumpOptions, ttFile, xmlFile1) doCommand(cmd, verbose) if compileWithMinusI: cmd = 'ttcompile -f -i "%s" "%s" "%s"' % (ttFile, xmlFile1, ttFile2) else: cmd = 'ttcompile -f "%s" "%s"' % (xmlFile1, ttFile2) doCommand(cmd, verbose) cmd = 'ttdump -f %s"%s" "%s"' % (dumpOptions, ttFile2, xmlFile2) doCommand(cmd, verbose) cmd = 'diff -c2 -I ".*modified value\|checkSumAdjustment.*" "%s" "%s"' % (xmlFile1, xmlFile2) diff = doCommand(cmd, 1, okresult=1) if diff: report.write("=============================================================\n") report.write("%s\n" % xmlFile1) report.write("%s\n" % xmlFile2) report.write("-------------------------------------------------------------\n") report.write(diff) print "--- done ---" except Error, why: report.write("=============================================================\n") report.write("%s\n" % ttFile) report.write("*** round tripping aborted (code %s) ***\n" % why) report.write("-------------------------------------------------------------\n") print "*** round tripping aborted (code %s) ***" % why for tmpFile in (xmlFile1, ttFile2, xmlFile2): if os.path.exists(tmpFile): os.remove(tmpFile)