From a48b3777b83f9703fb1dbde48bdc6d5f22f66a38 Mon Sep 17 00:00:00 2001 From: Ira Rice Date: Thu, 29 Jan 2009 00:40:12 -0700 Subject: Fixed a crash caused by getting the wrong string substring. In doing so, the algorithm was optimized further, so that only one correction needs to be done if the last line is the longest. Signed-off-by: Ira Rice --- src/gui/textbox.cpp | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/gui/textbox.cpp b/src/gui/textbox.cpp index b36897d6..c04b4262 100644 --- a/src/gui/textbox.cpp +++ b/src/gui/textbox.cpp @@ -65,6 +65,19 @@ void TextBox::setTextWrapped(const std::string &text, int minDimension) std::string::size_type spacePos, lastSpacePos = 0; xpos = 0; + spacePos = text.rfind(" ", text.size()); + if (spacePos == std::string::npos) + { + spacePos = 0; + } + else + { + const std::string word = text.substr(spacePos + 1); + const int length = getFont()->getWidth(word); + + if (length > mMinWidth) + mMinWidth = length; + } do { spacePos = line.find(" ", lastSpacePos); @@ -103,6 +116,8 @@ void TextBox::setTextWrapped(const std::string &text, int minDimension) spacePos = 0; lastNewlinePos = 0; newlinePos = text.find("\n", lastNewlinePos); + if (newlinePos == std::string::npos) + newlinePos = text.size(); line = text.substr(lastNewlinePos, newlinePos - lastNewlinePos); width = 0; @@ -122,28 +137,6 @@ void TextBox::setTextWrapped(const std::string &text, int minDimension) wrappedStream << "\n"; lastNewlinePos = newlinePos + 1; - - // Is the last line we're trying to text wrap longer than the minimum - // width we're trying to beat in the first place? If so, then rewrap - // using that length. - if (newlinePos == text.size()) - { - spacePos = text.rfind(" ", text.size()); - const std::string word = line.substr(spacePos + 1); - const int length = getFont()->getWidth(word); - - if ((length > xpos || length > minWidth) && length > mMinWidth) - { - mMinWidth = length; - wrappedStream.clear(); - wrappedStream.str(""); - spacePos = 0; - lastNewlinePos = 0; - newlinePos = text.find("\n", lastNewlinePos); - line = text.substr(lastNewlinePos, newlinePos - - lastNewlinePos); - } - } } while (newlinePos != text.size()); -- cgit v1.2.3-70-g09d2