blob: ea803f8dd327934dbff928a6e6e8569238108b81 [file] [log] [blame]
Just6cc58871999-12-16 22:04:30 +00001#! /usr/bin/env python
2
3"""\
jvr656a6cc2001-08-09 23:39:05 +00004usage: %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
Just9b79e522000-01-17 18:49:34 +00008 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.
Just94940da1999-12-27 19:52:01 +000010
11 Options:
jvr656a6cc2001-08-09 23:39:05 +000012 -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.
Just9b79e522000-01-17 18:49:34 +000016 -b Don't recalc glyph boundig boxes: use the values in the TTX file as-is.
jvr656a6cc2001-08-09 23:39:05 +000017 -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
Just6cc58871999-12-16 22:04:30 +000021"""
jvr656a6cc2001-08-09 23:39:05 +000022
Just6cc58871999-12-16 22:04:30 +000023import sys, os, getopt
24from fontTools import ttLib
25
jvr93d048b2002-05-03 17:05:39 +000026def usage():
27 print __doc__ % sys.argv[0]
28 sys.exit(2)
29
30try:
31 options, args = getopt.getopt(sys.argv[1:], "hvbfd:i:")
32except getopt.GetoptError:
33 usage()
Just6cc58871999-12-16 22:04:30 +000034
jvr656a6cc2001-08-09 23:39:05 +000035# default values
Just6cc58871999-12-16 22:04:30 +000036verbose = 0
Justac7c6fa1999-12-18 18:12:15 +000037ttInFile = None
38recalcBBoxes = 1
jvr656a6cc2001-08-09 23:39:05 +000039forceOverwrite = 0
40outputDir = None
41
Just6cc58871999-12-16 22:04:30 +000042for option, value in options:
43 if option == "-i":
Justac7c6fa1999-12-18 18:12:15 +000044 ttInFile = value
Just6cc58871999-12-16 22:04:30 +000045 elif option == "-v":
46 verbose = 1
47 elif option == "-h":
48 print __doc__ % sys.argv[0]
49 sys.exit(0)
Justac7c6fa1999-12-18 18:12:15 +000050 elif option == "-b":
51 recalcBBoxes = 0
jvr656a6cc2001-08-09 23:39:05 +000052 elif option == "-d":
53 outputDir = value
54 elif option == "-f":
55 forceOverwrite = 1
Just6cc58871999-12-16 22:04:30 +000056
jvr656a6cc2001-08-09 23:39:05 +000057if not args:
jvr93d048b2002-05-03 17:05:39 +000058 usage()
Just6cc58871999-12-16 22:04:30 +000059
jvr656a6cc2001-08-09 23:39:05 +000060if ttInFile and len(args) > 1:
61 print "Must specify exactly one TTX file (or directory) when using -i"
62 sys.exit(2)
Just94940da1999-12-27 19:52:01 +000063
jvr656a6cc2001-08-09 23:39:05 +000064
65for 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
jvr7b51bbf2001-08-09 23:45:32 +000078 print 'Compiling "%s" to "%s"...' % (xmlPath, ttPath)
79
jvr656a6cc2001-08-09 23:39:05 +000080 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()))