-
Notifications
You must be signed in to change notification settings - Fork 14
/
glyph.py
2114 lines (1803 loc) · 64.5 KB
/
glyph.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
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import weakref
from warnings import warn
from fontTools.misc import arrayTools
from defcon.objects.base import BaseObject
from defcon.objects.contour import Contour
from defcon.objects.point import Point
from defcon.objects.component import Component
from defcon.objects.anchor import Anchor
from defcon.objects.lib import Lib
from defcon.objects.guideline import Guideline
from defcon.objects.image import Image
from defcon.objects.color import Color
from defcon.tools.representations import glyphBoundsRepresentationFactory, glyphControlPointBoundsRepresentationFactory
from defcon.pens.decomposeComponentPointPen import DecomposeComponentPointPen
def addRepresentationFactory(name, factory):
warn(
"addRepresentationFactory is deprecated. Use the functions in defcon.__init__.",
DeprecationWarning)
Glyph.representationFactories[name] = dict(
factory=factory,
destructiveNotifications=["Glyph.Changed"])
def removeRepresentationFactory(name):
warn(
"removeRepresentationFactory is deprecated. Use the functions in defcon.__init__.",
DeprecationWarning)
del Glyph.representationFactories[name]
class Glyph(BaseObject):
"""
This object represents a glyph and it contains contour, component, anchor
and other assorted bits data about the glyph.
**This object posts the following notifications:**
============================
Name
============================
Glyph.Changed
Glyph.BeginUndo
Glyph.EndUndo
Glyph.BeginRedo
Glyph.EndRedo
Glyph.NameWillChange
Glyph.NameChanged
Glyph.UnicodesChanged
Glyph.WidthChanged
Glyph.HeightChanged
Glyph.NoteChanged
Glyph.LibChanged
Glyph.ImageChanged
Glyph.ImageWillBeDeleted
Glyph.ContourWillBeDeleted
Glyph.ContoursChanged
Glyph.ComponentWillBeDeleted
Glyph.ComponentsChanged
Glyph.AnchorWillBeDeleted
Glyph.AnchorsChanged
Glyph.GuidelineWillBeDeleted
Glyph.GuidelinesChanged
Glyph.MarkColorChanged
============================
The Glyph object has list like behavior. This behavior allows you to interact
with contour data directly. For example, to get a particular contour::
contour = glyph[0]
To iterate over all contours::
for contour in glyph:
To get the number of contours::
contourCount = len(glyph)
To interact with components or anchors in a similar way,
use the ``components`` and ``anchors`` attributes.
"""
changeNotificationName = "Glyph.Changed"
beginUndoNotificationName = "Glyph.BeginUndo"
endUndoNotificationName = "Glyph.EndUndo"
beginRedoNotificationName = "Glyph.BeginRedo"
endRedoNotificationName = "Glyph.EndRedo"
representationFactories = {
"defcon.glyph.bounds":
dict(factory=glyphBoundsRepresentationFactory,
destructiveNotifications=("Glyph.ContoursChanged",
"Glyph.ComponentsChanged",
"Glyph.ComponentBaseGlyphDataChanged")),
"defcon.glyph.controlPointBounds":
dict(factory=glyphControlPointBoundsRepresentationFactory,
destructiveNotifications=("Glyph.ContoursChanged",
"Glyph.ComponentsChanged",
"Glyph.ComponentBaseGlyphDataChanged"))
}
def __init__(self,
layer=None,
contourClass=None,
pointClass=None,
componentClass=None,
anchorClass=None,
guidelineClass=None,
libClass=None,
imageClass=None):
layerSet = font = None
if layer is not None:
font = weakref.ref(layer.layerSet.font)
layerSet = weakref.ref(layer.layerSet)
layer = weakref.ref(layer)
self._font = font
self._layerSet = layerSet
self._layer = layer
super(Glyph, self).__init__()
self.beginSelfNotificationObservation()
self._isLoading = False
self._dirty = False
self._name = None
self._unicodes = []
self._width = 0
self._height = 0
self._note = None
self._image = None
self._identifiers = set()
self._shallowLoadedContours = None
self._contours = []
self._components = []
self._anchors = []
self._guidelines = []
self._lib = None
if contourClass is None:
contourClass = Contour
if pointClass is None:
pointClass = Point
if componentClass is None:
componentClass = Component
if anchorClass is None:
anchorClass = Anchor
if guidelineClass is None:
guidelineClass = Guideline
if libClass is None:
libClass = Lib
if imageClass is None:
imageClass = Image
self._contourClass = contourClass
self._pointClass = pointClass
self._componentClass = componentClass
self._anchorClass = anchorClass
self._guidelineClass = Guideline
self._libClass = libClass
self._imageClass = imageClass
def __del__(self):
super(Glyph, self).__del__()
self._contours = None
self._components = None
self._anchors = None
self._guidelines = None
self._lib = None
self._image = None
# --------------
# Parent Objects
# --------------
def getParent(self):
return self.font
def _get_font(self):
if self._font is None:
return None
return self._font()
font = property(_get_font,
doc="The :class:`Font` that this glyph belongs to.")
def _get_layerSet(self):
if self._layerSet is None:
return None
return self._layerSet()
layerSet = property(
_get_layerSet,
doc="The :class:`LayerSet` that this glyph belongs to.")
def _get_layer(self):
if self._layer is None:
return None
return self._layer()
layer = property(_get_layer,
doc="The :class:`Layer` that this glyph belongs to.")
# ----------------
# Basic Attributes
# ----------------
# identifiers
def _get_identifiers(self):
return self._identifiers
identifiers = property(
_get_identifiers,
doc="Set of identifiers for the glyph. This is primarily for internal use.")
# name
def _set_name(self, value):
oldName = self._name
if oldName != value:
self.postNotification(notification="Glyph.NameWillChange",
data=dict(oldValue=oldName,
newValue=value))
self._name = value
self.postNotification(notification="Glyph.NameChanged",
data=dict(oldValue=oldName,
newValue=value))
self.dirty = True
def _get_name(self):
return self._name
name = property(
_get_name,
_set_name,
doc="The name of the glyph. Setting this posts *GLyph.NameChanged* and *Glyph.NameChanged* notifications.")
# unicodes
def _get_unicodes(self):
return list(self._unicodes)
def _set_unicodes(self, value):
oldValue = self.unicodes
if oldValue != value:
self._unicodes = value
self.postNotification(notification="Glyph.UnicodesChanged",
data=dict(oldValue=oldValue,
newValue=value))
self.dirty = True
unicodes = property(
_get_unicodes,
_set_unicodes,
doc="The list of unicode values assigned to the glyph. Setting this posts *Glyph.UnicodesChanged* and *Glyph.Changed* notifications.")
def _get_unicode(self):
if self._unicodes:
return self._unicodes[0]
return None
def _set_unicode(self, value):
if value is None:
self.unicodes = []
else:
existing = list(self._unicodes)
if value in existing:
existing.pop(existing.index(value))
existing.insert(0, value)
self.unicodes = existing
unicode = property(
_get_unicode,
_set_unicode,
doc="The primary unicode value for the glyph. This is the equivalent of ``glyph.unicodes[0]``. This is a convenience attribute that works with the ``unicodes`` attribute.")
# -------
# Metrics
# -------
# bounds
def _get_bounds(self):
return self.getRepresentation("defcon.glyph.bounds")
bounds = property(
_get_bounds,
doc="The bounds of the glyph's outline expressed as a tuple of form (xMin, yMin, xMax, yMax).")
def _get_controlPointBounds(self):
return self.getRepresentation("defcon.glyph.controlPointBounds")
controlPointBounds = property(
_get_controlPointBounds,
doc="The control bounds of all points in the glyph. This only measures the point positions, it does not measure curves. So, curves without points at the extrema will not be properly measured.")
# margins
def _get_leftMargin(self):
bounds = self.bounds
if bounds is None:
return None
xMin, yMin, xMax, yMax = bounds
return xMin
def _set_leftMargin(self, value):
bounds = self.bounds
if bounds is None:
return
xMin, yMin, xMax, yMax = bounds
oldValue = xMin
diff = value - xMin
if value != oldValue:
self.move((diff, 0))
self.width += diff
self.dirty = True
leftMargin = property(
_get_leftMargin,
_set_leftMargin,
doc="The left margin of the glyph. Setting this post *Glyph.WidthChanged* and *Glyph.Changed* notifications among others.")
def _get_rightMargin(self):
bounds = self.bounds
if bounds is None:
return None
xMin, yMin, xMax, yMax = bounds
return self._width - xMax
def _set_rightMargin(self, value):
bounds = self.bounds
if bounds is None:
return
xMin, yMin, xMax, yMax = bounds
oldValue = self._width - xMax
if oldValue != value:
self.width = xMax + value
self.dirty = True
rightMargin = property(
_get_rightMargin,
_set_rightMargin,
doc="The right margin of the glyph. Setting this posts *Glyph.WidthChanged* and *Glyph.Changed* notifications among others.")
# width
def _get_width(self):
return self._width
def _set_width(self, value):
oldValue = self._width
if oldValue != value:
self._width = value
self.postNotification(notification="Glyph.WidthChanged",
data=dict(oldValue=oldValue,
newValue=value))
self.dirty = True
width = property(
_get_width,
_set_width,
doc="The width of the glyph. Setting this posts *Glyph.WidthChanged* and *Glyph.Changed* notifications.")
# height
def _get_height(self):
return self._height
def _set_height(self, value):
oldValue = self._height
if oldValue != value:
self._height = value
self.postNotification(notification="Glyph.HeightChanged",
data=dict(oldValue=oldValue,
newValue=value))
self.dirty = True
height = property(
_get_height,
_set_height,
doc="The height of the glyph. Setting this posts *Glyph.HeightChanged* and *Glyph.Changed* notifications.")
# ----------------------
# Lib Wrapped Attributes
# ----------------------
# mark color
def _get_markColor(self):
value = self.lib.get("public.markColor")
if value is not None:
value = Color(value)
return value
def _set_markColor(self, value):
# convert to a color object
if value is not None:
value = Color(value)
# don't write if there is no change
oldValue = self.lib.get("public.markColor")
if oldValue is not None:
oldValue = Color(oldValue)
if value == oldValue:
return
# remove
if value is None:
if "public.markColor" in self.lib:
del self.lib["public.markColor"]
# store
else:
self.lib["public.markColor"] = value
self.postNotification(notification="Glyph.MarkColorChanged",
data=dict(oldValue=oldValue,
newValue=value))
markColor = property(
_get_markColor,
_set_markColor,
doc="The glyph's mark color. When setting, the value can be a UFO color string, a sequence of (r, g, b, a) or a :class:`Color` object. Setting this posts *Glyph.MarkColorChanged* and *Glyph.Changed* notifications.")
# -------
# Pen API
# -------
def draw(self, pen):
"""
Draw the glyph with **pen**.
"""
from ufoLib.pointPen import PointToSegmentPen
pointPen = PointToSegmentPen(pen)
self.drawPoints(pointPen)
def drawPoints(self, pointPen):
"""
Draw the glyph with **pointPen**.
"""
if self._shallowLoadedContours:
self._drawShallowLoadedContours(pointPen,
self._shallowLoadedContours)
else:
for contour in self._contours:
contour.drawPoints(pointPen)
for component in self._components:
component.drawPoints(pointPen)
def _drawShallowLoadedContours(self, pointPen, contours):
for contour in contours:
try:
pointPen.beginPath(identifier=contour.get("identifier"))
except TypeError:
pointPen.beginPath()
warn(
"The beginPath method needs an identifier kwarg. The contour's identifier value has been discarded.",
DeprecationWarning)
for args, kwargs in contour["points"]:
pointPen.addPoint(*args, **kwargs)
pointPen.endPath()
def getPen(self):
"""
Get the pen used to draw into this glyph.
"""
from ufoLib.pointPen import SegmentToPointPen
return SegmentToPointPen(self.getPointPen())
def getPointPen(self):
"""
Get the point pen used to draw into this glyph.
"""
from defcon.pens.glyphObjectPointPen import GlyphObjectPointPen, GlyphObjectLoadingPointPen
if self._isLoading:
self._shallowLoadedContours = []
return GlyphObjectLoadingPointPen(self)
else:
return GlyphObjectPointPen(self)
# --------
# Contours
# --------
def _get_contourClass(self):
return self._contourClass
contourClass = property(_get_contourClass,
doc="The class used for contours.")
def _get_pointClass(self):
return self._pointClass
pointClass = property(_get_pointClass, doc="The class used for points.")
def _fullyLoadShallowLoadedContours(self):
if not self._shallowLoadedContours:
self._shallowLoadedContours = None
return
self.disableNotifications()
contours = list(self._shallowLoadedContours)
self._shallowLoadedContours = None
dirty = self.dirty
pointPen = self.getPointPen()
self._drawShallowLoadedContours(pointPen, contours)
self.dirty = dirty
self.enableNotifications()
def instantiateContour(self):
contour = self._contourClass(glyph=self, pointClass=self.pointClass)
return contour
def beginSelfContourNotificationObservation(self, contour):
if contour.dispatcher is None:
return
contour.addObserver(observer=self,
methodName="_contourChanged",
notification="Contour.Changed")
def endSelfContourNotificationObservation(self, contour):
if contour.dispatcher is None:
return
contour.removeObserver(observer=self, notification="Contour.Changed")
contour.endSelfNotificationObservation()
def appendContour(self, contour):
"""
Append **contour** to the glyph. The contour must be a defcon
:class:`Contour` object or a subclass of that object. An error
will be raised if the contour's identifier or a point identifier
conflicts with any of the identifiers within the glyph.
This will post a *Glyph.Changed* notification.
"""
self.insertContour(len(self), contour)
def insertContour(self, index, contour):
"""
Insert **contour** into the glyph at index. The contour
must be a defcon :class:`Contour` object or a subclass
of that object. An error will be raised if the contour's
identifier or a point identifier conflicts with any of
the identifiers within the glyph.
This will post a *Glyph.Changed* notification.
"""
assert contour not in self
assert contour.glyph in (self, None
), "This contour belongs to another glyph."
if contour.glyph is None:
identifiers = self._identifiers
if contour.identifier is not None:
assert contour.identifier not in identifiers
identifiers.add(contour.identifier)
for point in contour:
if point.identifier is not None:
assert point.identifier not in identifiers
identifiers.add(point.identifier)
contour.glyph = self
contour.beginSelfNotificationObservation()
self.beginSelfContourNotificationObservation(contour)
self._contours.insert(index, contour)
self.postNotification(notification="Glyph.ContoursChanged")
self.dirty = True
def removeContour(self, contour):
"""
Remove **contour** from the glyph.
This will post a *Glyph.Changed* notification.
"""
if contour not in self:
raise IndexError("contour not in glyph")
self.postNotification(notification="Glyph.ContourWillBeDeleted",
data=dict(object=contour))
identifiers = self._identifiers
if contour.identifier is not None:
identifiers.remove(contour.identifier)
for point in contour:
if point.identifier is not None:
identifiers.remove(point.identifier)
self._contours.remove(contour)
self.endSelfContourNotificationObservation(contour)
self.postNotification(notification="Glyph.ContoursChanged")
self.dirty = True
def contourIndex(self, contour):
"""
Get the index for **contour**.
"""
return self._getContourIndex(contour)
def clearContours(self):
"""
Clear all contours from the glyph.
This posts a *Glyph.Changed* notification.
"""
self.holdNotifications()
for contour in reversed(self):
self.removeContour(contour)
self.releaseHeldNotifications()
# ----------
# Components
# ----------
def _get_componentClass(self):
return self._componentClass
componentClass = property(_get_componentClass,
doc="The class used for components.")
def _get_components(self):
return list(self._components)
components = property(
_get_components,
doc="An ordered list of :class:`Component` objects stored in the glyph.")
def instantiateComponent(self):
component = self._componentClass(glyph=self)
return component
def beginSelfComponentNotificationObservation(self, component):
if component.dispatcher is None:
return
component.addObserver(observer=self,
methodName="_componentChanged",
notification="Component.Changed")
component.addObserver(observer=self,
methodName="_componentBaseGlyphDataChanged",
notification="Component.BaseGlyphDataChanged")
def endSelfComponentNotificationObservation(self, component):
if component.dispatcher is None:
return
component.removeObserver(observer=self,
notification="Component.Changed")
component.endSelfNotificationObservation()
def appendComponent(self, component):
"""
Append **component** to the glyph. The component must be a defcon
:class:`Component` object or a subclass of that object. An error
will be raised if the component's identifier conflicts with any of
the identifiers within the glyph.
This will post a *Glyph.Changed* notification.
"""
self.insertComponent(len(self._components), component)
def insertComponent(self, index, component):
"""
Insert **component** into the glyph at index. The component
must be a defcon :class:`Component` object or a subclass
of that object. An error will be raised if the component's
identifier conflicts with any of the identifiers within
the glyph.
This will post a *Glyph.Changed* notification.
"""
assert component not in self._components
assert component.glyph in (
self, None), "This component belongs to another glyph."
if component.glyph is None:
if component.identifier is not None:
identifiers = self._identifiers
assert component.identifier not in identifiers
identifiers.add(component.identifier)
component.glyph = self
component.beginSelfNotificationObservation()
self.beginSelfComponentNotificationObservation(component)
self._components.insert(index, component)
self.postNotification(notification="Glyph.ComponentsChanged")
self.dirty = True
def removeComponent(self, component):
"""
Remove **component** from the glyph.
This will post a *Glyph.Changed* notification.
"""
self.postNotification(notification="Glyph.ComponentWillBeDeleted",
data=dict(object=component))
if component.identifier is not None:
self._identifiers.remove(component.identifier)
self._components.remove(component)
self.endSelfComponentNotificationObservation(component)
self.postNotification(notification="Glyph.ComponentsChanged")
self.dirty = True
def componentIndex(self, component):
"""
Get the index for **component**.
"""
return self._components.index(component)
def clearComponents(self):
"""
Clear all components from the glyph.
This posts a *Glyph.Changed* notification.
"""
self.holdNotifications()
for component in reversed(self._components):
self.removeComponent(component)
self.releaseHeldNotifications()
def decomposeComponent(self, component):
"""
Decompose **component**. This will preserve the identifiers
in the incoming contours and points unless there is a conflict.
In that case, the conflicting incoming identifier will be discarded.
This posts *Glyph.ComponentsChanged*, *Glyph.ContoursChanged*
and *Glyph.Changed* notifications.
"""
self.holdNotifications()
layer = self.layer
pointPen = DecomposeComponentPointPen(self, layer)
self._decomposeComponent(component, layer, pointPen)
self.releaseHeldNotifications()
self.postNotification(notification="Glyph.ContoursChanged")
def decomposeAllComponents(self):
"""
Decompose all components in this glyph. This will preserve the
identifiers in the incoming contours and points unless there is a
conflict. In that case, the conflicting incoming identifier will
be discarded.
This posts *Glyph.ComponentsChanged*, *Glyph.ContoursChanged*
and *Glyph.Changed* notifications.
"""
if not self.components:
return
self.holdNotifications()
layer = self.layer
pointPen = DecomposeComponentPointPen(self, layer)
for component in self.components:
self._decomposeComponent(component, layer, pointPen)
self.releaseHeldNotifications()
self.postNotification(notification="Glyph.ContoursChanged")
def _decomposeComponent(self, component, layer, pointPen):
pointPen.skipConflictingIdentifiers = True
component.drawPoints(pointPen)
self.removeComponent(component)
# -------
# Anchors
# -------
def _get_anchorClass(self):
return self._anchorClass
anchorClass = property(_get_anchorClass, doc="The class used for anchors.")
def _get_anchors(self):
return list(self._anchors)
def _set_anchors(self, value):
self.clearAnchors()
self.holdNotifications()
for anchor in value:
self.appendAnchor(anchor)
self.releaseHeldNotifications()
anchors = property(
_get_anchors,
_set_anchors,
doc="An ordered list of :class:`Anchor` objects stored in the glyph.")
def instantiateAnchor(self, anchorDict=None):
anchor = self._anchorClass(anchorDict=anchorDict)
return anchor
def beginSelfAnchorNotificationObservation(self, anchor):
if anchor.dispatcher is None:
return
anchor.addObserver(observer=self,
methodName="_anchorChanged",
notification="Anchor.Changed")
def endSelfAnchorNotificationObservation(self, anchor):
if anchor.dispatcher is None:
return
anchor.removeObserver(observer=self, notification="Anchor.Changed")
anchor.endSelfNotificationObservation()
def appendAnchor(self, anchor):
"""
Append **anchor** to the glyph. The anchor must be a defcon
:class:`Anchor` object or a subclass of that object. An error
will be raised if the anchor's identifier conflicts with any of
the identifiers within the glyph.
This will post a *Glyph.Changed* notification.
"""
self.insertAnchor(len(self._anchors), anchor)
def insertAnchor(self, index, anchor):
"""
Insert **anchor** into the glyph at index. The anchor
must be a defcon :class:`Anchor` object or a subclass
of that object. An error will be raised if the anchor's
identifier conflicts with any of the identifiers within
the glyph.
This will post a *Glyph.Changed* notification.
"""
try:
assert anchor.glyph != self
except AttributeError:
pass
if not isinstance(anchor, self._anchorClass):
anchor = self.instantiateAnchor(anchorDict=anchor)
assert anchor.glyph in (self, None
), "This anchor belongs to another glyph."
if anchor.glyph is None:
if anchor.identifier is not None:
identifiers = self._identifiers
assert anchor.identifier not in identifiers
identifiers.add(anchor.identifier)
anchor.glyph = self
anchor.beginSelfNotificationObservation()
self.beginSelfAnchorNotificationObservation(anchor)
self._anchors.insert(index, anchor)
self.postNotification(notification="Glyph.AnchorsChanged")
self.dirty = True
def removeAnchor(self, anchor):
"""
Remove **anchor** from the glyph.
This will post a *Glyph.Changed* notification.
"""
self.postNotification(notification="Glyph.AnchorWillBeDeleted",
data=dict(object=anchor))
if anchor.identifier is not None:
self._identifiers.remove(anchor.identifier)
self._anchors.remove(anchor)
self.endSelfAnchorNotificationObservation(anchor)
self.postNotification(notification="Glyph.AnchorsChanged")
self.dirty = True
def anchorIndex(self, anchor):
"""
Get the index for **anchor**.
"""
return self._anchors.index(anchor)
def clearAnchors(self):
"""
Clear all anchors from the glyph.
This posts a *Glyph.Changed* notification.
"""
self.holdNotifications()
for anchor in reversed(self._anchors):
self.removeAnchor(anchor)
self.releaseHeldNotifications()
# ----------
# Guidelines
# ----------
def _get_guidelineClass(self):
return self._guidelineClass
guidelineClass = property(_get_guidelineClass,
doc="The class used for guidelines.")
def _get_guidelines(self):
return list(self._guidelines)
def _set_guidelines(self, value):
self.clearGuidelines()
self.holdNotifications()
for guideline in value:
self.appendGuideline(guideline)
self.releaseHeldNotifications()
guidelines = property(
_get_guidelines,
_set_guidelines,
doc="An ordered list of :class:`Guideline` objects stored in the glyph. Setting this will post a *Glyph.Changed* notification along with any notifications posted by the :py:meth:`Glyph.appendGuideline` and :py:meth:`Glyph.clearGuidelines` methods.")
def instantiateGuideline(self, guidelineDict=None):
guideline = self._guidelineClass(guidelineDict=guidelineDict)
return guideline
def beginSelfGuidelineNotificationObservation(self, guideline):
if guideline.dispatcher is None:
return
guideline.addObserver(observer=self,
methodName="_guidelineChanged",
notification="Guideline.Changed")
def endSelfGuidelineNotificationObservation(self, guideline):
if guideline.dispatcher is None:
return
guideline.removeObserver(observer=self,
notification="Guideline.Changed")
guideline.endSelfNotificationObservation()
def appendGuideline(self, guideline):
"""
Append **guideline** to the glyph. The guideline must be a defcon
:class:`Guideline` object or a subclass of that object. An error
will be raised if the guideline's identifier conflicts with any of
the identifiers within the glyph.
This will post a *Glyph.Changed* notification.
"""
self.insertGuideline(len(self._guidelines), guideline)
def insertGuideline(self, index, guideline):
"""
Insert **guideline** into the glyph at index. The guideline
must be a defcon :class:`Guideline` object or a subclass
of that object. An error will be raised if the guideline's
identifier conflicts with any of the identifiers within
the glyph.
This will post a *Glyph.Changed* notification.
"""
try:
assert guideline.glyph != self
except AttributeError:
pass
if not isinstance(guideline, self._guidelineClass):
guideline = self.instantiateGuideline(guidelineDict=guideline)
assert guideline.glyph in (
self, None), "This guideline belongs to another glyph."
if guideline.glyph is None:
assert guideline.fontInfo is None, "This guideline belongs to a font."
if guideline.glyph is None:
if guideline.identifier is not None:
identifiers = self._identifiers
assert guideline.identifier not in identifiers
if guideline.identifier is not None:
identifiers.add(guideline.identifier)
guideline.glyph = self
guideline.beginSelfNotificationObservation()
self.beginSelfGuidelineNotificationObservation(guideline)
self._guidelines.insert(index, guideline)
self.postNotification(notification="Glyph.GuidelinesChanged")
self.dirty = True
def removeGuideline(self, guideline):
"""
Remove **guideline** from the glyph.
This will post a *Glyph.Changed* notification.
"""
self.postNotification(notification="Glyph.GuidelineWillBeDeleted",
data=dict(object=guideline))
if guideline.identifier is not None:
self._identifiers.remove(guideline.identifier)
self._guidelines.remove(guideline)
self.endSelfGuidelineNotificationObservation(guideline)
self.postNotification(notification="Glyph.GuidelinesChanged")
self.dirty = True
def guidelineIndex(self, guideline):
"""
Get the index for **guideline**.
"""
return self._guidelines.index(guideline)
def clearGuidelines(self):
"""
Clear all guidelines from the glyph.
This posts a *Glyph.Changed* notification.
"""
self.holdNotifications()
for guideline in reversed(self._guidelines):
self.removeGuideline(guideline)
self.releaseHeldNotifications()
# ----
# Note
# ----
def _get_note(self):
return self._note
def _set_note(self, value):
if value is not None:
assert isinstance(value, str)
oldValue = self._note
if oldValue != value:
self._note = value
self.postNotification(notification="Glyph.NoteChanged",
data=dict(oldValue=oldValue,
newValue=value))
self.dirty = True
note = property(
_get_note,
_set_note,
doc="An arbitrary note for the glyph. Setting this will post a *Glyph.Changed* notification.")