diff --git a/svgpathtools/__init__.py b/svgpathtools/__init__.py index a57b18d..60d2561 100644 --- a/svgpathtools/__init__.py +++ b/svgpathtools/__init__.py @@ -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 diff --git a/svgpathtools/paths2svg.py b/svgpathtools/paths2svg.py index 4854d4e..935c5ec 100644 --- a/svgpathtools/paths2svg.py +++ b/svgpathtools/paths2svg.py @@ -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) diff --git a/svgpathtools/svg2paths.py b/svgpathtools/svg2paths.py index 75f16df..acc51a4 100644 --- a/svgpathtools/svg2paths.py +++ b/svgpathtools/svg2paths.py @@ -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() diff --git a/test/circle.svg b/test/circle.svg new file mode 100644 index 0000000..aa5b6f8 --- /dev/null +++ b/test/circle.svg @@ -0,0 +1,4 @@ + + + + diff --git a/test/ellipse.svg b/test/ellipse.svg new file mode 100644 index 0000000..299b17b --- /dev/null +++ b/test/ellipse.svg @@ -0,0 +1,4 @@ + + + + diff --git a/test/rects.svg b/test/rects.svg new file mode 100644 index 0000000..e852bb3 --- /dev/null +++ b/test/rects.svg @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/test/test_svg2paths.py b/test/test_svg2paths.py index 176d750..45a4b48 100644 --- a/test/test_svg2paths.py +++ b/test/test_svg2paths.py @@ -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())