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