deeplook / svglib

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

wrong width and height attributes of drawing obj #362

Open ronak1009 opened 1 year ago

ronak1009 commented 1 year ago

I observed that the dimensions of the drawing object have got interchanged between height and width of the svg.

import svglib.svglib as svglib
svgStr = ""
bbytes = io.StringIO(svgStr)
dwg = svglib.svg2rlg(bbytes)
print(f" dwg original {drawing.width=}, {drawing.height=}")

Now, I want to resize this drawing to fix the bounding box (size if dynamic) for pdf creation using reportlab, the resize does not work properly.

To resize:

scale = min( [(maxWidth- 5*mm)/dwg.width,             (maxHeight- 2*mm)/dwg.height])
dwg.width = dwg.width * scale
dwg.height =dwg.height * scale
dwg.scale(sx, sy)

The diagram gets scaled but something seems to be not proper.

dwg original dwg.width=450.0, dwg.height=700.0
--- scale ---
avail maxWidth=504.56692913385825 , maxHeight=657.9685039370078
sx=0.7005624296962879, sy=0.7005624296962879
dwg.width=490.39370078740154 , dwg.height=315.2530933633296

--- scale ---
avail maxWidth=504.56692913385825 , maxHeight=657.9685039370078
sx=1.0, sy=1.0
dwg.width=490.39370078740154 , dwg.height=315.2530933633296

In the attached snap of the PDF generated, the block outside box is the bounding box and the svg does not fit into it correctly, The output of the dimensions are shown above and that gives a false impression that the drawing dwg is within the correct bounding box.

image

Attaching the svg svgSample.txt

deeplook commented 1 year ago

Sorry, but your code doesn't execute...