blob: 77b2e3b755097677e925de49e127e16579b4fe1e [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:
34 options, args = getopt.getopt(sys.argv[1:], "hvbfd:i:")
35except 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]
71 print "000", outPath
72 if os.path.isdir(outPath):
73 outPath = makeOutputFileName(xmlPath, outPath)
74 files = [(xmlPath, outPath)]
75else:
76 outputDir = args[-1]
77 files = []
78 for xmlPath in args[:-1]:
79 files.append((xmlPath, makeOutputFileName(xmlPath, outputDir)))
80
81
82for xmlPath, ttPath in files:
jvr96aa48a2002-05-03 18:55:27 +000083 if not forceOverwrite and os.path.exists(ttPath):
84 answer = raw_input('Overwrite "%s"? ' % ttPath)
85 if not answer[:1] in ("Y", "y"):
86 print "skipped."
87 continue
88
89 print 'Compiling "%s" to "%s"...' % (xmlPath, ttPath)
90
91 tt = ttLib.TTFont(ttInFile, recalcBBoxes=recalcBBoxes, verbose=verbose)
jvrb1ad7eb2002-05-11 21:16:05 +000092 tt.importXML(xmlPath)
jvr96aa48a2002-05-03 18:55:27 +000093 tt.save(ttPath)
94
95 if verbose:
96 import time
97 print "%s finished at" % sys.argv[0], time.strftime("%H:%M:%S", time.localtime(time.time()))