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