From 9423f5fdc3ea0824194173dd7ac21753aa8dbe91 Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 16 Mar 2017 01:58:48 -0700 Subject: [PATCH] fixed svg2path parsing of svg-polygon elements with redundant closure points --- setup.py | 2 +- svgpathtools/svg2paths.py | 35 ++++++++++++++++++++++++++++------- test/polygons.svg | 5 +++++ test/test_svg2paths.py | 30 ++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 test/polygons.svg create mode 100644 test/test_svg2paths.py diff --git a/setup.py b/setup.py index 10cea45..9ebcf95 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ import codecs import os -VERSION = '1.3.1' +VERSION = '1.3.2beta' AUTHOR_NAME = 'Andy Port' AUTHOR_EMAIL = 'AndyAPort@gmail.com' diff --git a/svgpathtools/svg2paths.py b/svgpathtools/svg2paths.py index f1ecbea..2085e75 100644 --- a/svgpathtools/svg2paths.py +++ b/svgpathtools/svg2paths.py @@ -12,16 +12,13 @@ from .parser import parse_path 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: @@ -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, @@ -87,7 +108,7 @@ def svg2paths(svg_file_location, # 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: diff --git a/test/polygons.svg b/test/polygons.svg new file mode 100644 index 0000000..ab6c9fd --- /dev/null +++ b/test/polygons.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/test/test_svg2paths.py b/test/test_svg2paths.py new file mode 100644 index 0000000..176d750 --- /dev/null +++ b/test/test_svg2paths.py @@ -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)