-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadjustFont.py
executable file
·214 lines (200 loc) · 5.82 KB
/
adjustFont.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/local/bin/fontforge
from sys import stderr, exit
from math import radians
from argparse import ArgumentParser
from locale import getdefaultlocale
import fontforge
import psMat
parser = ArgumentParser()
parser.add_argument(
'-g', '--generate-font',
action='store_true', dest='generate', default=False,
help="Generate font instead of saving ")
actionGroup = parser.add_mutually_exclusive_group()
actionGroup.add_argument(
'-p', '--pad',
action='store_const', const='pad', dest='action',
help="Pads to width specified by -w")
actionGroup.add_argument(
'-s', '--scale',
action='store_const', const='scale', dest='action',
help="Scales to width specified by -w")
parser.add_argument(
'-M', '--em',
type=int,
help="Set em size")
parser.add_argument(
'-w', '--width',
type=float,
help="Set to specified width")
parser.add_argument(
'-k', '--skew',
type=float, metavar='ANGLE',
help="Skews by specified angle")
parser.add_argument(
'-n', '--font-name',
type=str, metavar='NAME',
help="Specify Postscript font name")
parser.add_argument(
'-f', '--family-name',
type=str, metavar='NAME',
help="Specify Postscript family name")
parser.add_argument(
'-l', '--full-name',
type=str, metavar='NAME',
help="Specify font name")
parser.add_argument(
'-t', '--weight', '--subfamily',
type=str, metavar='NAME',
help="Specify subfamily (font weight)")
parser.add_argument(
'-V', '--font-version',
type=str, metavar='VERSION',
help="Set font version")
parser.add_argument(
'-N', '--sfnt-name',
action='append', type=str, metavar='LANGID:STRID:NAME',
help="Specify SFNT name (may be specified more than once)")
parser.add_argument(
'-F', '--sfnt-family-name',
action='append', type=str, metavar='LANGID:NAME',
help="Same as -N LANGID:1:NAME")
parser.add_argument(
'-L', '--sfnt-full-name',
action='append', type=str, metavar='LANGID:NAME',
help="Same as -N LANGID:4:NAME")
parser.add_argument(
'-T', '--sfnt-weight', '--sfnt-subfamily',
action='append', type=str, metavar='LANGID:NAME',
help="Same as -N LANGID:2:NAME")
parser.add_argument(
'-W', '--os2-weight',
type=int, metavar='WEIGHT',
help="Specify OS/2 weight (400 for regular, 700 for bold)")
parser.add_argument(
'--os2-family-class',
type=int, metavar='FAMILY',
help="Specify OS/2 family class")
parser.add_argument(
'-m', '--merge-with',
action='append', type=str, metavar='FILENAME',
help="Merge specified fonts (may be specified more than once)")
parser.add_argument(
'-r', '--round-to-int',
action='store_true', default=False,
help="Round coordinates to integer")
parser.add_argument(
'srcfile',
type=str,
help="Source font file")
parser.add_argument(
'destfile',
type=str,
help="Target font file")
args = parser.parse_args()
def glyphsWorthOutputting(font):
for glyph in font.glyphs():
if glyph.isWorthOutputting():
yield glyph
if (args.action is not None) and (args.width is None):
parser.error('width not specified')
elif (args.action is None) and (args.width is not None):
parser.error('action not specified')
font = fontforge.open(args.srcfile)
if args.font_name is not None:
font.fontname = args.font_name
if args.family_name is not None:
font.familyname = args.family_name
if args.full_name is not None:
font.fullname = args.full_name
if args.weight is not None:
font.weight = args.weight
if args.os2_weight is not None:
font.os2_weight = args.os2_weight
if args.os2_family_class is not None:
font.os2_family_class = args.os2_family_class
if args.font_version is not None:
font.version = args.font_version
font.sfntRevision = None
if args.em is not None:
font.em = args.em
sfnt = []
def addSfntNames(lst, strid = None):
if lst is not None:
for names in lst:
try: # Python 2
if strid is None:
nm = names.split(':', 2)
sfnt.append([
int(nm[0], 16 if nm[0][0:2] == '0x' else 10),
int(nm[1]),
nm[2].decode(getdefaultlocale()[1]).encode('UTF-8')
])
else:
nm = names.split(':', 1)
sfnt.append([
int(nm[0], 16 if nm[0][0:2] == '0x' else 10),
strid,
nm[1].decode(getdefaultlocale()[1]).encode('UTF-8')
])
except AttributeError: # Python 3
if strid is None:
nm = names.split(':', 2)
sfnt.append([
int(nm[0], 16 if nm[0][0:2] == '0x' else 10),
int(nm[1]),
nm[2]
])
else:
nm = names.split(':', 1)
sfnt.append([
int(nm[0], 16 if nm[0][0:2] == '0x' else 10),
strid,
nm[1]
])
addSfntNames(args.sfnt_name)
addSfntNames(args.sfnt_family_name, 1)
addSfntNames(args.sfnt_full_name, 4)
addSfntNames(args.sfnt_weight, 2)
for nomen in sfnt:
font.appendSFNTName(nomen[0], nomen[1], nomen[2])
for glyph in glyphsWorthOutputting(font):
if args.action == 'scale':
glyph.transform(
psMat.scale(args.width / glyph.width, 1.0),
('partialRefs', 'round'))
glyph.width = int(args.width)
elif args.action == 'pad':
glyph.transform(
psMat.translate((args.width - glyph.width) / 2, 0),
('partialRefs', 'round'))
glyph.width = int(args.width)
if args.skew is not None:
glyph.transform(
psMat.skew(radians(args.skew)),
('partialRefs', 'round'))
if args.merge_with is not None:
for fileName in args.merge_with:
font2 = fontforge.open(fileName)
font.encoding = font2.encoding
font2.em = font.em
font.selection.none()
font2.selection.none()
for glyph in glyphsWorthOutputting(font2):
if (glyph.glyphname not in font) and (not list(filter(lambda gn: font[gn].unicode == glyph.unicode, font))):
font2.selection.select(('more',), glyph.glyphname)
font.selection.select(('more',), glyph.glyphname)
font2.copy()
font.paste()
font.copyright += "\n\n" + font2.copyright
font2.close()
if args.round_to_int:
font.selection.none()
for glyph in glyphsWorthOutputting(font):
font.selection.select(('more',), glyph.glyphname)
font.round()
if args.generate:
font.generate(args.destfile)
else:
font.save(args.destfile)
font.close()