This repository has been archived by the owner on Aug 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmerge.py
207 lines (185 loc) · 6 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
import sys
import copy
import json
import codecs
from types import SimpleNamespace as Namespace
from fontlib.merge import MergeBelow
from fontlib.pkana import ApplyPalt
from fontlib.dereference import Dereference
from fontlib.transform import Transform, ChangeAdvanceWidth
from fontlib.gsub import GetGsubFlat
from fontlib.gsub import ApplyGsubSingle
import configure
def NameFont(param, font):
family = configure.GenerateFamily(param)
subfamily = configure.GenerateSubfamily(param)
friendly = configure.GenerateFriendlyFamily(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": 1,
"nameString": friendly
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 2,
"nameString": "Regular"
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 3,
"nameString": "{} {}".format(friendly, configure.config.version)
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 4,
"nameString": friendly
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 5,
"nameString": configure.config.version
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 6,
"nameString": friendly.replace(" ", "-")
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 13,
"nameString": configure.config.license
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 16,
"nameString": family
},
{
"platformID": 3,
"encodingID": 1,
"languageID": 1033,
"nameID": 17,
"nameString": subfamily
},
]
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.replace(" ", "-")
cff['fullName'] = friendly
cff['familyName'] = family
cff['weight'] = subfamily
if __name__ == '__main__':
param = sys.argv[1]
param = Namespace(**json.loads(param))
dep = configure.ResolveDependency(param)
with open("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'] = 850
baseFont['hhea']['descender'] = -150
baseFont['hhea']['lineGap'] = 200
baseFont['OS_2']['sTypoAscender'] = 850
baseFont['OS_2']['sTypoDescender'] = -150
baseFont['OS_2']['sTypoLineGap'] = 200
baseFont['OS_2']['fsSelection']['useTypoMetrics'] = True
baseFont['OS_2']['usWinAscent'] = 1050
baseFont['OS_2']['usWinDescent'] = 300
# oldstyle figure
if configure.GetRegion(param) == "OSF":
ApplyGsubSingle('pnum', baseFont)
ApplyGsubSingle('onum', baseFont)
# replace numerals
if param.family in [ "WarcraftSans", "WarcraftUI" ]:
with open("noto/{}.otd".format(configure.GenerateFilename(dep['Numeral'])), 'rb') as numFile:
numFont = json.loads(numFile.read().decode('UTF-8', errors='replace'))
maxWidth = 490
numWidth = numFont['glyf']['zero']['advanceWidth']
changeWidth = maxWidth - numWidth if numWidth > maxWidth else 0
gsubPnum = GetGsubFlat('pnum', numFont)
gsubTnum = GetGsubFlat('tnum', numFont)
gsubOnum = GetGsubFlat('onum', numFont)
num = [ 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine' ]
pnum = [ gsubPnum[n] for n in num ]
onum = [ gsubOnum[n] for n in pnum ]
tonum = [ gsubOnum[n] for n in num ]
# 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("milan/{}.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)
# for zh-Hant NPC names
asianFont['cmap'][str(0x2027)] = asianFont['cmap'][str(0x00B7)]
MergeBelow(baseFont, asianFont)
# use CJK middle dots, quotes, em-dash and ellipsis in non-UI family
if "UI" not in param.family:
for u in [
0x00B7, # MIDDLE DOT
0x2014, # EM DASH
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
]:
if str(u) in asianFont['cmap']:
baseFont['glyf'][baseFont['cmap'][str(u)]] = asianFont['glyf'][asianFont['cmap'][str(u)]]
# remap `丶` to `·` in RP variant
if param.region == "RP":
baseFont['cmap'][str(ord('丶'))] = baseFont['cmap'][str(ord('·'))]
outStr = json.dumps(baseFont, ensure_ascii=False)
with codecs.open("out/{}.otd".format(configure.GenerateFilename(param)), 'w', 'UTF-8') as outFile:
outFile.write(outStr)