Rewrote the text wrapping algorithm.

Fixes some subtle issues with newlines at the end of a word that would be at the end of a line.
Still not quite perfect (sometimes goes to a new line when it's not necessary), but at least things aren't overflowing out of TextComponents anymore.
This commit is contained in:
Aloshi 2014-05-18 21:13:36 -05:00
parent 0d39586c2d
commit 4dd60c14e7

View file

@ -280,56 +280,37 @@ std::string Font::wrapText(std::string text, float xLen) const
std::string out; std::string out;
std::string line, word, temp; std::string line, word, temp;
size_t space, newline; size_t space;
Eigen::Vector2f textSize; Eigen::Vector2f textSize;
while(text.length() > 0 || !line.empty()) //while there's text or we still have text to render while(text.length() > 0) //while there's text or we still have text to render
{ {
space = text.find(' ', 0); space = text.find_first_of(" \t\n");
if(space == std::string::npos) if(space == std::string::npos)
space = text.length() - 1; space = text.length() - 1;
word = text.substr(0, space + 1); word = text.substr(0, space + 1);
text.erase(0, space + 1);
//check if the next word contains a newline
newline = word.find('\n', 0);
if(newline != std::string::npos)
{
//get everything up to the newline
word = word.substr(0, newline);
text.erase(0, newline + 1);
}else{
text.erase(0, space + 1);
}
temp = line + word; temp = line + word;
textSize = sizeText(temp); textSize = sizeText(temp);
//if we're on the last word and it'll fit on the line, just add it to the line // if the word will fit on the line, add it to our line, and continue
if((textSize.x() <= xLen && text.length() == 0) || newline != std::string::npos) if(textSize.x() <= xLen)
{ {
line = temp; line = temp;
word = ""; continue;
}
//if the next line will be too long or we're on the last of the text, render it
if(textSize.x() > xLen || text.length() == 0 || newline != std::string::npos)
{
//output line now
out += line + '\n';
//move the word we skipped to the next line
line = word;
}else{ }else{
//there's still space, continue building the line // the next word won't fit, so break here
line = temp; out += line + '\n';
line = word;
} }
} }
if(!out.empty() && newline == std::string::npos) //chop off the last newline if we added one // whatever's left should fit
out.erase(out.length() - 1, 1); out += line;
return out; return out;
} }