blob: b560e02ea483084bdbb1df96153d98e436431270 [file] [log] [blame]
jvr96aa48a2002-05-03 18:55:27 +00001#! /usr/bin/env python
2
3"""\
jvrb1ad7eb2002-05-11 21:16:05 +00004usage: ttcompile [-hvbf] [-i input-TTF] scr [target]
5 ttcompile [-hvbf] [-i input-TTF] src1 ... srcN directory
jvr96aa48a2002-05-03 18:55:27 +00006
jvrc0903262002-05-11 21:52:27 +00007 Translate one or more TTX files (as output by ttdump) to a TrueType
jvrb1ad7eb2002-05-11 21:16:05 +00008 font file.
jvr96aa48a2002-05-03 18:55:27 +00009
10 Options:
11 -h Help: print this message
12 -i TrueType-input-file: specify a TT file to be merged with the TTX file.
jvrb1ad7eb2002-05-11 21:16:05 +000013 This option is only valid when at most one TTX file is specified.
jvr96aa48a2002-05-03 18:55:27 +000014 -b Don't recalc glyph boundig boxes: use the values in the TTX file as-is.
jvr96aa48a2002-05-03 18:55:27 +000015 -f Force overwriting existing files.
16 -v Verbose: messages will be written to stdout about what is being done
17"""
18
19import sys, os, getopt
20from fontTools import ttLib
21
22def usage():
23 print __doc__
24 sys.exit(2)
25
jvrb1ad7eb2002-05-11 21:16:05 +000026def 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
jvr96aa48a2002-05-03 18:55:27 +000033try:
jvr72d4bad2002-05-25 16:08:55 +000034 options, args = getopt.getopt(sys.argv[1:], "hvbfi:")
jvr96aa48a2002-05-03 18:55:27 +000035except getopt.GetoptError:
36 usage()
37
38# default values
39verbose = 0
40ttInFile = None
41recalcBBoxes = 1
42forceOverwrite = 0
jvr96aa48a2002-05-03 18:55:27 +000043
44for 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
jvr96aa48a2002-05-03 18:55:27 +000054 elif option == "-f":
55 forceOverwrite = 1
56
57if not args:
58 usage()
59
jvrb1ad7eb2002-05-11 21:16:05 +000060if ttInFile and len(args) > 2:
61 print "Must specify exactly one TTX source file when using -i"
jvr96aa48a2002-05-03 18:55:27 +000062 sys.exit(2)
63
64
jvrb1ad7eb2002-05-11 21:16:05 +000065nargs = len(args)
66if nargs == 1:
67 files = [(args[0], makeOutputFileName(args[0], None))]
68elif nargs == 2:
69 xmlPath = args[0]
70 outPath = args[1]
jvrb1ad7eb2002-05-11 21:16:05 +000071 if os.path.isdir(outPath):
72 outPath = makeOutputFileName(xmlPath, outPath)
73 files = [(xmlPath, outPath)]
74else:
75 outputDir = args[-1]
jvrbf3dc202002-05-12 18:46:27 +000076 if not os.path.isdir(outputDir):
77 print "last argument must be an existing directory"
78 sys.exit(2)
jvrb1ad7eb2002-05-11 21:16:05 +000079 files = []
80 for xmlPath in args[:-1]:
81 files.append((xmlPath, makeOutputFileName(xmlPath, outputDir)))
82
83
84for xmlPath, ttPath in files:
jvr96aa48a2002-05-03 18:55:27 +000085 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)
jvrb1ad7eb2002-05-11 21:16:05 +000094 tt.importXML(xmlPath)
jvr96aa48a2002-05-03 18:55:27 +000095 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()))