-
Notifications
You must be signed in to change notification settings - Fork 1
/
decompose.py
134 lines (110 loc) · 4.17 KB
/
decompose.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
from font import mapTuple
from transform import composeTransform, Identity
from mathRecording import MathRecording
from rcjkTools import *
from fontTools.pens.recordingPen import RecordingPointPen
from fontTools.pens.transformPen import TransformPointPen
from fontTools.varLib.models import normalizeLocation, VariationModel
from fontTools.misc.vector import Vector
async def decomposeGlyph(glyph, rcjkfont, location=(), trans=Identity):
value = []
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
}
)
glyph_masters = glyphMasters(glyph)
masterLocs = list(dictifyLocation(l) for l in glyph_masters.keys())
masterLocs = [normalizeLocation(m, axes, validate=True) for m in masterLocs]
model = VariationModel(masterLocs, list(axes.keys()))
# Interpolate outline
masterShapes = [
await decomposeLayer(layer, rcjkfont, trans, shallow=True)
for layer in glyph_masters.values()
]
loc = normalizeLocation(location, axes) # , validate=True)
shape = model.interpolateFromMasters(loc, masterShapes)
value.extend(shape.value)
# Interpolate components
numComps = len(next(iter(glyph_masters.values())).glyph.components)
for compIndex in range(numComps):
compTransforms = []
compLocations = []
name = None
for layer in glyph_masters.values():
compName = layer.glyph.components[compIndex].name
if name is not None:
assert name == compName
name = compName
compTransforms.append(layer.glyph.components[compIndex].transformation)
compLocations.append(layer.glyph.components[compIndex].location)
locKeys = set()
for locations in compLocations:
locKeys.update(locations.keys())
locKeys = sorted(locKeys)
locationVectors = []
for locations in compLocations:
locationVectors.append(Vector(locations.get(k, 0) for k in locKeys))
transformVectors = []
for t in compTransforms:
transformVectors.append(
Vector(
(
t.translateX,
t.translateY,
t.rotation,
t.scaleX,
t.scaleY,
t.skewX,
t.skewY,
t.tCenterX,
t.tCenterY,
)
)
)
locationVector = model.interpolateFromMasters(loc, locationVectors)
transformVector = model.interpolateFromMasters(loc, transformVectors)
location = {k: v for k, v in zip(locKeys, locationVector)}
transform = composeTransform(*transformVector)
composedTrans = trans.transform(transform)
componentGlyph = await rcjkfont.getGlyph(name)
shape = await decomposeGlyph(componentGlyph, rcjkfont, location, composedTrans)
value.extend(shape.value)
return MathRecording(value)
async def decomposeLayer(layer, rcjkfont, trans=Identity, shallow=False):
pen = RecordingPointPen()
tpen = TransformPointPen(pen, trans)
layer.glyph.path.drawPoints(tpen)
value = pen.value
if shallow:
return MathRecording(value)
for component in layer.glyph.components:
t = component.transformation
componentTrans = composeTransform(
t.translateX,
t.translateY,
t.rotation,
t.scaleX,
t.scaleY,
t.skewX,
t.skewY,
t.tCenterX,
t.tCenterY,
)
composedTrans = trans.transform(componentTrans)
componentGlyph = await rcjkfont.getGlyph(component.name)
value.extend(
(
await decomposeGlyph(
componentGlyph, rcjkfont, component.location, composedTrans
)
).value
)
return MathRecording(value)