From 153bd6f4cba50f86040775a530dab3deea09e575 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sun, 16 Aug 2015 01:04:51 -0300 Subject: [PATCH] Cleaned up the Font:getWrap code a bit. --- src/modules/graphics/opengl/Font.cpp | 44 +++++++++++++++------------- src/modules/system/wrap_System.cpp | 2 +- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/modules/graphics/opengl/Font.cpp b/src/modules/graphics/opengl/Font.cpp index c229571d0..3d425b17a 100644 --- a/src/modules/graphics/opengl/Font.cpp +++ b/src/modules/graphics/opengl/Font.cpp @@ -671,38 +671,37 @@ int Font::getWidth(char character) void Font::getWrap(const std::string &text, float wrap, std::vector &lines, std::vector *linewidths, std::vector *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 words; - istringstream word_iss(line); - copy(istream_iterator(word_iss), istream_iterator(), - back_inserter< vector >(words)); + std::istringstream iss(text); + std::string line; + std::ostringstream string_builder; + + // Split text at newlines. + while (std::getline(iss, line, '\n')) + { + std::vector words; + std::istringstream word_iss(line); + + // split line into words + std::copy(std::istream_iterator(word_iss), std::istream_iterator(), std::back_inserter(words)); - // put words back together until a wrap occurs float width = 0.0f; float oldwidth = 0.0f; string_builder.str(""); - vector::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(getWidth(word)); @@ -715,15 +714,18 @@ void Font::getWrap(const std::string &text, float wrap, std::vector 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. diff --git a/src/modules/system/wrap_System.cpp b/src/modules/system/wrap_System.cpp index 505865457..841edb7a5 100644 --- a/src/modules/system/wrap_System.cpp +++ b/src/modules/system/wrap_System.cpp @@ -88,7 +88,7 @@ int w_openURL(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); return 0; }