forked from simoncozens/GlyphsScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglyphmonkey.py
410 lines (344 loc) · 11.6 KB
/
glyphmonkey.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#MenuTitle:
# -*- coding: utf-8 -*-
__doc__=""
import GlyphsApp
from GlyphsApp import Proxy
from math import atan2, sqrt, cos, sin, radians, degrees
from Foundation import NSMakePoint, NSValue, NSMakeRect
# Make GSNodes hashable
class GSLineSegment(object):
def __init__(self, tuple = None, owner = None, idx = 0):
self._seg = tuple
if not self._seg: self._seg = owner._segments[idx]
self._owner = owner
self._owneridx = idx
def __repr__(self):
"""Return list-lookalike of representation string of objects"""
return "<GSSegment (%s, %s)--(%s, %s)>" % (self.start.x,self.start.y,self.end.x,self.end.y)
def _seg(self): return self._seg
@property
def start(self): return self._seg[0].position
@property
def end(self): return self._seg[-1].position
# For backward compatibility
def __getitem__(self, Key):
if Key < 0:
Key = self.__len__() + Key
# There is a horribly subtle distinction between an NSValue and
# an NSPoint. SpeedPunk expects to see an NSValue here and dies
# if it doesn't have one.
return NSValue.valueWithPoint_(self._seg[Key].position)
@property
def area(self):
xa, ya = self.start.x, self.start.y/20
xb, yb = xa, ya
xc, yc = self.end.x, self.end.y/20
xd, yd = xc, yc
return (xb-xa)*(10*ya + 6*yb + 3*yc + yd) + (xc-xb)*( 4*ya + 6*yb + 6*yc + 4*yd) +(xd-xc)*( ya + 3*yb + 6*yc + 10*yd)
@property
def length(self):
l1 = self.start.x - self.end.x
l2 = self.start.y - self.end.y
return sqrt(l1 * l1 + l2 * l2)
@property
def angle(self):
e = self.end
s = self.start
return degrees(atan2(e.y - s.y, e.x - s.x))
@property
def selected(self):
return self.start.selected and self.end.selected
def reverse(self):
self._seg = self._seg[::-1]
def _interpolatenspoint(self, p1, p2, t):
dx = (p2.x - p1.x) * t
dy = (p2.y - p1.y) * t
return NSMakePoint(p1.x + dx, p1.y + dy)
def interpolate(self, other, t):
if type(other) != type(self): raise TypeError
l = []
for i in range(0, len(self)):
newnode = self._seg[i].copy() # Urgh
newnode.position = self._interpolatenspoint(self._seg[i].position, other._seg[i].position, t)
l.append(newnode)
return type(self)(l)
def __len__(self):
return 2
class GSCurveSegment(GSLineSegment):
def __repr__(self):
return "<GSSegment (%s, %s)..[%s,%s]..[%s,%s]..(%s, %s)>" % (
self.start.x, self.start.y,
self.handle1.x, self.handle1.y,
self.handle2.x, self.handle2.y,
self.end.x,self.end.y
)
@property
def handle1(self): return self._seg[1].position
@property
def handle2(self): return self._seg[2].position
@property
def area(self):
xa, ya = self.start.x, self.start.y/20
xb, yb = self.handle1.x, self.handle1.y/20
xc, yc = self.handle2.x, self.handle2.y/20
xd, yd = self.end.x, self.end.y/20
return (xb-xa)*(10*ya + 6*yb + 3*yc + yd) + (xc-xb)*( 4*ya + 6*yb + 6*yc + 4*yd) +(xd-xc)*( ya + 3*yb + 6*yc + 10*yd)
def angle(self): # XXX This is wrong
e = self.end
s = self.start
return degrees(atan2(e.y - s.y, e.x - s.x))
def interpolate_at_fraction(self, t):
if t < 0 or t > 1:
raise Exception("interpolate_at_fraction should be called with a number between 0 and 1")
t1 = 1.0 - t;
t1_3 = t1*t1*t1
t1_3a = (3*t)*(t1*t1)
t1_3b = (3*(t*t))*t1;
t1_3c = (t * t * t )
x = (self.start.x * t1_3) + (t1_3a * self.handle1.x) + (t1_3b * self.handle2.x) + (t1_3c * self.end.x)
y = (self.start.y * t1_3) + (t1_3a * self.handle1.y) + (t1_3b * self.handle2.y) + (t1_3c * self.end.y)
return (x,y)
@property
def length(self):
steps = 50
t = 0.0
length = 0
previous = ()
while t < 1.0:
this = self.interpolate_at_fraction(t)
if t > 0:
dx = previous[0] - this[0]
dy = previous[1] - this[1]
length = length + sqrt(dx*dx+dy*dy)
t = t + 1.0/steps
previous = this
return length
def __len__(self):
return 4
class PathSegmentsProxy (Proxy):
# Actually we're not going to use .segments at all, because we
# want to be able to access things like GSNode.selected
def toSegments(p):
segList = []
nodeList = p._owner.nodes
thisSeg = (nodeList[-1],)
for i in range(0,len(nodeList)):
thisSeg = thisSeg + (nodeList[i],)
if nodeList[i].type != GlyphsApp.GSOFFCURVE:
segList.append(thisSeg)
thisSeg = (nodeList[i],)
return segList
def __getitem__(self, Key):
if Key < 0:
Key = self.__len__() + Key
segs = self.toSegments()
if len(segs[Key]) == 2:
return GSLineSegment( owner = self._owner, idx = Key, tuple = segs[Key])
else:
return GSCurveSegment( owner = self._owner, idx = Key, tuple = segs[Key])
def __setitem__(self, Key, Layer):
if Key < 0:
Key = self.__len__() + Key
# XXX
def __len__(self):
return len(self.toSegments())
def values(self):
return map(self.__getitem__, range(0,self.__len__()))
# Unfortunately working with segments doesn't always *work*. So we
# map a segment list to a node list
GSNode = GlyphsApp.GSNode
def toNodeList(segments):
nodelist = []
closed = (segments[-1].end.x == segments[0].start.x and segments[-1].end.y == segments[0].start.y)
nodelist.append( GSNode((segments[0].start.x, segments[0].start.y), GlyphsApp.GSLINE) )
for i in range(0,len(segments)):
s = segments[i]
t = GlyphsApp.GSCURVE
c = GlyphsApp.GSSMOOTH
if type(s) is GSLineSegment:
t = GlyphsApp.GSLINE
else:
s1 = s.handle1
nodelist.append(GSNode((s1.x,s1.y), type = GlyphsApp.GSOFFCURVE))
s2 = s.handle2
nodelist.append(GSNode((s2.x,s2.y), type = GlyphsApp.GSOFFCURVE))
ns = i+1
if ns >= len(segments): ns = 0
if type(segments[ns]) is GSLineSegment:
c = GlyphsApp.GSSHARP
e = s.end
node = GSNode((e.x, e.y), t)
node.connection = c
nodelist.append(node)
n = segments[0].start
return nodelist
GlyphsApp.GSPath.segments = property( lambda self: PathSegmentsProxy(self),
lambda self, value:
self.setNodes_(toNodeList(value))
)
def nodeRotate(self, ox, oy, angle):
angle = radians(angle)
newX = ox + (self.position.x-ox)*cos(angle) - (self.position.y-oy)*sin(angle)
newY = oy + (self.position.x-ox)*sin(angle) + (self.position.y-oy)*cos(angle)
self.position = (round(newX,2), round(newY,2))
def nodeReflect(self, p0, p1):
dx = p1.x - p0.x
dy = p1.y - p0.y
a = (dx * dx - dy * dy) / (dx * dx + dy * dy)
b = 2 * dx * dy / (dx * dx + dy * dy)
x = a * (self.position.x - p0.x) + b * (self.position.y - p0.y) + p0.x
y = b * (self.position.x - p0.x) - a * (self.position.y - p0.y) + p0.y
self.position =(round(x,2), round(y,2))
def nodeInterpolate(self, other, t):
dx = (other.position.x - self.position.x) * t
dy = (other.position.y - self.position.y) * t
new = self.copy()
new.position = (self.position.x + dx, self.position.y + dy)
return new
GlyphsApp.GSNode.rotate = nodeRotate
GlyphsApp.GSNode.reflect = nodeReflect
GlyphsApp.GSNode.interpolate = nodeInterpolate
def _layerCenter(self):
bounds = self.bounds
ox = bounds.origin.x + bounds.size.width / 2
oy = bounds.origin.y + bounds.size.height / 2
return NSMakePoint(ox, oy)
GlyphsApp.GSLayer.center = _layerCenter
def horizontalCenterOfWeight(self):
sampleX = self.bounds.origin.x
sampleHeight = 5
stripAreas = []
totalArea = 0
p = self.bezierPath()
# This is slow and stupid but it works
while sampleX < self.bounds.origin.x + self.bounds.size.width:
sampleY = self.bounds.origin.y
thisArea = 0
while sampleY < self.bounds.origin.y + self.bounds.size.height:
if p.containsPoint_([sampleX, sampleY]):
thisArea = thisArea + 1
sampleY = sampleY + sampleHeight
stripAreas.append(thisArea)
totalArea = totalArea + thisArea
sampleX = sampleX + 1
area = 0
for i, v in enumerate(stripAreas):
area = area + v
if area > totalArea /2 :
return i
GlyphsApp.GSLayer.horizontalCenterOfWeight = horizontalCenterOfWeight
def horizontalOpticalCenter(self):
cw = self.horizontalCenterOfWeight()
tc = self.center()[0]
return tc + (cw-tc) / 3 # This is an approximation, obviously...
GlyphsApp.GSLayer.horizontalOpticalCenter = horizontalOpticalCenter
### additional GSPath methods
def layerCenter(self):
return self.parent.center()
def pathCenter(self):
bounds = self.bounds
ox = bounds.origin.x + bounds.size.width / 2
oy = bounds.origin.y + bounds.size.height / 2
return NSMakePoint(ox, oy)
def pathRotate(self, angle=-1, ox=-1, oy=-1):
if angle == -1: angle = 180
if ox == -1 and oy == -1:
if self.parent: # Almost always
ox, oy = self.layerCenter().x, self.layerCenter().y
else:
ox, oy = self.center().x, self.center().y
for n in self.nodes:
n.rotate(ox, oy, angle)
return self
def pathReflect(self, p0 = -1, p1 = -1):
if p0 == -1 and p1 == -1:
if self.parent: # Almost always
p0 = self.layerCenter()
p1 = self.layerCenter()
else:
p0 = self.center()
p1 = self.center()
p1.y = p1.y + 100
for n in self.nodes:
n.reflect(p0, p1)
return self
def pathDiff(p1, p2):
nodes1 = set((n.position.x,n.position.y) for n in p1.nodes)
nodes2 = set((n.position.x,n.position.y) for n in p2.nodes)
return nodes1 - nodes2
def pathEqual(p1, p2):
pd = pathDiff(p1, p2)
return len(pd) == 0
def pathToNodeSet(self):
return GSNodeSet(self.nodes)
GlyphsApp.GSPath.layerCenter = layerCenter
GlyphsApp.GSPath.center = pathCenter
GlyphsApp.GSPath.rotate = pathRotate
GlyphsApp.GSPath.reflect = pathReflect
GlyphsApp.GSPath.equal = pathEqual
GlyphsApp.GSPath.diff = pathDiff
GlyphsApp.GSPath.toNodeSet = pathToNodeSet
class GSNodeSet(object):
def toKey(self,n):
return "%s %s %s" % (n.position.x, n.position.y, n.type)
def __init__(self, nodes):
self._dict = {}
for n in nodes:
self._dict[self.toKey(n)] = n
def __repr__(self):
return "<GSNodeSet (%s nodes)>" % (len(self))
def __len__(self):
return len(self._dict)
@property
def nodes(self):
return self._dict.values()
@property
def bounds(self):
minx, maxx, miny, maxy = None, None, None, None
if len(self) < 1: return None
for p in self.nodes:
pos = p.position
if minx == None or pos.x < minx: minx = pos.x
if maxx == None or pos.x > maxx: maxx = pos.x
if miny == None or pos.y < minx: miny = pos.y
if maxy == None or pos.y > maxx: maxy = pos.y
return NSMakeRect(minx, miny, maxx-minx, maxy-miny)
@property
def center(self):
if len(self) < 1: return None
b = self.bounds
return NSMakePoint(b.origin.x + b.size.width / 2, b.origin.y + b.size.height / 2, )
def copy(self):
return GSNodeSet(n.copy() for n in self.nodes)
def diff(ns1, ns2):
nodes1 = set((n.position.x,n.position.y) for n in ns1.nodes)
nodes2 = set((n.position.x,n.position.y) for n in ns2.nodes)
return nodes1 - nodes2
def equal(p1, p2):
pd = p1.diff(p2)
return len(pd) == 0
def rotate(self, angle=-1, ox=-1, oy=-1):
if angle == -1: angle = 180
if ox == -1 and oy == -1:
ox, oy = self.center.x, self.center.y
for n in self.nodes:
n.rotate(ox, oy, angle)
return self
def reflect(self, p0 = -1, p1 = -1):
if p0 == -1 and p1 == -1:
p0 = self.center
p1 = self.center
p1.y = p1.y + 100
for n in self.nodes:
n.reflect(p0, p1)
return self
def selectedNodeSet(layer):
sel = []
for n in layer.selection:
if isinstance(n, GSNode):
sel.append(n)
return GSNodeSet(sel)
GlyphsApp.GSLayer.selectedNodeSet = selectedNodeSet
# Does p have rotational symmetry?
# ox, oy = p.layerCenter()
# p.equal(p.copy().rotate(angle=180, ox=ox, oy=oy))