diff --git a/svgpathtools/svg2paths.py b/svgpathtools/svg2paths.py index df519a5..cf768d2 100644 --- a/svgpathtools/svg2paths.py +++ b/svgpathtools/svg2paths.py @@ -27,8 +27,34 @@ def polyline2pathd(polyline_d): return d +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 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).""" @@ -55,11 +81,12 @@ def svg2paths(svg_file_location, convert_lines_to_paths=True, convert_polylines_to_paths=True, convert_polygons_to_paths=True, - return_svg_attributes=False): + return_svg_attributes=False, + convert_ellipses_to_paths=True): """ Converts an SVG file into a list of Path objects and a list of dictionaries containing their attributes. This currently supports - SVG Path, Line, Polyline, and Polygon elements. + SVG Path, Line, Polyline, Circle and Ellipse, and Polygon elements. :param svg_file_location: the location of the svg file :param convert_lines_to_paths: Set to False to disclude SVG-Line objects (converted to Paths) @@ -69,6 +96,8 @@ def svg2paths(svg_file_location, objects (converted to Paths) :param return_svg_attributes: Set to True and a dictionary of svg-attributes will be extracted and returned + :param convert_ellipses_to_paths: Set to False to disclude SVG-Ellipse + objects (converted to Paths). Circles are treated as ellipses. :return: list of Path objects, list of path attribute dictionaries, and (optionally) a dictionary of svg-attributes """ @@ -108,6 +137,12 @@ def svg2paths(svg_file_location, 'L' + l['x2'] + ' ' + l['y2']) for l in lines] attribute_dictionary_list += lines + if convert_ellipses_to_paths: + ellipses = [dom2dict(el) for el in doc.getElementsByTagName('ellipse')] + ellipses += [dom2dict(el) for el in doc.getElementsByTagName('circle')] + d_strings += [ellipse2pathd(e) for e in ellipses] + attribute_dictionary_list += ellipses + 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/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())