-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerRuler.py
288 lines (249 loc) · 9.83 KB
/
PowerRuler.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
"""
Copyright 2020 Black Foundry.
This file is part of Power Ruler.
Power Ruler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Power Ruler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Power Ruler. If not, see <https://www.gnu.org/licenses/>.
"""
from fontPens.penTools import estimateCubicCurveLength, distance, interpolatePoint, getCubicPoint
from fontTools.misc.bezierTools import calcQuadraticArcLength
from fontTools.pens.basePen import BasePen
from mojo.drawingTools import *
from mojo.events import addObserver, removeObserver, getActiveEventTool
from mojo.drawingTools import *
from mojo.UI import UpdateCurrentGlyphView
from mojo.events import getActiveEventTool
from mojo.tools import IntersectGlyphWithLine
from math import sqrt
from vanilla import *
from AppKit import *
import os
import lib.eventTools
class TriggerButton():
def __init__(self):
self.colorOne = NSColor.colorWithCalibratedRed_green_blue_alpha_(1,0,0,.5)
self.colorTwo = NSColor.colorWithCalibratedRed_green_blue_alpha_(0,.2,.8,.3)
self.active = False
self.closest = None
self.ortho = None
self.mousePt = None
self.activDraw = 0
self.keydidUp = 0
addObserver(self, "drawClosest", "draw")
addObserver(self, "drawClosest", "drawPreview")
addObserver(self, "mouseMoved", "mouseMoved")
addObserver(self, "changeGlyph", "currentGlyphChanged")
addObserver(self, "keyDown", "keyDown")
addObserver(self, "keyUp", "keyUp")
def getRCJKI(self):
for o in lib.eventTools.eventManager.allObservers():
obsClass = o[1]
if obsClass.__class__.__name__ == 'RoboCJKController':
return obsClass
return None
@property
def g(self):
currentGlyph = CurrentGlyph()
if currentGlyph is None:
return None
_glyph = currentGlyph.copy()
RCJKI = self.getRCJKI()
if RCJKI is None:
return _glyph
RCJKI_glyph = RCJKI.currentFont[_glyph.name]
RCJKI_glyph.computeDeepComponents()
for i, atomicInstanceGlyph in RCJKI_glyph.atomicInstancesGlyphs:
for c in atomicInstanceGlyph:
_glyph.appendContour(c)
return _glyph
def keyDown(self, sender):
if self.g is None: return
if sender['event'].keyCode() == 15:
self.activDraw = 1
self.keydidUp = 0
if sender['event'].keyCode() == 15 and getActiveEventTool().getModifiers()['commandDown']:
self.activDraw = 0
UpdateCurrentGlyphView()
def keyUp(self, sender):
self.keydidUp = 1
UpdateCurrentGlyphView()
def changeGlyph(self, info):
if not self.mousePt: return
self.closest = self.ortho = None
self.sections = []
if not self.g: return
self.pen = ClosestPointPen(self.mousePt, self.g.getParent())
self.g.draw(self.pen)
self.closest, self.ortho = self.pen.getClosestData()
UpdateCurrentGlyphView()
def drawClosest(self, info):
if not self.activDraw: return
if self.closest and self.ortho:
p = self.closest[0], self.closest[1]
s = info['scale']
r = 2.5*s
rOutline = 5*s
rOutline2 = 8*s
strokeWidth(.5*s)
normOrtho = self.normalize(self.ortho)
save()
newPath()
stroke(.2)
moveTo((p))
endOut = p[0] + normOrtho[0]*2000, p[1] + normOrtho[1]*2000
lineTo(endOut)
closePath()
drawPath()
restore()
save()
newPath()
stroke(.2)
moveTo((p))
endIn = p[0] - normOrtho[0]*2000, p[1] - normOrtho[1]*2000
lineTo(endIn)
closePath()
drawPath()
restore()
save()
fill(None)
stroke(1, 0, 1)
oval(p[0]-rOutline, p[1]-rOutline, 2*rOutline, 2*rOutline)
oval(p[0]-rOutline2, p[1]-rOutline2, 2*rOutline2, 2*rOutline2)
restore()
self.sections = IntersectGlyphWithLine(self.g, (endOut, endIn), canHaveComponent=True, addSideBearings=False)
self.sections.sort()
save()
lens = len(self.sections)
if lens > 1:
for i in range(lens-1):
cur = self.sections[i]
next = self.sections[(i+1)%lens]
dist = distance(cur, next)
fontsize = 9*s#*(max(1, min(1.5, 100/dist)))
midPt = (cur[0]+next[0])*.5, (cur[1]+next[1])*.5
if self.g.pointInside(midPt):
fillText = 0
fillDisc = 1
else:
fillText = 1
fillDisc = 0
save()
fill(1, 0, 1)
stroke(None)
oval(cur[0]-r, cur[1]-r, 2*r, 2*r)
restore()
txt = str(int(dist))
stroke(None)
fill(fillDisc, fillDisc, fillDisc, .8)
oval(midPt[0]-fontsize*1.5, midPt[1]-fontsize*1.35, 2*fontsize*1.5, 2*fontsize*1.5)
font('Menlo-Bold', fontsize)
fill(fillText)
text(txt, midPt[0]-fontsize*.3*len(txt), midPt[1]-fontsize*.5)
save()
fill(1, 0, 1)
stroke(None)
oval(next[0]-r, next[1]-r, 2*r, 2*r)
restore()
restore()
def mouseMoved(self, info):
if not self.activDraw: return
if self.keydidUp == 1: return
self.mousePt = (info['point'].x, info['point'].y)
self.pen = ClosestPointPen(self.mousePt, self.g.getParent())
self.g.draw(self.pen)
self.closest, self.ortho = self.pen.getClosestData()
UpdateCurrentGlyphView()
def normalize(self, a):
l = self.lengtha(a)
return(a[0]/l, a[1]/l)
def lengtha(self, a):
return(sqrt(a[0]*a[0]+a[1]*a[1]))
class ClosestPointPen(BasePen):
def __init__(self, point, font):
BasePen.__init__(self, glyphSet=font)
self.currentPt = None
self.firstPt = None
self.orthoPt = None
self.approximateSegmentLength = 1
self.point = point
self.bestDistance = 100000
def _moveTo(self, pt):
self.currentPt = pt
self.firstPt = pt
def _lineTo(self, pt):
if pt == self.currentPt:
return
d = distance(self.currentPt, pt)
maxSteps = int(round(d / self.approximateSegmentLength))
if maxSteps < 1:
self.currentPt = pt
return
step = 1.0 / maxSteps
for factor in range(1, maxSteps + 1):
pti = interpolatePoint(self.currentPt, pt, factor * step)
prev_pt = interpolatePoint(self.currentPt, pt, (factor-1) * step)
dist = distance(pti, self.point)
if self.bestDistance > dist:
self.bestDistance = dist
self.closest = pti
self.orthoPt = self.getOrtho(prev_pt, pti)
self.currentPt = pt
def _curveToOne(self, pt1, pt2, pt3):
falseCurve = (pt1 == self.currentPt) and (pt2 == pt3)
if falseCurve:
self._lineTo(pt3)
return
est = estimateCubicCurveLength(self.currentPt, pt1, pt2, pt3) / self.approximateSegmentLength
maxSteps = int(round(est))
if maxSteps < 1:
self.currentPt = pt3
return
step = 1.0 / maxSteps
for factor in range(1, maxSteps + 1):
pt = getCubicPoint(factor * step, self.currentPt, pt1, pt2, pt3)
prev_pt = getCubicPoint((factor-1) * step, self.currentPt, pt1, pt2, pt3)
dist = distance(pt, self.point)
if self.bestDistance > dist:
self.bestDistance = dist
self.closest = pt
self.orthoPt = self.getOrtho(prev_pt, pt)
self.currentPt = pt3
def _qCurveToOne(self, pt1, pt2):
falseCurve = (pt1 == self.currentPt) or (pt1 == pt2)
if falseCurve:
self._lineTo(pt2)
return
est = calcQuadraticArcLength(self.currentPt, pt1, pt2) / self.approximateSegmentLength
maxSteps = int(round(est))
if maxSteps < 1:
self.currentPt = pt2
return
step = 1.0 / maxSteps
for factor in range(1, maxSteps + 1):
pt = getQuadraticPoint(factor * step, self.currentPt, pt1, pt2)
prev_pt = getQuadraticPoint((factor-1) * step, self.currentPt, pt1, pt2)
dist = distance(pt, self.point)
if self.bestDistance > dist:
self.bestDistance = dist
self.closest = pt
self.orthoPt = self.getOrtho(prev_pt, pt)
self.currentPt = pt2
def _closePath(self):
self.lineTo(self.firstPt)
self.currentPt = None
def _endPath(self):
self.currentPt = None
def getClosestData(self):
return(self.closest, self.orthoPt)
def getOrtho(self, p1, p2):
dx1 = p2[0] - p1[0]
dy1 = p2[1] - p1[1]
return(-dy1, dx1)
TriggerButton()