ttx.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. --optimize-font-speed
  94. Enable optimizations that prioritize speed over file size.
  95. This mainly affects how glyf t able and gvar / VARC tables are
  96. compiled. The produced fonts will be larger, but rendering
  97. performance will be improved with HarfBuzz and other text
  98. layout engines.
  99. """
  100. from fontTools.ttLib import OPTIMIZE_FONT_SPEED, TTFont, TTLibError
  101. from fontTools.misc.macCreatorType import getMacCreatorAndType
  102. from fontTools.unicode import setUnicodeData
  103. from fontTools.misc.textTools import Tag, tostr
  104. from fontTools.misc.timeTools import timestampSinceEpoch
  105. from fontTools.misc.loggingTools import Timer
  106. from fontTools.misc.cliTools import makeOutputFileName
  107. import os
  108. import sys
  109. import getopt
  110. import re
  111. import logging
  112. log = logging.getLogger("fontTools.ttx")
  113. opentypeheaderRE = re.compile("""sfntVersion=['"]OTTO["']""")
  114. class Options(object):
  115. listTables = False
  116. outputDir = None
  117. outputFile = None
  118. overWrite = False
  119. verbose = False
  120. quiet = False
  121. splitTables = False
  122. splitGlyphs = False
  123. disassembleInstructions = True
  124. mergeFile = None
  125. recalcBBoxes = True
  126. ignoreDecompileErrors = True
  127. bitmapGlyphDataFormat = "raw"
  128. unicodedata = None
  129. newlinestr = "\n"
  130. recalcTimestamp = None
  131. flavor = None
  132. useZopfli = False
  133. optimizeFontSpeed = False
  134. def __init__(self, rawOptions, numFiles):
  135. self.onlyTables = []
  136. self.skipTables = []
  137. self.fontNumber = -1
  138. for option, value in rawOptions:
  139. # general options
  140. if option == "-h":
  141. print(__doc__)
  142. sys.exit(0)
  143. elif option == "--version":
  144. from fontTools import version
  145. print(version)
  146. sys.exit(0)
  147. elif option == "-d":
  148. if not os.path.isdir(value):
  149. raise getopt.GetoptError(
  150. "The -d option value must be an existing directory"
  151. )
  152. self.outputDir = value
  153. elif option == "-o":
  154. self.outputFile = value
  155. elif option == "-f":
  156. self.overWrite = True
  157. elif option == "-v":
  158. self.verbose = True
  159. elif option == "-q":
  160. self.quiet = True
  161. # dump options
  162. elif option == "-l":
  163. self.listTables = True
  164. elif option == "-t":
  165. # pad with space if table tag length is less than 4
  166. value = value.ljust(4)
  167. self.onlyTables.append(value)
  168. elif option == "-x":
  169. # pad with space if table tag length is less than 4
  170. value = value.ljust(4)
  171. self.skipTables.append(value)
  172. elif option == "-s":
  173. self.splitTables = True
  174. elif option == "-g":
  175. # -g implies (and forces) splitTables
  176. self.splitGlyphs = True
  177. self.splitTables = True
  178. elif option == "-i":
  179. self.disassembleInstructions = False
  180. elif option == "-z":
  181. validOptions = ("raw", "row", "bitwise", "extfile")
  182. if value not in validOptions:
  183. raise getopt.GetoptError(
  184. "-z does not allow %s as a format. Use %s"
  185. % (option, validOptions)
  186. )
  187. self.bitmapGlyphDataFormat = value
  188. elif option == "-y":
  189. self.fontNumber = int(value)
  190. # compile options
  191. elif option == "-m":
  192. self.mergeFile = value
  193. elif option == "-b":
  194. self.recalcBBoxes = False
  195. elif option == "-e":
  196. self.ignoreDecompileErrors = False
  197. elif option == "--unicodedata":
  198. self.unicodedata = value
  199. elif option == "--newline":
  200. validOptions = ("LF", "CR", "CRLF")
  201. if value == "LF":
  202. self.newlinestr = "\n"
  203. elif value == "CR":
  204. self.newlinestr = "\r"
  205. elif value == "CRLF":
  206. self.newlinestr = "\r\n"
  207. else:
  208. raise getopt.GetoptError(
  209. "Invalid choice for --newline: %r (choose from %s)"
  210. % (value, ", ".join(map(repr, validOptions)))
  211. )
  212. elif option == "--recalc-timestamp":
  213. self.recalcTimestamp = True
  214. elif option == "--no-recalc-timestamp":
  215. self.recalcTimestamp = False
  216. elif option == "--flavor":
  217. self.flavor = value
  218. elif option == "--with-zopfli":
  219. self.useZopfli = True
  220. elif option == "--optimize-font-speed":
  221. self.optimizeFontSpeed = True
  222. if self.verbose and self.quiet:
  223. raise getopt.GetoptError("-q and -v options are mutually exclusive")
  224. if self.verbose:
  225. self.logLevel = logging.DEBUG
  226. elif self.quiet:
  227. self.logLevel = logging.WARNING
  228. else:
  229. self.logLevel = logging.INFO
  230. if self.mergeFile and self.flavor:
  231. raise getopt.GetoptError("-m and --flavor options are mutually exclusive")
  232. if self.onlyTables and self.skipTables:
  233. raise getopt.GetoptError("-t and -x options are mutually exclusive")
  234. if self.mergeFile and numFiles > 1:
  235. raise getopt.GetoptError(
  236. "Must specify exactly one TTX source file when using -m"
  237. )
  238. if self.flavor != "woff" and self.useZopfli:
  239. raise getopt.GetoptError("--with-zopfli option requires --flavor 'woff'")
  240. def ttList(input, output, options):
  241. ttf = TTFont(input, fontNumber=options.fontNumber, lazy=True)
  242. reader = ttf.reader
  243. tags = sorted(reader.keys())
  244. print('Listing table info for "%s":' % input)
  245. format = " %4s %10s %8s %8s"
  246. print(format % ("tag ", " checksum", " length", " offset"))
  247. print(format % ("----", "----------", "--------", "--------"))
  248. for tag in tags:
  249. entry = reader.tables[tag]
  250. if ttf.flavor == "woff2":
  251. # WOFF2 doesn't store table checksums, so they must be calculated
  252. from fontTools.ttLib.sfnt import calcChecksum
  253. data = entry.loadData(reader.transformBuffer)
  254. checkSum = calcChecksum(data)
  255. else:
  256. checkSum = int(entry.checkSum)
  257. if checkSum < 0:
  258. checkSum = checkSum + 0x100000000
  259. checksum = "0x%08X" % checkSum
  260. print(format % (tag, checksum, entry.length, entry.offset))
  261. print()
  262. ttf.close()
  263. @Timer(log, "Done dumping TTX in %(time).3f seconds")
  264. def ttDump(input, output, options):
  265. input_name = input
  266. if input == "-":
  267. input, input_name = sys.stdin.buffer, sys.stdin.name
  268. output_name = output
  269. if output == "-":
  270. output, output_name = sys.stdout, sys.stdout.name
  271. log.info('Dumping "%s" to "%s"...', input_name, output_name)
  272. if options.unicodedata:
  273. setUnicodeData(options.unicodedata)
  274. ttf = TTFont(
  275. input,
  276. 0,
  277. ignoreDecompileErrors=options.ignoreDecompileErrors,
  278. fontNumber=options.fontNumber,
  279. )
  280. ttf.saveXML(
  281. output,
  282. tables=options.onlyTables,
  283. skipTables=options.skipTables,
  284. splitTables=options.splitTables,
  285. splitGlyphs=options.splitGlyphs,
  286. disassembleInstructions=options.disassembleInstructions,
  287. bitmapGlyphDataFormat=options.bitmapGlyphDataFormat,
  288. newlinestr=options.newlinestr,
  289. )
  290. ttf.close()
  291. @Timer(log, "Done compiling TTX in %(time).3f seconds")
  292. def ttCompile(input, output, options):
  293. input_name = input
  294. if input == "-":
  295. input, input_name = sys.stdin, sys.stdin.name
  296. output_name = output
  297. if output == "-":
  298. output, output_name = sys.stdout.buffer, sys.stdout.name
  299. log.info('Compiling "%s" to "%s"...' % (input_name, output))
  300. if options.useZopfli:
  301. from fontTools.ttLib import sfnt
  302. sfnt.USE_ZOPFLI = True
  303. ttf = TTFont(
  304. options.mergeFile,
  305. flavor=options.flavor,
  306. recalcBBoxes=options.recalcBBoxes,
  307. recalcTimestamp=options.recalcTimestamp,
  308. )
  309. if options.optimizeFontSpeed:
  310. ttf.cfg[OPTIMIZE_FONT_SPEED] = options.optimizeFontSpeed
  311. ttf.importXML(input)
  312. if options.recalcTimestamp is None and "head" in ttf and input is not sys.stdin:
  313. # use TTX file modification time for head "modified" timestamp
  314. mtime = os.path.getmtime(input)
  315. ttf["head"].modified = timestampSinceEpoch(mtime)
  316. ttf.save(output)
  317. def guessFileType(fileName):
  318. if fileName == "-":
  319. header = sys.stdin.buffer.peek(256)
  320. ext = ""
  321. else:
  322. base, ext = os.path.splitext(fileName)
  323. try:
  324. with open(fileName, "rb") as f:
  325. header = f.read(256)
  326. except IOError:
  327. return None
  328. if header.startswith(b"\xef\xbb\xbf<?xml"):
  329. header = header.lstrip(b"\xef\xbb\xbf")
  330. cr, tp = getMacCreatorAndType(fileName)
  331. if tp in ("sfnt", "FFIL"):
  332. return "TTF"
  333. if ext == ".dfont":
  334. return "TTF"
  335. head = Tag(header[:4])
  336. if head == "OTTO":
  337. return "OTF"
  338. elif head == "ttcf":
  339. return "TTC"
  340. elif head in ("\0\1\0\0", "true"):
  341. return "TTF"
  342. elif head == "wOFF":
  343. return "WOFF"
  344. elif head == "wOF2":
  345. return "WOFF2"
  346. elif head == "<?xm":
  347. # Use 'latin1' because that can't fail.
  348. header = tostr(header, "latin1")
  349. if opentypeheaderRE.search(header):
  350. return "OTX"
  351. else:
  352. return "TTX"
  353. return None
  354. def parseOptions(args):
  355. rawOptions, files = getopt.gnu_getopt(
  356. args,
  357. "ld:o:fvqht:x:sgim:z:baey:",
  358. [
  359. "unicodedata=",
  360. "recalc-timestamp",
  361. "no-recalc-timestamp",
  362. "flavor=",
  363. "version",
  364. "with-zopfli",
  365. "newline=",
  366. "optimize-font-speed",
  367. ],
  368. )
  369. options = Options(rawOptions, len(files))
  370. jobs = []
  371. if not files:
  372. raise getopt.GetoptError("Must specify at least one input file")
  373. for input in files:
  374. if input != "-" and not os.path.isfile(input):
  375. raise getopt.GetoptError('File not found: "%s"' % input)
  376. tp = guessFileType(input)
  377. if tp in ("OTF", "TTF", "TTC", "WOFF", "WOFF2"):
  378. extension = ".ttx"
  379. if options.listTables:
  380. action = ttList
  381. else:
  382. action = ttDump
  383. elif tp == "TTX":
  384. extension = "." + options.flavor if options.flavor else ".ttf"
  385. action = ttCompile
  386. elif tp == "OTX":
  387. extension = "." + options.flavor if options.flavor else ".otf"
  388. action = ttCompile
  389. else:
  390. raise getopt.GetoptError('Unknown file type: "%s"' % input)
  391. if options.outputFile:
  392. output = options.outputFile
  393. else:
  394. if input == "-":
  395. raise getopt.GetoptError("Must provide -o when reading from stdin")
  396. output = makeOutputFileName(
  397. input, options.outputDir, extension, options.overWrite
  398. )
  399. # 'touch' output file to avoid race condition in choosing file names
  400. if action != ttList:
  401. open(output, "a").close()
  402. jobs.append((action, input, output))
  403. return jobs, options
  404. def process(jobs, options):
  405. for action, input, output in jobs:
  406. action(input, output, options)
  407. def main(args=None):
  408. """Convert OpenType fonts to XML and back"""
  409. from fontTools import configLogger
  410. if args is None:
  411. args = sys.argv[1:]
  412. try:
  413. jobs, options = parseOptions(args)
  414. except getopt.GetoptError as e:
  415. print("%s\nERROR: %s" % (__doc__, e), file=sys.stderr)
  416. sys.exit(2)
  417. configLogger(level=options.logLevel)
  418. try:
  419. process(jobs, options)
  420. except KeyboardInterrupt:
  421. log.error("(Cancelled.)")
  422. sys.exit(1)
  423. except SystemExit:
  424. raise
  425. except TTLibError as e:
  426. log.error(e)
  427. sys.exit(1)
  428. except:
  429. log.exception("Unhandled exception has occurred")
  430. sys.exit(1)
  431. if __name__ == "__main__":
  432. sys.exit(main())