/
glyphConstructionController.py
872 lines (684 loc) · 30.6 KB
/
glyphConstructionController.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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
from fontTools.misc.py23 import *
from AppKit import *
import re
import weakref
from vanilla import *
from vanilla.dialogs import getFile
from defconAppKit.windows.baseWindow import BaseWindowController
from mojo.roboFont import version as RoboFontVersion
from mojo.events import addObserver, removeObserver
from mojo.drawingTools import *
from mojo.UI import MultiLineView
from mojo.glyphPreview import GlyphPreview
from mojo.extensions import getExtensionDefault, setExtensionDefault, getExtensionDefaultColor, setExtensionDefaultColor
from lib.UI.splitter import Splitter
from lib.UI.enterTextEditor import EnterTextEditor
from lib.tools.misc import NSColorToRgba, rgbaToNSColor
from lib.tools.glyphList import GN2UV
from lib.UI.statusBar import StatusBar
# debug only
# import glyphConstruction
# import importlib
# importlib.reload(glyphConstruction)
from glyphConstruction import GlyphConstructionBuilder, ParseGlyphConstructionListFromString, GlyphBuilderError, ParseVariables
from glyphConstructionLexer import GlyphConstructionLexer
from lib.scripting.codeEditor import CodeEditor
import os
defaultKey = "com.typemytype.glyphBuilder"
constructions = """# capitals
Agrave = A + grave.cap@center,top
Aacute = A + acute.cap@center,top
Acircumflex = A + circumflex.cap@center,top
Atilde = A + tilde.cap@center,top
Adieresis = A + dieresis.cap@center,top
Aring = A + ring.cap@center,top
Ccedilla = C + cedilla.cap@center, bottom
Egrave = E + grave.cap@center, top
Eacute = E + acute.cap@center, top
Ecircumflex = E + circumflex.cap@center, top
Edieresis = E + dieresis.cap@center, top
Igrave = I + grave.cap@center, top
Iacute = I + acute.cap@center, top
Icircumflex = I + circumflex.cap@center, top
Idieresis = I + dieresis.cap@center, top
Ntilde = N + tilde@center,top
Ograve = O + grave.cap@center, top
Oacute = O + acute.cap@center, top
Ocircumflex = O + circumflex.cap@center, top
Otilde = O + tilde.cap@center, top
Odieresis = O + dieresis.cap@center, top
Oslash = O + slash@center,center
Scaron = S + caron.cap@center, top
Ugrave = U + grave.cap@center, top
Uacute = U + acute.cap@center, top
Ucircumflex = U + circumflex.cap@center, top
Udieresis = U + dieresis.cap@center, top
Zcaron = Z + caron.cap@center, top
# capitals ligatures
F_L = F & L
F_I = F & I
AE = A & E@75%,origin
OE = O & E@75%,origin
# lowercase
agrave = a + grave@center,top
aacute = a + acute@center,top
acircumflex = a + circumflex@center,top
atilde = a + tilde@center,top
adieresis = a + dieresis@center,top
aring = a + ring@center,top
amacron = a + macron@center,top
abreve = a + breve@center,top
aogonek = a + ogonek@innerRight,bottom
aringacute = a + ring@center,top + acute@center,110%
ccedilla =c + cedilla@center,bottom
egrave = e + grave@center,top
eacute = e + acute@center,top
ecircumflex = e + circumflex@center,top
edieresis = e + dieresis@center,top
emacron = e + macron@center,top
ebreve = e + breve@center,top
edotaccent = e + dotaccent@center,top
eogonek = e + ogonek@center,bottom
ecaron = e + caron@center,top
igrave = dotlessi + grave@center,top
iacute = dotlessi + acute@center,top
icircumflex = dotlessi + circumflex@center,top
idieresis = dotlessi + dieresis@center,top
ograve = o + grave@center,top
oacute = o + acute@center,top
otilde = o + tilde@center,top
odieresis = o + dieresis@center,top
ohungarumlaut = o + hungarumlaut@center,top
oslash = o + slash@center,center
scaron = s + caron@center,top
yacute = y + acute@center,top
ydieresis = y + dieresis@center,top
zcaron = z + caron@center,top
# lowercase ligatures
fi = f & i
fl = f & l
f_f_i = f & f & i
f_f_l = f & f & l
ae = a & e@80%,orgin
oe = o & e@80%,orgin
# fractions
onequarter = fraction@110%,origin + one.superior@innerLeft,innerTop + four.inferior@ fraction:innerRight,fraction:innerBottom
onehalf = fraction@110%,origin + one.superior@innerLeft,innerTop + two.inferior@ fraction:innerRight,fraction:innerBottom
threequarters = fraction@110%,origin + three.superior@innerLeft,innerTop + four.inferior@ fraction:innerRight,fraction:innerBottom
percent = fraction@110%,origin + zero.superior@innerLeft,innerTop + zero.inferior@ fraction:innerRight,fraction:innerBottom
perthousand = fraction@110%,origin + zero.superior@innerLeft,innerTop + zero.inferior@ fraction:innerRight,fraction:innerBottom & zero.inferior@fraction:right,fraction:innerBottom
# some test cases
L_aringacute=L & a+ring@center,top+acute@center,top
"""
constructions = ""
overWriteRE = re.compile(
r"^\s*" # space before, not required
r"#" # command symbol is required
r"\s*" # space before, not required
r"OverwriteExistingGlyphs" # OverwriteExistingGlyphs, required, and ignore case
r"\s*" # space before, not required
r":" # : is required
r"\s*" # space before, not required
r"(True|False)" # capture True, False
r"\s*" # space after, not required
r"$", # end of line
re.IGNORECASE | re.MULTILINE
)
autoUnicodesRE = re.compile(
r"^\s*" # space before, not required
r"#" # command symbol is required
r"\s*" # space before, not required
r"AutoUnicodes" # AutoUnicodes, required, and ignore case
r"\s*" # space before, not required
r":" # : is required
r"\s*" # space before, not required
r"(True|False)" # capture True, False
r"\s*" # space after, not required
r"$", # end of line
re.IGNORECASE | re.MULTILINE
)
markGlyphRE = re.compile(
r"^\s*" # space before, not required
r"#" # command symbol is required
r"\s*" # space before, not required
r"MarkGlyphs" # MarkGlyphs, required, and ignore case
r"\s*" # space before, not required
r":" # : is required
r"\s*" # space before, not required
r"([-+]?\d*\.\d+|\d+)" # a float number
r"\s*" # space before, not required
r"," # comma
r"\s*" # space before, not required
r"([-+]?\d*\.\d+|\d+)" # a float number
r"\s*" # space before, not required
r"," # comma
r"\s*" # space before, not required
r"([-+]?\d*\.\d+|\d+)" # a float number
r"\s*" # space before, not required
r"," # comma
r"\s*" # space before, not required
r"([-+]?\d*\.\d+|\d+)" # a float number
r"\s*" # space after, not required
r"$", # end of line
re.IGNORECASE | re.MULTILINE
)
dontMarkGlyphRE = re.compile(
r"^\s*" # space before, not required
r"#" # command symbol is required
r"\s*" # space before, not required
r"MarkGlyphs" # MarkGlyphs, required, and ignore case
r"\s*" # space before, not required
r":" # : is required
r"\s*" # space before, not required
r"(False)" # capture True, False
r"\s*" # space after, not required
r"$", # end of line
re.IGNORECASE | re.MULTILINE
)
class GlyphConstructorFont(object):
def __init__(self, font):
self.font = font
self.glyphsDone = {}
def __getattr__(self, attr):
return getattr(self.font, attr)
def __getitem__(self, glyphName):
if glyphName in self.glyphsDone:
return self.glyphsDone[glyphName]
return self.font[glyphName]
def __contains__(self, glyphName):
if glyphName in self.glyphsDone:
return True
return glyphName in self.font
def __len__(self):
return len(self.keys())
def keys(self):
return set(list(self.font.keys()) + list(self.glyphsDone.keys()))
def __iter__(self):
names = self.keys()
while names:
name = names[0]
yield self[name]
names = names[1:]
class AnalyserTextEditor(EnterTextEditor):
def __init__(self, *args, **kwargs):
super(AnalyserTextEditor, self).__init__(*args, **kwargs)
self.getNSScrollView().setBorderType_(NSNoBorder)
try:
self.getNSTextView().setUsesFindBar_(True)
except Exception:
self.getNSTextView().setUsesFindPanel_(True)
# basicAttrs = getBasicTextAttributes()
font = NSFont.fontWithName_size_("Menlo", 10)
# self.getNSTextView().setTypingAttributes_(basicAttrs)
self.getNSTextView().setFont_(font)
def _stringDict(d, verb):
out = []
for key in sorted(d.keys()):
value = d[key]
out.append("\tGlyph %s %s %s" % (key, verb, ", ".join(value)))
return "\n".join(out)
def analyseConstructions(font, constructionGlyphs):
missingGlyphs = []
notMissingGlyphs = []
missingComponents = {}
unusedComponents = {}
doubleEntries = []
done = []
for construction in constructionGlyphs:
if construction.name is None:
continue
if construction.name not in font:
missingGlyphs.append(construction.name)
continue
notMissingGlyphs.append(construction.name)
if construction.name in done:
doubleEntries.append(construction.name)
done.append(construction.name)
glyph = font[construction.name]
glyphComponentNames = [component.baseGlyph for component in glyph.components]
constructionComponentNames = [component.baseGlyph for component in construction.components]
if glyphComponentNames == constructionComponentNames:
# same values in the same order
continue
other = list(constructionComponentNames)
for name in glyphComponentNames:
if name in other:
other.remove(name)
else:
if name not in unusedComponents:
unusedComponents[glyph.name] = []
unusedComponents[glyph.name].append(name)
other = list(glyphComponentNames)
for name in constructionComponentNames:
if name in other:
other.remove(name)
else:
if name not in unusedComponents:
missingComponents[glyph.name] = []
missingComponents[glyph.name].append(name)
text = []
if doubleEntries:
text += [
"Double entries:",
"---------------",
"\t" + "\n\t".join(doubleEntries),
"\n"
]
if missingGlyphs:
text += [
"Missing Glyphs:",
"---------------",
"\t" + "\n\t".join(missingGlyphs),
"\n"
]
if notMissingGlyphs:
text += [
"Existing Glyphs:",
"---------------",
"\t" + "\n\t".join(notMissingGlyphs),
"\n"
]
if missingComponents:
text += [
"Existing Glyphs with Missing Components:",
"----------------------------------------",
_stringDict(missingComponents, "is missing"),
"\n"
]
if unusedComponents:
text += [
"Existing Glyphs with different components:",
"------------------------------------------",
_stringDict(unusedComponents, "has no"),
"\n"
]
return "\n".join(text)
class BuildGlyphsSheet(BaseWindowController):
overWriteKey = "%s.overWrite" % defaultKey
useMarkColorKey = "%s.useMarkColor" % defaultKey
markColorKey = "%s.markColor" % defaultKey
autoUnicodesKey = "%s.autoUnicodes" % defaultKey
def __init__(self, constructions, font, parentWindow, shouldOverWrite=None, shouldAutoUnicodes=None, shouldUseMarkColor=None):
self.font = font
self.constructions = constructions
self.w = Sheet((300, 170), parentWindow=parentWindow)
getExtensionDefault, setExtensionDefault, getExtensionDefaultColor, setExtensionDefaultColor
y = 15
if shouldOverWrite is None:
shouldOverWrite = getExtensionDefault(self.overWriteKey, True)
self.w.overWrite = CheckBox((15, y, 200, 22), "Overwrite Existing Glyphs", value=shouldOverWrite)
y += 35
if shouldAutoUnicodes is None:
shouldAutoUnicodes = getExtensionDefault(self.autoUnicodesKey, True)
self.w.autoUnicodes = CheckBox((15, y, 200, 22), "Auto Unicodes", value=shouldAutoUnicodes)
y += 35
if shouldUseMarkColor is None:
shouldUseMarkColor = getExtensionDefault(self.useMarkColorKey, True)
markColor = getExtensionDefaultColor(self.markColorKey, NSColor.redColor())
elif not shouldUseMarkColor:
markColor = getExtensionDefaultColor(self.markColorKey, NSColor.redColor())
else:
markColor = rgbaToNSColor(shouldUseMarkColor)
shouldUseMarkColor = True
self.w.markGlyphs = CheckBox((15, y, 200, 22), "Mark Glyphs", value=shouldUseMarkColor, callback=self.markGlyphsCallback)
self.w.markGlyphColor = ColorWell((130, y - 5, 50, 30), color=markColor)
self.w.markGlyphColor.enable(getExtensionDefault(self.overWriteKey, True))
self.w.okButton = Button((-70, -30, -15, 20), "Build", callback=self.buildCallback, sizeStyle="small")
self.w.setDefaultButton(self.w.okButton)
self.w.closeButton = Button((-140, -30, -80, 20), "Cancel", callback=self.closeCallback, sizeStyle="small")
self.w.closeButton.bind(".", ["command"])
self.w.closeButton.bind(unichr(27), [])
self.w.open()
def markGlyphsCallback(self, sender):
self.w.markGlyphColor.enable(sender.get())
def buildCallback(self, sender):
overWrite = self.w.overWrite.get()
markColor = None
if self.w.markGlyphs.get():
markColor = NSColorToRgba(self.w.markGlyphColor.get())
characterMap = None
if self.w.autoUnicodes.get():
characterMap = GN2UV
progress = self.startProgress("Building Glyphs...", len(self.constructions))
font = self.font
font.naked().holdNotifications()
for construction in self.constructions:
progress.update()
if construction.name is None:
continue
if construction.name in font and not overWrite:
continue
glyph = self.font.newGlyph(construction.name)
glyph.clear()
glyph.width = construction.width
if construction.unicode is not None:
glyph.unicode = construction.unicode
elif characterMap and construction.name in characterMap:
glyph.unicode = characterMap[construction.name]
else:
glyph.unicode = None
glyph.note = construction.note
construction.draw(glyph.getPen())
if construction.markColor:
glyph.markColor = tuple(construction.markColor)
elif markColor:
glyph.markColor = markColor
font.naked().releaseHeldNotifications()
progress.close()
self.closeCallback(sender)
def closeCallback(self, sender):
overWrite = self.w.overWrite.get()
autoUnicodes = self.w.autoUnicodes.get()
useMarkColor = self.w.markGlyphs.get()
markColor = None
if useMarkColor:
markColor = self.w.markGlyphColor.get()
setExtensionDefault(self.overWriteKey, bool(overWrite))
setExtensionDefault(self.autoUnicodesKey, bool(autoUnicodes))
setExtensionDefault(self.useMarkColorKey, bool(useMarkColor))
if markColor is not None:
setExtensionDefaultColor(self.markColorKey, markColor)
self.w.close()
class GlyphBuilderController(BaseWindowController):
fileNameKey = "%s.lastSavedFileName" % defaultKey
glyphLibConstructionKey = "%s.construction" % defaultKey
def __init__(self, font):
self.font = None
self._glyphs = []
statusBarHeight = 20
self.w = Window((900, 700), "Glyph Builder", minSize=(400, 400))
self.w.getNSWindow().setCollectionBehavior_(128) # NSWindowCollectionBehaviorFullScreenPrimary
toolbarItems = [
dict(
itemIdentifier="save",
label="Save",
imageNamed="toolbarScriptSave",
callback=self.saveFile,
),
dict(
itemIdentifier="open",
label="Open",
imageNamed="toolbarScriptOpen",
callback=self.openFile,
),
dict(itemIdentifier=NSToolbarSpaceItemIdentifier),
dict(
itemIdentifier="reload",
label="Update",
imageNamed="toolbarScriptReload",
callback=self.reload,
),
dict(itemIdentifier=NSToolbarSpaceItemIdentifier),
dict(
itemIdentifier="analyse",
label="Analyse",
imageNamed="prefToolbarSort",
callback=self.analyse,
),
dict(itemIdentifier=NSToolbarFlexibleSpaceItemIdentifier),
dict(
itemIdentifier="buildGlyphs",
label="Build Glyphs",
imageNamed="toolbarRun",
callback=self.generateGlyphs
),
]
self.w.addToolbar(toolbarIdentifier="GlyphBuilderControllerToolbar", toolbarItems=toolbarItems, addStandardItems=False)
self.constructions = CodeEditor((0, 0, -0, -0), constructions, lexer=GlyphConstructionLexer())
# self.constructions.wrapWord(False) # in only availbel in the RoboFont 1.7 beta
self.constructions.getNSScrollView().setBorderType_(NSNoBorder)
self.preview = MultiLineView(
(0, 0, -0, -0),
pointSize=50,
lineHeight=500,
applyKerning=False,
displayOptions={
"Beam": False,
"displayMode": "Multi Line"
},
selectionCallback=self.previewSelectionCallback
)
self.analyser = AnalyserTextEditor((0, 0, -0, -0), readOnly=True)
self.analyserPreview = Group((0, 0, -0, -0))
constructionColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(0, 0, 0, .6)
self.analyserPreview.construction = GlyphPreview((0, 0, -0, -0), contourColor=constructionColor, componentColor=constructionColor)
self.analyserPreview.construction.getNSView()._buffer = 100
originColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(1, 0, 0, .6)
self.analyserPreview.origin = GlyphPreview((0, 0, -0, -0), contourColor=originColor, componentColor=originColor)
self.analyserPreview.origin.getNSView()._buffer = 100
self.analyserPreview.build = Button((10, -30, -10, 20), "Build", sizeStyle="small", callback=self.buildSingleGlyph)
self.analyserPreview.build.enable(False)
paneDescriptions = [
dict(view=self.analyser, identifier="analyserText", canCollapse=False, minSize=100),
dict(view=self.analyserPreview, identifier="analyserPreview", canCollapse=False, minSize=100),
]
self.analyserSplit = Splitter((0, 0, -0, -statusBarHeight), paneDescriptions=paneDescriptions, drawBorderLine=False, isVertical=False, dividerThickness=1)
paneDescriptions = [
dict(view=self.constructions, identifier="constructions", canCollapse=False, minSize=200, maxSize=600, liveResizeable=False),
dict(view=self.preview, identifier="preview", canCollapse=False, minSize=300, liveResizeable=True),
dict(view=self.analyserSplit, identifier="analyser", canCollapse=True, minSize=100, maxSize=300, liveResizeable=False)
]
self.w.split = Splitter((0, 0, -0, -statusBarHeight), paneDescriptions=paneDescriptions, drawBorderLine=False, dividerThickness=1)
# self.w.split.showPane("analyser", True)
self.w.statusBar = StatusBar((0, -statusBarHeight, -0, statusBarHeight))
self.w.statusBar.hiddenReload = Button((0, 0, -0, -0), "Reload", self.reload)
button = self.w.statusBar.hiddenReload.getNSButton()
button.setBezelStyle_(NSRoundRectBezelStyle)
button.setAlphaValue_(0)
self.w.statusBar.hiddenReload.bind("\r", ["command"])
self.w.statusBar.hiddenSave = Button((0, 0, -0, -0), "Reload", self.saveFile)
button = self.w.statusBar.hiddenSave.getNSButton()
button.setBezelStyle_(NSRoundRectBezelStyle)
button.setAlphaValue_(0)
self.w.statusBar.hiddenSave.bind("s", ["command"])
self.subscribeFont(font)
self.setUpBaseWindowBehavior()
addObserver(self, "fontBecameCurrent", "fontBecameCurrent")
addObserver(self, "fontResignCurrent", "fontResignCurrent")
self.w.open()
def subscribeFont(self, font):
self.unsubscribeFont()
self.font = font
if font is not None:
self.preview.setFont(font)
self.font.naked().addObserver(self, "fontChanged", "Font.Changed")
self.constructionsCallback(self.constructions)
def unsubscribeFont(self):
if self.font is not None:
self.preview.setFont(None)
self.preview.set([])
self.font.removeObserver(self, notification="Font.Changed")
self.font = None
def constructionsCallback(self, sender, update=True):
if self.font is None:
return
font = self.font.naked()
self.glyphConstructorFont = GlyphConstructorFont(font)
self._glyphs = []
errors = []
try:
constructions = ParseGlyphConstructionListFromString(sender.get(), font)
except GlyphBuilderError as err:
constructions = []
errors.append(str(err))
for construction in constructions:
if not construction:
glyph = self.preview.createNewLineGlyph()
elif construction in self.glyphConstructorFont.glyphsDone:
glyph = self.glyphConstructorFont.glyphsDone[construction]
else:
try:
constructionGlyph = GlyphConstructionBuilder(construction, self.glyphConstructorFont, characterMap=None)
except GlyphBuilderError as err:
errors.append(str(err))
continue
if constructionGlyph.name is None:
errors.append(construction)
continue
if RoboFontVersion < "2.0":
glyph = font._instantiateGlyphObject()
else:
glyph = font.layers.defaultLayer.instantiateGlyphObject()
glyph.lib[self.glyphLibConstructionKey] = construction
glyph.name = constructionGlyph.name
glyph.unicode = constructionGlyph.unicode
glyph.note = constructionGlyph.note
glyph.markColor = constructionGlyph.markColor
if RoboFontVersion < "2.0":
glyph.setParent(self.glyphConstructorFont)
glyph.dispatcher = font.dispatcher
else:
glyph._font = weakref.ref(self.glyphConstructorFont)
# glyph._dispatcher = font._dispatcher
glyph.width = constructionGlyph.width
constructionGlyph.draw(glyph.getPen())
self.glyphConstructorFont.glyphsDone[glyph.name] = glyph
self._glyphs.append(glyph)
if errors:
print("Errors:")
print("\n".join(errors))
if update:
self.preview.set(self._glyphs)
self.analyser.set(analyseConstructions(font, self._glyphs))
# preview
def previewSelectionCallback(self, sender):
def _niceNumber(value):
i = int(value)
if i == value:
return "%i" % value
else:
return "%.2f" % value
glyph = sender.getSelectedGlyph()
if glyph is not None and glyph.name is None:
glyph = None
status = []
if glyph is not None:
width = _niceNumber(glyph.width)
leftMargin = _niceNumber(glyph.leftMargin)
rightMargin = _niceNumber(glyph.rightMargin)
status = [
glyph.name,
"width: %s left: %s right: %s" % (width, leftMargin, rightMargin),
"components: %s" % (", ".join([component.baseGlyph for component in glyph.components]))
]
if glyph.unicode:
status.append("unicode: %04X" % glyph.unicode)
if glyph.note:
status.append("note: %s" % (glyph.note[:30] + (glyph.note[30:] and unichr(0x2026))))
if glyph.markColor:
status.append("mark: %s" % ", ".join([str(c) for c in glyph.markColor]))
rawConstructions = self.constructions.get()
searchConstruction = glyph.lib.get(self.glyphLibConstructionKey)
if searchConstruction is not None:
if searchConstruction not in rawConstructions:
_, variables = ParseVariables(rawConstructions)
for variableName, variableValue in variables.items():
searchConstruction = searchConstruction.replace(variableValue, "{%s}" % variableName)
if searchConstruction in rawConstructions:
selectedRange = NSMakeRange(rawConstructions.index(searchConstruction), len(searchConstruction))
self.constructions.getNSTextView().setSelectedRange_(selectedRange)
self.w.statusBar.set(status)
self.analyserPreview.construction.setGlyph(glyph)
self.analyserPreview.build.enable(glyph is not None)
if glyph is not None:
self.analyserPreview.build.setTitle("Build %s" % glyph.name)
else:
self.analyserPreview.build.setTitle("Build")
if glyph is not None and glyph.name in self.font:
self.analyserPreview.origin.setGlyph(self.font[glyph.name])
else:
self.analyserPreview.origin.setGlyph(None)
def buildSingleGlyph(self, sender):
glyph = self.preview.getSelectedGlyph()
if glyph is None:
return
if self.font is None:
return
dest = self.font.newGlyph(glyph.name)
dest.clear()
glyph.draw(dest.getPen())
dest.unicode = glyph.unicode
dest.note = glyph.note
if glyph.markColor:
dest.markColor = glyph.markColor
dest.width = glyph.width
# toolbar
def generateGlyphs(self, sender):
self.reload(update=False)
if not self._glyphs:
return
if self.font is None:
return
rawConstructions = self.constructions.get()
overWriteResult = overWriteRE.search(rawConstructions)
if overWriteResult:
overWriteResult = overWriteResult.groups()[0].strip().lower() == "true"
autoUnicodesResult = autoUnicodesRE.search(rawConstructions)
if autoUnicodesResult:
autoUnicodesResult = autoUnicodesResult.groups()[0].strip().lower() == "true"
dontMarkGlyphResult = dontMarkGlyphRE.search(rawConstructions)
if dontMarkGlyphResult:
markGlyphResult = False
else:
markGlyphResult = markGlyphRE.search(rawConstructions)
if markGlyphResult:
try:
markGlyphResult = float(markGlyphResult.groups()[0]), float(markGlyphResult.groups()[1]), float(markGlyphResult.groups()[2]), float(markGlyphResult.groups()[3])
except Exception:
pass
BuildGlyphsSheet(self._glyphs, self.font, self.w, shouldOverWrite=overWriteResult, shouldAutoUnicodes=autoUnicodesResult, shouldUseMarkColor=markGlyphResult)
def reload(self, sender=None, update=True):
self.constructionsCallback(self.constructions, update)
def _saveFile(self, path):
if self.font is not None:
self.font.lib[self.fileNameKey] = os.path.splitext(os.path.basename(path))[0]
txt = self.constructions.get()
f = open(path, "w", encoding="utf-8")
f.write(txt)
f.close()
def saveFile(self, sender=None):
preferredName = None
if self.font is not None and self.font.path is not None:
preferredName = os.path.splitext(os.path.basename(self.font.path))[0]
if self.fileNameKey in self.font.lib.keys():
# see if we have saved this file before and use that as first choice
preferredName = self.font.lib.get(self.fileNameKey)
self.showPutFile(["glyphConstruction"], fileName=preferredName, callback=self._saveFile)
def setFile(self, path):
f = open(path, "r", encoding="utf-8")
txt = f.read()
f.close()
self.constructions.set(txt)
def _openFile(self, paths):
if paths:
path = paths[0]
self.setFile(path)
def openFile(self, sender=None):
directory = fileName = None
if self.font is not None and self.font.path is not None:
if self.fileNameKey in self.font.lib.keys():
fileName = self.font.lib.get(self.fileNameKey, "")
fileName += ".glyphConstruction"
directory = os.path.dirname(self.font.path)
fileName = os.path.join(directory, fileName)
directory = None
getFile(fileTypes=["glyphConstruction"], parentWindow=self.w.getNSWindow(), directory=directory, fileName=fileName, resultCallback=self._openFile)
# self.showGetFile(["glyphConstruction"], callback=self._openFile)
def analyse(self, sender=None):
self.w.split.togglePane("analyser", False)
self.reload()
# notifications
def fontChanged(self, notification):
self.reload()
def fontBecameCurrent(self, notification):
font = notification["font"]
self.subscribeFont(font)
def fontResignCurrent(self, notification):
self.unsubscribeFont()
def windowCloseCallback(self, sender):
self.unsubscribeFont()
removeObserver(self, "fontBecameCurrent")
removeObserver(self, "fontResignCurrent")
super(GlyphBuilderController, self).windowCloseCallback(sender)
if __name__=='__main__':
GlyphBuilderController(CurrentFont())