From f72987d69b053af82d70301ca90c0ed4b75a9c7b Mon Sep 17 00:00:00 2001 From: derVedro Date: Fri, 31 Mar 2017 00:17:59 +0200 Subject: [PATCH 01/14] fixed ignored height and width arguments issue #17 fix --- svgpathtools/paths2svg.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/svgpathtools/paths2svg.py b/svgpathtools/paths2svg.py index 4854d4e..7f61957 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("height", "100%") + szy = svg_attributes.get("width", "100%") + dwg = Drawing(filename=filename, size=(szx, szy), **svg_attributes) else: dwg = Drawing(filename=filename, size=(szx, szy), viewBox=viewbox) From 88185419f53ba112554d0176ac5c4b69f58cbaba Mon Sep 17 00:00:00 2001 From: derVedro Date: Fri, 31 Mar 2017 00:26:02 +0200 Subject: [PATCH 02/14] fliped width and height the x and y were swaped by my mistake --- svgpathtools/paths2svg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/svgpathtools/paths2svg.py b/svgpathtools/paths2svg.py index 7f61957..492911f 100644 --- a/svgpathtools/paths2svg.py +++ b/svgpathtools/paths2svg.py @@ -278,8 +278,8 @@ def disvg(paths=None, colors=None, # Create an SVG file if svg_attributes: - szx = svg_attributes.get("height", "100%") - szy = svg_attributes.get("width", "100%") + szx = svg_attributes.get("width", "100%") + szy = svg_attributes.get("height", "100%") dwg = Drawing(filename=filename, size=(szx, szy), **svg_attributes) else: dwg = Drawing(filename=filename, size=(szx, szy), viewBox=viewbox) From d8dfbd01fc944777eff603ba458f165856261077 Mon Sep 17 00:00:00 2001 From: Andy Port Date: Thu, 6 Apr 2017 16:05:28 -0700 Subject: [PATCH 03/14] changed default (when svg_attributes doesn't have a height/width) to the szx and szy constructed from path bounding boxes @DerVedro was there a reason you thought the default should be 100% (beyond that being the current behavior -- which I see as a bug)? --- svgpathtools/paths2svg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/svgpathtools/paths2svg.py b/svgpathtools/paths2svg.py index 492911f..935c5ec 100644 --- a/svgpathtools/paths2svg.py +++ b/svgpathtools/paths2svg.py @@ -278,8 +278,8 @@ def disvg(paths=None, colors=None, # Create an SVG file if svg_attributes: - szx = svg_attributes.get("width", "100%") - szy = svg_attributes.get("height", "100%") + 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) From 2a24cf640d761b5bf609cd448c7fa362cfebb200 Mon Sep 17 00:00:00 2001 From: Juan Pablo Contreras Date: Fri, 7 Apr 2017 09:12:04 +0200 Subject: [PATCH 04/14] Added functionality to process both circles and ellipses. Added tests related to the functionality. --- svgpathtools/svg2paths.py | 41 ++++++++++++++++++++++++++++++++++++--- test/circle.svg | 4 ++++ test/ellipse.svg | 4 ++++ test/test_svg2paths.py | 22 +++++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 test/circle.svg create mode 100644 test/ellipse.svg 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()) From 7b4bd38b21ffef69575f0ef340dbce6452280ee9 Mon Sep 17 00:00:00 2001 From: Juan Pablo Contreras Franco Date: Sun, 23 Apr 2017 07:38:24 +0200 Subject: [PATCH 05/14] Small syntax error in the docstring. --- svgpathtools/svg2paths.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/svgpathtools/svg2paths.py b/svgpathtools/svg2paths.py index cf768d2..be9ef1d 100644 --- a/svgpathtools/svg2paths.py +++ b/svgpathtools/svg2paths.py @@ -11,7 +11,7 @@ from .parser import parse_path 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(' ,', ',') @@ -55,8 +55,8 @@ def ellipse2pathd(ellipse): def polygon2pathd(polyline_d): - Path object d-attribute. - Note: For a polygon made from n points, the resulting path will be + """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(' ,', ',') @@ -67,7 +67,7 @@ def polygon2pathd(polyline_d): 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. From 216965e6abdfe16775330aff248c6692f2c25a8b Mon Sep 17 00:00:00 2001 From: Juan Pablo Contreras Franco Date: Sun, 23 Apr 2017 07:50:43 +0200 Subject: [PATCH 06/14] Corrected wrongly deleted docstring line --- svgpathtools/svg2paths.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/svgpathtools/svg2paths.py b/svgpathtools/svg2paths.py index cf768d2..8d7225e 100644 --- a/svgpathtools/svg2paths.py +++ b/svgpathtools/svg2paths.py @@ -11,7 +11,7 @@ from .parser import parse_path 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(' ,', ',') @@ -55,8 +55,9 @@ def ellipse2pathd(ellipse): 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 + 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(' ,', ',') @@ -67,7 +68,7 @@ def polygon2pathd(polyline_d): 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. From 900d5ba93b36ffdbf92603064a4712a166dae544 Mon Sep 17 00:00:00 2001 From: Andy Date: Sun, 23 Apr 2017 00:45:41 -0700 Subject: [PATCH 07/14] fixed typo and long line in docstrings --- svgpathtools/svg2paths.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/svgpathtools/svg2paths.py b/svgpathtools/svg2paths.py index 8d7225e..6587c6d 100644 --- a/svgpathtools/svg2paths.py +++ b/svgpathtools/svg2paths.py @@ -28,8 +28,8 @@ def polyline2pathd(polyline_d): def ellipse2pathd(ellipse): - """converts the parameters from an ellipse or a circle to a string for a Path - object d-attribute""" + """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) @@ -87,7 +87,7 @@ def svg2paths(svg_file_location, """ 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, Circle and Ellipse, and Polygon elements. + SVG Path, Line, Polyline, Polygon, Circle, and Ellipse 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) From aa02116e8770927b56762d1efc84936f203cee52 Mon Sep 17 00:00:00 2001 From: Andy Date: Sun, 23 Apr 2017 01:12:12 -0700 Subject: [PATCH 08/14] prettify svg2paths docstring (Google style) --- svgpathtools/svg2paths.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/svgpathtools/svg2paths.py b/svgpathtools/svg2paths.py index 6587c6d..1b4a4fc 100644 --- a/svgpathtools/svg2paths.py +++ b/svgpathtools/svg2paths.py @@ -84,23 +84,29 @@ def svg2paths(svg_file_location, convert_polygons_to_paths=True, return_svg_attributes=False, convert_ellipses_to_paths=True): - """ + """Converts an SVG into a list of Path objects and attribute dictionaries. + 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, Polygon, Circle, and Ellipse 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) - :param convert_polylines_to_paths: Set to False to disclude SVG-Polyline - objects (converted to Paths) - :param convert_polygons_to_paths: Set to False to disclude SVG-Polygon - 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 + + Args: + svg_file_location (string): the location of the svg file + convert_lines_to_paths (bool): Set to False to disclude SVG-Line objects + (converted to Paths) + convert_polylines_to_paths (bool): Set to False to disclude SVG-Polyline + objects (converted to Paths) + convert_polygons_to_paths (bool): Set to False to disclude SVG-Polygon + objects (converted to Paths) + return_svg_attributes (bool): Set to True and a dictionary of + svg-attributes will be extracted and returned + convert_ellipses_to_paths (bool): Set to False to disclude SVG-Ellipse + objects (converted to Paths). Circles are treated as ellipses. + + Returns: + list: The list of Path objects. + list: The list of corresponding path attribute dictionaries. + dict (optional): A dictionary ofsvg-attributes. """ if os_path.dirname(svg_file_location) == '': svg_file_location = os_path.join(getcwd(), svg_file_location) From 1d1bec1877a937ac253f2b9a2098c684031a5876 Mon Sep 17 00:00:00 2001 From: Andy Date: Sun, 23 Apr 2017 01:14:59 -0700 Subject: [PATCH 09/14] add ellipse/circle option to svg2paths2 and change return_svg_attributes to be second argument. --- svgpathtools/svg2paths.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/svgpathtools/svg2paths.py b/svgpathtools/svg2paths.py index 1b4a4fc..7de2fed 100644 --- a/svgpathtools/svg2paths.py +++ b/svgpathtools/svg2paths.py @@ -79,10 +79,10 @@ def polygon2pathd(polyline_d): def svg2paths(svg_file_location, + return_svg_attributes=False, convert_lines_to_paths=True, convert_polylines_to_paths=True, convert_polygons_to_paths=True, - return_svg_attributes=False, convert_ellipses_to_paths=True): """Converts an SVG into a list of Path objects and attribute dictionaries. @@ -92,21 +92,21 @@ def svg2paths(svg_file_location, Args: svg_file_location (string): the location of the svg file - convert_lines_to_paths (bool): Set to False to disclude SVG-Line objects + convert_lines_to_paths (bool): Set to False to exclude SVG-Line objects (converted to Paths) - convert_polylines_to_paths (bool): Set to False to disclude SVG-Polyline + convert_polylines_to_paths (bool): Set to False to exclude SVG-Polyline objects (converted to Paths) - convert_polygons_to_paths (bool): Set to False to disclude SVG-Polygon + convert_polygons_to_paths (bool): Set to False to exclude SVG-Polygon objects (converted to Paths) return_svg_attributes (bool): Set to True and a dictionary of svg-attributes will be extracted and returned - convert_ellipses_to_paths (bool): Set to False to disclude SVG-Ellipse + convert_ellipses_to_paths (bool): Set to False to exclude SVG-Ellipse objects (converted to Paths). Circles are treated as ellipses. Returns: list: The list of Path objects. list: The list of corresponding path attribute dictionaries. - dict (optional): A dictionary ofsvg-attributes. + dict (optional): A dictionary of svg-attributes (see `svg2paths2()`). """ if os_path.dirname(svg_file_location) == '': svg_file_location = os_path.join(getcwd(), svg_file_location) @@ -162,15 +162,17 @@ def svg2paths(svg_file_location, def svg2paths2(svg_file_location, + return_svg_attributes=True, convert_lines_to_paths=True, convert_polylines_to_paths=True, convert_polygons_to_paths=True, - return_svg_attributes=True): + convert_ellipses_to_paths=True): """Convenience function; identical to svg2paths() except that return_svg_attributes=True by default. See svg2paths() docstring for more info.""" return svg2paths(svg_file_location=svg_file_location, + return_svg_attributes=return_svg_attributes, convert_lines_to_paths=convert_lines_to_paths, convert_polylines_to_paths=convert_polylines_to_paths, convert_polygons_to_paths=convert_polygons_to_paths, - return_svg_attributes=return_svg_attributes) + convert_ellipses_to_paths=convert_ellipses_to_paths) From a3a529899cdafaa4551a64ea9b5b23e33a659d44 Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 26 Apr 2017 01:53:34 -0700 Subject: [PATCH 10/14] added rect element support to svg2paths --- svgpathtools/svg2paths.py | 94 +++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 28 deletions(-) diff --git a/svgpathtools/svg2paths.py b/svgpathtools/svg2paths.py index 7de2fed..668e2fe 100644 --- a/svgpathtools/svg2paths.py +++ b/svgpathtools/svg2paths.py @@ -10,23 +10,6 @@ from os import path as os_path, getcwd from .parser import parse_path -def polyline2pathd(polyline_d): - """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() - - closed = points[0] == points[-1] - - d = 'M' + points.pop(0).replace(',', ' ') - for p in points: - d += 'L' + p.replace(',', ' ') - if closed: - d += 'z' - return d - - def ellipse2pathd(ellipse): """converts the parameters from an ellipse or a circle to a string for a Path object d-attribute""" @@ -54,6 +37,23 @@ def ellipse2pathd(ellipse): return d +def polyline2pathd(polyline_d): + """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() + + closed = points[0] == points[-1] + + d = 'M' + points.pop(0).replace(',', ' ') + for p in points: + d += 'L' + p.replace(',', ' ') + if closed: + d += 'z' + return d + + def polygon2pathd(polyline_d): """converts the string from a polygon points-attribute to a string for a Path object d-attribute. @@ -78,12 +78,30 @@ def polygon2pathd(polyline_d): return d + 'z' +def rect2pathd(rect): + """Converts an SVG-rect element to a Path d-string. + + The rectangle will start at the (x,y) coordinate specified by the rectangle + object and proceed counter-clockwise.""" + x0, y0 = rect['x'], rect['y'] + w, h = rect["width"], rect["height"] + x1, y1 = x0 + w, y0 + x2, y2 = x0 + w, y0 + h + x3, y3 = x0, y0 + h + + d = ("M{} {} L {} {} L {} {} L {} {} z" + "".format(x0, y0, x1, y1, x2, y2, x3, y3)) + return d + + def svg2paths(svg_file_location, return_svg_attributes=False, + convert_circles_to_paths=True, + convert_ellipses_to_paths=True, convert_lines_to_paths=True, convert_polylines_to_paths=True, convert_polygons_to_paths=True, - convert_ellipses_to_paths=True): + convert_rectangles_to_paths=True): """Converts an SVG into a list of Path objects and attribute dictionaries. Converts an SVG file into a list of Path objects and a list of @@ -92,16 +110,23 @@ def svg2paths(svg_file_location, Args: svg_file_location (string): the location of the svg file - convert_lines_to_paths (bool): Set to False to exclude SVG-Line objects + return_svg_attributes (bool): Set to True and a dictionary of + svg-attributes will be extracted and returned. See also the + `svg2paths2()` function. + convert_circles_to_paths: Set to False to exclude SVG-Circle + elements (converted to Paths). By default circles are included as + paths of two `Arc` objects. + convert_ellipses_to_paths (bool): Set to False to exclude SVG-Ellipse + elements (converted to Paths). By default ellipses are included as + paths of two `Arc` objects. + convert_lines_to_paths (bool): Set to False to exclude SVG-Line elements (converted to Paths) convert_polylines_to_paths (bool): Set to False to exclude SVG-Polyline - objects (converted to Paths) + elements (converted to Paths) convert_polygons_to_paths (bool): Set to False to exclude SVG-Polygon - objects (converted to Paths) - return_svg_attributes (bool): Set to True and a dictionary of - svg-attributes will be extracted and returned - convert_ellipses_to_paths (bool): Set to False to exclude SVG-Ellipse - objects (converted to Paths). Circles are treated as ellipses. + elements (converted to Paths) + convert_rectangles_to_paths (bool): Set to False to exclude SVG-Rect + elements (converted to Paths). Returns: list: The list of Path objects. @@ -146,10 +171,19 @@ def svg2paths(svg_file_location, 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 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() @@ -163,16 +197,20 @@ def svg2paths(svg_file_location, def svg2paths2(svg_file_location, return_svg_attributes=True, + convert_circles_to_paths=True, + convert_ellipses_to_paths=True, convert_lines_to_paths=True, convert_polylines_to_paths=True, convert_polygons_to_paths=True, - convert_ellipses_to_paths=True): + convert_rectangles_to_paths=True): """Convenience function; identical to svg2paths() except that return_svg_attributes=True by default. See svg2paths() docstring for more info.""" return svg2paths(svg_file_location=svg_file_location, return_svg_attributes=return_svg_attributes, + convert_circles_to_paths=convert_circles_to_paths, + convert_ellipses_to_paths=convert_ellipses_to_paths, convert_lines_to_paths=convert_lines_to_paths, convert_polylines_to_paths=convert_polylines_to_paths, convert_polygons_to_paths=convert_polygons_to_paths, - convert_ellipses_to_paths=convert_ellipses_to_paths) + convert_rectangles_to_paths=convert_rectangles_to_paths) From a094f92b3a5541f28d63de164585064d4a312b21 Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 26 Apr 2017 01:54:44 -0700 Subject: [PATCH 11/14] added polyline and polygon functions (points --> Path of Line objects) --- svgpathtools/path.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/svgpathtools/path.py b/svgpathtools/path.py index 7afd58e..41989de 100644 --- a/svgpathtools/path.py +++ b/svgpathtools/path.py @@ -97,6 +97,21 @@ def bbox2path(xmin, xmax, ymin, ymax): return Path(b, r, t.reversed(), l.reversed()) +def polyline(*points): + """Converts a list of points to a Path composed of lines connecting those + points (i.e. a linear spline or polyline). See also `polygon()`.""" + return Path(*[Line(points[i], points[i+1]) + for i in range(len(points) - 1)]) + + +def polygon(*points): + """Converts a list of points to a Path composed of lines connecting those + points, then closes the path by connecting the last point to the first. + See also `polyline()`.""" + return Path(*[Line(points[i], points[(i + 1) % len(points)]) + for i in range(len(points))]) + + # Conversion################################################################### def bpoints2bezier(bpoints): From d6705471497dd649eb3ac886271c2c72aa13c3d7 Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 26 Apr 2017 02:20:11 -0700 Subject: [PATCH 12/14] added polygon() and polyline() to __init__ --- svgpathtools/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 38eeac858c97c1c50fb88fe03ae65f3768f068ce Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 26 Apr 2017 02:21:01 -0700 Subject: [PATCH 13/14] fixed support for rectangles when no x,y attributes are specified defaults to 0,0 --- svgpathtools/svg2paths.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/svgpathtools/svg2paths.py b/svgpathtools/svg2paths.py index 668e2fe..8f62932 100644 --- a/svgpathtools/svg2paths.py +++ b/svgpathtools/svg2paths.py @@ -83,8 +83,8 @@ def rect2pathd(rect): The rectangle will start at the (x,y) coordinate specified by the rectangle object and proceed counter-clockwise.""" - x0, y0 = rect['x'], rect['y'] - w, h = rect["width"], rect["height"] + x0, y0 = float(rect.get('x', 0)), float(rect.get('y', 0)) + w, h = float(rect["width"]), float(rect["height"]) x1, y1 = x0 + w, y0 x2, y2 = x0 + w, y0 + h x3, y3 = x0, y0 + h From e8367d463a070f8a33f282d0c03ed098b391eed2 Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 26 Apr 2017 02:21:43 -0700 Subject: [PATCH 14/14] added svg (planning to make unittest in future) TODO: create unittest for rect element support --- test/rects.svg | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 test/rects.svg 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