/
lockLayerWidths.py
382 lines (300 loc) · 11 KB
/
lockLayerWidths.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
from vanilla import *
from defconAppKit.windows.baseWindow import BaseWindowController
import mojo.drawingTools as ctx
from mojo.UI import getDefault, UpdateCurrentGlyphView, CurrentGlyphWindow, CurrentSpaceCenter
from mojo.roboFont import OpenWindow
from mojo.events import addObserver, removeObserver
from mojo.canvas import CanvasGroup
class GlyphWidthObserver(BaseWindowController):
glyph = None
verbose = True
modes = ['all unlocked', 'all locked', 'glyph locks']
padding = 10
buttonHeight = 25
textHeight = 20
width = 123
height = textHeight * 9 + buttonHeight + padding * 7
key = 'lockLayerWidths'
def __init__(self):
self.w = FloatingWindow((self.width, self.height))
x = y = p = self.padding
self.w.modeLabel = TextBox(
(x, y, -p, self.textHeight),
'lock layer widths:',
sizeStyle='small')
y += self.textHeight
self.w.mode = RadioGroup(
(x, y, -p, self.textHeight * len(self.modes)),
self.modes,
sizeStyle='small',
callback=self.radioGroupCallback,
isVertical=True)
self.w.mode.set(0)
y += self.textHeight * len(self.modes) + p
self.w.separator1 = HorizontalLine((x, y, -p, 1))
y += p
self.w.lockGlyhsLabel = TextBox(
(x, y, -p, self.textHeight),
'set glyph locks:',
sizeStyle='small')
y += self.textHeight
self.w.currentGlyph = CheckBox(
(x, y, -p, self.textHeight),
'current glyph',
value=True,
sizeStyle='small')
self.w.currentGlyph.enable(False)
y += self.textHeight
self.w.selectedGlyphs = CheckBox(
(x, y, -p, self.textHeight),
'font selection',
value=True,
sizeStyle='small')
self.w.selectedGlyphs.enable(False)
y += self.textHeight
self.w.spaceCenter = CheckBox(
(x, y, -p, self.textHeight),
'space center',
value=True,
sizeStyle='small')
self.w.spaceCenter.enable(False)
buttonWidth = (self.width - p * 2) * 0.5
y += self.textHeight + p
self.w.lockGlyphsButton = SquareButton(
(x, y, buttonWidth, self.buttonHeight),
'lock',
callback=self.lockGlyphsCallback,
sizeStyle='small')
self.w.lockGlyphsButton.enable(False)
x += buttonWidth - 1
self.w.unlockGlyphsButton = SquareButton(
(x, y, buttonWidth, self.buttonHeight),
'unlock',
callback=self.unlockGlyphsCallback,
sizeStyle='small')
self.w.unlockGlyphsButton.enable(False)
x = p
y += self.buttonHeight + p + 2
self.w.separator2 = HorizontalLine((x, y, -p, 1))
y += p
self.w.verbose = CheckBox(
(x, y, -p, self.textHeight),
'verbose',
value=True,
sizeStyle='small')
self.setUpBaseWindowBehavior()
addObserver(self, "drawObserver", "draw")
self.w.open()
# ---------
# callbacks
# ---------
def lockGlyphsCallback(self, sender):
self._setLockUnlockStatus(True)
def unlockGlyphsCallback(self, sender):
self._setLockUnlockStatus(False)
def radioGroupCallback(self, sender):
mode = sender.get()
if mode == 0:
removeObserver(self, "currentGlyphChanged")
self.unsubscribeGlyph()
else:
addObserver(self, 'currentGlyphChangedObserver', 'currentGlyphChanged')
addObserver(self, 'spaceCenterDrawObserver', "spaceCenterDraw")
addObserver(self, 'drawCellObserver', 'glyphCellDrawBackground')
self.subscribeGlyph(CurrentGlyph())
if mode == 2:
self.w.lockGlyhsLabel.enable(True)
self.w.currentGlyph.enable(True)
self.w.selectedGlyphs.enable(True)
self.w.spaceCenter.enable(True)
self.w.lockGlyphsButton.enable(True)
self.w.unlockGlyphsButton.enable(True)
else:
self.w.lockGlyhsLabel.enable(False)
self.w.currentGlyph.enable(False)
self.w.selectedGlyphs.enable(False)
self.w.spaceCenter.enable(False)
self.w.lockGlyphsButton.enable(False)
self.w.unlockGlyphsButton.enable(False)
self._updateWindows()
def windowCloseCallback(self, sender):
self.unsubscribeGlyph()
removeObserver(self, 'currentGlyphChanged')
removeObserver(self, 'draw')
removeObserver(self, 'spaceCenterDraw')
removeObserver(self, 'glyphCellDrawBackground')
super(GlyphWidthObserver, self).windowCloseCallback(sender)
# ---------
# observers
# ---------
def currentGlyphChangedObserver(self, notification):
# observer for RF notification -> add defcon observer for glyph
self.subscribeGlyph(notification["glyph"])
def widthChangedObserver(self, notification):
mode = self.w.mode.get()
# observer for defcon notification -> copy width to other layers
glyph = notification.object
glyph = RGlyph(glyph)
width = notification.data['newValue']
# mode 0 : OFF
if mode == 0:
return
# mode 1 : only marked glyphs
elif mode == 2:
f = glyph.font
if f is None:
return
if self.key not in f.lib:
return
if glyph.name not in f.lib[self.key]:
return
# mode 2 : all glyphs
elif mode == 1:
pass
# copy glyph width to other layers
for layerName in glyph.font.layerOrder:
if layerName == glyph.layer.name:
continue
g = glyph.getLayer(layerName)
if self.w.verbose.get():
print('copying width from %s.%s to %s.%s...' % (glyph.name, glyph.layer.name, glyph.name, layerName))
g.width = width
if self.w.verbose.get():
print()
def drawObserver(self, notification):
glyph = notification['glyph']
scale = notification['scale']
lockStatus = self.getLockStatus(glyph)
if not lockStatus: # is None:
return
lockStatusIcon = '🔒' if lockStatus else '🔓'
ctx.save()
ctx.fontSize(24 * scale)
ctx.textBox(lockStatusIcon, (0, -100, glyph.width, 24 * scale), align='right')
ctx.restore()
def spaceCenterDrawObserver(self, notification):
glyph = notification['glyph']
scale = notification['scale']
lockStatus = self.getLockStatus(glyph)
if not lockStatus: # is None:
return
lockStatusIcon = '🔒' if lockStatus else '🔓' # 'L' if lockStatus else 'U'
ctx.save()
ctx.scale(1, -1)
ctx.fontSize(120)
ctx.text(lockStatusIcon, (glyph.width - 100, 30))
ctx.restore()
def drawCellObserver(self, notification):
# print(notification.keys())
# print(notification["glyphCell"])
# print(notification["cell"])
glyph = notification["glyph"]
if self.w.mode.get() == 0:
return
lockStatus = self.getLockStatus(glyph)
if not lockStatus: # is None:
return
lockStatusIcon = '🔒' if lockStatus else '🔓'
ctx.save()
ctx.fontSize(16)
ctx.text(lockStatusIcon, (0, 0))
ctx.restore()
# -------
# methods
# -------
def _updateWindows(self):
# update glyph window
UpdateCurrentGlyphView()
# update font overview
f = CurrentFont()
if f is not None:
f.changed()
# update space center
s = CurrentSpaceCenter()
if s:
i = s.glyphLineView.getSelection()
s.updateGlyphLineView()
if i is not None:
s.glyphLineView.setSelection(i)
def _setLockUnlockStatus(self, value):
self.setLockStatus(value)
self._updateWindows()
def getGlyphs(self):
glyphs = []
# font window selection
if self.w.selectedGlyphs.get():
font = CurrentFont()
if font is not None:
glyphs += font.selectedGlyphs
# current glyph window
if self.w.currentGlyph.get():
glyphWindow = CurrentGlyphWindow()
if glyphWindow is not None:
g = glyphWindow.getGlyph()
if g is not None and g not in glyphs:
glyphs += [RGlyph(g)]
# space center selection
if self.w.spaceCenter.get():
spaceCenter = CurrentSpaceCenter(currentFontOnly=True)
if spaceCenter is not None:
g = spaceCenter.glyphLineView.getSelected()
if g is not None and g not in glyphs:
glyphs += [RGlyph(g)]
return glyphs
def getLockStatus(self, glyph):
mode = self.w.mode.get()
if mode == 1:
lockStatus = True
elif mode == 2:
if self.key not in glyph.font.lib:
lockStatus = False
else:
lockStatus = True if glyph.name in glyph.font.lib[self.key] else False
else: # mode == 0:
lockStatus = None
return lockStatus
def subscribeGlyph(self, glyph):
# subscribe to defcon notification
self.unsubscribeGlyph()
if glyph is not None:
self.glyph = glyph
self.glyph.addObserver(self, "widthChangedObserver", "Glyph.WidthChanged")
def unsubscribeGlyph(self):
# unsubscribe from defcon notification
if self.glyph is not None:
self.glyph.removeObserver(self, "Glyph.WidthChanged")
self.glyph = None
def setLockStatus(self, value):
verbose = self.w.verbose.get()
font = CurrentFont()
if not font:
return
# ----------
# get glyphs
# ----------
glyphs = self.getGlyphs()
if not len(glyphs):
return
# ---------------
# set lock status
# ---------------
if not self.key in font.lib:
font.lib[self.key] = []
for glyph in glyphs:
if value:
if verbose:
print('locking layer widths (%s)...' % glyph.name)
if glyph.name not in font.lib[self.key]:
font.lib[self.key].append(glyph.name)
else:
if verbose:
print('unlocking layer widths (%s)...' % glyph.name)
if glyph.name in font.lib[self.key]:
font.lib[self.key].remove(glyph.name)
glyph.changed()
if len(glyphs) and verbose:
print()
# -------
# testing
# -------
OpenWindow(GlyphWidthObserver)