summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2022-12-21 15:29:58 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2022-12-22 23:20:30 +0100
commitc381d4b124b6dd8bb800402817750436cba06ee9 (patch)
treea478ee09fbf600d1276d4a362273a978629581ab
parent1197590299fa9ee4ea23dbd2497e35b1979279b4 (diff)
QTextImageHandler: Resolve Nx images correctly for file or qrc URLs
The qt_findAtNxFile helper in qicon.cpp expects a local file name that can be probed with QFile::exists. If the src attribute of an <img> element specifies the location of the image as a file:/ or qrc:/ url rather than as a local file name, then we need to strip the scheme off the file path, and in the case of a qrc URL leave the :/ prefix before calling the qt_findAtNxFile helper. Amends, and partially reverts, 760df7256588e76d082ea959399af36d7ba80a86. We can't avoid testing whether the source in the HTML is provided as a URL before interpreting it as a file name. Fixes: QTBUG-109212 Change-Id: I7ea7a5bfde79bab90a8025c42e754129813dd0fc Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> (cherry picked from commit 2d87c4d881b74619fef966ffb0d7a00cb4ccea50) Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
-rw-r--r--src/gui/text/qtextimagehandler.cpp22
-rw-r--r--tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp7
2 files changed, 18 insertions, 11 deletions
diff --git a/src/gui/text/qtextimagehandler.cpp b/src/gui/text/qtextimagehandler.cpp
index 788daf24cd..51decb5bec 100644
--- a/src/gui/text/qtextimagehandler.cpp
+++ b/src/gui/text/qtextimagehandler.cpp
@@ -51,8 +51,22 @@
QT_BEGIN_NAMESPACE
-extern QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio,
- qreal *sourceDevicePixelRatio);
+static inline QString findAtNxFileOrResource(const QString &baseFileName,
+ qreal targetDevicePixelRatio,
+ qreal *sourceDevicePixelRatio)
+{
+ // qt_findAtNxFile expects a file name that can be tested with QFile::exists.
+ // so if the format.name() is a file:/ or qrc:/ URL, then we need to strip away the schema.
+ QString localFile = baseFileName;
+ if (localFile.startsWith(QLatin1String("file:/")))
+ localFile = localFile.sliced(6);
+ else if (localFile.startsWith(QLatin1String("qrc:/")))
+ localFile = localFile.sliced(3);
+
+ extern QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio,
+ qreal *sourceDevicePixelRatio);
+ return qt_findAtNxFile(localFile, targetDevicePixelRatio, sourceDevicePixelRatio);
+}
static inline QUrl fromLocalfileOrResources(QString path)
{
@@ -64,7 +78,7 @@ static inline QUrl fromLocalfileOrResources(QString path)
static QPixmap getPixmap(QTextDocument *doc, const QTextImageFormat &format, const qreal devicePixelRatio = 1.0)
{
qreal sourcePixelRatio = 1.0;
- const QString name = qt_findAtNxFile(format.name(), devicePixelRatio, &sourcePixelRatio);
+ const QString name = findAtNxFileOrResource(format.name(), devicePixelRatio, &sourcePixelRatio);
const QUrl url = fromLocalfileOrResources(name);
QPixmap pm;
@@ -135,7 +149,7 @@ static QSize getPixmapSize(QTextDocument *doc, const QTextImageFormat &format)
static QImage getImage(QTextDocument *doc, const QTextImageFormat &format, const qreal devicePixelRatio = 1.0)
{
qreal sourcePixelRatio = 1.0;
- const QString name = qt_findAtNxFile(format.name(), devicePixelRatio, &sourcePixelRatio);
+ const QString name = findAtNxFileOrResource(format.name(), devicePixelRatio, &sourcePixelRatio);
const QUrl url = fromLocalfileOrResources(name);
QImage image;
diff --git a/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp b/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp
index fc8900f94f..a0f5e4c133 100644
--- a/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp
+++ b/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp
@@ -96,13 +96,6 @@ void tst_QTextImageHandler::loadAtNImages()
p.end();
QVERIFY(!img.isNull());
const auto expectedColor = dpr == 1 ? Qt::red : Qt::green;
-#ifdef Q_OS_ANDROID // On Android, file:/ fails completely
- QEXPECT_FAIL("file_url", "file:/ schema not handled - QTBUG-109212", Continue);
-#else
- if (dpr != 1)
- QEXPECT_FAIL("file_url", "Nx images not resolved for file:/ schema - QTBUG-109212", Continue);
-#endif
- QEXPECT_FAIL("qrc_url", "qrc:/ schema not handled - QTBUG-109212", Continue);
QCOMPARE(img.pixelColor(0, 0), expectedColor);
}
}