From 0aaa84b71479a4d7c45266d79b78c56e7001260c Mon Sep 17 00:00:00 2001 From: Jarkko Koivikko Date: Mon, 27 Nov 2023 15:07:51 +0200 Subject: [PATCH] pagesize: Fix pixel overflow in QPageSize::sizePixels and rectPixels This update addresses an issue in converting page size from points to device pixels. Previously, rounding the result could lead to an overflow beyond the physical pixel capacity. Example case: A4 paper size at 600 dpi: - points to pixels: 842 pt / (72.0 / 600) = 7016.666666666667 However, the physical pixel height for an HP printer: - GetDeviceCaps(hdc, PHYSICALHEIGHT)) = 7016 This fix prevents pixel size from exceeding the physical print area, avoiding unprinted pixels. Change-Id: I66eabc628d3374d9cfb19b0eb5928f83afbc13dc Reviewed-by: Qt CI Bot Reviewed-by: David Faure (cherry picked from commit c49fd15a4253a79d70ae2b26ac4cc04454ffd7a0) Reviewed-by: Qt Cherry-pick Bot --- src/gui/painting/qpagesize.cpp | 2 +- tests/auto/gui/painting/qpagesize/tst_qpagesize.cpp | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/gui/painting/qpagesize.cpp b/src/gui/painting/qpagesize.cpp index 3ed633bf992..4c453c830fc 100644 --- a/src/gui/painting/qpagesize.cpp +++ b/src/gui/painting/qpagesize.cpp @@ -543,7 +543,7 @@ static QSize qt_convertPointsToPixels(const QSize &size, int resolution) if (!size.isValid() || resolution <= 0) return QSize(); const qreal multiplier = qt_pixelMultiplier(resolution); - return QSize(qRound(size.width() / multiplier), qRound(size.height() / multiplier)); + return QSize(qFloor(size.width() / multiplier), qFloor(size.height() / multiplier)); } static QSizeF qt_convertPointsToUnits(const QSize &size, QPageSize::Unit units) diff --git a/tests/auto/gui/painting/qpagesize/tst_qpagesize.cpp b/tests/auto/gui/painting/qpagesize/tst_qpagesize.cpp index bac7e9cdf81..bfcd4d7691f 100644 --- a/tests/auto/gui/painting/qpagesize/tst_qpagesize.cpp +++ b/tests/auto/gui/painting/qpagesize/tst_qpagesize.cpp @@ -49,11 +49,13 @@ void tst_QPageSize::basics() QCOMPARE(a4.size(QPageSize::Pica), QSizeF(49.58, 70.17)); QCOMPARE(a4.sizePoints(), QSize(595, 842)); QCOMPARE(a4.sizePixels(72), QSize(595, 842)); + QCOMPARE(a4.sizePixels(600), QSize(4958, 7016)); // Rounded down QCOMPARE(a4.rect(QPageSize::Millimeter), QRectF(0, 0, 210, 297)); QCOMPARE(a4.rect(QPageSize::Inch), QRectF(0, 0, 8.27, 11.69)); QCOMPARE(a4.rect(QPageSize::Pica), QRectF(0, 0, 49.58, 70.17)); QCOMPARE(a4.rectPoints(), QRect(0, 0, 595, 842)); QCOMPARE(a4.rectPixels(72), QRect(0, 0, 595, 842)); + QCOMPARE(a4.rectPixels(600), QRect(0, 0, 4958, 7016)); // Rounded down // Simple QPageSize::PaperSizeId later in list QPageSize folio = QPageSize(QPageSize::Folio);