Commit 4dd60c14 authored by Aloshi's avatar Aloshi

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.
parent 0d39586c
......@@ -280,56 +280,37 @@ std::string Font::wrapText(std::string text, float xLen) const
std::string out;
std::string line, word, temp;
size_t space, newline;
size_t space;
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)
space = text.length() - 1;
word = text.substr(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);
}
text.erase(0, space + 1);
temp = line + word;
textSize = sizeText(temp);
//if we're on the last word and it'll fit on the line, just add it to the line
if((textSize.x() <= xLen && text.length() == 0) || newline != std::string::npos)
// if the word will fit on the line, add it to our line, and continue
if(textSize.x() <= xLen)
{
line = temp;
word = "";
}
//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
continue;
}else{
// the next word won't fit, so break here
out += line + '\n';
//move the word we skipped to the next line
line = word;
}else{
//there's still space, continue building the line
line = temp;
}
}
if(!out.empty() && newline == std::string::npos) //chop off the last newline if we added one
out.erase(out.length() - 1, 1);
// whatever's left should fit
out += line;
return out;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment