deeplook / svglib

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

visibility ignored using svg2rlg and renderPM.drawToFile #359

Open RobertSchueler opened 2 years ago

RobertSchueler commented 2 years ago

An svg element where the attribute "visibility" is set to "hidden" should not be plotted, but it is.

Small example:

from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM

svg_string = """
<ns0:svg xmlns:ns0="http://www.w3.org/2000/svg" xmlns:ns1="http://www.inkscape.org/namespaces/inkscape" xmlns:ns2="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" width="210mm" height="297mm" viewBox="0 0 210 297" version="1.1" id="svg5" ns1:version="1.1 (c68e22c387, 2021-05-23)" ns2:docname="test.svg">
  <ns2:namedview id="namedview7" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" ns1:pageshadow="2" ns1:pageopacity="0.0" ns1:pagecheckerboard="0" ns1:document-units="mm" showgrid="false" ns1:zoom="0.67081229" ns1:cx="396.53418" ns1:cy="561.25984" ns1:window-width="1680" ns1:window-height="987" ns1:window-x="-8" ns1:window-y="-8" ns1:window-maximized="1" ns1:current-layer="layer1" />
  <ns0:defs id="defs2" />
  <ns0:g ns1:label="Ebene 1" ns1:groupmode="layer" id="layer1">
    <ns0:path style="fill:none;stroke:#FF0000;stroke-width:5.63426" d="M 93.91599,350.32155 V 204.97537 h 84.08396 84.08396 l -5.14971,13.04389 c -25.58743,64.81145 -44.58875,161.07403 -52.08331,263.85921 l -1.00544,13.78925 H 148.88072 93.91599 Z" id="path948" transform="scale(0.26458333)" ns1:label="LustigerPfad" visibility="hidden" />
  </ns0:g>
</ns0:svg>
"""

with open("my.svg", "w", encoding="utf-8") as file:
    file.write(svg_string)

drawing = svg2rlg("my.svg")

renderPM.drawToFile(drawing, "my.png", fmt="PNG")

Resulting image:

my

claudep commented 2 years ago

Sure, svglib does only support a subset of the (did I say huge??) CSS specification, so visibility is currently not handled. Patch welcome!

deeplook commented 2 years ago

Sounds like a low hanging froot. But if you generate the SVG yourself, you could of course skip the invisible le element. πŸ˜‰

RobertSchueler commented 2 years ago

Sounds like a low hanging froot. But if you generate the SVG yourself, you could of course skip the invisible le element. πŸ˜‰

Yes, this is exactly what I do right now. But it would be nice if I could switch visibility on and off as I wish without loosing everything else.