Cleaned up the Font:getWrap code a bit.

This commit is contained in:
Alex Szpakowski
2015-08-16 01:04:51 -03:00
parent 9a1c4ad565
commit 153bd6f4cb
2 changed files with 24 additions and 22 deletions
+23 -21
View File
@@ -671,38 +671,37 @@ int Font::getWidth(char character)
void Font::getWrap(const std::string &text, float wrap, std::vector<std::string> &lines, std::vector<int> *linewidths, std::vector<bool> *wrappedlines)
{
using namespace std;
const float width_space = (float) getWidth(' ');
//split text at newlines
istringstream iss(text);
string line;
ostringstream string_builder;
while (getline(iss, line, '\n'))
{
// split line into words
vector<string> words;
istringstream word_iss(line);
copy(istream_iterator<string>(word_iss), istream_iterator<string>(),
back_inserter< vector<string> >(words));
std::istringstream iss(text);
std::string line;
std::ostringstream string_builder;
// Split text at newlines.
while (std::getline(iss, line, '\n'))
{
std::vector<std::string> words;
std::istringstream word_iss(line);
// split line into words
std::copy(std::istream_iterator<std::string>(word_iss), std::istream_iterator<std::string>(), std::back_inserter(words));
// put words back together until a wrap occurs
float width = 0.0f;
float oldwidth = 0.0f;
string_builder.str("");
vector<string>::const_iterator word_iter, wend = words.end();
for (word_iter = words.begin(); word_iter != wend; ++word_iter)
// Put words back together until a wrap occurs.
for (const std::string &word : words)
{
const string &word = *word_iter;
width += getWidth(word);
// on wordwrap, push line to line buffer and clear string builder
// On wordwrap, push line to line buffer and clear string builder.
if (width > wrap && oldwidth > 0)
{
int realw = (int) width;
// remove trailing space
string tmp = string_builder.str();
// Remove trailing space.
std::string tmp = string_builder.str();
lines.push_back(tmp.substr(0,tmp.size()-1));
string_builder.str("");
width = static_cast<float>(getWidth(word));
@@ -715,15 +714,18 @@ void Font::getWrap(const std::string &text, float wrap, std::vector<std::string>
if (wrappedlines)
wrappedlines->push_back(true);
}
string_builder << word << " ";
width += width_space;
oldwidth = width;
}
// push last line
// Push last line.
if (linewidths)
linewidths->push_back(width);
string tmp = string_builder.str();
std::string tmp = string_builder.str();
lines.push_back(tmp.substr(0,tmp.size()-1));
// Indicate that this line was not automatically wrapped.