2016-07-06 04:51:11 +00:00
|
|
|
"""This submodule contains tools for creating path objects from SVG files.
|
|
|
|
The main tool being the svg2paths() function."""
|
|
|
|
|
|
|
|
# External dependencies
|
|
|
|
from __future__ import division, absolute_import, print_function
|
|
|
|
from xml.dom.minidom import parse
|
2022-06-06 04:47:37 +00:00
|
|
|
import os
|
2022-05-22 12:37:27 +00:00
|
|
|
from io import StringIO
|
2018-05-27 08:47:44 +00:00
|
|
|
import re
|
2022-06-06 04:47:37 +00:00
|
|
|
try:
|
|
|
|
from os import PathLike as FilePathLike
|
|
|
|
except ImportError:
|
|
|
|
FilePathLike = str
|
2016-07-06 04:51:11 +00:00
|
|
|
|
|
|
|
# Internal dependencies
|
|
|
|
from .parser import parse_path
|
|
|
|
|
2018-05-29 02:46:38 +00:00
|
|
|
|
2018-05-27 08:47:44 +00:00
|
|
|
COORD_PAIR_TMPLT = re.compile(
|
|
|
|
r'([\+-]?\d*[\.\d]\d*[eE][\+-]?\d+|[\+-]?\d*[\.\d]\d*)' +
|
|
|
|
r'(?:\s*,\s*|\s+|(?=-))' +
|
|
|
|
r'([\+-]?\d*[\.\d]\d*[eE][\+-]?\d+|[\+-]?\d*[\.\d]\d*)'
|
|
|
|
)
|
2016-07-06 04:51:11 +00:00
|
|
|
|
2022-02-04 02:11:55 +00:00
|
|
|
|
2018-08-22 01:00:29 +00:00
|
|
|
def path2pathd(path):
|
|
|
|
return path.get('d', '')
|
2018-05-29 02:46:38 +00:00
|
|
|
|
2022-02-04 02:11:55 +00:00
|
|
|
|
2017-04-07 07:12:04 +00:00
|
|
|
def ellipse2pathd(ellipse):
|
2017-04-23 07:45:41 +00:00
|
|
|
"""converts the parameters from an ellipse or a circle to a string for a
|
|
|
|
Path object d-attribute"""
|
2017-04-07 07:12:04 +00:00
|
|
|
|
2018-11-13 23:43:09 +00:00
|
|
|
cx = ellipse.get('cx', 0)
|
|
|
|
cy = ellipse.get('cy', 0)
|
2017-04-07 07:12:04 +00:00
|
|
|
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'
|
|
|
|
|
2023-04-01 19:30:04 +00:00
|
|
|
return d + 'z'
|
2017-04-07 07:12:04 +00:00
|
|
|
|
|
|
|
|
2020-06-20 03:32:01 +00:00
|
|
|
def polyline2pathd(polyline, is_polygon=False):
|
2017-04-26 08:53:34 +00:00
|
|
|
"""converts the string from a polyline points-attribute to a string for a
|
|
|
|
Path object d-attribute"""
|
2020-07-07 00:33:24 +00:00
|
|
|
if isinstance(polyline, str):
|
|
|
|
points = polyline
|
|
|
|
else:
|
|
|
|
points = COORD_PAIR_TMPLT.findall(polyline.get('points', ''))
|
|
|
|
|
2018-05-27 08:47:44 +00:00
|
|
|
closed = (float(points[0][0]) == float(points[-1][0]) and
|
|
|
|
float(points[0][1]) == float(points[-1][1]))
|
2017-04-26 08:53:34 +00:00
|
|
|
|
2018-05-27 08:47:44 +00:00
|
|
|
# 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.
|
|
|
|
if is_polygon and closed:
|
|
|
|
points.append(points[0])
|
2017-04-26 08:53:34 +00:00
|
|
|
|
2018-05-27 08:47:44 +00:00
|
|
|
d = 'M' + 'L'.join('{0} {1}'.format(x,y) for x,y in points)
|
|
|
|
if is_polygon or closed:
|
2017-04-26 08:53:34 +00:00
|
|
|
d += 'z'
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
2020-06-20 03:32:01 +00:00
|
|
|
def polygon2pathd(polyline):
|
2018-05-29 02:46:38 +00:00
|
|
|
"""converts the string from a polygon points-attribute to a string
|
|
|
|
for a Path object d-attribute.
|
2017-04-23 05:50:43 +00:00
|
|
|
Note: For a polygon made from n points, the resulting path will be
|
2018-05-29 02:46:38 +00:00
|
|
|
composed of n lines (even if some of these lines have length zero).
|
|
|
|
"""
|
2020-06-20 03:32:01 +00:00
|
|
|
return polyline2pathd(polyline, True)
|
2017-03-16 08:58:48 +00:00
|
|
|
|
|
|
|
|
2017-04-26 08:53:34 +00:00
|
|
|
def rect2pathd(rect):
|
|
|
|
"""Converts an SVG-rect element to a Path d-string.
|
|
|
|
|
2018-05-29 02:46:38 +00:00
|
|
|
The rectangle will start at the (x,y) coordinate specified by the
|
|
|
|
rectangle object and proceed counter-clockwise."""
|
2022-01-26 01:23:04 +00:00
|
|
|
x, y = float(rect.get('x', 0)), float(rect.get('y', 0))
|
2018-08-22 01:00:29 +00:00
|
|
|
w, h = float(rect.get('width', 0)), float(rect.get('height', 0))
|
2022-01-26 01:23:04 +00:00
|
|
|
if 'rx' in rect or 'ry' in rect:
|
2022-02-04 02:09:09 +00:00
|
|
|
|
|
|
|
# if only one, rx or ry, is present, use that value for both
|
|
|
|
# https://developer.mozilla.org/en-US/docs/Web/SVG/Element/rect
|
|
|
|
rx = rect.get('rx', None)
|
|
|
|
ry = rect.get('ry', None)
|
|
|
|
if rx is None:
|
|
|
|
rx = ry or 0.
|
|
|
|
if ry is None:
|
|
|
|
ry = rx or 0.
|
|
|
|
rx, ry = float(rx), float(ry)
|
|
|
|
|
2022-02-04 02:10:00 +00:00
|
|
|
d = "M {} {} ".format(x + rx, y) # right of p0
|
|
|
|
d += "L {} {} ".format(x + w - rx, y) # go to p1
|
|
|
|
d += "A {} {} 0 0 1 {} {} ".format(rx, ry, x+w, y+ry) # arc for p1
|
|
|
|
d += "L {} {} ".format(x+w, y+h-ry) # above p2
|
|
|
|
d += "A {} {} 0 0 1 {} {} ".format(rx, ry, x+w-rx, y+h) # arc for p2
|
|
|
|
d += "L {} {} ".format(x+rx, y+h) # right of p3
|
|
|
|
d += "A {} {} 0 0 1 {} {} ".format(rx, ry, x, y+h-ry) # arc for p3
|
|
|
|
d += "L {} {} ".format(x, y+ry) # below p0
|
|
|
|
d += "A {} {} 0 0 1 {} {} z".format(rx, ry, x+rx, y) # arc for p0
|
2022-01-26 01:23:04 +00:00
|
|
|
return d
|
|
|
|
|
|
|
|
x0, y0 = x, y
|
|
|
|
x1, y1 = x + w, y
|
|
|
|
x2, y2 = x + w, y + h
|
|
|
|
x3, y3 = x, y + h
|
2017-04-26 08:53:34 +00:00
|
|
|
|
|
|
|
d = ("M{} {} L {} {} L {} {} L {} {} z"
|
|
|
|
"".format(x0, y0, x1, y1, x2, y2, x3, y3))
|
2022-01-26 01:23:04 +00:00
|
|
|
|
2017-04-26 08:53:34 +00:00
|
|
|
return d
|
|
|
|
|
2020-06-20 01:58:25 +00:00
|
|
|
|
2018-08-22 01:00:29 +00:00
|
|
|
def line2pathd(l):
|
2020-06-20 01:58:25 +00:00
|
|
|
return (
|
|
|
|
'M' + l.attrib.get('x1', '0') + ' ' + l.attrib.get('y1', '0')
|
|
|
|
+ 'L' + l.attrib.get('x2', '0') + ' ' + l.attrib.get('y2', '0')
|
|
|
|
)
|
|
|
|
|
2017-04-26 08:53:34 +00:00
|
|
|
|
2022-05-25 15:39:10 +00:00
|
|
|
def svg2paths(svg_file_location,
|
2017-04-23 08:14:59 +00:00
|
|
|
return_svg_attributes=False,
|
2017-04-26 08:53:34 +00:00
|
|
|
convert_circles_to_paths=True,
|
|
|
|
convert_ellipses_to_paths=True,
|
2016-07-16 05:10:59 +00:00
|
|
|
convert_lines_to_paths=True,
|
|
|
|
convert_polylines_to_paths=True,
|
|
|
|
convert_polygons_to_paths=True,
|
2017-04-26 08:53:34 +00:00
|
|
|
convert_rectangles_to_paths=True):
|
2017-04-23 08:12:12 +00:00
|
|
|
"""Converts an SVG into a list of Path objects and attribute dictionaries.
|
|
|
|
|
2016-07-06 04:51:11 +00:00
|
|
|
Converts an SVG file into a list of Path objects and a list of
|
|
|
|
dictionaries containing their attributes. This currently supports
|
2017-04-23 07:45:41 +00:00
|
|
|
SVG Path, Line, Polyline, Polygon, Circle, and Ellipse elements.
|
2017-04-23 08:12:12 +00:00
|
|
|
|
|
|
|
Args:
|
2022-05-25 15:39:10 +00:00
|
|
|
svg_file_location (string or file-like object): the location of the
|
2022-05-22 11:09:13 +00:00
|
|
|
svg file on disk or a file-like object containing the content of a
|
|
|
|
svg file
|
2017-04-26 08:53:34 +00:00
|
|
|
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
|
2017-04-23 08:12:12 +00:00
|
|
|
(converted to Paths)
|
2017-04-23 08:14:59 +00:00
|
|
|
convert_polylines_to_paths (bool): Set to False to exclude SVG-Polyline
|
2017-04-26 08:53:34 +00:00
|
|
|
elements (converted to Paths)
|
2017-04-23 08:14:59 +00:00
|
|
|
convert_polygons_to_paths (bool): Set to False to exclude SVG-Polygon
|
2017-04-26 08:53:34 +00:00
|
|
|
elements (converted to Paths)
|
|
|
|
convert_rectangles_to_paths (bool): Set to False to exclude SVG-Rect
|
|
|
|
elements (converted to Paths).
|
2017-04-23 08:12:12 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
list: The list of Path objects.
|
|
|
|
list: The list of corresponding path attribute dictionaries.
|
2017-04-23 08:14:59 +00:00
|
|
|
dict (optional): A dictionary of svg-attributes (see `svg2paths2()`).
|
2016-07-06 04:51:11 +00:00
|
|
|
"""
|
2022-05-22 11:09:13 +00:00
|
|
|
# strings are interpreted as file location everything else is treated as
|
|
|
|
# file-like object and passed to the xml parser directly
|
2022-06-06 04:47:37 +00:00
|
|
|
from_filepath = isinstance(svg_file_location, str) or isinstance(svg_file_location, FilePathLike)
|
|
|
|
svg_file_location = os.path.abspath(svg_file_location) if from_filepath else svg_file_location
|
2016-07-06 04:51:11 +00:00
|
|
|
|
2022-05-25 15:39:10 +00:00
|
|
|
doc = parse(svg_file_location)
|
2016-07-06 04:51:11 +00:00
|
|
|
|
|
|
|
def dom2dict(element):
|
|
|
|
"""Converts DOM elements to dictionaries of attributes."""
|
2017-03-01 07:04:37 +00:00
|
|
|
keys = list(element.attributes.keys())
|
|
|
|
values = [val.value for val in list(element.attributes.values())]
|
|
|
|
return dict(list(zip(keys, values)))
|
2016-07-06 04:51:11 +00:00
|
|
|
|
|
|
|
# Use minidom to extract path strings from input SVG
|
|
|
|
paths = [dom2dict(el) for el in doc.getElementsByTagName('path')]
|
|
|
|
d_strings = [el['d'] for el in paths]
|
|
|
|
attribute_dictionary_list = paths
|
|
|
|
|
|
|
|
# Use minidom to extract polyline strings from input SVG, convert to
|
|
|
|
# path strings, add to list
|
|
|
|
if convert_polylines_to_paths:
|
|
|
|
plins = [dom2dict(el) for el in doc.getElementsByTagName('polyline')]
|
2020-06-20 03:32:01 +00:00
|
|
|
d_strings += [polyline2pathd(pl) for pl in plins]
|
2016-07-06 04:51:11 +00:00
|
|
|
attribute_dictionary_list += plins
|
|
|
|
|
|
|
|
# Use minidom to extract polygon strings from input SVG, convert to
|
|
|
|
# path strings, add to list
|
|
|
|
if convert_polygons_to_paths:
|
|
|
|
pgons = [dom2dict(el) for el in doc.getElementsByTagName('polygon')]
|
2020-06-20 03:32:01 +00:00
|
|
|
d_strings += [polygon2pathd(pg) for pg in pgons]
|
2016-07-06 04:51:11 +00:00
|
|
|
attribute_dictionary_list += pgons
|
|
|
|
|
|
|
|
if convert_lines_to_paths:
|
|
|
|
lines = [dom2dict(el) for el in doc.getElementsByTagName('line')]
|
|
|
|
d_strings += [('M' + l['x1'] + ' ' + l['y1'] +
|
|
|
|
'L' + l['x2'] + ' ' + l['y2']) for l in lines]
|
|
|
|
attribute_dictionary_list += lines
|
2016-07-16 05:10:59 +00:00
|
|
|
|
2017-04-07 07:12:04 +00:00
|
|
|
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
|
|
|
|
|
2017-04-26 08:53:34 +00:00
|
|
|
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
|
|
|
|
|
2016-07-16 05:10:59 +00:00
|
|
|
if return_svg_attributes:
|
|
|
|
svg_attributes = dom2dict(doc.getElementsByTagName('svg')[0])
|
|
|
|
doc.unlink()
|
|
|
|
path_list = [parse_path(d) for d in d_strings]
|
|
|
|
return path_list, attribute_dictionary_list, svg_attributes
|
|
|
|
else:
|
|
|
|
doc.unlink()
|
|
|
|
path_list = [parse_path(d) for d in d_strings]
|
|
|
|
return path_list, attribute_dictionary_list
|
|
|
|
|
|
|
|
|
2022-05-25 15:39:10 +00:00
|
|
|
def svg2paths2(svg_file_location,
|
2017-04-23 08:14:59 +00:00
|
|
|
return_svg_attributes=True,
|
2017-04-26 08:53:34 +00:00
|
|
|
convert_circles_to_paths=True,
|
|
|
|
convert_ellipses_to_paths=True,
|
2016-07-16 05:10:59 +00:00
|
|
|
convert_lines_to_paths=True,
|
|
|
|
convert_polylines_to_paths=True,
|
|
|
|
convert_polygons_to_paths=True,
|
2017-04-26 08:53:34 +00:00
|
|
|
convert_rectangles_to_paths=True):
|
2016-07-16 05:10:59 +00:00
|
|
|
"""Convenience function; identical to svg2paths() except that
|
|
|
|
return_svg_attributes=True by default. See svg2paths() docstring for more
|
|
|
|
info."""
|
2022-05-25 15:39:10 +00:00
|
|
|
return svg2paths(svg_file_location=svg_file_location,
|
2017-04-23 08:14:59 +00:00
|
|
|
return_svg_attributes=return_svg_attributes,
|
2017-04-26 08:53:34 +00:00
|
|
|
convert_circles_to_paths=convert_circles_to_paths,
|
|
|
|
convert_ellipses_to_paths=convert_ellipses_to_paths,
|
2016-07-16 05:10:59 +00:00
|
|
|
convert_lines_to_paths=convert_lines_to_paths,
|
|
|
|
convert_polylines_to_paths=convert_polylines_to_paths,
|
|
|
|
convert_polygons_to_paths=convert_polygons_to_paths,
|
2017-04-26 08:53:34 +00:00
|
|
|
convert_rectangles_to_paths=convert_rectangles_to_paths)
|
2022-05-22 12:37:27 +00:00
|
|
|
|
|
|
|
|
2022-05-25 17:24:50 +00:00
|
|
|
def svgstr2paths(svg_string,
|
2022-05-22 12:37:27 +00:00
|
|
|
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_rectangles_to_paths=True):
|
|
|
|
"""Convenience function; identical to svg2paths() except that it takes the
|
|
|
|
svg object as string. See svg2paths() docstring for more
|
|
|
|
info."""
|
|
|
|
# wrap string into StringIO object
|
|
|
|
svg_file_obj = StringIO(svg_string)
|
2022-05-25 15:39:10 +00:00
|
|
|
return svg2paths(svg_file_location=svg_file_obj,
|
2022-05-22 12:37:27 +00:00
|
|
|
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_rectangles_to_paths=convert_rectangles_to_paths)
|