jvr | 96aa48a | 2002-05-03 18:55:27 +0000 | [diff] [blame^] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """\ |
| 4 | usage: ttcompile [-hvbf] [-d output-dir] [-i TTF-input-file] [TTX-file...] |
| 5 | |
| 6 | Translate a TTX file (as output by ttDump) to a TrueType font file. |
| 7 | If a TTX-file argument is a directory instead of a file, all files in |
| 8 | that directory ending in '.ttx' will be merged into one TrueType file. |
| 9 | This is mostly useful in conjunction with the -s option of ttDump.py. |
| 10 | |
| 11 | Options: |
| 12 | -h Help: print this message |
| 13 | -i TrueType-input-file: specify a TT file to be merged with the TTX file. |
| 14 | This option is only valid when at most one TTX file (or directory |
| 15 | containing separated TTX files) is specified. |
| 16 | -b Don't recalc glyph boundig boxes: use the values in the TTX file as-is. |
| 17 | -d <output-dir> Specify a directory in which the output file(s) |
| 18 | should end up. The directory must exist. |
| 19 | -f Force overwriting existing files. |
| 20 | -v Verbose: messages will be written to stdout about what is being done |
| 21 | """ |
| 22 | |
| 23 | import sys, os, getopt |
| 24 | from fontTools import ttLib |
| 25 | |
| 26 | def usage(): |
| 27 | print __doc__ |
| 28 | sys.exit(2) |
| 29 | |
| 30 | try: |
| 31 | options, args = getopt.getopt(sys.argv[1:], "hvbfd:i:") |
| 32 | except getopt.GetoptError: |
| 33 | usage() |
| 34 | |
| 35 | # default values |
| 36 | verbose = 0 |
| 37 | ttInFile = None |
| 38 | recalcBBoxes = 1 |
| 39 | forceOverwrite = 0 |
| 40 | outputDir = None |
| 41 | |
| 42 | for option, value in options: |
| 43 | if option == "-i": |
| 44 | ttInFile = value |
| 45 | elif option == "-v": |
| 46 | verbose = 1 |
| 47 | elif option == "-h": |
| 48 | print __doc__ |
| 49 | sys.exit(0) |
| 50 | elif option == "-b": |
| 51 | recalcBBoxes = 0 |
| 52 | elif option == "-d": |
| 53 | outputDir = value |
| 54 | elif option == "-f": |
| 55 | forceOverwrite = 1 |
| 56 | |
| 57 | if not args: |
| 58 | usage() |
| 59 | |
| 60 | if ttInFile and len(args) > 1: |
| 61 | print "Must specify exactly one TTX file (or directory) when using -i" |
| 62 | sys.exit(2) |
| 63 | |
| 64 | |
| 65 | for xmlPath in args: |
| 66 | path, ext = os.path.splitext(xmlPath) |
| 67 | if outputDir is not None: |
| 68 | fileName = os.path.basename(path) |
| 69 | path = os.path.join(outputDir, fileName) |
| 70 | ttPath = path + '.ttf' |
| 71 | |
| 72 | if not forceOverwrite and os.path.exists(ttPath): |
| 73 | answer = raw_input('Overwrite "%s"? ' % ttPath) |
| 74 | if not answer[:1] in ("Y", "y"): |
| 75 | print "skipped." |
| 76 | continue |
| 77 | |
| 78 | print 'Compiling "%s" to "%s"...' % (xmlPath, ttPath) |
| 79 | |
| 80 | tt = ttLib.TTFont(ttInFile, recalcBBoxes=recalcBBoxes, verbose=verbose) |
| 81 | |
| 82 | if os.path.isdir(xmlPath): |
| 83 | import glob |
| 84 | files = glob.glob1(xmlPath, "*.ttx") |
| 85 | for xmlFile in files: |
| 86 | xmlFile = os.path.join(xmlPath, xmlFile) |
| 87 | tt.importXML(xmlFile) |
| 88 | else: |
| 89 | tt.importXML(xmlPath) |
| 90 | |
| 91 | tt.save(ttPath) |
| 92 | |
| 93 | if verbose: |
| 94 | import time |
| 95 | print "%s finished at" % sys.argv[0], time.strftime("%H:%M:%S", time.localtime(time.time())) |