From 07acfc22f474ed95fc296aab70c4c01db0ffd0a9 Mon Sep 17 00:00:00 2001 From: Frederik Berlaen Date: Sat, 3 Sep 2022 22:05:42 +0200 Subject: [PATCH] add contour.setStartPoint + test (#668) --- Lib/fontParts/base/contour.py | 39 ++++++++++++++++++++++++++++++ Lib/fontParts/test/test_contour.py | 17 +++++++++++++ 2 files changed, 56 insertions(+) diff --git a/Lib/fontParts/base/contour.py b/Lib/fontParts/base/contour.py index 141f926f..efd34eee 100644 --- a/Lib/fontParts/base/contour.py +++ b/Lib/fontParts/base/contour.py @@ -957,6 +957,45 @@ def _removePoint(self, index, preserveCurve, **kwargs): """ self.raiseNotImplementedError() + def setStartPoint(self, point): + """ + Set the first point on the contour. + point can be a segment object or an index. + """ + if self.open: + raise FontPartsError("An open contour can not change the starting point.") + + points = self.points + if not isinstance(point, int): + pointIndex = points.index(point) + else: + pointIndex = point + if pointIndex == 0: + return + + if pointIndex >= len(points): + raise ValueError(("The contour does not contain a point " + "at index %d" % pointIndex)) + self._setStartPoint(pointIndex) + + def _setStartPoint(self, pointIndex, **kwargs): + """ + Subclasses may override this method. + """ + points = self.points + points = points[pointIndex:] + points[:pointIndex] + # Clear the points. + for point in self.points: + self.removePoint(point) + # Add the points. + for point in points: + self.appendPoint( + (point.x, point.y), + type=point.type, + smooth=point.smooth, + name=point.name, + identifier=point.identifier + ) # --------- # Selection # --------- diff --git a/Lib/fontParts/test/test_contour.py b/Lib/fontParts/test/test_contour.py index 9aaecf86..6e78743e 100644 --- a/Lib/fontParts/test/test_contour.py +++ b/Lib/fontParts/test/test_contour.py @@ -669,3 +669,20 @@ def test_segment_insert_curve_closed(self): [(point.x, point.y) for point in contour.points], [(0, 0), (2, 2), (3, 3), (4, 4), (1, 1), (5, 5)] ) + + # ------ + # points + # ------ + + def test_setStartPoint(self): + contour, _ = self.objectGenerator("contour") + contour.appendPoint((0, 0), "line") + contour.appendPoint((1, 1), "line") + contour.appendPoint((2, 2), "line") + contour.appendPoint((3, 3), "line") + + contour.setStartPoint(2) + self.assertEqual( + [(point.x, point.y) for point in contour.points], + [(2, 2), (3, 3), (0, 0), (1, 1)] + )