Skip to content

QQuickText: fix fractional pointSize rounding error bug

Uszie requested to merge (removed):kde/5.15 into kde/5.15

This fixes KDE Bug: 432372 https://bugs.kde.org/show_bug.cgi?id=432372

It's been fixed in QT 6.2.5 and later.

Problem: The layout is initialized with a copy of the current font before the laying out begins. In cases when setupTextLayout() iterates through the main loop multiple times, it will update the layout font, if it differs. (See code below)

if (!once) { if (pixelSize) scaledFont.setPixelSize(scaledFontSize); else scaledFont.setPointSizeF(scaledFontSize); if (layout.font() != scaledFont) layout.setFont(scaledFont); }

The whole reason why we might update the font in the first place, is because the QQuickText has a fontSizeMode property which can be set to e.g. HorizontalFit, which will cause the layouting to downscale the font in order to fit (assuming the text doesn't fit the space available), instead of eliding.

The problem here is that QFont internally uses a float to store the point size, and we would convert that value to an int when temporarily storing it on the stack. This would modify the pointSize value in cases where the pointSize is fractional, and cause a mismatch between the QQuickText and QTextLayout.

The lineWidth is set only during the first loop iteration. During successive iterations of the main loop, the layout's font would be updated to one that has either a floored or ceiled pointSize. If the pointSize is a fractional value, and is ceiled when being updated during the second loop iteration, the layout would use a larger font value, which would cause the QTextLayout::naturalTextWidth() to return a larger value than that of the lineWidth, triggering eliding.

Solution: Keep the font precision, by storing the values as floats instead of ints.

Fixes: QTBUG-92006 Change-Id: Ibf64ac2dfbf262c6aae05b8eb8251d2f5a869b69 Reviewed-by: Jan Arve Sæther jan-arve.saether@qt.io (cherry picked from commit ae62122a) Reviewed-by: Qt Cherry-pick Bot cherrypick_bot@qt-project.org

Merge request reports

Loading