fonttools/Tools/ttdump
jvr bf3dc20b46 test whether final argument is a directory
git-svn-id: svn://svn.code.sf.net/p/fonttools/code/trunk@221 4cde692c-a291-49d1-8350-778aa11640f8
2002-05-12 18:46:27 +00:00

126 lines
3.3 KiB
Python
Executable File

#! /usr/bin/env python
"""\
usage: ttdump [-hvisf] [-t <table>] [-x <table>] src [target]
ttdump [-hvisf] [-t <table>] [-x <table>] src1 ... srcN directory
Dump one or more TrueType/OpenType fonts as TTX files (an XML-based
text format).
Options:
-h Help: print this message
-v Verbose: messages will be written to stdout about what is
being done.
-i Disassemble TT instructions: when this option is given, all
TrueType programs (glyph programs, the font program and the
pre-program) will be written to the TTX file as assembly
instead of hex data.
-s Split tables: save the TTX data into separate TTX files per
table and write one small TTX file that contains references
to the individual table dumps. This file can be used as
input to ttcompile, as long as the table files are in the
same directory.
-f Force overwriting existing files.
-t <table> Specify a table to dump. Multiple -t options
are allowed. When no -t option is specified, all tables
will be dumped.
-x <table> Specify a table to exclude from the dump. Multiple
-x options are allowed. -t and -x are mutually exclusive.
"""
import sys, os, getopt
from fontTools import ttLib
def usage():
print __doc__
sys.exit(2)
def makeOutputFileName(ttPath, outputDir):
dir, file = os.path.split(ttPath)
file, ext = os.path.splitext(file)
if outputDir:
dir = outputDir
return os.path.join(dir, file + ".ttx")
try:
options, args = getopt.getopt(sys.argv[1:], "hvisft:x:d:")
except getopt.GetoptError:
usage()
# default values
verbose = 0
splitTables = 0
disassembleInstructions = 0
forceOverwrite = 0
tables = []
skipTables = []
for option, value in options:
if option == "-t":
if len(value) > 4:
print "illegal table tag: " + value
sys.exit(2)
# normalize tag
value = value + (4 - len(value)) * " "
tables.append(value)
elif option == "-x":
if len(value) > 4:
print "illegal table tag: " + value
sys.exit(2)
# normalize tag
value = value + (4 - len(value)) * " "
skipTables.append(value)
elif option == "-v":
verbose = 1
elif option == "-f":
forceOverwrite = 1
elif option == "-h":
print __doc__
sys.exit(0)
elif option == "-s":
splitTables = 1
elif option == "-i":
disassembleInstructions = 1
if tables and skipTables:
print "-t and -x options are mutually exlusive"
sys.exit(2)
if not args:
usage()
nargs = len(args)
if nargs == 1:
files = [(args[0], makeOutputFileName(args[0], None))]
elif nargs == 2:
ttPath = args[0]
outPath = args[1]
if os.path.isdir(outPath):
outPath = makeOutputFileName(ttPath, outPath)
files = [(ttPath, outPath)]
else:
outputDir = args[-1]
if not os.path.isdir(outputDir):
print "last argument must be an existing directory"
sys.exit(2)
files = []
for ttPath in args[:-1]:
files.append((ttPath, makeOutputFileName(ttPath, outputDir)))
for ttPath, outFile in files:
if not forceOverwrite and os.path.exists(outFile):
answer = raw_input('Overwrite "%s"? ' % outFile)
if not answer[:1] in ("Y", "y"):
print "skipped."
continue
if not os.path.isdir(outFile):
os.remove(outFile)
print 'Dumping "%s" to "%s"...' % (ttPath, outFile)
tt = ttLib.TTFont(ttPath, 0, verbose=verbose)
tt.saveXML(outFile, tables=tables, skipTables=skipTables,
splitTables=splitTables, disassembleInstructions=disassembleInstructions)