jvr | 175f048 | 2004-09-25 08:24:37 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
jvr | a0af6c7 | 2002-09-12 18:34:59 +0000 | [diff] [blame] | 2 | |
| 3 | """usage: ttroundtrip [options] font1 ... fontN |
| 4 | |
| 5 | Dump each TT/OT font as a TTX file, compile again to TTF or OTF |
| 6 | and dump again. Then do a diff on the two TTX files. Append problems |
| 7 | and diffs to a file called "report.txt" in the current directory. |
| 8 | This is only for testing FontTools/TTX, the resulting files are |
| 9 | deleted afterwards. |
| 10 | |
jvr | 848d25d | 2002-09-12 18:53:49 +0000 | [diff] [blame] | 11 | This tool supports some of ttx's command line options (-i, -t |
| 12 | and -x). Specifying -t or -x implies ttx -m <originalfile> on |
| 13 | the way back. |
jvr | a0af6c7 | 2002-09-12 18:34:59 +0000 | [diff] [blame] | 14 | """ |
| 15 | |
| 16 | |
| 17 | import sys |
| 18 | import os |
| 19 | import tempfile |
| 20 | import getopt |
| 21 | import traceback |
| 22 | from fontTools import ttx |
| 23 | |
| 24 | class Error(Exception): pass |
| 25 | |
| 26 | |
| 27 | def usage(): |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 28 | print(__doc__) |
jvr | a0af6c7 | 2002-09-12 18:34:59 +0000 | [diff] [blame] | 29 | sys.exit(2) |
| 30 | |
| 31 | |
| 32 | def roundTrip(ttFile1, options, report): |
| 33 | fn = os.path.basename(ttFile1) |
| 34 | xmlFile1 = tempfile.mktemp(".%s.ttx1" % fn) |
| 35 | ttFile2 = tempfile.mktemp(".%s" % fn) |
| 36 | xmlFile2 = tempfile.mktemp(".%s.ttx2" % fn) |
| 37 | |
| 38 | try: |
| 39 | ttx.ttDump(ttFile1, xmlFile1, options) |
| 40 | if options.onlyTables or options.skipTables: |
| 41 | options.mergeFile = ttFile1 |
| 42 | ttx.ttCompile(xmlFile1, ttFile2, options) |
| 43 | options.mergeFile = None |
| 44 | ttx.ttDump(ttFile2, xmlFile2, options) |
| 45 | |
pabs3 | 044f898 | 2009-03-24 09:41:35 +0000 | [diff] [blame] | 46 | diffcmd = 'diff -U2 -I ".*modified value\|checkSumAdjustment.*" "%s" "%s"' % (xmlFile1, xmlFile2) |
jvr | a0af6c7 | 2002-09-12 18:34:59 +0000 | [diff] [blame] | 47 | output = os.popen(diffcmd, "r", 1) |
| 48 | lines = [] |
Behdad Esfahbod | ac1b435 | 2013-11-27 04:15:34 -0500 | [diff] [blame] | 49 | while True: |
jvr | a0af6c7 | 2002-09-12 18:34:59 +0000 | [diff] [blame] | 50 | line = output.readline() |
| 51 | if not line: |
| 52 | break |
| 53 | sys.stdout.write(line) |
| 54 | lines.append(line) |
| 55 | if lines: |
| 56 | report.write("=============================================================\n") |
| 57 | report.write(" \"%s\" differs after round tripping\n" % ttFile1) |
| 58 | report.write("-------------------------------------------------------------\n") |
| 59 | report.writelines(lines) |
| 60 | else: |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 61 | print("(TTX files are the same)") |
jvr | a0af6c7 | 2002-09-12 18:34:59 +0000 | [diff] [blame] | 62 | finally: |
| 63 | for tmpFile in (xmlFile1, ttFile2, xmlFile2): |
| 64 | if os.path.exists(tmpFile): |
| 65 | os.remove(tmpFile) |
| 66 | |
| 67 | |
| 68 | def main(args): |
| 69 | try: |
jvr | 848d25d | 2002-09-12 18:53:49 +0000 | [diff] [blame] | 70 | rawOptions, files = getopt.getopt(args, "it:x:") |
jvr | a0af6c7 | 2002-09-12 18:34:59 +0000 | [diff] [blame] | 71 | except getopt.GetoptError: |
| 72 | usage() |
| 73 | |
| 74 | if not files: |
| 75 | usage() |
| 76 | |
Haibo Huang | 79019a0 | 2019-01-08 14:14:22 -0800 | [diff] [blame] | 77 | with open("report.txt", "a+") as report: |
| 78 | options = ttx.Options(rawOptions, len(files)) |
| 79 | for ttFile in files: |
| 80 | try: |
| 81 | roundTrip(ttFile, options, report) |
| 82 | except KeyboardInterrupt: |
| 83 | print("(Cancelled)") |
| 84 | break |
| 85 | except: |
| 86 | print("*** round tripping aborted ***") |
| 87 | traceback.print_exc() |
| 88 | report.write("=============================================================\n") |
| 89 | report.write(" An exception occurred while round tripping") |
| 90 | report.write(" \"%s\"\n" % ttFile) |
| 91 | traceback.print_exc(file=report) |
| 92 | report.write("-------------------------------------------------------------\n") |
jvr | a0af6c7 | 2002-09-12 18:34:59 +0000 | [diff] [blame] | 93 | |
| 94 | |
| 95 | main(sys.argv[1:]) |