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 | |
Behdad Esfahbod | 32c10ee | 2013-11-27 17:46:17 -0500 | [diff] [blame] | 69 | from __future__ import print_function, division |
Behdad Esfahbod | 30e691e | 2013-11-27 17:27:45 -0500 | [diff] [blame] | 70 | from fontTools.misc.py23 import * |
Olivier Berten | 70343cc | 2013-11-19 10:32:09 +0100 | [diff] [blame] | 71 | from fontTools.ttLib import TTFont, TTLibError |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 72 | from fontTools.ttLib.tables.otBase import OTLOffsetOverflowError |
| 73 | from fontTools.ttLib.tables.otTables import fixLookupOverFlows, fixSubTableOverFlows |
jvr | 45d1f3b | 2008-03-01 11:34:54 +0000 | [diff] [blame] | 74 | from fontTools.misc.macCreatorType import getMacCreatorAndType |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 75 | from fontTools import version |
Behdad Esfahbod | 30e691e | 2013-11-27 17:27:45 -0500 | [diff] [blame] | 76 | import os |
| 77 | import sys |
| 78 | import getopt |
| 79 | import re |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 80 | |
| 81 | def usage(): |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 82 | print(__doc__ % version) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 83 | sys.exit(2) |
| 84 | |
jvr | 2e838ce | 2003-08-22 18:50:44 +0000 | [diff] [blame] | 85 | |
Behdad Esfahbod | eac107f | 2013-11-01 00:43:06 +0000 | [diff] [blame] | 86 | numberAddedRE = re.compile("#\d+$") |
pabs3 | 278d4d8 | 2013-06-22 08:16:33 +0000 | [diff] [blame] | 87 | opentypeheaderRE = re.compile('''sfntVersion=['"]OTTO["']''') |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 88 | |
| 89 | def makeOutputFileName(input, outputDir, extension): |
| 90 | dir, file = os.path.split(input) |
| 91 | file, ext = os.path.splitext(file) |
| 92 | if outputDir: |
| 93 | dir = outputDir |
Behdad Esfahbod | d1072dd | 2013-11-14 20:27:07 -0500 | [diff] [blame] | 94 | file = numberAddedRE.split(file)[0] |
Behdad Esfahbod | 271f25f | 2013-11-14 21:57:25 -0500 | [diff] [blame] | 95 | output = os.path.join(dir, file + extension) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 96 | n = 1 |
| 97 | while os.path.exists(output): |
| 98 | output = os.path.join(dir, file + "#" + repr(n) + extension) |
| 99 | n = n + 1 |
| 100 | return output |
| 101 | |
| 102 | |
Behdad Esfahbod | e388db5 | 2013-11-28 14:26:58 -0500 | [diff] [blame^] | 103 | class Options(object): |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 104 | |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame] | 105 | listTables = False |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 106 | outputDir = None |
pabs3 | fb37a24 | 2013-06-22 06:43:01 +0000 | [diff] [blame] | 107 | outputFile = None |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame] | 108 | verbose = False |
| 109 | quiet = False |
| 110 | splitTables = False |
| 111 | disassembleInstructions = True |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 112 | mergeFile = None |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame] | 113 | recalcBBoxes = True |
| 114 | allowVID = False |
jvr | 1bcc11d | 2008-03-01 09:42:58 +0000 | [diff] [blame] | 115 | ignoreDecompileErrors = True |
Matt Fontaine | 7baa136 | 2013-08-09 13:25:15 -0700 | [diff] [blame] | 116 | bitmapGlyphDataFormat = 'raw' |
jvr | 1bcc11d | 2008-03-01 09:42:58 +0000 | [diff] [blame] | 117 | |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 118 | def __init__(self, rawOptions, numFiles): |
| 119 | self.onlyTables = [] |
| 120 | self.skipTables = [] |
pabs3 | 7e91e77 | 2009-02-22 08:55:00 +0000 | [diff] [blame] | 121 | self.fontNumber = -1 |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 122 | for option, value in rawOptions: |
| 123 | # general options |
| 124 | if option == "-h": |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 125 | print(__doc__ % version) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 126 | sys.exit(0) |
| 127 | elif option == "-d": |
| 128 | if not os.path.isdir(value): |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 129 | print("The -d option value must be an existing directory") |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 130 | sys.exit(2) |
| 131 | self.outputDir = value |
pabs3 | fb37a24 | 2013-06-22 06:43:01 +0000 | [diff] [blame] | 132 | elif option == "-o": |
| 133 | self.outputFile = value |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 134 | elif option == "-v": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame] | 135 | self.verbose = True |
Dave Crossland | b158597 | 2013-09-04 13:16:39 +0100 | [diff] [blame] | 136 | elif option == "-q": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame] | 137 | self.quiet = True |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 138 | # dump options |
| 139 | elif option == "-l": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame] | 140 | self.listTables = True |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 141 | elif option == "-t": |
| 142 | self.onlyTables.append(value) |
| 143 | elif option == "-x": |
| 144 | self.skipTables.append(value) |
| 145 | elif option == "-s": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame] | 146 | self.splitTables = True |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 147 | elif option == "-i": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame] | 148 | self.disassembleInstructions = False |
Matt Fontaine | 7baa136 | 2013-08-09 13:25:15 -0700 | [diff] [blame] | 149 | elif option == "-z": |
| 150 | validOptions = ('raw', 'row', 'bitwise', 'extfile') |
| 151 | if value not in validOptions: |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 152 | print("-z does not allow %s as a format. Use %s" % (option, validOptions)) |
Matt Fontaine | 7baa136 | 2013-08-09 13:25:15 -0700 | [diff] [blame] | 153 | sys.exit(2) |
| 154 | self.bitmapGlyphDataFormat = value |
pabs3 | 7e91e77 | 2009-02-22 08:55:00 +0000 | [diff] [blame] | 155 | elif option == "-y": |
| 156 | self.fontNumber = int(value) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 157 | # compile options |
| 158 | elif option == "-m": |
| 159 | self.mergeFile = value |
| 160 | elif option == "-b": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame] | 161 | self.recalcBBoxes = False |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 162 | elif option == "-a": |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame] | 163 | self.allowVID = True |
jvr | 1bcc11d | 2008-03-01 09:42:58 +0000 | [diff] [blame] | 164 | elif option == "-e": |
| 165 | self.ignoreDecompileErrors = False |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 166 | if self.onlyTables and self.skipTables: |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 167 | print("-t and -x options are mutually exclusive") |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 168 | sys.exit(2) |
| 169 | if self.mergeFile and numFiles > 1: |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 170 | print("Must specify exactly one TTX source file when using -m") |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 171 | sys.exit(2) |
| 172 | |
| 173 | |
| 174 | def ttList(input, output, options): |
Behdad Esfahbod | 188f2a3 | 2013-11-24 19:04:25 -0500 | [diff] [blame] | 175 | ttf = TTFont(input, fontNumber=options.fontNumber, lazy=True) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 176 | reader = ttf.reader |
Behdad Esfahbod | ac1b435 | 2013-11-27 04:15:34 -0500 | [diff] [blame] | 177 | tags = sorted(reader.keys()) |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 178 | print('Listing table info for "%s":' % input) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 179 | format = " %4s %10s %7s %7s" |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 180 | print(format % ("tag ", " checksum", " length", " offset")) |
| 181 | print(format % ("----", "----------", "-------", "-------")) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 182 | for tag in tags: |
| 183 | entry = reader.tables[tag] |
Behdad Esfahbod | 7cc6d27 | 2013-11-27 04:00:15 -0500 | [diff] [blame] | 184 | checkSum = int(entry.checkSum) |
jvr | e0912bb | 2004-12-24 15:59:35 +0000 | [diff] [blame] | 185 | if checkSum < 0: |
Behdad Esfahbod | ecbe4c8 | 2013-11-27 03:37:29 -0500 | [diff] [blame] | 186 | checkSum = checkSum + 0x100000000 |
Behdad Esfahbod | 14fb031 | 2013-11-27 05:47:34 -0500 | [diff] [blame] | 187 | checksum = "0x%08X" % checkSum |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 188 | print(format % (tag, checksum, entry.length, entry.offset)) |
| 189 | print() |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 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: |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 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, |
Behdad Esfahbod | 188f2a3 | 2013-11-24 19:04:25 -0500 | [diff] [blame] | 197 | lazy=False, |
Dave Crossland | d7efd56 | 2013-09-04 14:51:16 +0100 | [diff] [blame] | 198 | quiet=options.quiet, |
pabs3 | 7e91e77 | 2009-02-22 08:55:00 +0000 | [diff] [blame] | 199 | ignoreDecompileErrors=options.ignoreDecompileErrors, |
| 200 | fontNumber=options.fontNumber) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 201 | ttf.saveXML(output, |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame] | 202 | quiet=options.quiet, |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 203 | tables=options.onlyTables, |
Behdad Esfahbod | 38fdae6 | 2013-11-24 18:49:35 -0500 | [diff] [blame] | 204 | skipTables=options.skipTables, |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 205 | splitTables=options.splitTables, |
Matt Fontaine | 7baa136 | 2013-08-09 13:25:15 -0700 | [diff] [blame] | 206 | disassembleInstructions=options.disassembleInstructions, |
| 207 | bitmapGlyphDataFormat=options.bitmapGlyphDataFormat) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 208 | ttf.close() |
| 209 | |
| 210 | |
| 211 | def ttCompile(input, output, options): |
Dave Crossland | 85af40e | 2013-09-04 13:30:21 +0100 | [diff] [blame] | 212 | if not options.quiet: |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 213 | print('Compiling "%s" to "%s"...' % (input, output)) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 214 | ttf = TTFont(options.mergeFile, |
Behdad Esfahbod | 188f2a3 | 2013-11-24 19:04:25 -0500 | [diff] [blame] | 215 | lazy=False, |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 216 | recalcBBoxes=options.recalcBBoxes, |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 217 | verbose=options.verbose, allowVID=options.allowVID) |
Dave Crossland | 85af40e | 2013-09-04 13:30:21 +0100 | [diff] [blame] | 218 | ttf.importXML(input, quiet=options.quiet) |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 219 | try: |
| 220 | ttf.save(output) |
Behdad Esfahbod | 223273f | 2013-11-27 05:09:00 -0500 | [diff] [blame] | 221 | except OTLOffsetOverflowError as e: |
jvr | 142506b | 2008-03-09 20:39:38 +0000 | [diff] [blame] | 222 | # XXX This shouldn't be here at all, it should be as close to the |
| 223 | # OTL code as possible. |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 224 | overflowRecord = e.value |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 225 | print("Attempting to fix OTLOffsetOverflowError", e) |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 226 | lastItem = overflowRecord |
Behdad Esfahbod | ac1b435 | 2013-11-27 04:15:34 -0500 | [diff] [blame] | 227 | while True: |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 228 | ok = 0 |
| 229 | if overflowRecord.itemName == None: |
| 230 | ok = fixLookupOverFlows(ttf, overflowRecord) |
| 231 | else: |
| 232 | ok = fixSubTableOverFlows(ttf, overflowRecord) |
| 233 | if not ok: |
| 234 | raise |
| 235 | |
| 236 | try: |
| 237 | ttf.save(output) |
| 238 | break |
Behdad Esfahbod | 223273f | 2013-11-27 05:09:00 -0500 | [diff] [blame] | 239 | except OTLOffsetOverflowError as e: |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 240 | print("Attempting to fix OTLOffsetOverflowError", e) |
jvr | 823f8cd | 2006-10-21 14:12:38 +0000 | [diff] [blame] | 241 | overflowRecord = e.value |
| 242 | if overflowRecord == lastItem: |
| 243 | raise |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 244 | |
| 245 | if options.verbose: |
| 246 | import time |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 247 | print("finished at", time.strftime("%H:%M:%S", time.localtime(time.time()))) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 248 | |
| 249 | |
| 250 | def guessFileType(fileName): |
jvr | 2e838ce | 2003-08-22 18:50:44 +0000 | [diff] [blame] | 251 | base, ext = os.path.splitext(fileName) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 252 | try: |
| 253 | f = open(fileName, "rb") |
| 254 | except IOError: |
| 255 | return None |
jvr | 45d1f3b | 2008-03-01 11:34:54 +0000 | [diff] [blame] | 256 | cr, tp = getMacCreatorAndType(fileName) |
| 257 | if tp in ("sfnt", "FFIL"): |
| 258 | return "TTF" |
| 259 | if ext == ".dfont": |
| 260 | return "TTF" |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 261 | header = f.read(256) |
Behdad Esfahbod | ac4672e | 2013-11-27 16:44:53 -0500 | [diff] [blame] | 262 | head = Tag(header[:4]) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 263 | if head == "OTTO": |
| 264 | return "OTF" |
pabs3 | 7e91e77 | 2009-02-22 08:55:00 +0000 | [diff] [blame] | 265 | elif head == "ttcf": |
| 266 | return "TTC" |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 267 | elif head in ("\0\1\0\0", "true"): |
| 268 | return "TTF" |
Behdad Esfahbod | ac4672e | 2013-11-27 16:44:53 -0500 | [diff] [blame] | 269 | elif head == "wOFF": |
Behdad Esfahbod | 8c35239 | 2013-11-26 12:58:28 -0500 | [diff] [blame] | 270 | return "WOFF" |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 271 | elif head.lower() == "<?xm": |
Behdad Esfahbod | c076261 | 2013-11-28 06:46:59 -0500 | [diff] [blame] | 272 | # Use 'latin1' because that can't fail. |
| 273 | header = tostr(header, 'latin1') |
Behdad Esfahbod | eac107f | 2013-11-01 00:43:06 +0000 | [diff] [blame] | 274 | if opentypeheaderRE.search(header): |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 275 | return "OTX" |
| 276 | else: |
| 277 | return "TTX" |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 278 | return None |
| 279 | |
| 280 | |
| 281 | def parseOptions(args): |
| 282 | try: |
Dave Crossland | 5ffb91e | 2013-09-04 14:49:37 +0100 | [diff] [blame] | 283 | rawOptions, files = getopt.getopt(args, "ld:o:vqht:x:sim:z:baey:") |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 284 | except getopt.GetoptError: |
| 285 | usage() |
| 286 | |
| 287 | if not files: |
| 288 | usage() |
| 289 | |
| 290 | options = Options(rawOptions, len(files)) |
| 291 | jobs = [] |
| 292 | |
| 293 | for input in files: |
| 294 | tp = guessFileType(input) |
Behdad Esfahbod | 8c35239 | 2013-11-26 12:58:28 -0500 | [diff] [blame] | 295 | if tp in ("OTF", "TTF", "TTC", "WOFF"): |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 296 | extension = ".ttx" |
| 297 | if options.listTables: |
| 298 | action = ttList |
| 299 | else: |
| 300 | action = ttDump |
| 301 | elif tp == "TTX": |
| 302 | extension = ".ttf" |
| 303 | action = ttCompile |
| 304 | elif tp == "OTX": |
| 305 | extension = ".otf" |
| 306 | action = ttCompile |
| 307 | else: |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 308 | print('Unknown file type: "%s"' % input) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 309 | continue |
| 310 | |
pabs3 | fb37a24 | 2013-06-22 06:43:01 +0000 | [diff] [blame] | 311 | if options.outputFile: |
| 312 | output = options.outputFile |
| 313 | else: |
| 314 | output = makeOutputFileName(input, options.outputDir, extension) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 315 | jobs.append((action, input, output)) |
jvr | 2921bb2 | 2002-09-12 20:05:23 +0000 | [diff] [blame] | 316 | return jobs, options |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 317 | |
| 318 | |
| 319 | def process(jobs, options): |
| 320 | for action, input, output in jobs: |
| 321 | action(input, output, options) |
| 322 | |
| 323 | |
| 324 | def waitForKeyPress(): |
| 325 | """Force the DOS Prompt window to stay open so the user gets |
| 326 | a chance to see what's wrong.""" |
| 327 | import msvcrt |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 328 | print('(Hit any key to exit)') |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 329 | while not msvcrt.kbhit(): |
| 330 | pass |
| 331 | |
| 332 | |
| 333 | def main(args): |
| 334 | jobs, options = parseOptions(args) |
| 335 | try: |
| 336 | process(jobs, options) |
| 337 | except KeyboardInterrupt: |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 338 | print("(Cancelled.)") |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 339 | except SystemExit: |
| 340 | if sys.platform == "win32": |
| 341 | waitForKeyPress() |
| 342 | else: |
| 343 | raise |
Behdad Esfahbod | 223273f | 2013-11-27 05:09:00 -0500 | [diff] [blame] | 344 | except TTLibError as e: |
Behdad Esfahbod | 3ec6a25 | 2013-11-27 04:57:33 -0500 | [diff] [blame] | 345 | print("Error:",e) |
jvr | 1c803b6 | 2002-09-12 17:33:12 +0000 | [diff] [blame] | 346 | except: |
| 347 | if sys.platform == "win32": |
| 348 | import traceback |
| 349 | traceback.print_exc() |
| 350 | waitForKeyPress() |
| 351 | else: |
| 352 | raise |
| 353 | |
| 354 | |
| 355 | if __name__ == "__main__": |
| 356 | main(sys.argv[1:]) |