3

Is there a way to get QSvgWidget/QSvgRenderer to work with units like cm/mm/in/pc?

Here's a sample SVG where all lines should have the same length (it renders correctly in Firefox and Chrome):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="500" width="500">
  <line x1="100" y1="50" x2="100" y2="450" style="stroke:rgb(0,0,0)" />
  <line x1="400" y1="50" x2="400" y2="450" style="stroke:rgb(0,0,0)" />
  <line x1="100" y1="100" x2="400" y2="100" style="stroke:rgb(255,0,0);stroke-width:20" />
  <line x1="100" y1="200" x2="105.82mm" y2="200" style="stroke:rgb(0,255,0);stroke-width:20" />
  <line x1="100" y1="300" x2="4.17in" y2="300" style="stroke:rgb(0,0,255);stroke-width:20" />
  <line x1="100" y1="400" x2="25pc" y2="400" style="stroke:rgb(127,127,127);stroke-width:20" />
</svg>

And here's my viewer:

from PySide2 import QtWidgets, QtSvg
import PySide2

app = QtWidgets.QApplication(sys.argv)
w = QtSvg.QSvgWidget("demo.svg")
w.show()
sys.exit(app.exec_())

If I open the SVG with this viewer it seems that QT ignores all the units and reads attribues like x2="105.82mm" as x2="105.82"

Result in Qt:

enter image description here

Result in Firefox:

enter image description here

QT and PySide2 are both version 5.14.1

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
rmweiss
  • 716
  • 1
  • 6
  • 16

1 Answers1

3

Explanation:

Analyzing the source code of the QSvgHandler I found that only the lengths (width and height attributes) support the units and not the coordinates, to understand it, it is enough to analyze the following 2 code fragments:

// https://code.qt.io/cgit/qt/qtsvg.git/tree/src/svg/qsvghandler.cpp#n2816
static QSvgNode *createLineNode(QSvgNode *parent,
                                const QXmlStreamAttributes &attributes,
                                QSvgHandler *)
{
    const QStringRef x1 = attributes.value(QLatin1String("x1"));
    const QStringRef y1 = attributes.value(QLatin1String("y1"));
    const QStringRef x2 = attributes.value(QLatin1String("x2"));
    const QStringRef y2 = attributes.value(QLatin1String("y2"));
    qreal nx1 = toDouble(x1);
    qreal ny1 = toDouble(y1);
    qreal nx2 = toDouble(x2);
    qreal ny2 = toDouble(y2);

    QLineF lineBounds(nx1, ny1, nx2, ny2);
    QSvgNode *line = new QSvgLine(parent, lineBounds);
    return line;
}
// https://code.qt.io/cgit/qt/qtsvg.git/tree/src/svg/qsvghandler.cpp#n3054
static QSvgNode *createRectNode(QSvgNode *parent,
                                const QXmlStreamAttributes &attributes,
                                QSvgHandler *handler)
{
    const QStringRef x      = attributes.value(QLatin1String("x"));
    const QStringRef y      = attributes.value(QLatin1String("y"));
    const QStringRef width  = attributes.value(QLatin1String("width"));
    const QStringRef height = attributes.value(QLatin1String("height"));
    const QStringRef rx      = attributes.value(QLatin1String("rx"));
    const QStringRef ry      = attributes.value(QLatin1String("ry"));

    QSvgHandler::LengthType type;
    qreal nwidth = parseLength(width, type, handler);
    nwidth = convertToPixels(nwidth, true, type);

    qreal nheight = parseLength(height, type, handler);
    nheight = convertToPixels(nheight, true, type);
    qreal nrx = toDouble(rx);
    qreal nry = toDouble(ry);

    QRectF bounds(toDouble(x), toDouble(y),
                  nwidth, nheight);

    //9.2 The 'rect'  element clearly specifies it
    // but the case might in fact be handled because
    // we draw rounded rectangles differently
    if (nrx > bounds.width()/2)
        nrx = bounds.width()/2;
    if (nry > bounds.height()/2)
        nry = bounds.height()/2;

    if (!rx.isEmpty() && ry.isEmpty())
        nry = nrx;
    else if (!ry.isEmpty() && rx.isEmpty())
        nrx = nry;

    //we draw rounded rect from 0...99
    //svg from 0...bounds.width()/2 so we're adjusting the
    //coordinates
    nrx *= (100/(bounds.width()/2));
    nry *= (100/(bounds.height()/2));

    QSvgNode *rect = new QSvgRect(parent, bounds,
                                  int(nrx),
                                  int(nry));
    return rect;
}

The method that converts the units is parseLength() which is only used in "width" and "height".

Solution:

A workaround is to use QWebEngineView:

import os
import sys

from PySide2 import QtCore, QtWidgets, QtSvg, QtWebEngineWidgets


CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))

if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)
    filename = os.path.join(CURRENT_DIR, "demo.svg")

    qsvgwidget = QtSvg.QSvgWidget(filename)

    qwebengineview = QtWebEngineWidgets.QWebEngineView()
    qwebengineview.load(QtCore.QUrl.fromLocalFile(filename))

    w = QtWidgets.QWidget()
    lay = QtWidgets.QHBoxLayout(w)
    lay.addWidget(qsvgwidget, strecth=1)
    lay.addWidget(qwebengineview, strecth=1)
    w.show()
    sys.exit(app.exec_())

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks, that looks much better. Do you maybe know how to render QWebEngineView to a pixmap? Because the reason I was looking at SVG in the beginning was that I want to include them in reports I create with QPrinter. – rmweiss Mar 29 '20 at 14:34
  • @rmweiss try this: https://stackoverflow.com/questions/47344947/pyqt5-render-with-qwebengineview/47353116#47353116 – eyllanesc Mar 29 '20 at 16:22
  • The method that converts the units is parseLength() which is only used in "width" and "height". - Why would anybody do that? – johnson Dec 07 '20 at 14:50