jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 1 | """\ |
| 2 | usage: ttx [options] inputfile1 [... inputfileN] |
| 3 | |
| 4 | TTX %s -- From OpenType To XML And Back |
| 5 | |
| 6 | If an input file is a TrueType or OpenType font file, it will be |
| 7 | dumped to an TTX file (an XML-based text format). |
| 8 | If an input file is a TTX file, it will be compiled to a TrueType |
| 9 | or OpenType font file. |
| 10 | |
| 11 | Output files are created so they are unique: an existing file is |
pabs3 | ca75e43 | 2011-10-30 12:26:09 +0000 | [diff] [blame] | 12 | never overwritten. |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 13 | |
| 14 | General options: |
| 15 | -h Help: print this message |
| 16 | -d <outputfolder> Specify a directory where the output files are |
| 17 | to be created. |
pabs3 | 5f41933 | 2013-06-22 06:47:34 +0000 | [diff] [blame] | 18 | -o <outputfile> Specify a file to write the output to. |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 19 | -v Verbose: more messages will be written to stdout about what |
| 20 | is being done. |
Dave Crossland | b158597 | 2013-09-04 13:16:39 +0100 | [diff] [blame] | 21 | -q Quiet: No messages will be written to stdout about what |
| 22 | is being done. |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 23 | -a allow virtual glyphs ID's on compile or decompile. |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 24 | |
| 25 | Dump options: |
| 26 | -l List table info: instead of dumping to a TTX file, list some |
| 27 | minimal info about each table. |
| 28 | -t <table> Specify a table to dump. Multiple -t options |
| 29 | are allowed. When no -t option is specified, all tables |
| 30 | will be dumped. |
| 31 | -x <table> Specify a table to exclude from the dump. Multiple |
| 32 | -x options are allowed. -t and -x are mutually exclusive. |
| 33 | -s Split tables: save the TTX data into separate TTX files per |
| 34 | table and write one small TTX file that contains references |
| 35 | to the individual table dumps. This file can be used as |
| 36 | input to ttx, as long as the table files are in the |
| 37 | same directory. |
| 38 | -i Do NOT disassemble TT instructions: when this option is given, |
| 39 | all TrueType programs (glyph programs, the font program and the |
| 40 | pre-program) will be written to the TTX file as hex data |
| 41 | instead of assembly. This saves some time and makes the TTX |
| 42 | file smaller. |
Matt Fontaine | 7baa136 | 2013-08-09 13:25:15 -0700 | [diff] [blame] | 43 | -z <format> Specify a bitmap data export option for EBDT: |
| 44 | {'raw', 'row', 'bitwise', 'extfile'} or for the CBDT: |
| 45 | {'raw', 'extfile'} Each option does one of the following: |
| 46 | -z raw |
| 47 | * export the bitmap data as a hex dump |
| 48 | -z row |
| 49 | * export each row as hex data |
| 50 | -z bitwise |
| 51 | * export each row as binary in an ASCII art style |
| 52 | -z extfile |
| 53 | * export the data as external files with XML refences |
| 54 | If no export format is specified 'raw' format is used. |
jvr | 1bcc11d | 2008-03-01 09:42:58 +0000 | [diff] [blame] | 55 | -e Don't ignore decompilation errors, but show a full traceback |
| 56 | and abort. |
pabs3 | 0a6dea0 | 2009-11-08 15:53:24 +0000 | [diff] [blame] | 57 | -y <number> Select font number for TrueType Collection, |
pabs3 | 7e91e77 | 2009-02-22 08:55:00 +0000 | [diff] [blame] | 58 | starting from 0. |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 59 | |
| 60 | Compile options: |
| 61 | -m Merge with TrueType-input-file: specify a TrueType or OpenType |
| 62 | font file to be merged with the TTX file. This option is only |
| 63 | valid when at most one TTX file is specified. |
pabs3 | ca75e43 | 2011-10-30 12:26:09 +0000 | [diff] [blame] | 64 | -b Don't recalc glyph bounding boxes: use the values in the TTX |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 65 | file as-is. |
| 66 | """ |
| 67 | |
| 68 | |
| 69 | import sys |
| 70 | import os |
| 71 | import getopt |
| 72 | import re |
| 73 | from fontTools.ttLib import TTFont |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 74 | from fontTools.ttLib.tables.otBase import OTLOffsetOverflowError |
| 75 | from fontTools.ttLib.tables.otTables import fixLookupOverFlows, fixSubTableOverFlows |
jvr | 45d1f3b | 2008-03-01 11:34:54 +0000 | [diff] [blame] | 76 | from fontTools.misc.macCreatorType import getMacCreatorAndType |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 77 | from fontTools import version |
| 78 | |
| 79 | def usage(): |
| 80 | print __doc__ % version |
| 81 | sys.exit(2) |
| 82 | |
jvr | 2e838ce | 2003-08-22 18:50:44 +0000 | [diff] [blame] | 83 | |
Behdad Esfahbod | eac107f | 2013-11-01 00:43:06 +0000 | [diff] [blame] | 84 | numberAddedRE = re.compile("#\d+$") |
pabs3 | 278d4d8 | 2013-06-22 08:16:33 +0000 | [diff] [blame] | 85 | opentypeheaderRE = re.compile('''sfntVersion=['"]OTTO["']''') |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 86 | |
| 87 | def makeOutputFileName(input, outputDir, extension): |
| 88 | dir, file = os.path.split(input) |
| 89 | file, ext = os.path.splitext(file) |
| 90 | if outputDir: |
| 91 | dir = outputDir |
Behdad Esfahbod | d1072dd | 2013-11-14 20:27:07 -0500 | [diff] [blame] | 92 | file = numberAddedRE.split(file)[0] |
Behdad Esfahbod | 271f25f | 2013-11-14 21:57:25 -0500 | [diff] [blame] | 93 | output = os.path.join(dir, file + extension) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 94 | n = 1 |
| 95 | while os.path.exists(output): |
| 96 | output = os.path.join(dir, file + "#" + repr(n) + extension) |
| 97 | n = n + 1 |
| 98 | return output |
| 99 | |
| 100 | |
| 101 | class Options: |
| 102 | |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame^] | 103 | listTables = False |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 104 | outputDir = None |
pabs3 | fb37a24 | 2013-06-22 06:43:01 +0000 | [diff] [blame] | 105 | outputFile = None |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame^] | 106 | verbose = False |
| 107 | quiet = False |
| 108 | splitTables = False |
| 109 | disassembleInstructions = True |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 110 | mergeFile = None |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame^] | 111 | recalcBBoxes = True |
| 112 | allowVID = False |
jvr | 1bcc11d | 2008-03-01 09:42:58 +0000 | [diff] [blame] | 113 | ignoreDecompileErrors = True |
Matt Fontaine | 7baa136 | 2013-08-09 13:25:15 -0700 | [diff] [blame] | 114 | bitmapGlyphDataFormat = 'raw' |
jvr | 1bcc11d | 2008-03-01 09:42:58 +0000 | [diff] [blame] | 115 | |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 116 | def __init__(self, rawOptions, numFiles): |
| 117 | self.onlyTables = [] |
| 118 | self.skipTables = [] |
pabs3 | 7e91e77 | 2009-02-22 08:55:00 +0000 | [diff] [blame] | 119 | self.fontNumber = -1 |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 120 | for option, value in rawOptions: |
| 121 | # general options |
| 122 | if option == "-h": |
| 123 | print __doc__ % version |
| 124 | sys.exit(0) |
| 125 | elif option == "-d": |
| 126 | if not os.path.isdir(value): |
| 127 | print "The -d option value must be an existing directory" |
| 128 | sys.exit(2) |
| 129 | self.outputDir = value |
pabs3 | fb37a24 | 2013-06-22 06:43:01 +0000 | [diff] [blame] | 130 | elif option == "-o": |
| 131 | self.outputFile = value |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 132 | elif option == "-v": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame^] | 133 | self.verbose = True |
Dave Crossland | b158597 | 2013-09-04 13:16:39 +0100 | [diff] [blame] | 134 | elif option == "-q": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame^] | 135 | self.quiet = True |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 136 | # dump options |
| 137 | elif option == "-l": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame^] | 138 | self.listTables = True |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 139 | elif option == "-t": |
| 140 | self.onlyTables.append(value) |
| 141 | elif option == "-x": |
| 142 | self.skipTables.append(value) |
| 143 | elif option == "-s": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame^] | 144 | self.splitTables = True |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 145 | elif option == "-i": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame^] | 146 | self.disassembleInstructions = False |
Matt Fontaine | 7baa136 | 2013-08-09 13:25:15 -0700 | [diff] [blame] | 147 | elif option == "-z": |
| 148 | validOptions = ('raw', 'row', 'bitwise', 'extfile') |
| 149 | if value not in validOptions: |
| 150 | print "-z does not allow %s as a format. Use %s" % (option, validOptions) |
| 151 | sys.exit(2) |
| 152 | self.bitmapGlyphDataFormat = value |
pabs3 | 7e91e77 | 2009-02-22 08:55:00 +0000 | [diff] [blame] | 153 | elif option == "-y": |
| 154 | self.fontNumber = int(value) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 155 | # compile options |
| 156 | elif option == "-m": |
| 157 | self.mergeFile = value |
| 158 | elif option == "-b": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame^] | 159 | self.recalcBBoxes = False |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 160 | elif option == "-a": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame^] | 161 | self.allowVID = True |
jvr | 1bcc11d | 2008-03-01 09:42:58 +0000 | [diff] [blame] | 162 | elif option == "-e": |
| 163 | self.ignoreDecompileErrors = False |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 164 | if self.onlyTables and self.skipTables: |
jvr | 6588c4e | 2004-09-25 07:35:05 +0000 | [diff] [blame] | 165 | print "-t and -x options are mutually exclusive" |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 166 | sys.exit(2) |
| 167 | if self.mergeFile and numFiles > 1: |
jvr | 6588c4e | 2004-09-25 07:35:05 +0000 | [diff] [blame] | 168 | print "Must specify exactly one TTX source file when using -m" |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 169 | sys.exit(2) |
| 170 | |
| 171 | |
| 172 | def ttList(input, output, options): |
jvr | f7f0f74 | 2002-09-14 15:31:26 +0000 | [diff] [blame] | 173 | import string |
pabs3 | 7e91e77 | 2009-02-22 08:55:00 +0000 | [diff] [blame] | 174 | ttf = TTFont(input, fontNumber=options.fontNumber) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 175 | reader = ttf.reader |
| 176 | tags = reader.keys() |
| 177 | tags.sort() |
| 178 | print 'Listing table info for "%s":' % input |
| 179 | format = " %4s %10s %7s %7s" |
| 180 | print format % ("tag ", " checksum", " length", " offset") |
| 181 | print format % ("----", "----------", "-------", "-------") |
| 182 | for tag in tags: |
| 183 | entry = reader.tables[tag] |
jvr | e0912bb | 2004-12-24 15:59:35 +0000 | [diff] [blame] | 184 | checkSum = long(entry.checkSum) |
| 185 | if checkSum < 0: |
| 186 | checkSum = checkSum + 0x100000000L |
| 187 | checksum = "0x" + string.zfill(hex(checkSum)[2:-1], 8) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 188 | print format % (tag, checksum, entry.length, entry.offset) |
| 189 | print |
| 190 | ttf.close() |
| 191 | |
| 192 | |
| 193 | def ttDump(input, output, options): |
Dave Crossland | b158597 | 2013-09-04 13:16:39 +0100 | [diff] [blame] | 194 | if not options.quiet: |
| 195 | print 'Dumping "%s" to "%s"...' % (input, output) |
jvr | 1bcc11d | 2008-03-01 09:42:58 +0000 | [diff] [blame] | 196 | ttf = TTFont(input, 0, verbose=options.verbose, allowVID=options.allowVID, |
Dave Crossland | d7efd56 | 2013-09-04 14:51:16 +0100 | [diff] [blame] | 197 | quiet=options.quiet, |
pabs3 | 7e91e77 | 2009-02-22 08:55:00 +0000 | [diff] [blame] | 198 | ignoreDecompileErrors=options.ignoreDecompileErrors, |
| 199 | fontNumber=options.fontNumber) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 200 | ttf.saveXML(output, |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame^] | 201 | quiet=options.quiet, |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 202 | tables=options.onlyTables, |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame^] | 203 | skipTables=options.skipTables, |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 204 | splitTables=options.splitTables, |
Matt Fontaine | 7baa136 | 2013-08-09 13:25:15 -0700 | [diff] [blame] | 205 | disassembleInstructions=options.disassembleInstructions, |
| 206 | bitmapGlyphDataFormat=options.bitmapGlyphDataFormat) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 207 | ttf.close() |
| 208 | |
| 209 | |
| 210 | def ttCompile(input, output, options): |
Dave Crossland | 85af40e | 2013-09-04 13:30:21 +0100 | [diff] [blame] | 211 | if not options.quiet: |
| 212 | print 'Compiling "%s" to "%s"...' % (input, output) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 213 | ttf = TTFont(options.mergeFile, |
| 214 | recalcBBoxes=options.recalcBBoxes, |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 215 | verbose=options.verbose, allowVID=options.allowVID) |
Dave Crossland | 85af40e | 2013-09-04 13:30:21 +0100 | [diff] [blame] | 216 | ttf.importXML(input, quiet=options.quiet) |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 217 | try: |
| 218 | ttf.save(output) |
| 219 | except OTLOffsetOverflowError, e: |
jvr | 142506b | 2008-03-09 20:39:38 +0000 | [diff] [blame] | 220 | # XXX This shouldn't be here at all, it should be as close to the |
| 221 | # OTL code as possible. |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 222 | overflowRecord = e.value |
| 223 | print "Attempting to fix OTLOffsetOverflowError", e |
| 224 | lastItem = overflowRecord |
| 225 | while 1: |
| 226 | ok = 0 |
| 227 | if overflowRecord.itemName == None: |
| 228 | ok = fixLookupOverFlows(ttf, overflowRecord) |
| 229 | else: |
| 230 | ok = fixSubTableOverFlows(ttf, overflowRecord) |
| 231 | if not ok: |
| 232 | raise |
| 233 | |
| 234 | try: |
| 235 | ttf.save(output) |
| 236 | break |
| 237 | except OTLOffsetOverflowError, e: |
| 238 | print "Attempting to fix OTLOffsetOverflowError", e |
| 239 | overflowRecord = e.value |
| 240 | if overflowRecord == lastItem: |
| 241 | raise |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 242 | |
| 243 | if options.verbose: |
| 244 | import time |
| 245 | print "finished at", time.strftime("%H:%M:%S", time.localtime(time.time())) |
| 246 | |
| 247 | |
| 248 | def guessFileType(fileName): |
jvr | 2e838ce | 2003-08-22 18:50:44 +0000 | [diff] [blame] | 249 | base, ext = os.path.splitext(fileName) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 250 | try: |
| 251 | f = open(fileName, "rb") |
| 252 | except IOError: |
| 253 | return None |
jvr | 45d1f3b | 2008-03-01 11:34:54 +0000 | [diff] [blame] | 254 | cr, tp = getMacCreatorAndType(fileName) |
| 255 | if tp in ("sfnt", "FFIL"): |
| 256 | return "TTF" |
| 257 | if ext == ".dfont": |
| 258 | return "TTF" |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 259 | header = f.read(256) |
| 260 | head = header[:4] |
| 261 | if head == "OTTO": |
| 262 | return "OTF" |
pabs3 | 7e91e77 | 2009-02-22 08:55:00 +0000 | [diff] [blame] | 263 | elif head == "ttcf": |
| 264 | return "TTC" |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 265 | elif head in ("\0\1\0\0", "true"): |
| 266 | return "TTF" |
| 267 | elif head.lower() == "<?xm": |
Behdad Esfahbod | eac107f | 2013-11-01 00:43:06 +0000 | [diff] [blame] | 268 | if opentypeheaderRE.search(header): |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 269 | return "OTX" |
| 270 | else: |
| 271 | return "TTX" |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 272 | return None |
| 273 | |
| 274 | |
| 275 | def parseOptions(args): |
| 276 | try: |
Dave Crossland | 5ffb91e | 2013-09-04 14:49:37 +0100 | [diff] [blame] | 277 | rawOptions, files = getopt.getopt(args, "ld:o:vqht:x:sim:z:baey:") |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 278 | except getopt.GetoptError: |
| 279 | usage() |
| 280 | |
| 281 | if not files: |
| 282 | usage() |
| 283 | |
| 284 | options = Options(rawOptions, len(files)) |
| 285 | jobs = [] |
| 286 | |
| 287 | for input in files: |
| 288 | tp = guessFileType(input) |
pabs3 | 7e91e77 | 2009-02-22 08:55:00 +0000 | [diff] [blame] | 289 | if tp in ("OTF", "TTF", "TTC"): |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 290 | extension = ".ttx" |
| 291 | if options.listTables: |
| 292 | action = ttList |
| 293 | else: |
| 294 | action = ttDump |
| 295 | elif tp == "TTX": |
| 296 | extension = ".ttf" |
| 297 | action = ttCompile |
| 298 | elif tp == "OTX": |
| 299 | extension = ".otf" |
| 300 | action = ttCompile |
| 301 | else: |
| 302 | print 'Unknown file type: "%s"' % input |
| 303 | continue |
| 304 | |
pabs3 | fb37a24 | 2013-06-22 06:43:01 +0000 | [diff] [blame] | 305 | if options.outputFile: |
| 306 | output = options.outputFile |
| 307 | else: |
| 308 | output = makeOutputFileName(input, options.outputDir, extension) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 309 | jobs.append((action, input, output)) |
jvr | 2921bb2 | 2002-09-12 20:05:23 +0000 | [diff] [blame] | 310 | return jobs, options |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 311 | |
| 312 | |
| 313 | def process(jobs, options): |
| 314 | for action, input, output in jobs: |
| 315 | action(input, output, options) |
| 316 | |
| 317 | |
| 318 | def waitForKeyPress(): |
| 319 | """Force the DOS Prompt window to stay open so the user gets |
| 320 | a chance to see what's wrong.""" |
| 321 | import msvcrt |
| 322 | print '(Hit any key to exit)' |
| 323 | while not msvcrt.kbhit(): |
| 324 | pass |
| 325 | |
| 326 | |
| 327 | def main(args): |
| 328 | jobs, options = parseOptions(args) |
| 329 | try: |
| 330 | process(jobs, options) |
| 331 | except KeyboardInterrupt: |
| 332 | print "(Cancelled.)" |
| 333 | except SystemExit: |
| 334 | if sys.platform == "win32": |
| 335 | waitForKeyPress() |
| 336 | else: |
| 337 | raise |
| 338 | except: |
| 339 | if sys.platform == "win32": |
| 340 | import traceback |
| 341 | traceback.print_exc() |
| 342 | waitForKeyPress() |
| 343 | else: |
| 344 | raise |
| 345 | |
| 346 | |
| 347 | if __name__ == "__main__": |
| 348 | main(sys.argv[1:]) |