-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathotfsurgeon
executable file
·105 lines (84 loc) · 3.33 KB
/
otfsurgeon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
import argparse
import re
import sys
from fontTools.misc.testTools import getXML
from fontTools.ttLib import TTFont, newTable
parser = argparse.ArgumentParser(description="Cut up and rebuild OTF files")
parser.add_argument(
"-i", dest="input", metavar="OTF", help="Input file name", required=True
)
subparsers = parser.add_subparsers(dest="command", required=True)
parser_strip = subparsers.add_parser("strip", help="Remove one or more tables")
parser_strip.add_argument("-o", dest="output", metavar="OTF", help="Output file name")
parser_strip.add_argument("tables", nargs="*")
parser_dump = subparsers.add_parser("dump", help="Print table contents to stdout")
parser_dump.add_argument(
"--ttx", dest="ttx", action="store_true", help="Output should be XML, not binary",
)
parser_dump.add_argument("table", action="store")
parser_add = subparsers.add_parser(
"add", help="Add a binary table to the font from standard input"
)
parser_add.add_argument("-o", dest="output", metavar="OTF", help="Output file name")
parser_add.add_argument("table", action="store")
parser_steal = subparsers.add_parser(
"steal", help="Copy one or more tables from another font"
)
parser_steal.add_argument("-o", dest="output", metavar="OTF", help="Output file name")
parser_steal.add_argument("fromotf", metavar="FROM_OTF")
parser_steal.add_argument("tables", nargs="*")
args = parser.parse_args()
font = TTFont(args.input)
def strip():
if not args.output:
args.output = re.sub(r"(\..*?)$", r"-strip\1", args.input)
for t in args.tables:
if t in font:
del font[t]
else:
print("%s table not found in %s" % (t, args.input), file=sys.stderr)
print("Writing on %s" % args.output)
font.save(args.output)
def dump():
if args.table not in font:
print("%s table not found in %s" % (args.table, args.input), file=sys.stderr)
sys.exit(1)
if args.ttx:
print("\n".join(getXML(font[args.table].toXML)))
else:
sys.stdout.buffer.write(font[args.table].compile(font))
def steal():
other = TTFont(args.fromotf)
if not args.output:
args.output = re.sub(r"(\..*?)$", r"-steal\1", args.input)
for t in args.tables:
if t in other:
font[t] = other[t]
else:
print("%s table not found in %s" % (t, args.fromotf), file=sys.stderr)
print("Writing on %s" % args.output)
try:
font.save(args.output)
except Exception as e:
exception = type(e).__name__
print("Can't steal those tables: %s: %s" % (exception, e), file=sys.stderr)
if exception == "KeyError":
print("\t(Often this means you don't have the required glyphs in the font)")
sys.exit(1)
def add():
if not args.output:
args.output = re.sub(r"(\..*?)$", r"-add\1", args.input)
data = sys.stdin.buffer.read()
font[args.table] = newTable(args.table)
font[args.table].decompile(data, font)
print("Writing on %s" % args.output)
try:
font.save(args.output)
except Exception as e:
exception = type(e).__name__
print("Can't add that table: %s: %s" % (exception, e), file=sys.stderr)
if exception == "KeyError":
print("\t(Often this means you don't have the required glyphs in the font)")
sys.exit(1)
globals()[args.command]() # Whoa