ttx.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. """\
  2. usage: ttx [options] inputfile1 [... inputfileN]
  3. TTX -- From OpenType To XML And Back
  4. If an input file is a TrueType or OpenType font file, it will be
  5. decompiled to a TTX file (an XML-based text format).
  6. If an input file is a TTX file, it will be compiled to whatever
  7. format the data is in, a TrueType or OpenType/CFF font file.
  8. A special input value of - means read from the standard input.
  9. Output files are created so they are unique: an existing file is
  10. never overwritten.
  11. General options
  12. ===============
  13. -h Help print this message.
  14. --version show version and exit.
  15. -d <outputfolder> Specify a directory where the output files are
  16. to be created.
  17. -o <outputfile> Specify a file to write the output to. A special
  18. value of - would use the standard output.
  19. -f Overwrite existing output file(s), ie. don't append
  20. numbers.
  21. -v Verbose: more messages will be written to stdout
  22. about what is being done.
  23. -q Quiet: No messages will be written to stdout about
  24. what is being done.
  25. -a allow virtual glyphs ID's on compile or decompile.
  26. Dump options
  27. ============
  28. -l List table info: instead of dumping to a TTX file, list
  29. some minimal info about each table.
  30. -t <table> Specify a table to dump. Multiple -t options
  31. are allowed. When no -t option is specified, all tables
  32. will be dumped.
  33. -x <table> Specify a table to exclude from the dump. Multiple
  34. -x options are allowed. -t and -x are mutually exclusive.
  35. -s Split tables: save the TTX data into separate TTX files per
  36. table and write one small TTX file that contains references
  37. to the individual table dumps. This file can be used as
  38. input to ttx, as long as the table files are in the
  39. same directory.
  40. -g Split glyf table: Save the glyf data into separate TTX files
  41. per glyph and write a small TTX for the glyf table which
  42. contains references to the individual TTGlyph elements.
  43. NOTE: specifying -g implies -s (no need for -s together
  44. with -g)
  45. -i Do NOT disassemble TT instructions: when this option is
  46. given, all TrueType programs (glyph programs, the font
  47. program and the pre-program) will be written to the TTX
  48. file as hex data instead of assembly. This saves some time
  49. and makes the TTX file smaller.
  50. -z <format> Specify a bitmap data export option for EBDT:
  51. {'raw', 'row', 'bitwise', 'extfile'} or for the CBDT:
  52. {'raw', 'extfile'} Each option does one of the following:
  53. -z raw
  54. export the bitmap data as a hex dump
  55. -z row
  56. export each row as hex data
  57. -z bitwise
  58. export each row as binary in an ASCII art style
  59. -z extfile
  60. export the data as external files with XML references
  61. If no export format is specified 'raw' format is used.
  62. -e Don't ignore decompilation errors, but show a full traceback
  63. and abort.
  64. -y <number> Select font number for TrueType Collection (.ttc/.otc),
  65. starting from 0.
  66. --unicodedata <UnicodeData.txt>
  67. Use custom database file to write character names in the
  68. comments of the cmap TTX output.
  69. --newline <value>
  70. Control how line endings are written in the XML file. It
  71. can be 'LF', 'CR', or 'CRLF'. If not specified, the
  72. default platform-specific line endings are used.
  73. Compile options
  74. ===============
  75. -m Merge with TrueType-input-file: specify a TrueType or
  76. OpenType font file to be merged with the TTX file. This
  77. option is only valid when at most one TTX file is specified.
  78. -b Don't recalc glyph bounding boxes: use the values in the
  79. TTX file as-is.
  80. --recalc-timestamp
  81. Set font 'modified' timestamp to current time.
  82. By default, the modification time of the TTX file will be
  83. used.
  84. --no-recalc-timestamp
  85. Keep the original font 'modified' timestamp.
  86. --flavor <type>
  87. Specify flavor of output font file. May be 'woff' or 'woff2'.
  88. Note that WOFF2 requires the Brotli Python extension,
  89. available at https://github.com/google/brotli
  90. --with-zopfli
  91. Use Zopfli instead of Zlib to compress WOFF. The Python
  92. extension is available at https://pypi.python.org/pypi/zopfli
  93. """
  94. from fontTools.ttLib import TTFont, TTLibError
  95. from fontTools.misc.macCreatorType import getMacCreatorAndType
  96. from fontTools.unicode import setUnicodeData
  97. from fontTools.misc.textTools import Tag, tostr
  98. from fontTools.misc.timeTools import timestampSinceEpoch
  99. from fontTools.misc.loggingTools import Timer
  100. from fontTools.misc.cliTools import makeOutputFileName
  101. import os
  102. import sys
  103. import getopt
  104. import re
  105. import logging
  106. log = logging.getLogger("fontTools.ttx")
  107. opentypeheaderRE = re.compile("""sfntVersion=['"]OTTO["']""")
  108. class Options(object):
  109. listTables = False
  110. outputDir = None
  111. outputFile = None
  112. overWrite = False
  113. verbose = False
  114. quiet = False
  115. splitTables = False
  116. splitGlyphs = False
  117. disassembleInstructions = True
  118. mergeFile = None
  119. recalcBBoxes = True
  120. ignoreDecompileErrors = True
  121. bitmapGlyphDataFormat = "raw"
  122. unicodedata = None
  123. newlinestr = "\n"
  124. recalcTimestamp = None
  125. flavor = None
  126. useZopfli = False
  127. def __init__(self, rawOptions, numFiles):
  128. self.onlyTables = []
  129. self.skipTables = []
  130. self.fontNumber = -1
  131. for option, value in rawOptions:
  132. # general options
  133. if option == "-h":
  134. print(__doc__)
  135. sys.exit(0)
  136. elif option == "--version":
  137. from fontTools import version
  138. print(version)
  139. sys.exit(0)
  140. elif option == "-d":
  141. if not os.path.isdir(value):
  142. raise getopt.GetoptError(
  143. "The -d option value must be an existing directory"
  144. )
  145. self.outputDir = value
  146. elif option == "-o":
  147. self.outputFile = value
  148. elif option == "-f":
  149. self.overWrite = True
  150. elif option == "-v":
  151. self.verbose = True
  152. elif option == "-q":
  153. self.quiet = True
  154. # dump options
  155. elif option == "-l":
  156. self.listTables = True
  157. elif option == "-t":
  158. # pad with space if table tag length is less than 4
  159. value = value.ljust(4)
  160. self.onlyTables.append(value)
  161. elif option == "-x":
  162. # pad with space if table tag length is less than 4
  163. value = value.ljust(4)
  164. self.skipTables.append(value)
  165. elif option == "-s":
  166. self.splitTables = True
  167. elif option == "-g":
  168. # -g implies (and forces) splitTables
  169. self.splitGlyphs = True
  170. self.splitTables = True
  171. elif option == "-i":
  172. self.disassembleInstructions = False
  173. elif option == "-z":
  174. validOptions = ("raw", "row", "bitwise", "extfile")
  175. if value not in validOptions:
  176. raise getopt.GetoptError(
  177. "-z does not allow %s as a format. Use %s"
  178. % (option, validOptions)
  179. )
  180. self.bitmapGlyphDataFormat = value
  181. elif option == "-y":
  182. self.fontNumber = int(value)
  183. # compile options
  184. elif option == "-m":
  185. self.mergeFile = value
  186. elif option == "-b":
  187. self.recalcBBoxes = False
  188. elif option == "-e":
  189. self.ignoreDecompileErrors = False
  190. elif option == "--unicodedata":
  191. self.unicodedata = value
  192. elif option == "--newline":
  193. validOptions = ("LF", "CR", "CRLF")
  194. if value == "LF":
  195. self.newlinestr = "\n"
  196. elif value == "CR":
  197. self.newlinestr = "\r"
  198. elif value == "CRLF":
  199. self.newlinestr = "\r\n"
  200. else:
  201. raise getopt.GetoptError(
  202. "Invalid choice for --newline: %r (choose from %s)"
  203. % (value, ", ".join(map(repr, validOptions)))
  204. )
  205. elif option == "--recalc-timestamp":
  206. self.recalcTimestamp = True
  207. elif option == "--no-recalc-timestamp":
  208. self.recalcTimestamp = False
  209. elif option == "--flavor":
  210. self.flavor = value
  211. elif option == "--with-zopfli":
  212. self.useZopfli = True
  213. if self.verbose and self.quiet:
  214. raise getopt.GetoptError("-q and -v options are mutually exclusive")
  215. if self.verbose:
  216. self.logLevel = logging.DEBUG
  217. elif self.quiet:
  218. self.logLevel = logging.WARNING
  219. else:
  220. self.logLevel = logging.INFO
  221. if self.mergeFile and self.flavor:
  222. raise getopt.GetoptError("-m and --flavor options are mutually exclusive")
  223. if self.onlyTables and self.skipTables:
  224. raise getopt.GetoptError("-t and -x options are mutually exclusive")
  225. if self.mergeFile and numFiles > 1:
  226. raise getopt.GetoptError(
  227. "Must specify exactly one TTX source file when using -m"
  228. )
  229. if self.flavor != "woff" and self.useZopfli:
  230. raise getopt.GetoptError("--with-zopfli option requires --flavor 'woff'")
  231. def ttList(input, output, options):
  232. ttf = TTFont(input, fontNumber=options.fontNumber, lazy=True)
  233. reader = ttf.reader
  234. tags = sorted(reader.keys())
  235. print('Listing table info for "%s":' % input)
  236. format = " %4s %10s %8s %8s"
  237. print(format % ("tag ", " checksum", " length", " offset"))
  238. print(format % ("----", "----------", "--------", "--------"))
  239. for tag in tags:
  240. entry = reader.tables[tag]
  241. if ttf.flavor == "woff2":
  242. # WOFF2 doesn't store table checksums, so they must be calculated
  243. from fontTools.ttLib.sfnt import calcChecksum
  244. data = entry.loadData(reader.transformBuffer)
  245. checkSum = calcChecksum(data)
  246. else:
  247. checkSum = int(entry.checkSum)
  248. if checkSum < 0:
  249. checkSum = checkSum + 0x100000000
  250. checksum = "0x%08X" % checkSum
  251. print(format % (tag, checksum, entry.length, entry.offset))
  252. print()
  253. ttf.close()
  254. @Timer(log, "Done dumping TTX in %(time).3f seconds")
  255. def ttDump(input, output, options):
  256. input_name = input
  257. if input == "-":
  258. input, input_name = sys.stdin.buffer, sys.stdin.name
  259. output_name = output
  260. if output == "-":
  261. output, output_name = sys.stdout, sys.stdout.name
  262. log.info('Dumping "%s" to "%s"...', input_name, output_name)
  263. if options.unicodedata:
  264. setUnicodeData(options.unicodedata)
  265. ttf = TTFont(
  266. input,
  267. 0,
  268. ignoreDecompileErrors=options.ignoreDecompileErrors,
  269. fontNumber=options.fontNumber,
  270. )
  271. ttf.saveXML(
  272. output,
  273. tables=options.onlyTables,
  274. skipTables=options.skipTables,
  275. splitTables=options.splitTables,
  276. splitGlyphs=options.splitGlyphs,
  277. disassembleInstructions=options.disassembleInstructions,
  278. bitmapGlyphDataFormat=options.bitmapGlyphDataFormat,
  279. newlinestr=options.newlinestr,
  280. )
  281. ttf.close()
  282. @Timer(log, "Done compiling TTX in %(time).3f seconds")
  283. def ttCompile(input, output, options):
  284. input_name = input
  285. if input == "-":
  286. input, input_name = sys.stdin, sys.stdin.name
  287. output_name = output
  288. if output == "-":
  289. output, output_name = sys.stdout.buffer, sys.stdout.name
  290. log.info('Compiling "%s" to "%s"...' % (input_name, output))
  291. if options.useZopfli:
  292. from fontTools.ttLib import sfnt
  293. sfnt.USE_ZOPFLI = True
  294. ttf = TTFont(
  295. options.mergeFile,
  296. flavor=options.flavor,
  297. recalcBBoxes=options.recalcBBoxes,
  298. recalcTimestamp=options.recalcTimestamp,
  299. )
  300. ttf.importXML(input)
  301. if options.recalcTimestamp is None and "head" in ttf and input is not sys.stdin:
  302. # use TTX file modification time for head "modified" timestamp
  303. mtime = os.path.getmtime(input)
  304. ttf["head"].modified = timestampSinceEpoch(mtime)
  305. ttf.save(output)
  306. def guessFileType(fileName):
  307. if fileName == "-":
  308. header = sys.stdin.buffer.peek(256)
  309. ext = ""
  310. else:
  311. base, ext = os.path.splitext(fileName)
  312. try:
  313. with open(fileName, "rb") as f:
  314. header = f.read(256)
  315. except IOError:
  316. return None
  317. if header.startswith(b"\xef\xbb\xbf<?xml"):
  318. header = header.lstrip(b"\xef\xbb\xbf")
  319. cr, tp = getMacCreatorAndType(fileName)
  320. if tp in ("sfnt", "FFIL"):
  321. return "TTF"
  322. if ext == ".dfont":
  323. return "TTF"
  324. head = Tag(header[:4])
  325. if head == "OTTO":
  326. return "OTF"
  327. elif head == "ttcf":
  328. return "TTC"
  329. elif head in ("\0\1\0\0", "true"):
  330. return "TTF"
  331. elif head == "wOFF":
  332. return "WOFF"
  333. elif head == "wOF2":
  334. return "WOFF2"
  335. elif head == "<?xm":
  336. # Use 'latin1' because that can't fail.
  337. header = tostr(header, "latin1")
  338. if opentypeheaderRE.search(header):
  339. return "OTX"
  340. else:
  341. return "TTX"
  342. return None
  343. def parseOptions(args):
  344. rawOptions, files = getopt.getopt(
  345. args,
  346. "ld:o:fvqht:x:sgim:z:baey:",
  347. [
  348. "unicodedata=",
  349. "recalc-timestamp",
  350. "no-recalc-timestamp",
  351. "flavor=",
  352. "version",
  353. "with-zopfli",
  354. "newline=",
  355. ],
  356. )
  357. options = Options(rawOptions, len(files))
  358. jobs = []
  359. if not files:
  360. raise getopt.GetoptError("Must specify at least one input file")
  361. for input in files:
  362. if input != "-" and not os.path.isfile(input):
  363. raise getopt.GetoptError('File not found: "%s"' % input)
  364. tp = guessFileType(input)
  365. if tp in ("OTF", "TTF", "TTC", "WOFF", "WOFF2"):
  366. extension = ".ttx"
  367. if options.listTables:
  368. action = ttList
  369. else:
  370. action = ttDump
  371. elif tp == "TTX":
  372. extension = "." + options.flavor if options.flavor else ".ttf"
  373. action = ttCompile
  374. elif tp == "OTX":
  375. extension = "." + options.flavor if options.flavor else ".otf"
  376. action = ttCompile
  377. else:
  378. raise getopt.GetoptError('Unknown file type: "%s"' % input)
  379. if options.outputFile:
  380. output = options.outputFile
  381. else:
  382. if input == "-":
  383. raise getopt.GetoptError("Must provide -o when reading from stdin")
  384. output = makeOutputFileName(
  385. input, options.outputDir, extension, options.overWrite
  386. )
  387. # 'touch' output file to avoid race condition in choosing file names
  388. if action != ttList:
  389. open(output, "a").close()
  390. jobs.append((action, input, output))
  391. return jobs, options
  392. def process(jobs, options):
  393. for action, input, output in jobs:
  394. action(input, output, options)
  395. def main(args=None):
  396. """Convert OpenType fonts to XML and back"""
  397. from fontTools import configLogger
  398. if args is None:
  399. args = sys.argv[1:]
  400. try:
  401. jobs, options = parseOptions(args)
  402. except getopt.GetoptError as e:
  403. print("%s\nERROR: %s" % (__doc__, e), file=sys.stderr)
  404. sys.exit(2)
  405. configLogger(level=options.logLevel)
  406. try:
  407. process(jobs, options)
  408. except KeyboardInterrupt:
  409. log.error("(Cancelled.)")
  410. sys.exit(1)
  411. except SystemExit:
  412. raise
  413. except TTLibError as e:
  414. log.error(e)
  415. sys.exit(1)
  416. except:
  417. log.exception("Unhandled exception has occurred")
  418. sys.exit(1)
  419. if __name__ == "__main__":
  420. sys.exit(main())