Made fork of svg2paths.py up to date.

pull/23/head
alphanoob1337 2017-05-17 17:46:25 +02:00
parent ecdade1be3
commit ddc3e6c7b1
1 changed files with 64 additions and 27 deletions

View File

@ -12,23 +12,6 @@ from .parser import parse_path
from .path import Path, bpoints2bezier
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"""
@ -56,6 +39,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.
@ -81,28 +81,54 @@ 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 = 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
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
dictionaries containing their attributes. This currently supports
SVG Path, Line, Polyline, Polygon, Circle, and Ellipse elements.
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)
elements (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.
convert_rectangles_to_paths (bool): Set to False to exclude SVG-Rect
elements (converted to Paths).
Returns:
list: The list of Path objects.
list: The list of corresponding path attribute dictionaries.
@ -238,10 +264,17 @@ def svg2paths(svg_file_location,
'L' + line['x2'] + ' ' + line['y2'])
path = parse_path(d_string)
return [path] + ret_list, [line] + attribute_dictionary_list_int
elif convert_ellipses_to_paths and (
node.nodeName == 'ellipse' or node.nodeName == 'circle'):
elif convert_ellipses_to_paths and node.nodeName == 'ellipse':
attrs = dom2dict(node)
path = parse_path(ellipse2pathd(attrs))
return [path] + ret_list, [attrs] + attribute_dictionary_list_int
elif convert_circles_to_paths and node.nodeName == 'circle':
attrs = dom2dict(node)
path = parse_path(ellipse2pathd(attrs))
return [path] + ret_list, [attrs] + attribute_dictionary_list_int
elif convert_rectangles_to_paths and node.nodeName == 'rect':
attrs = dom2dict(node)
path = parse_path(rect2pathd(attrs))
return [path] + ret_list, [attrs] + attribute_dictionary_list_int
else:
return ret_list, attribute_dictionary_list_int
@ -258,16 +291,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)