qtutils: fixed issues with word wrap
Fixes issue that triggers infinite loop in some circumstances. Fixes #1947 probably
This commit is contained in:
+32
-6
@@ -20,6 +20,8 @@
|
||||
|
||||
#include "qtutils.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
namespace olive {
|
||||
|
||||
int QtUtils::QFontMetricsWidth(QFontMetrics fm, const QString& s) {
|
||||
@@ -94,19 +96,43 @@ QStringList QtUtils::WordWrapString(const QString &s, const QFontMetrics &fm, in
|
||||
QString this_line = lines.at(i);
|
||||
|
||||
while (this_line.size() > 1 && QFontMetricsWidth(fm, this_line) >= bounding_width) {
|
||||
int old_size = this_line.size();
|
||||
int hard_break = -1;
|
||||
|
||||
for (int j=this_line.size()-1; j>=0; j--) {
|
||||
if (this_line.at(j).isSpace()) {
|
||||
const QChar &char_test = this_line.at(j);
|
||||
|
||||
if (char_test.isSpace()
|
||||
|| char_test == '-') {
|
||||
if (QFontMetricsWidth(fm, this_line.left(j)) < bounding_width) {
|
||||
if (!char_test.isSpace()) {
|
||||
j++;
|
||||
}
|
||||
|
||||
QString chopped = this_line.left(j);
|
||||
if (QFontMetricsWidth(fm, chopped) < bounding_width) {
|
||||
|
||||
list.append(chopped);
|
||||
|
||||
int k = j+1;
|
||||
while (k < this_line.size() && this_line.at(k).isSpace()) {
|
||||
k++;
|
||||
while (j < this_line.size() && this_line.at(j).isSpace()) {
|
||||
j++;
|
||||
}
|
||||
this_line.remove(0, k);
|
||||
this_line.remove(0, j);
|
||||
break;
|
||||
}
|
||||
} else if (hard_break == -1 && QFontMetricsWidth(fm, this_line.left(j)) < bounding_width) {
|
||||
// In case we can't find a better place to split, split at the earliest time the line
|
||||
// goes under the width limit
|
||||
hard_break = j;
|
||||
}
|
||||
}
|
||||
|
||||
if (old_size == this_line.size()) {
|
||||
if (hard_break != -1) {
|
||||
list.append(this_line.left(hard_break));
|
||||
this_line.remove(0, hard_break);
|
||||
} else {
|
||||
qWarning() << "Failed to find anywhere to wrap. Returning full line.";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user