deeplook / svglib

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

Size in percent take into account the total size instead of the size of the surrounding container. #246

Closed molitoris closed 4 years ago

molitoris commented 4 years ago

I am drawing an SVG graphic that contains a container with absolute size. The elements contained in it have relative sizes and are drawn accordingly.

When converting to PDF, the nested elements have a different size. It appears that the nested elements are drawn relative to the total size. I expect that the size of the nested elements will also be drawn in PDF relative to the surrounding container.

image.svg

<?xml version="1.0" encoding="utf-8" ?>
<svg baseProfile="full" height="50mm" version="1.1" width="50mm" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs />
    <rect fill="yellow" height="100%" width="100%" x="0" y="0" />
    <svg height="7mm" id="container" width="7mm" x="21.5mm" y="21.5mm">
        <defs />
        <rect fill="black" height="100%" width="100%" x="0" y="0" />
        <rect fill="white" height="18.75%" width="62.5%" x="18.75%" y="40.625%" />
        <rect fill="white" height="62.5%" width="18.75%" x="40.625%" y="18.75%" />
    </svg>
</svg>

Output: Drawing000.pdf

Minimal example

from svglib.svglib import svg2rlg

drawing = svg2rlg("image.svg")
drawing.save()
claudep commented 4 years ago

Maybe you want to check the above patch whether it solves your issue?

molitoris commented 4 years ago

Thank you for your help.

I am not sure, if I install the svglib correctly. I ran the following command pip install git+git://github.com/claudep/svglib.git@issue246 to install the adapted svglib package.

claudep commented 4 years ago

Chances are that if you didn't get errors, the install worked!

molitoris commented 4 years ago

After setting up a new environment and install all dependencies again, I got the expected result. However, when I ran it with a larger SVG image, I get the following warning multiple times: Unable to resolve percentage unit without knowing the node name. The resulting pdf has a lot of scaling problems.

claudep commented 4 years ago

Would be great if you could post one of those SVGs (or send it directly to me).