deeplook / svglib

Read SVG files and convert them to other formats.
GNU Lesser General Public License v3.0
311 stars 80 forks source link

Render drawing with dasharray="0" freezes up #302

Closed cbeytas closed 2 years ago

cbeytas commented 2 years ago

If stroke-dasharray="0" is specified, renderPM freezes up using 100% CPU. All major browsers (Chrome, Firefox, Edge) render it as a solid line.

import lxml
from lxml.html import open_in_browser
import reportlab.graphics.renderPM
from svglib import svglib
SVG = """
<svg width="100" height="100">
    <circle cx="50" cy="50" r="40" fill="green" stroke="black" stroke-width="5"/>
    <path stroke="red" d="M10 50 90 50" stroke-opacity="0.5" stroke-width="10"
          stroke-dasharray="0"/>
</svg>"""
parser = lxml.etree.XMLParser(recover=True, resolve_entities=False)
svg_root = lxml.etree.fromstring(SVG, parser=parser)
open_in_browser(svg_root)
renderer = svglib.SvgRenderer('test')
drawing = renderer.render(svg_root)
pil_image = reportlab.graphics.renderPM.drawToPIL(drawing)
pil_image.show()
cbeytas commented 2 years ago

Rendering to PDF throws an exception. I suppose this is a Reportlab issue, not svglib.

File "C:\Python38\lib\site-packages\reportlab\pdfgen\canvas.py", line 1778, in setDash
    raise ValueError('setDash: array=%r phase=%r\n%s' % (array, phase, reason))
ValueError: setDash: array=[0.0] phase=0
dash cycle should be larger than zero