#! /usr/bin/env python """\ usage: ttcompile [-hvbf] [-i input-TTF] scr [target] ttcompile [-hvbf] [-i input-TTF] src1 ... srcN directory Translate one or more TTX files (as output by ttdump) to a TrueType font file. Options: -h Help: print this message -i TrueType-input-file: specify a TT file to be merged with the TTX file. This option is only valid when at most one TTX file is specified. -b Don't recalc glyph boundig boxes: use the values in the TTX file as-is. -f Force overwriting existing files. -v Verbose: messages will be written to stdout about what is being done """ import sys, os, getopt from fontTools import ttLib def usage(): print __doc__ sys.exit(2) def makeOutputFileName(xmlPath, outputDir): dir, file = os.path.split(xmlPath) file, ext = os.path.splitext(file) if outputDir: dir = outputDir return os.path.join(dir, file + ".ttf") try: options, args = getopt.getopt(sys.argv[1:], "hvbfi:") except getopt.GetoptError: usage() # default values verbose = 0 ttInFile = None recalcBBoxes = 1 forceOverwrite = 0 for option, value in options: if option == "-i": ttInFile = value elif option == "-v": verbose = 1 elif option == "-h": print __doc__ sys.exit(0) elif option == "-b": recalcBBoxes = 0 elif option == "-f": forceOverwrite = 1 if not args: usage() if ttInFile and len(args) > 2: print "Must specify exactly one TTX source file when using -i" sys.exit(2) nargs = len(args) if nargs == 1: files = [(args[0], makeOutputFileName(args[0], None))] elif nargs == 2: xmlPath = args[0] outPath = args[1] if os.path.isdir(outPath): outPath = makeOutputFileName(xmlPath, outPath) files = [(xmlPath, outPath)] else: outputDir = args[-1] if not os.path.isdir(outputDir): print "last argument must be an existing directory" sys.exit(2) files = [] for xmlPath in args[:-1]: files.append((xmlPath, makeOutputFileName(xmlPath, outputDir))) for xmlPath, ttPath in files: if not forceOverwrite and os.path.exists(ttPath): answer = raw_input('Overwrite "%s"? ' % ttPath) if not answer[:1] in ("Y", "y"): print "skipped." continue print 'Compiling "%s" to "%s"...' % (xmlPath, ttPath) tt = ttLib.TTFont(ttInFile, recalcBBoxes=recalcBBoxes, verbose=verbose) tt.importXML(xmlPath) tt.save(ttPath) if verbose: import time print "%s finished at" % sys.argv[0], time.strftime("%H:%M:%S", time.localtime(time.time()))