Skip to content

Commit

Permalink
Manually revert #134, add test for degenerate subpaths
Browse files Browse the repository at this point in the history
  • Loading branch information
tatarize committed Apr 2, 2022
1 parent 7a7f419 commit 33d3c9e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = svgelements
version = 1.6.11
version = 1.6.12
description = Svg Elements Parsing
long_description_content_type=text/markdown
long_description = file: README.md
Expand Down
45 changes: 11 additions & 34 deletions svgelements/svgelements.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
and the Arc can do exact arc calculations if scipy is installed.
"""

SVGELEMENTS_VERSION = "1.6.11"
SVGELEMENTS_VERSION = "1.6.12"

MIN_DEPTH = 5
ERROR = 1e-12
Expand Down Expand Up @@ -6049,44 +6049,21 @@ def reverse(self):
self._segments[0].start = prepoint
return self

def _subpath_indices(self):
"""
Returns the indexes of Move segments assuming that the first segment is a Move.
This can be used to count subpaths or to get segment start and end numbers for subpaths.
"""
if len(self._segments) == 0:
return
yield 0
last = self._segments[0]
for i in range(1, len(self._segments)):
if isinstance(self._segments[i], Move) or isinstance(last, Close):
yield i
last = self._segments[i]

def subpath(self, index):
if index < 0:
raise IndexError("Subpath negative index")
start = None
for i, end in enumerate(self._subpath_indices()):
if i > index:
return Subpath(self, start, end - 1)
start = end
if i == index:
return Subpath(self, start, len(self) - 1)
else:
raise IndexError("Subpath index out of range")
subpaths = list(self.as_subpaths())
return subpaths[index]

def count_subpaths(self):
return len(tuple(self._subpath_indices()))
subpaths = list(self.as_subpaths())
return len(subpaths)

def as_subpaths(self):
last = None
for end in self._subpath_indices():
if last is not None:
yield Subpath(self, last, end - 1)
last = end
yield Subpath(self, end, len(self) - 1)
last = 0
for current, seg in enumerate(self):
if current != last and isinstance(seg, Move):
yield Subpath(self, last, current - 1)
last = current
yield Subpath(self, last, len(self) - 1)

def as_points(self):
"""Returns the list of defining points within path"""
Expand Down
5 changes: 5 additions & 0 deletions test/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def test_subpaths(self):
self.assertEqual(p.d(), "M 0,100 L 50,50 L 100,0")
self.assertLessEqual(i, 1)

def test_subpath_degenerate(self):
path = Path("")
for i, p in enumerate(path.as_subpaths()):
pass

def test_subpaths_no_move(self):
path = Path("M0,0 50,0 50,50 0,50 Z L0,100 100,100 100,0")
for i, p in enumerate(path.as_subpaths()):
Expand Down

0 comments on commit 33d3c9e

Please sign in to comment.