Merge pull request #21 from jpcofr/ellipse_d_squashed
Added functionality to extract circle and ellipse elements from SVG files. Circles and ellipses are treated as paths of two `Arc` objects.pull/14/merge
commit
9a1b1ff1e5
|
@ -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(' ,', ',')
|
||||
|
@ -27,10 +27,37 @@ 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
|
||||
"""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(' ,', ',')
|
||||
|
@ -41,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.
|
||||
|
@ -55,11 +82,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 +97,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 +138,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()
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" ?>
|
||||
<svg baseProfile="full" height="600px" version="1.1" viewBox="-10.05 -10.05 120.1 120.1" width="600px" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<circle cx="100" cy="100" r="50"/>
|
||||
</svg>
|
After Width: | Height: | Size: 294 B |
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" ?>
|
||||
<svg baseProfile="full" height="600px" version="1.1" viewBox="-10.05 -10.05 120.1 120.1" width="600px" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<ellipse cx="100" cy="100" rx="50" ry="50"/>
|
||||
</svg>
|
After Width: | Height: | Size: 304 B |
|
@ -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())
|
||||
|
|
Loading…
Reference in New Issue