Commit 36185dbe authored by Nyan Pasu's avatar Nyan Pasu 🐈
Browse files

Fix hang in line-break algorithm

findBreak() previously mixed QFontMetrics::boundingRect().width() (the
width a string takes up, including left/right overhangs) and
QFontMetrics::horizontalAdvance() (which excludes overhangs). This
resulted in inconsistent behaviors within the function.

For borderline strings where fm->boundingRect(text).width() >= maxWidth
but fm->horizontalAdvance(text) < maxWidth, the code would not return
early, and would never exit the binary search loop because halfWidth <
maxWidth even when halfPos == text.length(). In fact, bottomPos gets set
to text.length() + 1 and the loop continues forever.

By consistently using QFontMetrics::boundingRect().width(), we make sure
the binary search loop always breaks out. It would be nice to add a
(halfWidth < maxWidth && halfPos < breakPos) check, but it's not
strictly necessary.

BUG: 428917
parent 1c8b8558
......@@ -357,7 +357,7 @@ int findBreak(int& breakPos, QString text, QFontMetrics* fm, int maxWidth)
int bottomPos = 0;
while(1) {
int halfPos = (bottomPos + breakPos)/2;
int halfWidth = fm->horizontalAdvance(text, halfPos);
int halfWidth = fm->boundingRect(text.left(halfPos)).width();
if (halfWidth < maxWidth) {
bottomPos = halfPos+1;
......@@ -388,7 +388,7 @@ int findBreak(int& breakPos, QString text, QFontMetrics* fm, int maxWidth)
lastCat = cat;
breakPos = pos;
usedWidth = fm->horizontalAdvance(text, breakPos);
usedWidth = fm->boundingRect(text.left(breakPos)).width();
if (usedWidth < maxWidth) break;
return usedWidth;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment