Merge branch 'issue16' into master

pull/23/head
Andy Port 2017-09-15 00:20:10 -04:00 committed by GitHub
commit e03d519ca0
7 changed files with 86 additions and 6 deletions

View File

@ -6,7 +6,7 @@ from .path import (Path, Line, QuadraticBezier, CubicBezier, Arc,
bezier_segment, is_bezier_segment, is_path_segment,
is_bezier_path, concatpaths, poly2bez, bpoints2bezier,
closest_point_in_path, farthest_point_in_path,
path_encloses_pt, bbox2path)
path_encloses_pt, bbox2path, polygon, polyline)
from .parser import parse_path
from .paths2svg import disvg, wsvg
from .polytools import polyroots, polyroots01, rational_limit, real, imag

View File

@ -278,7 +278,9 @@ def disvg(paths=None, colors=None,
# Create an SVG file
if svg_attributes:
dwg = Drawing(filename=filename, **svg_attributes)
szx = svg_attributes.get("width", szx)
szy = svg_attributes.get("height", szy)
dwg = Drawing(filename=filename, size=(szx, szy), **svg_attributes)
else:
dwg = Drawing(filename=filename, size=(szx, szy), viewBox=viewbox)

View File

@ -12,6 +12,33 @@ from .parser import parse_path
from .path import Path, bpoints2bezier
def ellipse2pathd(ellipse):
"""converts the parameters from an ellipse or a circle to a string for a
Path object d-attribute"""
cx = ellipse.get('cx', None)
cy = ellipse.get('cy', None)
rx = ellipse.get('rx', None)
ry = ellipse.get('ry', None)
r = ellipse.get('r', None)
if r is not None:
rx = ry = float(r)
else:
rx = float(rx)
ry = float(ry)
cx = float(cx)
cy = float(cy)
d = ''
d += 'M' + str(cx - rx) + ',' + str(cy)
d += 'a' + str(rx) + ',' + str(ry) + ' 0 1,0 ' + str(2 * rx) + ',0'
d += 'a' + str(rx) + ',' + str(ry) + ' 0 1,0 ' + str(-2 * rx) + ',0'
return d
def ellipse2pathd(ellipse):
"""converts the parameters from an ellipse or a circle to a string for a
Path object d-attribute"""
@ -40,7 +67,7 @@ def ellipse2pathd(ellipse):
def polyline2pathd(polyline_d):
"""converts the string from a polyline points-attribute to a string for a
"""converts the string from a polyline points-attribute to a string for a
Path object d-attribute"""
points = polyline_d.replace(', ', ',')
points = points.replace(' ,', ',')
@ -57,9 +84,9 @@ def polyline2pathd(polyline_d):
def polygon2pathd(polyline_d):
"""converts the string from a polygon points-attribute to a string for a
"""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
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(' ,', ',')
@ -281,7 +308,21 @@ def svg2paths(svg_file_location,
else:
return ret_list, attribute_dictionary_list_int
path_list, attribute_dictionary_list = parse_node(doc)
if convert_ellipses_to_paths:
ellipses = [dom2dict(el) for el in doc.getElementsByTagName('ellipse')]
d_strings += [ellipse2pathd(e) for e in ellipses]
attribute_dictionary_list += ellipses
if convert_circles_to_paths:
circles = [dom2dict(el) for el in doc.getElementsByTagName('circle')]
d_strings += [ellipse2pathd(c) for c in circles]
attribute_dictionary_list += circles
if convert_rectangles_to_paths:
rectangles = [dom2dict(el) for el in doc.getElementsByTagName('rect')]
d_strings += [rect2pathd(r) for r in rectangles]
attribute_dictionary_list += rectangles
if return_svg_attributes:
svg_attributes = dom2dict(doc.getElementsByTagName('svg')[0])
doc.unlink()

4
test/circle.svg Normal file
View File

@ -0,0 +1,4 @@
<?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">
<circle cx="100" cy="100" r="50"/>
</svg>

After

Width:  |  Height:  |  Size: 294 B

4
test/ellipse.svg Normal file
View File

@ -0,0 +1,4 @@
<?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">
<ellipse cx="100" cy="100" rx="50" ry="50"/>
</svg>

After

Width:  |  Height:  |  Size: 304 B

7
test/rects.svg Normal file
View File

@ -0,0 +1,7 @@
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<rect width="300" height="100" stroke='red'/>
<rect x="200" y="200" width="300" height="100" stroke='blue'/>
</svg>

After

Width:  |  Height:  |  Size: 207 B

View File

@ -28,3 +28,25 @@ class TestSVG2Paths(unittest.TestCase):
self.assertTrue(path.isclosed())
self.assertTrue(len(path)==4)
self.assertTrue(path==path_correct)
def test_svg2paths_ellipses(self):
paths, _ = svg2paths(join(dirname(__file__), 'ellipse.svg'))
# ellipse tests
path_ellipse = paths[0]
path_ellipse_correct = Path(Arc(50+100j, 50+50j, 0.0, True, False, 150+100j),
Arc(150+100j, 50+50j, 0.0, True, False, 50+100j))
self.assertTrue(len(path_ellipse)==2)
self.assertTrue(path_ellipse==path_ellipse_correct)
self.assertTrue(path_ellipse.isclosed())
# circle tests
paths, _ = svg2paths(join(dirname(__file__), 'circle.svg'))
path_circle = paths[0]
path_circle_correct = Path(Arc(50+100j, 50+50j, 0.0, True, False, 150+100j),
Arc(150+100j, 50+50j, 0.0, True, False, 50+100j))
self.assertTrue(len(path_circle)==2)
self.assertTrue(path_circle==path_circle_correct)
self.assertTrue(path_circle.isclosed())