1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-07-02 18:32:20 +03:00

Changed word wrapping to avoid trailing empty line

If a text file ends in a newline, the previous line splitting code
would append an empty line at the end, which looked odd. So now we
explicitly check that a newline is only inserted if more characters
follow.
This commit is contained in:
Maarten ter Huurne 2014-07-24 12:34:19 +02:00
parent bac622fc39
commit e44db20f31

View File

@ -76,21 +76,19 @@ int Font::getTextWidth(const string &text)
string Font::wordWrap(const string &text, int width) string Font::wordWrap(const string &text, int width)
{ {
const size_t len = text.length();
string result; string result;
result.reserve(text.length()); result.reserve(len);
size_t start = 0, end = text.find('\n'); size_t start = 0;
while (end != string::npos) { while (true) {
if (start != end) { size_t end = min(text.find('\n', start), len);
result.append(wordWrapSingleLine(text, start, end, width)); result.append(wordWrapSingleLine(text, start, end, width));
}
result.append("\n");
start = end + 1; start = end + 1;
end = text.find('\n', start); if (start >= len) {
} break;
}
if (start < text.length()) { result.push_back('\n');
result.append(wordWrapSingleLine(text, start, text.length(), width));
} }
return result; return result;