Skip to content

Commit

Permalink
fixed svg2path parsing of svg-polygon elements with redundant closure…
Browse files Browse the repository at this point in the history
… points
  • Loading branch information
mathandy committed Mar 16, 2017
1 parent b019e30 commit 9423f5f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os


VERSION = '1.3.1'
VERSION = '1.3.2beta'
AUTHOR_NAME = 'Andy Port'
AUTHOR_EMAIL = '[email protected]'

Expand Down
35 changes: 28 additions & 7 deletions svgpathtools/svg2paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@


def polyline2pathd(polyline_d):
"""converts the string from a polyline d-attribute to a string for a Path
object d-attribute"""
"""converts the string from a polyline points-attribute to a string for a
Path object d-attribute"""
points = polyline_d.replace(', ', ',')
points = points.replace(' ,', ',')
points = points.split()

if points[0] == points[-1]:
closed = True
else:
closed = False
closed = points[0] == points[-1]

d = 'M' + points.pop(0).replace(',', ' ')
for p in points:
Expand All @@ -31,6 +28,30 @@ def polyline2pathd(polyline_d):
return d


def polygon2pathd(polyline_d):
"""converts the string from a polygon points-attribute to a string for a
Path object d-attribute.
Note: For a polygon made from n points, the resulting path will be
composed of n lines (even if some of these lines have length zero)."""
points = polyline_d.replace(', ', ',')
points = points.replace(' ,', ',')
points = points.split()

reduntantly_closed = points[0] == points[-1]

d = 'M' + points[0].replace(',', ' ')
for p in points[1:]:
d += 'L' + p.replace(',', ' ')

# The `parse_path` call ignores redundant 'z' (closure) commands
# e.g. `parse_path('M0 0L100 100Z') == parse_path('M0 0L100 100L0 0Z')`
# This check ensures that an n-point polygon is converted to an n-Line path.
if reduntantly_closed:
d += 'L' + points[0].replace(',', ' ')

return d + 'z'


def svg2paths(svg_file_location,
convert_lines_to_paths=True,
convert_polylines_to_paths=True,
Expand Down Expand Up @@ -87,7 +108,7 @@ def dom2dict(element):
# path strings, add to list
if convert_polygons_to_paths:
pgons = [dom2dict(el) for el in doc.getElementsByTagName('polygon')]
d_strings += [polyline2pathd(pg['points']) + 'z' for pg in pgons]
d_strings += [polygon2pathd(pg['points']) for pg in pgons]
attribute_dictionary_list += pgons

if convert_lines_to_paths:
Expand Down
5 changes: 5 additions & 0 deletions test/polygons.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions test/test_svg2paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from __future__ import division, absolute_import, print_function
import unittest
from svgpathtools import *
from os.path import join, dirname

class TestSVG2Paths(unittest.TestCase):
def test_svg2paths_polygons(self):

paths, _ = svg2paths(join(dirname(__file__), 'polygons.svg'))

# triangular polygon test
path = paths[0]
path_correct = Path(Line(55.5+0j, 55.5+50j),
Line(55.5+50j, 105.5+50j),
Line(105.5+50j, 55.5+0j)
)
self.assertTrue(path.isclosed())
self.assertTrue(len(path)==3)
self.assertTrue(path==path_correct)

# triangular quadrilateral (with a redundant 4th "closure" point)
path = paths[1]
path_correct = Path(Line(0+0j, 0+100j),
Line(0+100j, 100+100j),
Line(100+100j, 0+0j),
Line(0+0j, 0+0j) # result of redundant point
)
self.assertTrue(path.isclosed())
self.assertTrue(len(path)==4)
self.assertTrue(path==path_correct)

0 comments on commit 9423f5f

Please sign in to comment.