1from fontTools.misc.arrayTools import pairwise
2from fontTools.pens.filterPen import ContourFilterPen
3
4
5__all__ = ["reversedContour", "ReverseContourPen"]
6
7
8class ReverseContourPen(ContourFilterPen):
9    """Filter pen that passes outline data to another pen, but reversing
10    the winding direction of all contours. Components are simply passed
11    through unchanged.
12
13    Closed contours are reversed in such a way that the first point remains
14    the first point.
15    """
16
17    def filterContour(self, contour):
18        return reversedContour(contour)
19
20
21def reversedContour(contour):
22    """ Generator that takes a list of pen's (operator, operands) tuples,
23    and yields them with the winding direction reversed.
24    """
25    if not contour:
26        return  # nothing to do, stop iteration
27
28    # valid contours must have at least a starting and ending command,
29    # can't have one without the other
30    assert len(contour) > 1, "invalid contour"
31
32    # the type of the last command determines if the contour is closed
33    contourType = contour.pop()[0]
34    assert contourType in ("endPath", "closePath")
35    closed = contourType == "closePath"
36
37    firstType, firstPts = contour.pop(0)
38    assert firstType in ("moveTo", "qCurveTo"), (
39        "invalid initial segment type: %r" % firstType)
40    firstOnCurve = firstPts[-1]
41    if firstType == "qCurveTo":
42        # special case for TrueType paths contaning only off-curve points
43        assert firstOnCurve is None, (
44            "off-curve only paths must end with 'None'")
45        assert not contour, (
46            "only one qCurveTo allowed per off-curve path")
47        firstPts = ((firstPts[0],) + tuple(reversed(firstPts[1:-1])) +
48                    (None,))
49
50    if not contour:
51        # contour contains only one segment, nothing to reverse
52        if firstType == "moveTo":
53            closed = False  # single-point paths can't be closed
54        else:
55            closed = True  # off-curve paths are closed by definition
56        yield firstType, firstPts
57    else:
58        lastType, lastPts = contour[-1]
59        lastOnCurve = lastPts[-1]
60        if closed:
61            # for closed paths, we keep the starting point
62            yield firstType, firstPts
63            if firstOnCurve != lastOnCurve:
64                # emit an implied line between the last and first points
65                yield "lineTo", (lastOnCurve,)
66                contour[-1] = (lastType,
67                               tuple(lastPts[:-1]) + (firstOnCurve,))
68
69            if len(contour) > 1:
70                secondType, secondPts = contour[0]
71            else:
72                # contour has only two points, the second and last are the same
73                secondType, secondPts = lastType, lastPts
74            # if a lineTo follows the initial moveTo, after reversing it
75            # will be implied by the closePath, so we don't emit one;
76            # unless the lineTo and moveTo overlap, in which case we keep the
77            # duplicate points
78            if secondType == "lineTo" and firstPts != secondPts:
79                del contour[0]
80                if contour:
81                    contour[-1] = (lastType,
82                                   tuple(lastPts[:-1]) + secondPts)
83        else:
84            # for open paths, the last point will become the first
85            yield firstType, (lastOnCurve,)
86            contour[-1] = (lastType, tuple(lastPts[:-1]) + (firstOnCurve,))
87
88        # we iterate over all segment pairs in reverse order, and yield
89        # each one with the off-curve points reversed (if any), and
90        # with the on-curve point of the following segment
91        for (curType, curPts), (_, nextPts) in pairwise(
92                contour, reverse=True):
93            yield curType, tuple(reversed(curPts[:-1])) + (nextPts[-1],)
94
95    yield "closePath" if closed else "endPath", ()
96