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 <oliver.wolff@qt.io>
This commit is contained in:
Eirik Aavitsland 2023-01-11 10:45:23 +01:00
parent 025659a18c
commit b44f222dbe

View File

@ -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