From b44f222dbe7886b9504a4af9f09521132d2262b2 Mon Sep 17 00:00:00 2001 From: Eirik Aavitsland Date: Wed, 11 Jan 2023 10:45:23 +0100 Subject: [PATCH] Support the scaling factor of some Windows pdf print devices The printer drivers of some pdf printers allows the user to set a global scaling factor, scaling up or down the number of device pixels available for the same paper size and dpi. Make sure to update the Qt print device metrics accordingly so that QPainter will see the entire page, and not more than the page. Fixes: QTBUG-106659 Pick-to: 6.5 Change-Id: Icb90c1aa742f56b2a2043ef7070530beebe541d5 Reviewed-by: Oliver Wolff --- .../platform/windows/qprintengine_win.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/printsupport/platform/windows/qprintengine_win.cpp b/src/printsupport/platform/windows/qprintengine_win.cpp index cd3e03d88e..99075f4ec3 100644 --- a/src/printsupport/platform/windows/qprintengine_win.cpp +++ b/src/printsupport/platform/windows/qprintengine_win.cpp @@ -1586,7 +1586,7 @@ void QWin32PrintEngine::setGlobalDevMode(HGLOBAL globalDevNames, HGLOBAL globalD #if defined QT_DEBUG_DRAW || defined QT_DEBUG_METRICS qDebug("QWin32PrintEngine::setGlobalDevMode()"); - debugMetrics(); + d->debugMetrics(); #endif // QT_DEBUG_DRAW || QT_DEBUG_METRICS } @@ -1675,13 +1675,22 @@ void QWin32PrintEnginePrivate::updatePageLayout() void QWin32PrintEnginePrivate::updateMetrics() { m_paintRectPixels = m_pageLayout.paintRectPixels(resolution); + // Some print devices allow scaling, so that "virtual" page size != current paper size + const int devWidth = GetDeviceCaps(hdc, PHYSICALWIDTH); + const int devHeight = GetDeviceCaps(hdc, PHYSICALHEIGHT); + const int pageWidth = m_pageLayout.fullRectPixels(dpi_x).width(); + const int pageHeight = m_pageLayout.fullRectPixels(dpi_y).height(); + const qreal pageScaleX = (devWidth && pageWidth) ? qreal(devWidth) / pageWidth : 1; + const qreal pageScaleY = (devHeight && pageHeight) ? qreal(devHeight) / pageHeight : 1; + m_paintRectPixels = QTransform::fromScale(pageScaleX, pageScaleY).mapRect(m_paintRectPixels); + QSizeF sizeMM = m_pageLayout.paintRect(QPageLayout::Millimeter).size(); m_paintSizeMM = QSize(qRound(sizeMM.width()), qRound(sizeMM.height())); // Calculate the origin using the physical device pixels, not our paint pixels // Origin is defined as User Margins - Device Margins QMarginsF margins = m_pageLayout.margins(QPageLayout::Millimeter) / 25.4; - origin_x = qRound(margins.left() * dpi_x) - GetDeviceCaps(hdc, PHYSICALOFFSETX); - origin_y = qRound(margins.top() * dpi_y) - GetDeviceCaps(hdc, PHYSICALOFFSETY); + origin_x = qRound(pageScaleX * margins.left() * dpi_x) - GetDeviceCaps(hdc, PHYSICALOFFSETX); + origin_y = qRound(pageScaleY * margins.top() * dpi_y) - GetDeviceCaps(hdc, PHYSICALOFFSETY); } void QWin32PrintEnginePrivate::debugMetrics() const