Generalize image file name @2x suffix handling to higher scale factors

@3x is in use on iOS already, so extend the handling in QImageReader
to all single-digit factors, like QIcon does.

Fixes: QTBUG-76273
Change-Id: Ic9442731c0549dbe8f797e1ddb1a09d8447e8441
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Eirik Aavitsland 2019-06-14 13:08:56 +02:00
parent 2c9ba84746
commit 3797704c4f
5 changed files with 31 additions and 4 deletions

View File

@ -1319,10 +1319,12 @@ bool QImageReader::read(QImage *image)
}
}
// successful read; check for "@2x" file name suffix and set device pixel ratio.
static bool disable2xImageLoading = !qEnvironmentVariableIsEmpty("QT_HIGHDPI_DISABLE_2X_IMAGE_LOADING");
if (!disable2xImageLoading && QFileInfo(fileName()).baseName().endsWith(QLatin1String("@2x"))) {
image->setDevicePixelRatio(2.0);
// successful read; check for "@Nx" file name suffix and set device pixel ratio.
static bool disableNxImageLoading = !qEnvironmentVariableIsEmpty("QT_HIGHDPI_DISABLE_2X_IMAGE_LOADING");
if (!disableNxImageLoading) {
const QByteArray suffix = QFileInfo(fileName()).baseName().right(3).toLatin1();
if (suffix.length() == 3 && suffix[0] == '@' && suffix[1] >= '2' && suffix[1] <= '9' && suffix[2] == 'x')
image->setDevicePixelRatio(suffix[1] - '0');
}
if (autoTransform())
qt_imageTransform(*image, transformation());

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

@ -164,6 +164,9 @@ private slots:
void preserveTexts_data();
void preserveTexts();
void devicePixelRatio_data();
void devicePixelRatio();
private:
QString prefix;
QTemporaryDir m_temporaryDir;
@ -1976,6 +1979,28 @@ void tst_QImageReader::preserveTexts()
QCOMPARE(r.text(key3), text3.simplified());
}
void tst_QImageReader::devicePixelRatio_data()
{
QTest::addColumn<QString>("fileName");
QTest::addColumn<QSize>("size");
QTest::addColumn<qreal>("dpr");
QTest::newRow("1x") << "qticon16.png" << QSize(16, 16) << 1.0;
QTest::newRow("2x") << "qticon16@2x.png" << QSize(32, 32) << 2.0;
QTest::newRow("3x") << "qticon16@3x.png" << QSize(48, 48) << 3.0;
}
void tst_QImageReader::devicePixelRatio()
{
QFETCH(QString, fileName);
QFETCH(QSize, size);
QFETCH(qreal, dpr);
QImageReader r(":/images/" + fileName);
QImage img = r.read();
QCOMPARE(img.size(), size);
QCOMPARE(img.devicePixelRatio(), dpr);
}
QTEST_MAIN(tst_QImageReader)
#include "tst_qimagereader.moc"