Skip to content

Commit

Permalink
add contour.setStartPoint + test (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
typemytype authored Sep 3, 2022
1 parent 4b431a2 commit 07acfc2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Lib/fontParts/base/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ---------
Expand Down
17 changes: 17 additions & 0 deletions Lib/fontParts/test/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
)

0 comments on commit 07acfc2

Please sign in to comment.