-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge.py
331 lines (300 loc) · 10.5 KB
/
merge.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import datetime
import sys
import copy
import json
import codecs
from libotd.rebase import Rebase
from libotd.dereference import Dereference
from libotd.merge import MergeBelow, MergeAbove
from libotd.pkana import ApplyPalt, NowarApplyPaltMultiplied
from libotd.transform import Transform, ChangeAdvanceWidth
from libotd.gsub import GetGsubFlat, ApplyGsubSingle
from libotd.gc import Gc, Consolidate, NowarRemoveFeatures
import configure
def NameFont(param, font):
fontName = configure.GenerateFontName(param)
family, subfamily = fontName["typographic"]
wwsF, wwsSf = fontName["wws"]
legacyF, legacySf = fontName["legacy"]
friendly = fontName["friendly"]
postscript = fontName["postscript"]
enUS = configure.LanguageId.enUS
os_2 = font["OS_2"]
fsSelection = os_2["fsSelection"]
head = font["head"]
macStyle = font["head"]["macStyle"]
weight = param["weight"]
width = param["width"]
slant = param.get("slant")
head['fontRevision'] = configure.config.fontRevision
os_2['achVendID'] = configure.config.vendorId
os_2['usWeightClass'] = weight
# Warcraft numeral hack
os_2['usWidthClass'] = 5 if width == 10 else width
fsSelection["wws"] = False
fsSelection["regular"] = (weight == 400) and (not slant) and (width == 5)
if weight == 700:
fsSelection["bold"] = True
macStyle["bold"] = True
if slant == "Italic":
fsSelection["italic"] = True
macStyle["italic"] = True
elif slant == "Oblique":
fsSelection["oblique"] = True
font['name'] = [
{
"platformID": 3,
"encodingID": 1,
"languageID": enUS,
"nameID": 0,
"nameString": configure.config.copyright
},
{
"platformID": 3,
"encodingID": 1,
"languageID": enUS,
"nameID": 2,
"nameString": legacySf
},
{
"platformID": 3,
"encodingID": 1,
"languageID": enUS,
"nameID": 3,
"nameString": "{}: {} {}".format(configure.config.vendorId, friendly[enUS], configure.config.version)
},
{
"platformID": 3,
"encodingID": 1,
"languageID": enUS,
"nameID": 5,
"nameString": configure.config.version
},
{
"platformID": 3,
"encodingID": 1,
"languageID": enUS,
"nameID": 6,
"nameString": postscript
},
{
"platformID": 3,
"encodingID": 1,
"languageID": enUS,
"nameID": 8,
"nameString": configure.config.vendor
},
{
"platformID": 3,
"encodingID": 1,
"languageID": enUS,
"nameID": 9,
"nameString": configure.config.designer
},
{
"platformID": 3,
"encodingID": 1,
"languageID": enUS,
"nameID": 11,
"nameString": configure.config.vendorUrl
},
{
"platformID": 3,
"encodingID": 1,
"languageID": enUS,
"nameID": 12,
"nameString": configure.config.designerUrl
},
{
"platformID": 3,
"encodingID": 1,
"languageID": enUS,
"nameID": 13,
"nameString": configure.config.license
},
{
"platformID": 3,
"encodingID": 1,
"languageID": enUS,
"nameID": 14,
"nameString": configure.config.licenseUrl
},
{
"platformID": 3,
"encodingID": 1,
"languageID": enUS,
"nameID": 17,
"nameString": subfamily
},
{
"platformID": 3,
"encodingID": 1,
"languageID": enUS,
"nameID": 22,
"nameString": wwsSf
},
] + sum(
[[
{
"platformID": 3,
"encodingID": 1,
"languageID": langId,
"nameID": 1,
"nameString": legacyF[langId]
},
{
"platformID": 3,
"encodingID": 1,
"languageID": langId,
"nameID": 4,
"nameString": friendly[langId]
},
{
"platformID": 3,
"encodingID": 1,
"languageID": langId,
"nameID": 16,
"nameString": family[langId]
},
{
"platformID": 3,
"encodingID": 1,
"languageID": langId,
"nameID": 21,
"nameString": wwsF[langId]
},
] for langId in configure.LanguageId],
[]
)
if 'CFF_' in font:
cff = font['CFF_']
cff['version'] = configure.config.version
if 'notice' in cff:
del cff['notice']
cff['copyright'] = configure.config.copyright
cff['fontName'] = postscript
cff['fullName'] = friendly[enUS]
cff['familyName'] = family[enUS]
cff['weight'] = subfamily
def GenerateAsianSymbolFont(font):
asianSymbol = [
0x00B7, # MIDDLE DOT
0x2014, # EM DASH
0x2015, # HORIZONTAL BAR
0x2018, # LEFT SINGLE QUOTATION MARK
0x2019, # RIGHT SINGLE QUOTATION MARK
0x201C, # LEFT DOUBLE QUOTATION MARK
0x201D, # RIGHT DOUBLE QUOTATION MARK
0x2026, # HORIZONTAL ELLIPSIS
0x2027, # HYPHENATION POINT
0x2E3A, # TWO-EM DASH
0x2E3B, # THREE-EM DASH
]
symbolFont = {}
symbolFont["cmap"] = {k: v for k,
v in font["cmap"].items() if int(k) in asianSymbol}
glyphSet = {symbolFont["cmap"].values()}
symbolFont["glyf"] = {k: v for k,
v in font["glyf"].items() if k in glyphSet}
symbolFont["glyph_order"] = ["symb.notdef"]
return symbolFont
def Simplify(font):
from opencc_t2s import OpenCC_T2S
cmap = asianFont['cmap']
for t, s in OpenCC_T2S.items():
us = str(ord(s))
if us in cmap:
ut = str(ord(t))
cmap[ut] = cmap[us]
if __name__ == '__main__':
param = sys.argv[1]
param = json.loads(param)
dep = configure.ResolveDependency(param)
with open("build/roboto/{}.otd".format(configure.GenerateFilename(dep['Latin'])), 'rb') as baseFile:
baseFont = json.loads(
baseFile.read().decode('UTF-8', errors='replace'))
upm = baseFont["head"]["unitsPerEm"]
if (upm != 1000):
Rebase(baseFont, 1000 / upm, roundToInt=True)
NameFont(param, baseFont)
hhea = baseFont["hhea"]
os_2 = baseFont["OS_2"]
if os_2["version"] < 4:
os_2["version"] = 4
hhea['ascender'] = 880
hhea['descender'] = -120
hhea['lineGap'] = 200
os_2['sTypoAscender'] = 880
os_2['sTypoDescender'] = -120
os_2['sTypoLineGap'] = 200
os_2['fsSelection']['useTypoMetrics'] = True
os_2['usWinAscent'] = 1050
os_2['usWinDescent'] = 300
# oldstyle figure
if "OSF" in param["feature"]:
ApplyGsubSingle('pnum', baseFont)
ApplyGsubSingle('onum', baseFont)
# small caps
if "SC" in param["feature"]:
ApplyGsubSingle('smcp', baseFont)
# Warcraft numeral hack
if param["width"] == 10:
with open("build/roboto/{}.otd".format(configure.GenerateFilename(dep['Numeral'])), 'rb') as numFile:
numFont = json.loads(
numFile.read().decode('UTF-8', errors='replace'))
if (upm != 1000):
Rebase(numFont, 1000 / upm, roundToInt=True)
gsubPnum = GetGsubFlat('pnum', numFont)
gsubTnum = GetGsubFlat('tnum', numFont)
gsubOnum = GetGsubFlat('onum', numFont)
num = [numFont['cmap'][str(ord('0') + i)] for i in range(10)]
pnum = [gsubPnum[n] for n in num]
onum = [gsubOnum[n] for n in pnum]
tonum = [gsubOnum[n] for n in num]
maxWidth = 490
numWidth = numFont['glyf'][num[0]]['advanceWidth']
changeWidth = maxWidth - numWidth if numWidth > maxWidth else 0
# dereference TT glyphs
if "CFF_" not in numFont:
for n in num + pnum + onum + tonum:
numFont['glyf'][n] = Dereference(
numFont['glyf'][n], numFont)
for n in num + tonum:
tGlyph = numFont['glyf'][n]
tWidth = tGlyph['advanceWidth']
pName = gsubPnum[n]
pGlyph = numFont['glyf'][pName]
pWidth = pGlyph['advanceWidth']
if pWidth > tWidth:
numFont['glyf'][pName] = copy.deepcopy(tGlyph)
pGlyph = numFont['glyf'][pName]
pWidth = tWidth
if changeWidth != 0:
ChangeAdvanceWidth(pGlyph, changeWidth)
Transform(pGlyph, 1, 0, 0, 1, (changeWidth + 1) // 2, 0)
for n in num + pnum + onum + tonum:
baseFont['glyf'][n] = numFont['glyf'][n]
ApplyGsubSingle('pnum', baseFont)
with open("build/shs/{}.otd".format(configure.GenerateFilename(dep['CJK'])), 'rb') as asianFile:
asianFont = json.loads(
asianFile.read().decode('UTF-8', errors='replace'))
# pre-apply `palt` in UI family
if "UI" in param["feature"]:
ApplyPalt(asianFont)
else:
NowarApplyPaltMultiplied(asianFont, 0.4)
asianSymbolFont = GenerateAsianSymbolFont(asianFont)
MergeAbove(baseFont, asianSymbolFont)
# pseudo-simplified font
if "Simp" in param["feature"]:
Simplify(asianFont)
NowarRemoveFeatures(asianFont)
MergeBelow(baseFont, asianFont)
# remap `丶` to `·` in RP variant
if "RP" in param["feature"]:
baseFont['cmap'][str(ord('丶'))] = baseFont['cmap'][str(ord('·'))]
Gc(baseFont)
Consolidate(baseFont)
outStr = json.dumps(baseFont, ensure_ascii=False, separators=(',', ':'))
with codecs.open("build/nowar/{}.otd".format(configure.GenerateFilename(param)), 'w', 'UTF-8') as outFile:
outFile.write(outStr)