fixed svg2path parsing of svg-polygon elements with redundant closure points

pull/7/merge
Andy 2017-03-16 01:58:48 -07:00
parent b019e30efd
commit 9423f5fdc3
4 changed files with 64 additions and 8 deletions

View File

@ -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'

View File

@ -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:

5
test/polygons.svg Normal file
View File

@ -0,0 +1,5 @@
<?xml version="1.0" ?>
<svg baseProfile="full" height="600px" version="1.1" viewBox="-10.05 -10.05 120.1 120.1" width="600px" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink">
<polygon points="55.5,0 55.5, 50 105.5,50" style="stroke:purple;stroke-width:1"/>
<polygon points="0,0 0,100 100,100 0,0" style="stroke:purple;stroke-width:1"/>
</svg>

After

Width:  |  Height:  |  Size: 422 B

30
test/test_svg2paths.py Normal file
View File

@ -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)