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) 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(' '); const float width_space = (float) getWidth(' ');
//split text at newlines std::istringstream iss(text);
istringstream iss(text); std::string line;
string line; std::ostringstream string_builder;
ostringstream string_builder;
while (getline(iss, line, '\n')) // Split text at newlines.
{ while (std::getline(iss, line, '\n'))
// split line into words {
vector<string> words; std::vector<std::string> words;
istringstream word_iss(line); std::istringstream word_iss(line);
copy(istream_iterator<string>(word_iss), istream_iterator<string>(),
back_inserter< vector<string> >(words)); // 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 width = 0.0f;
float oldwidth = 0.0f; float oldwidth = 0.0f;
string_builder.str(""); 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); 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) if (width > wrap && oldwidth > 0)
{ {
int realw = (int) width; int realw = (int) width;
// remove trailing space // Remove trailing space.
string tmp = string_builder.str(); std::string tmp = string_builder.str();
lines.push_back(tmp.substr(0,tmp.size()-1)); lines.push_back(tmp.substr(0,tmp.size()-1));
string_builder.str(""); string_builder.str("");
width = static_cast<float>(getWidth(word)); 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) if (wrappedlines)
wrappedlines->push_back(true); wrappedlines->push_back(true);
} }
string_builder << word << " "; string_builder << word << " ";
width += width_space; width += width_space;
oldwidth = width; oldwidth = width;
} }
// push last line
// Push last line.
if (linewidths) if (linewidths)
linewidths->push_back(width); linewidths->push_back(width);
string tmp = string_builder.str(); std::string tmp = string_builder.str();
lines.push_back(tmp.substr(0,tmp.size()-1)); lines.push_back(tmp.substr(0,tmp.size()-1));
// Indicate that this line was not automatically wrapped. // Indicate that this line was not automatically wrapped.
+1 -1
View File
@@ -88,7 +88,7 @@ int w_openURL(lua_State *L)
int w_vibrate(lua_State *L) int w_vibrate(lua_State *L)
{ {
double seconds = luaL_checknumber(L, 1); double seconds = luaL_optnumber(L, 1, 0.5);
instance()->vibrate(seconds); instance()->vibrate(seconds);
return 0; return 0;
} }