Made fork of svg2paths.py up to date.
parent
ecdade1be3
commit
ddc3e6c7b1
|
@ -12,23 +12,6 @@ from .parser import parse_path
|
||||||
from .path import Path, bpoints2bezier
|
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):
|
def ellipse2pathd(ellipse):
|
||||||
"""converts the parameters from an ellipse or a circle to a string for a
|
"""converts the parameters from an ellipse or a circle to a string for a
|
||||||
Path object d-attribute"""
|
Path object d-attribute"""
|
||||||
|
@ -56,6 +39,23 @@ def ellipse2pathd(ellipse):
|
||||||
return d
|
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):
|
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.
|
Path object d-attribute.
|
||||||
|
@ -81,28 +81,54 @@ def polygon2pathd(polyline_d):
|
||||||
return d + 'z'
|
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,
|
def svg2paths(svg_file_location,
|
||||||
return_svg_attributes=False,
|
return_svg_attributes=False,
|
||||||
|
convert_circles_to_paths=True,
|
||||||
|
convert_ellipses_to_paths=True,
|
||||||
convert_lines_to_paths=True,
|
convert_lines_to_paths=True,
|
||||||
convert_polylines_to_paths=True,
|
convert_polylines_to_paths=True,
|
||||||
convert_polygons_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 into a list of Path objects and attribute dictionaries.
|
||||||
Converts an SVG file into a list of Path objects and a list of
|
Converts an SVG file into a list of Path objects and a list of
|
||||||
dictionaries containing their attributes. This currently supports
|
dictionaries containing their attributes. This currently supports
|
||||||
SVG Path, Line, Polyline, Polygon, Circle, and Ellipse elements.
|
SVG Path, Line, Polyline, Polygon, Circle, and Ellipse elements.
|
||||||
Args:
|
Args:
|
||||||
svg_file_location (string): the location of the svg file
|
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)
|
(converted to Paths)
|
||||||
convert_polylines_to_paths (bool): Set to False to exclude SVG-Polyline
|
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
|
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
|
return_svg_attributes (bool): Set to True and a dictionary of
|
||||||
svg-attributes will be extracted and returned
|
svg-attributes will be extracted and returned
|
||||||
convert_ellipses_to_paths (bool): Set to False to exclude SVG-Ellipse
|
convert_rectangles_to_paths (bool): Set to False to exclude SVG-Rect
|
||||||
objects (converted to Paths). Circles are treated as ellipses.
|
elements (converted to Paths).
|
||||||
Returns:
|
Returns:
|
||||||
list: The list of Path objects.
|
list: The list of Path objects.
|
||||||
list: The list of corresponding path attribute dictionaries.
|
list: The list of corresponding path attribute dictionaries.
|
||||||
|
@ -238,10 +264,17 @@ def svg2paths(svg_file_location,
|
||||||
'L' + line['x2'] + ' ' + line['y2'])
|
'L' + line['x2'] + ' ' + line['y2'])
|
||||||
path = parse_path(d_string)
|
path = parse_path(d_string)
|
||||||
return [path] + ret_list, [line] + attribute_dictionary_list_int
|
return [path] + ret_list, [line] + attribute_dictionary_list_int
|
||||||
elif convert_ellipses_to_paths and (
|
elif convert_ellipses_to_paths and node.nodeName == 'ellipse':
|
||||||
node.nodeName == 'ellipse' or node.nodeName == 'circle'):
|
|
||||||
attrs = dom2dict(node)
|
attrs = dom2dict(node)
|
||||||
path = parse_path(ellipse2pathd(attrs))
|
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
|
return [path] + ret_list, [attrs] + attribute_dictionary_list_int
|
||||||
else:
|
else:
|
||||||
return ret_list, attribute_dictionary_list_int
|
return ret_list, attribute_dictionary_list_int
|
||||||
|
@ -258,16 +291,20 @@ def svg2paths(svg_file_location,
|
||||||
|
|
||||||
def svg2paths2(svg_file_location,
|
def svg2paths2(svg_file_location,
|
||||||
return_svg_attributes=True,
|
return_svg_attributes=True,
|
||||||
|
convert_circles_to_paths=True,
|
||||||
|
convert_ellipses_to_paths=True,
|
||||||
convert_lines_to_paths=True,
|
convert_lines_to_paths=True,
|
||||||
convert_polylines_to_paths=True,
|
convert_polylines_to_paths=True,
|
||||||
convert_polygons_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
|
"""Convenience function; identical to svg2paths() except that
|
||||||
return_svg_attributes=True by default. See svg2paths() docstring for more
|
return_svg_attributes=True by default. See svg2paths() docstring for more
|
||||||
info."""
|
info."""
|
||||||
return svg2paths(svg_file_location=svg_file_location,
|
return svg2paths(svg_file_location=svg_file_location,
|
||||||
return_svg_attributes=return_svg_attributes,
|
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_lines_to_paths=convert_lines_to_paths,
|
||||||
convert_polylines_to_paths=convert_polylines_to_paths,
|
convert_polylines_to_paths=convert_polylines_to_paths,
|
||||||
convert_polygons_to_paths=convert_polygons_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)
|
||||||
|
|
Loading…
Reference in New Issue