Skip to content

Commit

Permalink
Support the scaling factor of some Windows pdf print devices
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
aavit committed Jan 24, 2023
1 parent 025659a commit b44f222
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/printsupport/platform/windows/qprintengine_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b44f222

Please sign in to comment.