-
Notifications
You must be signed in to change notification settings - Fork 2
/
merge.py
269 lines (246 loc) · 7.45 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
import sys
import copy
import json
import codecs
from types import SimpleNamespace as Namespace
from libotd.merge import MergeBelow, MergeAbove
from libotd.pkana import ApplyPalt, NowarApplyPaltMultiplied
from libotd.dereference import Dereference
from libotd.transform import Transform, ChangeAdvanceWidth
from libotd.gsub import GetGsubFlat, ApplyGsubSingle
from libotd.gc import Gc, NowarRemoveFeatures
import configure
langIdList = [ 0x0409, 0x0804, 0x0404, 0x0C04, 0x0411, 0x0412 ]
def NameFont(param, font):
family = configure.GenerateFamily(param)
subfamily = configure.GenerateSubfamily(param)
friendly = configure.GenerateFriendlyFamily(param)
legacyf, legacysubf = configure.GenerateLegacySubfamily(param)
font['head']['fontRevision'] = configure.config.fontRevision
font['OS_2']['achVendID'] = configure.config.vendorId
font['OS_2']['usWeightClass'] = param.weight
font['OS_2']['usWidthClass'] = param.width
font['name'] = [
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 0,
"nameString": configure.config.copyright
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 2,
"nameString": legacysubf
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 3,
"nameString": "{} {}".format(friendly[1033], configure.config.version)
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 5,
"nameString": configure.config.version
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 6,
"nameString": friendly[1033].replace(" ", "-").replace("C²", "C&C")
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 8,
"nameString": configure.config.vendor
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 9,
"nameString": configure.config.designer
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 11,
"nameString": configure.config.vendorUrl
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 12,
"nameString": configure.config.designerUrl
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 13,
"nameString": configure.config.license
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 14,
"nameString": configure.config.licenseUrl
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 17,
"nameString": subfamily
},
] + sum(
[[
{
"platformID": 3,
"encodingID": 1,
"languageID": langId,
"nameID": 1,
"nameString": "{} {}".format(family[langId], legacyf).strip()
},
{
"platformID": 3,
"encodingID": 1,
"languageID": langId,
"nameID": 4,
"nameString": friendly[langId]
},
{
"platformID": 3,
"encodingID": 1,
"languageID": langId,
"nameID": 16,
"nameString": family[langId] + "; keep hinted, like DFKaiShu" if langId == 0x0409 else family[langId]
},
] for langId in langIdList],
[]
)
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'] = friendly[1033].replace(" ", "-")
cff['fullName'] = friendly[1033]
cff['familyName'] = family[1033]
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
]
font = copy.deepcopy(font)
if 'cmap_uvs' in font:
del font['cmap_uvs']
rm = []
for k in font['cmap']:
if int(k) not in asianSymbol:
rm.append(k)
for k in rm:
del font['cmap'][k]
NowarRemoveFeatures(font)
Gc(font)
return font
if __name__ == '__main__':
param = sys.argv[1]
param = Namespace(**json.loads(param))
dep = configure.ResolveDependency(param)
with open("build/noto/{}.otd".format(configure.GenerateFilename(dep['Latin'])), 'rb') as baseFile:
baseFont = json.loads(baseFile.read().decode('UTF-8', errors='replace'))
NameFont(param, baseFont)
baseFont['hhea']['ascender'] = 880
baseFont['hhea']['descender'] = -120
baseFont['hhea']['lineGap'] = 200
baseFont['OS_2']['sTypoAscender'] = 880
baseFont['OS_2']['sTypoDescender'] = -120
baseFont['OS_2']['sTypoLineGap'] = 200
baseFont['OS_2']['fsSelection']['useTypoMetrics'] = True
baseFont['OS_2']['usWinAscent'] = 1050
baseFont['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)
# replace numerals
if param.family in [ "WarcraftSans", "WarcraftUI" ]:
with open("build/noto/{}.otd".format(configure.GenerateFilename(dep['Numeral'])), 'rb') as numFile:
numFont = json.loads(numFile.read().decode('UTF-8', errors='replace'))
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 glyphs for futher modification
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)
# merge CJK
if param.family in [ "Sans", "UI", "WarcraftSans", "WarcraftUI" ]:
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.family:
ApplyPalt(asianFont)
else:
NowarApplyPaltMultiplied(asianFont, 0.4)
asianSymbolFont = GenerateAsianSymbolFont(asianFont)
MergeAbove(baseFont, asianSymbolFont)
NowarRemoveFeatures(asianFont)
Gc(asianFont)
MergeBelow(baseFont, asianFont)
# remap `丶` to `·` in RP variant
if "RP" in param.feature:
baseFont['cmap'][str(ord('丶'))] = baseFont['cmap'][str(ord('·'))]
Gc(baseFont)
outStr = json.dumps(baseFont, ensure_ascii=False, separators=(',',':'))
with codecs.open("build/unhinted/{}.otd".format(configure.GenerateFilename(param)), 'w', 'UTF-8') as outFile:
outFile.write(outStr)