-
Notifications
You must be signed in to change notification settings - Fork 1
/
varcFont.py
287 lines (242 loc) · 10 KB
/
varcFont.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
from font import *
from rcjkTools import *
from flatFont import buildFlatGlyph
from component import *
from fontTools.ttLib import newTable
from fontTools.ttLib.tables._g_l_y_f import Glyph, GlyphCoordinates
from fontTools.varLib.models import normalizeLocation, VariationModel
from fontTools.varLib.multiVarStore import OnlineMultiVarStoreBuilder
import fontTools.ttLib.tables.otTables as ot
from fontTools.misc.vector import Vector
from fontTools.misc.fixedTools import fixedToFloat as fi2fl
from functools import partial
from collections import defaultdict
from fontTools.designspaceLib import AxisDescriptor
import struct
async def closureGlyph(rcjkfont, glyphs, glyph):
assert glyph.sources[0].name == "<default>"
assert glyph.sources[0].layerName == "foreground"
layer = glyph.layers["foreground"]
for component in layer.glyph.components:
if component.name not in glyphs:
componentGlyph = await rcjkfont.getGlyph(component.name)
if componentGlyph is None:
print("Missing component", component.name, "in glyph", glyph.name)
continue
glyphs[component.name] = componentGlyph
await closureGlyph(rcjkfont, glyphs, componentGlyph)
async def closureGlyphs(rcjkfont, glyphs):
for glyph in list(glyphs.values()):
await closureGlyph(rcjkfont, glyphs, glyph)
async def setupFvarAxes(rcjkfont, glyphs):
fvarAxes = []
for axis in (await rcjkfont.getAxes()).axes:
fvarAxes.append(
AxisDescriptor(
tag=axis.tag,
minimum=axis.minValue,
default=axis.defaultValue,
maximum=axis.maxValue,
name=axis.name,
)
)
fvarTags = {axis.tag for axis in fvarAxes}
fvarNames = {axis.name for axis in fvarAxes}
maxAxes = 0
for glyph in glyphs.values():
axes = {
axis.name: (axis.minValue, axis.defaultValue, axis.maxValue)
for axis in glyph.axes
if axis.name not in fvarNames and axis.name not in fvarTags
}
maxAxes = max(maxAxes, len(axes))
for i in range(maxAxes):
tag = "%04d" % i
fvarAxes.append(
AxisDescriptor(
tag=tag,
minimum=-1,
default=0,
maximum=1,
name=tag,
hidden=True,
)
)
return fvarAxes
async def buildVarcFont(rcjkfont, glyphs):
print("Building varc.ttf")
glyphs = dict(glyphs)
await closureGlyphs(rcjkfont, glyphs)
publicAxes = dict()
for axis in (await rcjkfont.getAxes()).axes:
publicAxes[axis.name] = axis.tag
fvarAxes = await setupFvarAxes(rcjkfont, glyphs)
fvarTags = [axis.tag for axis in fvarAxes]
fb = await createFontBuilder(rcjkfont, "rcjk", "varc", glyphs, glyphDataFormat=1)
reverseGlyphMap = fb.font.getReverseGlyphMap()
fbGlyphs = {".notdef": Glyph()}
fbVariations = {}
varcGlyphs = {}
axisIndicesList = []
axisIndicesMap = {}
axisValuesList = []
axisValuesMap = {}
transformList = []
transformMap = {}
varStoreBuilder = OnlineMultiVarStoreBuilder(fvarTags)
for glyphName, glyph in glyphs.items():
print("Processing varc glyph", glyphName)
glyph_masters = glyphMasters(glyph)
axes = {
axis.name: mapTuple(
(axis.minValue, axis.defaultValue, axis.maxValue), axis.mapping
)
for axis in (await rcjkfont.getAxes()).axes
}
axes.update(
{
axis.name: (axis.minValue, axis.defaultValue, axis.maxValue)
for axis in glyph.axes
}
)
axesNames = set(axes.keys())
axesMap = {}
i = 0
for name in sorted(axes.keys()):
if name in publicAxes:
axesMap[name] = publicAxes[name]
elif name in fvarTags:
axesMap[name] = name
else:
while "%04d" % i in axesNames:
i += 1
axesMap[name] = "%04d" % i
i += 1
if (
glyph_masters[()].glyph.path.coordinates
or not glyph_masters[()].glyph.components
):
# Glyph has outline...
fbGlyphs[glyph.name], fbVariations[glyph.name] = await buildFlatGlyph(
rcjkfont, glyph, axesMap
)
# VarComposite glyph...
if not glyph_masters[()].glyph.components:
continue
glyphRecord = varcGlyphs[glyph.name] = ot.VarCompositeGlyph()
componentRecords = glyphRecord.components
if not glyph_masters[()].glyph.path.coordinates:
fbGlyphs[glyph.name] = Glyph()
componentAnalysis = analyzeComponents(glyph_masters, glyphs, axes, publicAxes)
layer = next(iter(glyph_masters.values())) # Default master
assert len(layer.glyph.components) == len(componentAnalysis), (
len(layer.glyph.components),
len(componentAnalysis),
)
for component, ca in zip(layer.glyph.components, componentAnalysis):
rec = VarComponent()
rec.flags = ca.getComponentFlags()
rec.glyphName = component.name
componentRecords.append(rec)
#
# Build variations
#
masterLocs = list(dictifyLocation(l) for l in glyph_masters.keys())
masterLocs = [normalizeLocation(m, axes, validate=True) for m in masterLocs]
masterLocs = [{axesMap[k]: v for k, v in loc.items()} for loc in masterLocs]
model = VariationModel(masterLocs, list(axes.keys()))
varStoreBuilder.setModel(model)
assert len(componentRecords) == len(componentAnalysis), (
len(componentRecords),
len(componentAnalysis),
)
for ci, (rec, ca) in enumerate(zip(componentRecords, componentAnalysis)):
allAxisIndexMasterValues = []
allAxisValueMasterValues = []
allTransformMasterValues = []
for loc, layer in glyph_masters.items():
component = layer.glyph.components[ci]
(
axisIndexMasters,
axisValueMasters,
transformMasters,
) = getComponentMasters(
rcjkfont,
component,
glyphs[component.name],
ca,
fvarTags,
publicAxes,
)
allAxisIndexMasterValues.append(axisIndexMasters)
allAxisValueMasterValues.append(axisValueMasters)
allTransformMasterValues.append(transformMasters)
allAxisIndexMasterValues = tuple(allAxisIndexMasterValues)
allAxisValueMasterValues = tuple(allAxisValueMasterValues)
allTransformMasterValues = tuple(allTransformMasterValues)
axisIndexMasterValues = allAxisIndexMasterValues[0]
assert all(axisIndexMasterValues == m for m in allAxisIndexMasterValues)
rec.numAxes = len(axisIndexMasterValues)
if axisIndexMasterValues:
if axisIndexMasterValues in axisIndicesMap:
idx = axisIndicesMap[axisIndexMasterValues]
else:
idx = len(axisIndicesList)
axisIndicesList.append(axisIndexMasterValues)
axisIndicesMap[axisIndexMasterValues] = idx
rec.axisIndicesIndex = idx
else:
rec.axisIndicesIndex = None
axisValues, rec.axisValuesVarIndex = varStoreBuilder.storeMasters(
[Vector(l) for l in allAxisValueMasterValues], round=Vector.__round__
)
rec.axisValues = tuple(fi2fl(axisValues, 14) for axisValues in axisValues)
transformBase, rec.transformVarIndex = varStoreBuilder.storeMasters(
[Vector(l) for l in allTransformMasterValues], round=Vector.__round__
)
rec.transform.scaleX = rec.transform.scaleY = 0
rec.applyTransformDeltas(transformBase)
if glyph_masters[()].glyph.path.coordinates:
# Add a component for the outline...
component = ot.VarComponent()
component.flags = 0
component.glyphName = glyph.name
componentRecords.append(component)
# Reorder axisIndices such that the more used ones come first
# Count users first.
axisIndicesUsers = [0] * len(axisIndicesList)
for glyph in varcGlyphs.values():
for component in glyph.components:
if component.axisIndicesIndex is not None:
axisIndicesUsers[component.axisIndicesIndex] += 1
# Then sort by usage
mapping = sorted(range(len(axisIndicesList)), key=lambda i: -axisIndicesUsers[i])
axisIndicesList = [axisIndicesList[i] for i in mapping]
reverseMapping = {mapping[i]: i for i in range(len(mapping))}
# Then remap axisIndicesIndex
for glyph in varcGlyphs.values():
for component in glyph.components:
if component.axisIndicesIndex is not None:
component.axisIndicesIndex = reverseMapping[component.axisIndicesIndex]
axisIndices = ot.AxisIndicesList()
axisIndices.Item = axisIndicesList
print("AxisIndicesList:", len(axisIndicesList))
varStore = varStoreBuilder.finish()
varCompositeGlyphs = ot.VarCompositeGlyphs()
varCompositeGlyphs.VarCompositeGlyph = list(varcGlyphs.values())
varc = newTable("VARC")
varcTable = varc.table = ot.VARC()
varcTable.Version = 0x00010000
coverage = varcTable.Coverage = ot.Coverage()
coverage.glyphs = list(varcGlyphs.keys())
varcTable.MultiVarStore = varStore
varcTable.AxisIndicesList = axisIndices
varcTable.VarCompositeGlyphs = varCompositeGlyphs
fb.setupFvar(fvarAxes, [])
fb.setupGlyf(fbGlyphs, validateGlyphFormat=False)
fb.setupGvar(fbVariations)
recalcSimpleGlyphBounds(fb)
fixLsb(fb)
fb.font["VARC"] = varc
print("Saving varc.ttf")
fb.save("varc.ttf")