Issue #419: Make font:getWidth(str) account for newlines.

This commit is contained in:
vrld
2012-07-05 16:15:38 +02:00
parent 85fa71c519
commit 12f714bf74
+16 -7
View File
@@ -234,13 +234,18 @@ void Font::print(char character, float x, float y)
} }
} }
int Font::getWidth(const std::string &line) int Font::getWidth(const std::string &str)
{ {
if (line.size() == 0) return 0; if (str.size() == 0) return 0;
int temp = 0;
std::istringstream iss(str);
std::string line;
Glyph *g; Glyph *g;
int max_width = 0;
while (getline(iss, line, '\n'))
{
int width = 0;
try try
{ {
utf8::iterator<std::string::const_iterator> i(line.begin(), line.begin(), line.end()); utf8::iterator<std::string::const_iterator> i(line.begin(), line.begin(), line.end());
@@ -250,7 +255,7 @@ int Font::getWidth(const std::string &line)
int c = *i++; int c = *i++;
g = glyphs[c]; g = glyphs[c];
if (!g) g = addGlyph(c); if (!g) g = addGlyph(c);
temp += static_cast<int>(g->spacing * mSpacing); width += static_cast<int>(g->spacing * mSpacing);
} }
} }
catch(utf8::exception &e) catch(utf8::exception &e)
@@ -258,12 +263,16 @@ int Font::getWidth(const std::string &line)
throw love::Exception("%s", e.what()); throw love::Exception("%s", e.what());
} }
return temp; if (width > max_width)
max_width = width;
}
return max_width;
} }
int Font::getWidth(const char *line) int Font::getWidth(const char *str)
{ {
return this->getWidth(std::string(line)); return this->getWidth(std::string(str));
} }
int Font::getWidth(const char character) int Font::getWidth(const char character)