CairoSVG
CairoSVG copied to clipboard
RecursionError: maximum recursion depth exceeded
I'm generating svg images in a python code that I want to convert into png, but it fails. I could pinpoint that the problem is because of the use of the 'defs', 'g' and 'use' tags. I'm not sure if these tags are supported or something else goes wrong.
I have the following code:
svg_string = """<svg width="100" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<g id="square">
<rect x="0" y="0" width="20" height="20" style="stroke-width:1;stroke:cornflowerblue; fill: firebrick" />
</g>
</defs>
<use x="50" y="50" href="#square"/>
</svg>"""
cairosvg.svg2png(svg_string, write_to='test.png')
The output:
---------------------------------------------------------------------------
RecursionError Traceback (most recent call last)
<ipython-input-11-7da7c8c78336> in <module>()
----> 1 cairosvg.svg2png(svg_string, write_to='test.png')
/Users/ds26/anaconda/lib/python3.6/site-packages/cairosvg/__init__.py in <lambda>(*args, **kwargs)
43 # Two lambdas needed for the closure
44 lambda surface_type: lambda *args, **kwargs:
---> 45 surface_type.convert(*args, **kwargs))(_surface_type)
46 _name = 'svg2{}'.format(_output_format.lower())
47 _function.__name__ = _name
/Users/ds26/anaconda/lib/python3.6/site-packages/cairosvg/surface.py in convert(cls, bytestring, **kwargs)
136 output = write_to or io.BytesIO()
137 instance = cls(
--> 138 tree, output, dpi, None, parent_width, parent_height, scale)
139 instance.finish()
140 if write_to is None:
/Users/ds26/anaconda/lib/python3.6/site-packages/cairosvg/surface.py in __init__(self, tree, output, dpi, parent_surface, parent_width, parent_height, scale)
193 width, height, viewbox, scale, preserved_ratio(tree))
194 self.context.move_to(0, 0)
--> 195 self.draw(tree)
196
197 @property
/Users/ds26/anaconda/lib/python3.6/site-packages/cairosvg/surface.py in draw(self, node)
411 if display and node.tag not in INVISIBLE_TAGS:
412 for child in node.children:
--> 413 self.draw(child)
414
415 # Apply filter, mask and opacity
/Users/ds26/anaconda/lib/python3.6/site-packages/cairosvg/surface.py in draw(self, node)
351 if node.tag in TAGS:
352 try:
--> 353 TAGS[node.tag](self, node)
354 except PointError:
355 # Error in point parsing, do nothing
/Users/ds26/anaconda/lib/python3.6/site-packages/cairosvg/defs.py in use(surface, node)
376 tree['width'], tree['height'] = node['width'], node['height']
377
--> 378 surface.draw(tree)
379 node.get('fill', None)
380 node.get('stroke', None)
... last 3 frames repeated, from the frame below ...
/Users/ds26/anaconda/lib/python3.6/site-packages/cairosvg/surface.py in draw(self, node)
411 if display and node.tag not in INVISIBLE_TAGS:
412 for child in node.children:
--> 413 self.draw(child)
414
415 # Apply filter, mask and opacity
RecursionError: maximum recursion depth exceeded
If I save it into the an svg file, all the browsers can plot it properly. Cairosvg works fine on svg strings that don't have defs and use tags. Do you have any idea how I could tackle this issue? It would be quite important to use these tags...
python version: 3.6.0 OS: OSX 10.12.6 cairosvg: 2.1.2
A minimal reproducible example:
import os
os.environ['path'] += r';C:\Program Files\Inkscape\bin' # https://stackoverflow.com/questions/46265677/get-cairosvg-working-in-windows
import cairosvg
svg_string = '''
<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10">
<use width="1" height="1"/>
</svg>
'''
try:
x = cairosvg.svg2png(bytestring=svg_string)
except RecursionError:
print("Caught a RecursionError!")
cairocffi: 1.6.1 CairoSVG: 2.7.1 python: 3.12 OS: Windows 11