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, 760df72565.
We can't avoid testing whether the source in the HTML is provided as a
URL before interpreting it as a file name.

Pick-to: 6.5 6.4 6.2
Fixes: QTBUG-109212
Change-Id: I7ea7a5bfde79bab90a8025c42e754129813dd0fc
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Volker Hilsheimer 2022-12-21 15:29:58 +01:00
parent 99893a914a
commit 2d87c4d881
2 changed files with 19 additions and 12 deletions

View File

@ -17,20 +17,34 @@ QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
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("file:/"_L1))
localFile = localFile.sliced(6);
else if (localFile.startsWith("qrc:/"_L1))
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)
{
if (path.startsWith(":/"_L1)) // auto-detect resources and convert them to url
path.prepend("qrc"_L1);
path = path.prepend("qrc"_L1);
return QUrl(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;
@ -100,7 +114,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;

View File

@ -71,13 +71,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);
}
}