-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawer.py
731 lines (614 loc) · 24.4 KB
/
Drawer.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
# coding=utf-8
"""
- load glyphContruction ass dict:
- compo : accents, anchors
- accent: base, anchors
TODO:
+ updte for the new list sructure
"""
# from _glyphConstructionCategories import *
from glyphConstruction import ParseGlyphConstructionListFromString, GlyphConstructionBuilder
from AppKit import NSColor
from vanilla import *
from defconAppKit.windows.baseWindow import BaseWindowController
from mojo.events import addObserver, removeObserver
from mojo.drawingTools import *
from mojo.extensions import getExtensionDefault, setExtensionDefault
from mojo.UI import *
from lib.tools.defaults import getDefault, setDefault
from lib.tools.notifications import PostNotification
from mojo.roboFont import CurrentFont, CurrentGlyph
from fontTools.pens.areaPen import AreaPen
import os
import imp
def loadConstuctions():
if 'nl.hallotype.glyphConstructions' in CurrentFont().lib:
constructions = CurrentFont().lib['nl.hallotype.glyphConstructions']
else:
constructions = []
# reloadConstructions()
# constructions = CurrentFont().lib['nl.hallotype.glyphConstructions']
return list(constructions)
def getLowerConstructions(constructions):
extraConstructions = []
compos = []
for i in constructions:
#print(type(i))
compos.append(i['compo'])
for i in constructions:
if i['base'][0] == i['base'][0].upper():
if i['compo'].lower() not in compos:
j = dict(i)
j['base'] = j['base'].lower()
j['compo'] = j['compo'].lower()
extraConstructions.append(j)
return extraConstructions
# def reloadConstructions():
# path = "/Users/thom/Library/Application Support/RoboFont/scripts/*accent/_glyphContructionsRF3.py"
# bc = imp.load_source('bc', path)
# asList = ParseGlyphConstructionListFromString(bc.all)
# asDict = []
# for i in asList:
# if i.find("+") != i.rfind("+"):
# continue
# compo = i.split("=")[0][:-1]
# base = i.split("=")[1][1:].split("+")[0].strip()
# accent = i.split("=")[1][1:].split("+")[1].strip().split("@")[0]
# anchor = i.split("=")[1][1:].split("+")[1].strip().split("@")[1]
# # for now only one accent@anchor pair!
# construction = i.split("+")[1].strip()
# asDict.append(dict(
# active=True,
# compo=compo,
# base=base,
# accent=accent,
# anchor=anchor,
# construction=construction,
# ))
# f.lib['nl.hallotype.glyphConstructions'] = asDict
def currentGlyphIsCap(g):
return g.name[0] == g.name[0].upper()
def isAccent(glyphName):
if CurrentFont()[glyphName].anchors:
return CurrentFont()[glyphName].anchors[0].name[0] == "_"
alles = ""
title = "Diacritics" # Keep them unique!
event = "draw"
turnOffItems = ['Guides']
turnOnItems = ['Anchors']
settingsWindow = "%sSettingsKey" % title
defaultKey = "nl.hallotype.diacritics"
# dont edit
selfKey = "%sKey" % title
wasOn = getExtensionDefault(selfKey)
pijlen = {
'top': u'\u2191',
'bottom': chr(8595),
'below': chr(8615),
'right': u"r",
'middle': u"m",
'bar': u"–",
'ogonek': u'˛',
'cedilla': u'¸',
'horn': u'h',
'ring': u'˚',
'fallbackAnchor': '?'
}
anchorsNames = {
u'\u2191': 'top',
chr(8595): 'bottom',
chr(8615): 'below',
u"r": 'right',
u"m": 'middle',
u"–": 'bar',
u'o': 'ogonek',
u'c': 'cedilla',
u'h': 'horn',
u'º': 'ring'
}
def makeLookupDicts(myset):
b = {}
a = {}
for construction in myset:
compo = construction['compo']
base = construction['base']
accent = construction['accent']
anchor = construction['anchor']
constr = {'accent': accent, 'anchor': anchor}
b_constr = {'base': base, 'anchor': anchor}
if base not in b:
b[base] = {}
b[base]['accents'] = [accent]
b[base]['anchors'] = [anchor]
b[base]['compos'] = [compo]
b[base]['constructions'] = [constr]
if accent not in b[base]['accents']:
b[base]['accents'].append(accent)
if anchor not in b[base]['anchors']:
b[base]['anchors'].append(anchor)
if compo not in b[base]['compos']:
b[base]['compos'].append(compo)
if constr not in b[base]['constructions']:
b[base]['constructions'].append(constr)
###
if accent not in a:
a[accent] = {}
a[accent]['bases'] = [base]
a[accent]['anchors'] = [anchor]
a[accent]['compos'] = [compo]
a[accent]['constructions'] = [b_constr]
if base not in a[accent]['bases']:
a[accent]['bases'].append(base)
if anchor not in a[accent]['anchors']:
a[accent]['anchors'].append(anchor)
if compo not in a[accent]['compos']:
a[accent]['compos'].append(compo)
if b_constr not in a[accent]['constructions']:
a[accent]['constructions'].append(b_constr)
return b, a
def anchorInGlyph(anchor, glyph):
for a in glyph.anchors:
if a.name == anchor:
return (True, a.y)
if a.name == "_"+anchor:
return (True, a.y)
return (False, None)
# dont change name of class
class ThisObserver(BaseWindowController):
def __init__(self, active=bool):
self.active = active
self.baseGlyph = None
self.accent = None
# self.anchor = None
self.accentPos = None
# self.composite = None
if getExtensionDefault(selfKey):
if self.active == True:
return
if self.active == False:
pass
if self.active == True:
self._activate()
self._turnOff()
self._turnOn()
if self.active == False:
self._end()
# dont change names of functions
# dont edit _functions!
# but you can add functions if needed
def _activate(self):
setExtensionDefault(selfKey, self)
addObserver(self, "mainFunction", event)
addObserver(self, "glyphChange", "currentGlyphChanged")
addObserver(self, "drawPreviewAccents", "drawPreview")
addObserver(self, "warn", "draw")
# addObserver(self, "testOverlap", "drawBackground")
self.reloadConstructions()
self.getSuffixGlyphsDict()
def windowCloseCallback(self, sender):
self._end()
def _end(self):
removeObserver(getExtensionDefault(selfKey), event)
removeObserver(getExtensionDefault(selfKey), "currentGlyphChanged")
removeObserver(getExtensionDefault(selfKey), "drawPreview")
removeObserver(getExtensionDefault(selfKey), "draw")
# removeObserver(getExtensionDefault(selfKey), 'drawBackground')
setExtensionDefault(selfKey, None)
# this is optional
if wasOn:
self._restoreGlyphViewItems()
# kill settings window
self.settingsWindow(onOff=False)
def mainFunction(self, info):
if not getExtensionDefault(settingsWindow):
self.settingsWindow(onOff=True)
self.w = getExtensionDefault(settingsWindow)
self.drawAccents(info)
self.warn(info)
def settingsWindow(self, onOff=bool):
if onOff == True:
self.w = FloatingWindow((1443.0, 108.0, 160.0, 72.0),
title, closable=True)
setExtensionDefault(settingsWindow, self.w)
# print getExtensionDefault(settingsWindow)
columnDescriptions = [
dict(title="", key="checkBox",
cell=CheckBoxListCell(), width=15),
dict(title="accent", editable=False),
dict(title="anchor", width=50, editable=False),
]
# self.w.latin_supp = SquareButton(
# (0, 0, 40, 17), "supp", sizeStyle='mini', callback=self.checkOnUnicode)
# self.w.latin_A = SquareButton(
# (40, 0, 40, 17), "ex a", sizeStyle='mini', callback=self.checkOnUnicode)
# self.w.latin_B = SquareButton(
# (80, 0, 40, 17), "ex b", sizeStyle='mini', callback=self.checkOnUnicode)
# self.w.latin_add = SquareButton(
# (120, 0, 40, 17), "add", sizeStyle='mini', callback=self.checkOnUnicode)
self.w.allOn = SquareButton(
(0, 0, 53, 17), "all on", callback=self.allOn, sizeStyle='mini')
self.w.allOff = SquareButton(
(53, 0, 53, 17), "all off", callback=self.allOff, sizeStyle='mini')
self.w.reload = SquareButton(
(106, 0, -0, 17), "reload", callback=self.reloadConstructions, sizeStyle='mini')
self.w.list = List((0, 17, -0, -20),
items=[],
columnDescriptions=columnDescriptions,
editCallback=self.listEditCallback,
doubleClickCallback=self.goToAccent,
)
self.w.addMissingAnchorsButton = SquareButton((0,-40,-0,20), 'add missing anchors', callback=self.addMissingAnchors, sizeStyle='mini')
self.w.addMissingAnchorsButton.show(False)
self.w.compile = SquareButton(
(0, -20, -0, -0), 'compile these glyphs', callback=self.compileGlyphs, sizeStyle='small')
self.setUpBaseWindowBehavior()
self.w.open()
self.buildAccentList()
if onOff == False:
try:
getExtensionDefault(settingsWindow).hide()
setExtensionDefault(settingsWindow, None)
except:
pass
def addMissingAnchors(self, sender):
g = glyph = CurrentGlyph()
glyphName = g.name
if ".sc" in glyphName:
glyphName = glyphName[:-3]
glyphAnchors = list()
for anchor in glyph.anchors:
glyphAnchors.append(anchor.name)
missing = list(set(self.baseDict[glyphName]['anchors']).difference(set(glyphAnchors)))
l = g.layerName
if "FullHeight" in l:
height = CurrentFont().info.capHeight
elif ".sc" in g.name:
height = CurrentFont().info.xHeight
elif g.name[0] == g.name[0].upper():
height = CurrentFont().info.capHeight
else:
height = CurrentFont().info.xHeight
for a in missing:
if a == 'top':g.appendAnchor("top", (g.width/2,height))
elif a == 'bottom':g.appendAnchor("bottom", (g.width/2,-20))
elif a == 'ogonek':g.appendAnchor("ogonek", (4*(g.width/5),0))
elif a == 'cedilla':g.appendAnchor("cedilla", (g.width/2,0))
else: g.appendAnchor(a, (20,height))
def reloadConstructions(self, sender=None):
self.construction = loadConstuctions()
self.construction += getLowerConstructions(self.construction)
self.baseDict, self.accentDict = makeLookupDicts(self.construction)
# self.sd = makeSuperDict()
self.buildAccentList()
def goToAccent(self, sender):
currentList = sender.get()
selection = sender.getSelection()[0]
accent = currentList[selection]['accent']
SetCurrentGlyphByName(accent)
def getGlyphName(self, glyph):
smallCaps = False
caps = False
suffix = ""
glyphName = glyph.name
if glyphName == glyph.name.upper():
caps = True
if ".sc" in glyph.name:
glyphName = glyph.name.split(".")[0].upper()
smallCaps = True
if ".alt" in glyph.name:
if smallCaps == False:
glyphName = glyph.name.split(".")[0]
suffix = ".%s" % glyph.name.split(".")[1]
else:
suffix += ".%s" % glyph.name.split(".")[2]
return (glyphName, smallCaps, caps, suffix)
def warn(self, info):
glyph = info['glyph']
glyphName = glyph.name
if "." in glyphName:
glyphName = glyphName.split(".")[0]
if glyphName not in self.baseDict.keys():
return
# print(glyphName)
glyphAnchors = list()
for anchor in glyph.anchors:
glyphAnchors.append(anchor.name)
missing = list(
set(self.baseDict[glyphName]['anchors']).difference(set(glyphAnchors)))
self.w.addMissingAnchorsButton.show(False)
if missing:
self.w.addMissingAnchorsButton.show(True)
save()
font("Consolas")
fill(1, 0, 0, .8)
stroke(None)
fontSize(40)
y = 50
for a in missing:
text("missing anchor: %s" % a, (glyph.width+50, y))
y += 36
restore()
def compileGlyphs(self, sender):
"""
need to rewrite for RF3
"""
font = CurrentFont()
g = CurrentGlyph()
base = g.name
l = self.w.list.get()
for i in l:
if i['checkBox']:
accent = i['construction']['accent']
anchor = i['construction']['anchor']
if ".sc" in base:
compo = base[:-3]+accent + ".sc"
else:
compo = base+accent
if base not in font.keys():
continue
if accent not in font.keys():
continue
inBase, height = anchorInGlyph(anchor, font[base])
if inBase and height > 600:
if accent+".cap" in font.keys():
accent = accent+".cap"
inAccent, height = anchorInGlyph(anchor, font[accent])
construction = "%s = %s + %s@%s" % (compo,
base, accent, anchor)
if not inBase:
continue
if not inAccent:
continue
self.compileGlyph(construction)
def compileGlyph(self, construction):
font = CurrentFont()
constructionGlyph = GlyphConstructionBuilder(construction, font)
glyph = font.newGlyph(constructionGlyph.name, clear=True)
constructionGlyph.draw(glyph.getPen())
glyph.name = constructionGlyph.name
glyph.unicode = constructionGlyph.unicode
glyph.width = constructionGlyph.width
glyph.markColor = 1, 1, 0, 0.5
if glyph.unicode is None:
glyph.autoUnicodes()
def allOn(self, sender):
l = list(self.w.list.get())
for i in l:
i['checkBox'] = True
self.w.list.set(l)
def allOff(self, sender):
l = list(self.w.list.get())
for i in l:
i['checkBox'] = False
self.w.list.set(l)
def checkSupp(self, sender):
pass
def checkOnUnicode(self, sender):
if sender == self.w.latin_supp:
myset = Latin_Supp_block
if sender == self.w.latin_A:
myset = Latin_Ext_A_block
if sender == self.w.latin_B:
myset = Latin_Ext_B_block
if sender == self.w.latin_add:
myset = Latin_Ext_A_block
myset = Latin_Ext_additional_block
g = CurrentGlyph()
defaults = getExtensionDefault(defaultKey, dict())
# print Latin_Supp_block
# print
# print defaults
# print
for glyphName in myset:
if glyphName == g.name:
# g.name
for anchorName in myset[glyphName]:
# top
for accent in myset[glyphName][anchorName]:
# grave
for listItem in self.w.list.get():
for pijl in pijlen.items():
if listItem["anchor"] == pijl[1]:
anchor = pijl[0]
if anchor == anchorName and accent == listItem['accent']:
listItem['checkBox'] = True
def listEditCallback(self, sender):
# save to defaults
#defaults = getExtensionDefault(defaultKey, dict())
# print defaults
for item in sender:
# anchor position
for pijl in pijlen.items():
if item["anchor"] == pijl[1]:
anchor = pijl[0]
##
key = defaultKey+"."+item["accent"]+"@"+anchor
# print(key)
value = item["checkBox"]
setExtensionDefault(key, value)
UpdateCurrentGlyphView()
def getSuffixGlyphsDict(self):
sgd = {}
for gn in CurrentFont().keys():
if "." in gn:
if gn.split(".")[0] in CurrentFont().keys():
m = gn.split(".")[0]
if m not in sgd.keys():
sgd[m] = [gn]
else:
sgd[m].append(gn)
self.sgd = sgd
def buildAccentList(self, sender=None):
self.w = getExtensionDefault(settingsWindow)
if not self.w:
return
font = CurrentFont()
glyph = CurrentGlyph()
self.w.addMissingAnchorsButton.show(False)
if glyph is None:
self.w.setTitle("---")
self.w.list.set([])
return
glyphName = glyph.name
isCap = currentGlyphIsCap(glyph)
self.w.setTitle(glyphName)
if "." in glyphName:
glyphName = glyphName.split(".")[0]
theAccentsList = []
# defaults = getExtensionDefault(defaultKey+".check", dict())
if glyphName in self.baseDict:
for accent in self.baseDict[glyphName]['accents']:
for c in self.baseDict[glyphName]['constructions']:
# print(c)
if c['accent'] == accent:
position = c['anchor']
thisConstructon = c
if position not in pijlen.keys():
pijlen[position] = position
theAccentsList.append(
dict(
anchor=pijlen[position],
accent=accent,
checkBox=getExtensionDefault(
defaultKey+"."+accent+"@"+position, True),
construction=thisConstructon,
),
)
if glyphName in self.accentDict:
for base in self.accentDict[glyphName]['bases']:
for c in self.accentDict[glyphName]['constructions']:
# print(c)
if c['base'] == base:
position = c['anchor']
# key = defaultKey+"."+item["accent"]+"@"+anchor
theAccentsList.append(
dict(
anchor=pijlen[position],
accent=base,
checkBox=getExtensionDefault(
defaultKey+"."+base+"@"+position, True),
construction=self.accentDict[glyphName],
),
)
if base in self.sgd:
for also in self.sgd[base]:
theAccentsList.append(
dict(
anchor=pijlen[position],
accent=also,
checkBox=getExtensionDefault(
defaultKey+"."+also+"@"+position, True),
construction=self.accentDict[glyphName],
),
)
self.w.list.set(theAccentsList)
# print len(items)
self.w.resize(160, 17+17+17+17+(19*len(theAccentsList))+21)
# notifications
def glyphChange(self, info):
self.buildAccentList()
def drawAccents(self, info, preview=False):
glyph = info["glyph"]
font = glyph.getParent()
layer = glyph.layerName
isAccent = False
glyphName = glyph.name
if "." in glyphName:
glyphName = glyphName.split(".")[0]
if glyphName in self.accentDict.keys():
isAccent = True
# self.baseGlyph = glyph
# self.accent = None
# caps = self.getGlyphName(glyph)[2]
# #print caps
items = self.w.list.get()
# save()
# baseGlyphAnchors = {}
# for a in glyph.anchors:
# baseGlyphAnchors[a.name] = [a.x, a.y]
for item in items:
if not item["checkBox"]:
continue
accent = item['accent']
# if caps:
# try:
# #print accent
# if accent+".cap" in font:
# accent = accent + ".cap"
# except:
# pass
for pijl in pijlen.items():
if item["anchor"] == pijl[1]:
anchor = pijl[0]
if accent in font:
for baseGlyphAnchor in glyph.getLayer(layer).anchors:
# test hier voor cap hoogte
if baseGlyphAnchor.y > 600 and anchor == baseGlyphAnchor.name:
try:
if accent+".cap" in font:
accent = accent + ".cap"
except:
pass
for accentAnchor in font[accent].getLayer(layer).anchors:
# zoek hier op welk anchor je moet hebben
if accentAnchor.name[1:] == anchor:
if accentAnchor.name[1:] == baseGlyphAnchor.name:
save()
# # do the transform here
x = baseGlyphAnchor.x - accentAnchor.x
y = baseGlyphAnchor.y - accentAnchor.y
# # print(baseGlyphAnchor)
self.accentPos = (x, y)
translate(x, y)
fill(0.34, 0.71, 0.14, .7)
if preview:
fill(0)
stroke(None)
drawGlyph(font[accent].getLayer(layer))
self.accent = font[accent]
restore()
if isAccent:
if accentAnchor.name == anchor:
if accentAnchor.name == baseGlyphAnchor.name[1:]:
save()
# # do the transform here
x = baseGlyphAnchor.x - accentAnchor.x
y = baseGlyphAnchor.y - accentAnchor.y
# # print(baseGlyphAnchor)
self.accentPos = (x, y)
translate(x, y)
fill(0.34, 0.71, 0.14, .7)
if preview:
fill(0)
stroke(None)
drawGlyph(font[accent].getLayer(layer))
self.accent = font[accent]
restore()
def drawPreviewAccents(self, info):
self.drawAccents(info, True)
def _turnOn(self):
pref_as_is = getDefault('glyphZoomViewShowItems')
pref_new = dict()
for i in pref_as_is:
if i in turnOnItems:
pref_new[i] = 1
else:
pref_new[i] = pref_as_is[i]
setDefault('glyphZoomViewShowItems', pref_new)
preferencesChanged()
def _turnOff(self):
pref_as_is = getDefault('glyphZoomViewShowItems')
setExtensionDefault('restoreGlyphZoomViewShowItems', pref_as_is)
pref_new = dict()
for i in pref_as_is:
if i in turnOffItems:
pref_new[i] = 0
else:
pref_new[i] = pref_as_is[i]
setDefault('glyphZoomViewShowItems', pref_new)
preferencesChanged()
def _restoreGlyphViewItems(self):
setDefault('glyphZoomViewShowItems',
getExtensionDefault('restoreGlyphZoomViewShowItems'))
preferencesChanged()
ThisObserver(active=True)