diff options
author | Max Horn | 2005-05-17 23:14:13 +0000 |
---|---|---|
committer | Max Horn | 2005-05-17 23:14:13 +0000 |
commit | b985f34049b5c6ae84d1c1a22501df4656a5e9fe (patch) | |
tree | 6e0c45884d0fa37965d3706ea60e1933d5b499b0 | |
parent | 5cc3c4b9035c5efbd34ed51c387c3a768b15c363 (diff) | |
download | scummvm-rg350-b985f34049b5c6ae84d1c1a22501df4656a5e9fe.tar.gz scummvm-rg350-b985f34049b5c6ae84d1c1a22501df4656a5e9fe.tar.bz2 scummvm-rg350-b985f34049b5c6ae84d1c1a22501df4656a5e9fe.zip |
Fix word wrapping: do not generate spaces at the start/end of the wrapped lines
svn-id: r18142
-rw-r--r-- | graphics/font.cpp | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/graphics/font.cpp b/graphics/font.cpp index f00e550c4d..3467ef6712 100644 --- a/graphics/font.cpp +++ b/graphics/font.cpp @@ -197,6 +197,7 @@ int Font::wordWrapText(const Common::String &str, int maxWidth, Common::StringLi for (Common::String::const_iterator x = str.begin(); x != str.end(); ++x) { const char c = *x; const int w = getCharWidth(c); + const bool wouldExceedWidth = (lineWidth + tmpWidth + w > maxWidth); // If this char is a whitespace, then it represents a potential // 'wrap point' where wrapping could take place. Everything that @@ -208,22 +209,28 @@ int Font::wordWrapText(const Common::String &str, int maxWidth, Common::StringLi tmpStr.clear(); tmpWidth = 0; - } - // If we encounter a line break (\n), the line is complete. - if (c == '\n') { - wrapper.add(line, lineWidth); - continue; + // If we encounter a line break (\n), or if the new space would + // cause the line to overflow: start a new line + if (c == '\n' || wouldExceedWidth) { + wrapper.add(line, lineWidth); + continue; + } } // If the max line width would be exceeded by adding this char, // insert a line break. - if (lineWidth + tmpWidth + w > maxWidth) { + if (wouldExceedWidth) { // Commit what we have so far, *if* we have anything. // If line is empty, then we are looking at a word // which exceeds the maximum line width. if (lineWidth > 0) { wrapper.add(line, lineWidth); + // Trim left side + while (tmpStr.size() && isspace(tmpStr[0])) { + tmpWidth -= getCharWidth(tmpStr[0]); + tmpStr.deleteChar(0); + } } else { wrapper.add(tmpStr, tmpWidth); } |