deeplook / svglib

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

Cannot read SVG files output by bqplot #248

Closed rosteen closed 4 years ago

rosteen commented 4 years ago

I have a project that uses bqplot to render some plots and (I hope) save the results out to SVG files for later conversion to other formats. However, when attempting to read the output SVG files with svglib, the read fails with the error below. I'm generally unfamiliar with SVG files, but since these files seem to render fine in a browser my initial guess is that the problem is on the svglib side. An example file is attached (saved as a txt to make Github happy). My environment is using Python 3.8.2, svglib 1.0.0 installed via pip, and the latest reportlab (3.5.46).

The error trace is:

>>> from svglib.svglib import svg2rlg
>>> drawing = svg2rlg("chrome_test2.svg")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/svglib/svglib.py", line 1423, in svg2rlg
    drawing = svgRenderer.render(svg_root)
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/svglib/svglib.py", line 574, in render
    main_group = self.renderSvg(node, outermost=True)
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/svglib/svglib.py", line 831, in renderSvg
    self.renderG(NodeTracker(def_node))
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/svglib/svglib.py", line 869, in renderG
    item = self.renderNode(child, parent=gr)
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/svglib/svglib.py", line 609, in renderNode
    self.renderStyle(n)
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/svglib/svglib.py", line 879, in renderStyle
    self.attrConverter.css_rules = CSSMatcher(node.text)
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/svglib/svglib.py", line 157, in __init__
    selectors = cssselect2.compile_selector_list(rule.prelude)
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/cssselect2/compiler.py", line 30, in compile_selector_list
    return [
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/cssselect2/compiler.py", line 30, in <listcomp>
    return [
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/cssselect2/parser.py", line 28, in parse
    yield parse_selector(tokens, namespaces)
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/cssselect2/parser.py", line 50, in parse_selector
    return Selector(result, pseudo_element)
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/cssselect2/parser.py", line 291, in __init__
    self.specificity = tree.specificity
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/cssselect2/parser.py", line 333, in specificity
    return tuple(map(sum, zip(
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/cssselect2/parser.py", line 334, in <genexpr>
    *(sel.specificity for sel in self.simple_selectors))))
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/cssselect2/parser.py", line 333, in specificity
    return tuple(map(sum, zip(
  File "/Users/rosteen/opt/anaconda3/envs/viz_dev/lib/python3.8/site-packages/cssselect2/parser.py", line 334, in <genexpr>
    *(sel.specificity for sel in self.simple_selectors))))
AttributeError: 'NoneType' object has no attribute 'specificity'

chrome_test2.svg.txt

claudep commented 4 years ago

Looks like the file uses advanced CSS features like css variables. It might be that cssselect2 doesn't support those. Could you try to report the error to cssselect2 (and link the report from here)?

rosteen commented 4 years ago

Thanks for the quick reply - I created an issue there. I also just realized that one of the bqplot devs raises the possibility of their use of CSS variables causing problems (in other software) in bqplot issue 1159.

claudep commented 4 years ago

Requiring complex css engine just to render SVG is very unfortunate, to say the least. The browser engines are becoming monsters because of the stack of more and more complex technologies to support. But ranting apart, svglib is counting on cssselect2 to interpret CSS, so I'm afraid we won't able to fix this ourselves. Feel free to reopen if you have any other idea.