From 1a66404fd0312a835da03a940894737c1bf77310 Mon Sep 17 00:00:00 2001 From: Lars Schmertmann Date: Sun, 25 Nov 2018 19:58:55 +0100 Subject: Consider relative path in image tags Task-number: QTBUG-16198 Change-Id: I26bf48cbac39af0fae490ed21579e8de326cd1a3 Reviewed-by: Eirik Aavitsland --- src/svg/qsvghandler.cpp | 24 +++++++++++++++---- src/svg/qsvghandler_p.h | 1 + tests/auto/qsvgplugin/imageInclude.svg | 18 ++++++++++++++ tests/auto/qsvgplugin/imageIncludeA.svg | 5 ++++ tests/auto/qsvgplugin/resources.qrc | 2 ++ tests/auto/qsvgplugin/tst_qsvgplugin.cpp | 41 ++++++++++++++++++++++++++++++++ 6 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 tests/auto/qsvgplugin/imageInclude.svg create mode 100644 tests/auto/qsvgplugin/imageIncludeA.svg diff --git a/src/svg/qsvghandler.cpp b/src/svg/qsvghandler.cpp index 5242ffe..e935649 100644 --- a/src/svg/qsvghandler.cpp +++ b/src/svg/qsvghandler.cpp @@ -2739,7 +2739,7 @@ static QSvgNode *createImageNode(QSvgNode *parent, const QStringRef y = attributes.value(QLatin1String("y")); const QStringRef width = attributes.value(QLatin1String("width")); const QStringRef height = attributes.value(QLatin1String("height")); - QStringRef filename = attributes.value(QLatin1String("xlink:href")); + QString filename = attributes.value(QLatin1String("xlink:href")).toString(); qreal nx = toDouble(x); qreal ny = toDouble(y); QSvgHandler::LengthType type; @@ -2764,17 +2764,26 @@ static QSvgNode *createImageNode(QSvgNode *parent, int idx = filename.lastIndexOf(QLatin1String("base64,")); if (idx != -1) { idx += 7; - QStringRef dataStr = filename.mid(idx); + const QString dataStr = filename.mid(idx); QByteArray data = QByteArray::fromBase64(dataStr.toLatin1()); image = QImage::fromData(data); } else { qCDebug(lcSvgHandler) << "QSvgHandler::createImageNode: Unrecognized inline image format!"; } - } else - image = QImage(filename.toString()); + } else { + const auto *file = qobject_cast(handler->device()); + if (file) { + QUrl url(filename); + if (url.isRelative()) { + QFileInfo info(file->fileName()); + filename = info.absoluteDir().absoluteFilePath(filename); + } + } + image = QImage(filename); + } if (image.isNull()) { - qDebug()<<"couldn't create image from "<device(); +} + QSvgTinyDocument * QSvgHandler::document() const { return m_doc; diff --git a/src/svg/qsvghandler_p.h b/src/svg/qsvghandler_p.h index 5c13003..8eb061b 100644 --- a/src/svg/qsvghandler_p.h +++ b/src/svg/qsvghandler_p.h @@ -99,6 +99,7 @@ public: QSvgHandler(QXmlStreamReader *const data); ~QSvgHandler(); + QIODevice *device() const; QSvgTinyDocument *document() const; inline bool ok() const { diff --git a/tests/auto/qsvgplugin/imageInclude.svg b/tests/auto/qsvgplugin/imageInclude.svg new file mode 100644 index 0000000..c78d3fa --- /dev/null +++ b/tests/auto/qsvgplugin/imageInclude.svg @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/tests/auto/qsvgplugin/imageIncludeA.svg b/tests/auto/qsvgplugin/imageIncludeA.svg new file mode 100644 index 0000000..5811505 --- /dev/null +++ b/tests/auto/qsvgplugin/imageIncludeA.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/tests/auto/qsvgplugin/resources.qrc b/tests/auto/qsvgplugin/resources.qrc index fcb311a..fd83b80 100644 --- a/tests/auto/qsvgplugin/resources.qrc +++ b/tests/auto/qsvgplugin/resources.qrc @@ -1,5 +1,7 @@ + imageInclude.svg + imageIncludeA.svg square.svg square_size.svg square_size_viewbox.svg diff --git a/tests/auto/qsvgplugin/tst_qsvgplugin.cpp b/tests/auto/qsvgplugin/tst_qsvgplugin.cpp index 4ec1737..da49b75 100644 --- a/tests/auto/qsvgplugin/tst_qsvgplugin.cpp +++ b/tests/auto/qsvgplugin/tst_qsvgplugin.cpp @@ -39,6 +39,16 @@ #endif +QStringList logMessages; + +static void messageHandler(QtMsgType pType, const QMessageLogContext& pContext, const QString& pMsg) +{ + Q_UNUSED(pType); + Q_UNUSED(pContext); + logMessages.append(pMsg); +} + + class tst_QSvgPlugin : public QObject { Q_OBJECT @@ -50,6 +60,7 @@ public: private slots: void checkSize_data(); void checkSize(); + void checkImageInclude(); }; @@ -103,6 +114,36 @@ void tst_QSvgPlugin::checkSize() QCOMPARE(imageWidth, image.width()); } +void tst_QSvgPlugin::checkImageInclude() +{ + const QString filename(SRCDIR "imageInclude.svg"); + + QFile file(filename); + file.open(QIODevice::ReadOnly); + + QSvgIOHandler plugin; + plugin.setDevice(&file); + + QImage image; + qInstallMessageHandler(messageHandler); + plugin.read(&image); + qInstallMessageHandler(nullptr); + + file.close(); + + QCOMPARE(logMessages.size(), 8); + QCOMPARE(logMessages.at(0), QString("Could not create image from \"%1notExisting.svg\"").arg(SRCDIR)); + QCOMPARE(logMessages.at(1), QString("Could not create image from \"%1./notExisting.svg\"").arg(SRCDIR)); + QCOMPARE(logMessages.at(2), QString("Could not create image from \"%1../notExisting.svg\"").arg(SRCDIR)); + QCOMPARE(logMessages.at(3), QString("Could not create image from \"%1notExisting.svg\"").arg(QDir::rootPath())); + QCOMPARE(logMessages.at(4), QLatin1String("Could not create image from \":/notExisting.svg\"")); + QCOMPARE(logMessages.at(5), QLatin1String("Could not create image from \"qrc:///notExisting.svg\"")); + QCOMPARE(logMessages.at(6), QLatin1String("Could not create image from \"file:///notExisting.svg\"")); + QCOMPARE(logMessages.at(7), QLatin1String("Could not create image from \"http://qt.io/notExisting.svg\"")); + + logMessages.clear(); +} + QTEST_MAIN(tst_QSvgPlugin) #include "tst_qsvgplugin.moc" -- cgit v1.2.1