Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """\ |
Just | af5694e | 2000-02-01 15:53:37 +0000 | [diff] [blame] | 4 | usage: %s [-hvis] [-t <table>] [-x <table>] TrueType-file [TTX-output-file] |
Just | 9b79e52 | 2000-01-17 18:49:34 +0000 | [diff] [blame] | 5 | Dump a TrueType font as a TTX file (an XML-based text format). If the |
| 6 | TTX-output-file argument is omitted, the out put file name will be |
| 7 | constructed from the input file name, like so: *.ttf becomes *.ttx. |
| 8 | Either way, existing files will be overwritten without warning! |
Just | 08ff1b4 | 1999-12-27 19:49:36 +0000 | [diff] [blame] | 9 | |
| 10 | Options: |
Just | af5694e | 2000-02-01 15:53:37 +0000 | [diff] [blame] | 11 | -h help: print this message |
Just | 08ff1b4 | 1999-12-27 19:49:36 +0000 | [diff] [blame] | 12 | -v verbose: messages will be written to stdout about what is |
| 13 | being done. |
Just | af5694e | 2000-02-01 15:53:37 +0000 | [diff] [blame] | 14 | -i disassemble TT instructions: when this option is given, all |
| 15 | TrueType programs (glyph programs, the font program and the |
| 16 | pre-program) will be written to the TTX file as assembly instead |
| 17 | of hex data. |
Just | 9b79e52 | 2000-01-17 18:49:34 +0000 | [diff] [blame] | 18 | -s split tables: save the TTX data into separate TTX files per table. |
Just | ab15e07 | 1999-12-29 13:10:59 +0000 | [diff] [blame] | 19 | The files will be saved in a directory. The name of this |
| 20 | directory will be constructed from the input filename (by |
| 21 | dropping the extension) or can be specified by the optional |
Just | 9b79e52 | 2000-01-17 18:49:34 +0000 | [diff] [blame] | 22 | TTX-output-file argument. |
Just | af5694e | 2000-02-01 15:53:37 +0000 | [diff] [blame] | 23 | -t <table> specify a table to dump. Multiple -t options |
| 24 | are allowed. When no -t option is specified, all tables |
| 25 | will be dumped |
| 26 | -x <table> specify a table to exclude from the dump. Multiple |
| 27 | -x options are allowed. -t and -x are mutually exclusive. |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 28 | """ |
| 29 | |
| 30 | import sys, os, getopt |
| 31 | from fontTools import ttLib |
| 32 | |
Just | af5694e | 2000-02-01 15:53:37 +0000 | [diff] [blame] | 33 | options, args = getopt.getopt(sys.argv[1:], "hvist:x:") |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 34 | |
| 35 | verbose = 0 |
Just | 08ff1b4 | 1999-12-27 19:49:36 +0000 | [diff] [blame] | 36 | splitTables = 0 |
Just | af5694e | 2000-02-01 15:53:37 +0000 | [diff] [blame] | 37 | disassembleInstructions = 0 |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 38 | tables = [] |
Just | e9af618 | 2000-01-05 20:41:34 +0000 | [diff] [blame] | 39 | skipTables = [] |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 40 | for option, value in options: |
| 41 | if option == "-t": |
| 42 | if len(value) > 4: |
| 43 | print "illegal table tag: " + value |
| 44 | sys.exit(2) |
| 45 | # normalize tag |
| 46 | value = value + (4 - len(value)) * " " |
| 47 | tables.append(value) |
Just | e9af618 | 2000-01-05 20:41:34 +0000 | [diff] [blame] | 48 | elif option == "-x": |
| 49 | if len(value) > 4: |
| 50 | print "illegal table tag: " + value |
| 51 | sys.exit(2) |
| 52 | # normalize tag |
| 53 | value = value + (4 - len(value)) * " " |
| 54 | skipTables.append(value) |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 55 | elif option == "-v": |
| 56 | verbose = 1 |
| 57 | elif option == "-h": |
| 58 | print __doc__ % sys.argv[0] |
| 59 | sys.exit(0) |
Just | 08ff1b4 | 1999-12-27 19:49:36 +0000 | [diff] [blame] | 60 | elif option == "-s": |
| 61 | splitTables = 1 |
Just | af5694e | 2000-02-01 15:53:37 +0000 | [diff] [blame] | 62 | elif option == "-i": |
| 63 | disassembleInstructions = 1 |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 64 | |
Just | e9af618 | 2000-01-05 20:41:34 +0000 | [diff] [blame] | 65 | if tables and skipTables: |
| 66 | print "-t and -x options are mutually exlusive" |
| 67 | sys.exit(2) |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 68 | |
| 69 | if len(args) == 1: |
Just | a7b9f29 | 1999-12-18 18:11:44 +0000 | [diff] [blame] | 70 | ttPath = args[0] |
Just | ab15e07 | 1999-12-29 13:10:59 +0000 | [diff] [blame] | 71 | path, ext = os.path.splitext(ttPath) |
| 72 | if splitTables: |
| 73 | xmlPath = path |
| 74 | else: |
Just | 9b79e52 | 2000-01-17 18:49:34 +0000 | [diff] [blame] | 75 | xmlPath = path + '.ttx' |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 76 | elif len(args) == 2: |
Just | a7b9f29 | 1999-12-18 18:11:44 +0000 | [diff] [blame] | 77 | ttPath, xmlPath = args |
Just | 6cc5887 | 1999-12-16 22:04:30 +0000 | [diff] [blame] | 78 | else: |
| 79 | print __doc__ % sys.argv[0] |
| 80 | sys.exit(2) |
| 81 | |
Just | af5694e | 2000-02-01 15:53:37 +0000 | [diff] [blame] | 82 | tt = ttLib.TTFont(ttPath, 0, verbose=verbose) |
| 83 | tt.saveXML(xmlPath, tables=tables, skipTables=skipTables, |
| 84 | splitTables=splitTables, disassembleInstructions=disassembleInstructions) |
| 85 | |