Get SVG size from QSvgRenderer

1.4k Views Asked by At

Suppose I have an SVG something like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   version="1.1"
   viewBox="0 0 100 200"
   height="600mm"
   width="300mm">
...

Using QSvgRenderer::viewBoxF() I can obtain the 0 0 100 200 viewbox, but is there any way to obtain the width="300mm" and height="600mm"?

(Other than the obvious thing of parsing the XML myself.)

1

There are 1 best solutions below

0
Timmmm On

I've read the source code. See QSvgHandler::createSvgNode(). The behaviour is something like this:

QSvgRenderer::viewBoxF() returns the viewBox attribute in user-defined units.

QSvgRenderer::defaultSize() reads the width and height attributes (if present). If the units are cm, in or mm they it is multiplied by an appropriate value to give pixels at a resolution of 90 DPI. For example if in then it is multiplied by 90, mm by 90/25.4 and so on. Other units - px, pt and even % are left as-is. defaultSize() uses integers so its resolution is 1/90 inch.

If the width and height are not present then defaultSize() returns the size of the viewBox in user-defined units.

This is rather ad-hoc and complex behaviour and doesn't fit my use case so I think I will just have to parse the XML twice (or the first bit of it anyway).