diff options
author | Jarkko Koivikko <jarkko.koivikko@code-q.fi> | 2023-10-26 14:02:48 +0300 |
---|---|---|
committer | Jarkko Koivikko <jarkko.koivikko@code-q.fi> | 2023-12-09 04:46:24 +0200 |
commit | d420c1e25a0fb3b25ba4c572f2938701ce8494ec (patch) | |
tree | 91dfc17727b5cf98d5375c809a280feadbfbaaca /src/gui | |
parent | 5fb2d50e42e1046bf43d876e0caf92c14d914661 (diff) |
print: QPageLayout: Fix pt unit conversion
This patch corrects the miscalculation in point unit conversion,
ensuring correct margin updates.
Previously, non-pt units were rounded to two decimal places. When
converting back to pt, rounding was to zero decimals, making the result
always less than the original. This could result in margins falling
below the minimum allowed.
Example:
original_points = 8.4
multiplier = 2.83464566929
mm: qRound(8.4 / multiplier * 100) / 100 = 2.96
new_points: qRound(2.96 * multiplier) = 8 // wrong!
The fix rounds back-converted values up to two decimals, ensuring they
are never less than the original and thus stay above minimum margins.
new_points: qCeil(2.96 * multiplier * 100) / 100 = 8.4
Also, remove unused function qt_convertPoint.
Pick-to: 6.6
Change-Id: I6109f8d381aec96db1ce04cc167f7b73c1c0b9a8
Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/painting/qpagelayout.cpp | 34 |
1 files changed, 6 insertions, 28 deletions
diff --git a/src/gui/painting/qpagelayout.cpp b/src/gui/painting/qpagelayout.cpp index 456fcf8802..4c57d33d5a 100644 --- a/src/gui/painting/qpagelayout.cpp +++ b/src/gui/painting/qpagelayout.cpp @@ -39,41 +39,19 @@ Q_GUI_EXPORT qreal qt_pointMultiplier(QPageLayout::Unit unit) // Multiplier for converting pixels to points. extern qreal qt_pixelMultiplier(int resolution); -QPointF qt_convertPoint(const QPointF &xy, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits) -{ - // If the size have the same units, or are all 0, then don't need to convert - if (fromUnits == toUnits || xy.isNull()) - return xy; - - // If converting to points then convert and round to 0 decimal places - if (toUnits == QPageLayout::Point) { - const qreal multiplier = qt_pointMultiplier(fromUnits); - return QPointF(qRound(xy.x() * multiplier), - qRound(xy.y() * multiplier)); - } - - // If converting to other units, need to convert to unrounded points first - QPointF pointXy = (fromUnits == QPageLayout::Point) ? xy : xy * qt_pointMultiplier(fromUnits); - - // Then convert from points to required units rounded to 2 decimal places - const qreal multiplier = qt_pointMultiplier(toUnits); - return QPointF(qRound(pointXy.x() * 100 / multiplier) / 100.0, - qRound(pointXy.y() * 100 / multiplier) / 100.0); -} - Q_GUI_EXPORT QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits) { // If the margins have the same units, or are all 0, then don't need to convert if (fromUnits == toUnits || margins.isNull()) return margins; - // If converting to points then convert and round to 0 decimal places + // If converting to points then convert and round up to 2 decimal places if (toUnits == QPageLayout::Point) { - const qreal multiplier = qt_pointMultiplier(fromUnits); - return QMarginsF(qRound(margins.left() * multiplier), - qRound(margins.top() * multiplier), - qRound(margins.right() * multiplier), - qRound(margins.bottom() * multiplier)); + const qreal multiplierX100 = qt_pointMultiplier(fromUnits) * 100; + return QMarginsF(qCeil(margins.left() * multiplierX100) / 100.0, + qCeil(margins.top() * multiplierX100) / 100.0, + qCeil(margins.right() * multiplierX100) / 100.0, + qCeil(margins.bottom() * multiplierX100) / 100.0); } // If converting to other units, need to convert to unrounded points first |