aboutsummaryrefslogtreecommitdiffstats
path: root/Lib/defconQt/pens/copySelectionPen.py
blob: 118f3baf8f82b9598d7ab09a8271a213a06260c0 (plain)
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
from defconQt.objects.defcon import TGlyph
from robofab.pens.pointPen import AbstractPointPen


class CopySelectionPen(AbstractPointPen):

    def __init__(self, glyph=None):
        if glyph is None:
            glyph = TGlyph()
        self._glyph = glyph
        self._contour = None
        self._havePoint = False
        self._originalContourIsOpen = False

    def beginPath(self):
        self._contour = self._glyph.contourClass(
            pointClass=self._glyph.pointClass)

    def endPath(self, keepOpened=False):
        if self._havePoint:
            self.elideOrphanOffCurves(True)
            if self._originalContourIsOpen or keepOpened:
                self._contour[0].segmentType = "move"
            self._contour.dirty = False
            self._glyph.appendContour(self._contour)
        self._contour = None
        self._havePoint = False
        self._originalContourIsOpen = False

    def addPoint(self, pt, segmentType=None, smooth=False, name=None,
                 selected=False, **kwargs):
        if segmentType == "move":
            self._originalContourIsOpen = True
        if segmentType is None or selected:
            if selected:
                self._havePoint = True
            self._contour.addPoint(pt, segmentType, smooth, name)
        else:
            self.elideOrphanOffCurves(False)
            if self._havePoint:
                # We started drawing a path and we have a gap in it. Create
                # a new contour (and leave this one opened).
                self.endPath(True)
                self.beginPath()

    def addComponent(self, baseGlyphName, transformation):
        pass  # XXX

    def elideOrphanOffCurves(self, arrivedAtBoundary):
        # onCurves that aren't selected and preceding offCurves if any are
        # elided
        for _ in range(2):
            if len(self._contour):
                if len(self._contour) > 1 and arrivedAtBoundary and \
                        self._contour[0].segmentType == "curve":
                    # We're at the end of drawing and the offCurves lead to
                    # begin onCurve. Let them in.
                    pass
                elif self._contour[-1].segmentType is None:
                    self._contour.removePoint(self._contour[-1])

    def getGlyph(self):
        return self._glyph