1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-06-30 21:13:51 +03:00

When computing the width of multi-line text, split lines and return the longest

Along with the newline support in Font::write(), Font::getTextWidth() now
splits text along '\n' and returns the width of the longest split.
This commit is contained in:
Nebuleon Fumika 2014-07-23 08:29:53 +00:00 committed by Maarten ter Huurne
parent d4344c7960
commit a468b33d1e
2 changed files with 22 additions and 12 deletions

View File

@ -49,14 +49,29 @@ Font::~Font()
}
}
int Font::getTextWidth(const char *text)
int Font::getTextWidth(const string &text)
{
if (font) {
int w, h;
TTF_SizeUTF8(font, text, &w, &h);
return w;
if (!font) {
return 1;
}
int w;
size_t pos = text.find('\n', 0);
if (pos == string::npos) {
TTF_SizeUTF8(font, text.c_str(), &w, nullptr);
return w;
} else {
int maxWidth = 1;
size_t prev = 0;
do {
TTF_SizeUTF8(font, text.substr(prev, pos - prev).c_str(), &w, nullptr);
maxWidth = max(w, maxWidth);
prev = pos + 1;
pos = text.find('\n', prev);
} while (pos != string::npos);
TTF_SizeUTF8(font, text.substr(prev).c_str(), &w, nullptr);
return max(w, maxWidth);
}
else return 1;
}
void Font::write(Surface *surface, const string &text,

View File

@ -22,12 +22,7 @@ public:
Font(const std::string &path, unsigned int size);
~Font();
int getTextWidth(const char *text);
int getTextWidth(const std::string& text)
{
return getTextWidth(text.c_str());
}
int getTextWidth(const std::string& text);
int getLineSpacing()
{