Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """\ |
jvr | 656a6cc | 2001-08-09 23:39:05 +0000 | [diff] [blame] | 4 | usage: %s [-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 |
Just | 9b79e52 | 2000-01-17 18:49:34 +0000 | [diff] [blame] | 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. |
Just | 94940da | 1999-12-27 19:52:01 +0000 | [diff] [blame] | 10 | |
| 11 | Options: |
jvr | 656a6cc | 2001-08-09 23:39:05 +0000 | [diff] [blame] | 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. |
Just | 9b79e52 | 2000-01-17 18:49:34 +0000 | [diff] [blame] | 16 | -b Don't recalc glyph boundig boxes: use the values in the TTX file as-is. |
jvr | 656a6cc | 2001-08-09 23:39:05 +0000 | [diff] [blame] | 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 |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 21 | """ |
jvr | 656a6cc | 2001-08-09 23:39:05 +0000 | [diff] [blame] | 22 | |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 23 | import sys, os, getopt |
| 24 | from fontTools import ttLib |
| 25 | |
jvr | 93d048b | 2002-05-03 17:05:39 +0000 | [diff] [blame^] | 26 | def usage(): |
| 27 | print __doc__ % sys.argv[0] |
| 28 | sys.exit(2) |
| 29 | |
| 30 | try: |
| 31 | options, args = getopt.getopt(sys.argv[1:], "hvbfd:i:") |
| 32 | except getopt.GetoptError: |
| 33 | usage() |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 34 | |
jvr | 656a6cc | 2001-08-09 23:39:05 +0000 | [diff] [blame] | 35 | # default values |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 36 | verbose = 0 |
Just | ac7c6fa | 1999-12-18 18:12:15 +0000 | [diff] [blame] | 37 | ttInFile = None |
| 38 | recalcBBoxes = 1 |
jvr | 656a6cc | 2001-08-09 23:39:05 +0000 | [diff] [blame] | 39 | forceOverwrite = 0 |
| 40 | outputDir = None |
| 41 | |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 42 | for option, value in options: |
| 43 | if option == "-i": |
Just | ac7c6fa | 1999-12-18 18:12:15 +0000 | [diff] [blame] | 44 | ttInFile = value |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 45 | elif option == "-v": |
| 46 | verbose = 1 |
| 47 | elif option == "-h": |
| 48 | print __doc__ % sys.argv[0] |
| 49 | sys.exit(0) |
Just | ac7c6fa | 1999-12-18 18:12:15 +0000 | [diff] [blame] | 50 | elif option == "-b": |
| 51 | recalcBBoxes = 0 |
jvr | 656a6cc | 2001-08-09 23:39:05 +0000 | [diff] [blame] | 52 | elif option == "-d": |
| 53 | outputDir = value |
| 54 | elif option == "-f": |
| 55 | forceOverwrite = 1 |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 56 | |
jvr | 656a6cc | 2001-08-09 23:39:05 +0000 | [diff] [blame] | 57 | if not args: |
jvr | 93d048b | 2002-05-03 17:05:39 +0000 | [diff] [blame^] | 58 | usage() |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 59 | |
jvr | 656a6cc | 2001-08-09 23:39:05 +0000 | [diff] [blame] | 60 | if ttInFile and len(args) > 1: |
| 61 | print "Must specify exactly one TTX file (or directory) when using -i" |
| 62 | sys.exit(2) |
Just | 94940da | 1999-12-27 19:52:01 +0000 | [diff] [blame] | 63 | |
jvr | 656a6cc | 2001-08-09 23:39:05 +0000 | [diff] [blame] | 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 | |
jvr | 7b51bbf | 2001-08-09 23:45:32 +0000 | [diff] [blame] | 78 | print 'Compiling "%s" to "%s"...' % (xmlPath, ttPath) |
| 79 | |
jvr | 656a6cc | 2001-08-09 23:39:05 +0000 | [diff] [blame] | 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())) |