Merge pull request #56 from DmitryMilk/polyline-adjust-to-specification

Polyline/polygon parsing adjusted to specification
pull/57/head
Andy Port 2018-05-28 18:50:12 -07:00 committed by GitHub
commit 6da601f8a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 30 deletions

View File

@ -5,10 +5,16 @@ The main tool being the svg2paths() function."""
from __future__ import division, absolute_import, print_function
from xml.dom.minidom import parse
from os import path as os_path, getcwd
import re
# Internal dependencies
from .parser import parse_path
COORD_PAIR_TMPLT = re.compile(
r'([\+-]?\d*[\.\d]\d*[eE][\+-]?\d+|[\+-]?\d*[\.\d]\d*)' +
r'(?:\s*,\s*|\s+|(?=-))' +
r'([\+-]?\d*[\.\d]\d*[eE][\+-]?\d+|[\+-]?\d*[\.\d]\d*)'
)
def ellipse2pathd(ellipse):
"""converts the parameters from an ellipse or a circle to a string for a
@ -37,19 +43,21 @@ def ellipse2pathd(ellipse):
return d
def polyline2pathd(polyline_d):
def polyline2pathd(polyline_d, is_polygon=False):
"""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()
points = COORD_PAIR_TMPLT.findall(polyline_d)
closed = (float(points[0][0]) == float(points[-1][0]) and
float(points[0][1]) == float(points[-1][1]))
closed = points[0] == points[-1]
# 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 is_polygon and closed:
points.append(points[0])
d = 'M' + points.pop(0).replace(',', ' ')
for p in points:
d += 'L' + p.replace(',', ' ')
if closed:
d = 'M' + 'L'.join('{0} {1}'.format(x,y) for x,y in points)
if is_polygon or closed:
d += 'z'
return d
@ -59,23 +67,7 @@ def polygon2pathd(polyline_d):
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'
return polyline2pathd(polyline_d, True)
def rect2pathd(rect):

View File

@ -1,5 +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"/>
<polygon points="0,0.0 0,-100 , 1.0e-1-1e2,0,0" style="stroke:purple;stroke-width:1"/>
</svg>

Before

Width:  |  Height:  |  Size: 422 B

After

Width:  |  Height:  |  Size: 431 B

View File

@ -20,9 +20,9 @@ class TestSVG2Paths(unittest.TestCase):
# 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),
path_correct = Path(Line(0+0j, 0-100j),
Line(0-100j, 0.1-100j),
Line(0.1-100j, 0+0j),
Line(0+0j, 0+0j) # result of redundant point
)
self.assertTrue(path.isclosed())