mirror of
git://projects.qi-hardware.com/gmenu2x.git
synced 2024-11-22 12:24:38 +02: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:
parent
d4344c7960
commit
a468b33d1e
27
src/font.cpp
27
src/font.cpp
@ -49,14 +49,29 @@ Font::~Font()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int Font::getTextWidth(const char *text)
|
int Font::getTextWidth(const string &text)
|
||||||
{
|
{
|
||||||
if (font) {
|
if (!font) {
|
||||||
int w, h;
|
return 1;
|
||||||
TTF_SizeUTF8(font, text, &w, &h);
|
}
|
||||||
return w;
|
|
||||||
|
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,
|
void Font::write(Surface *surface, const string &text,
|
||||||
|
@ -22,12 +22,7 @@ public:
|
|||||||
Font(const std::string &path, unsigned int size);
|
Font(const std::string &path, unsigned int size);
|
||||||
~Font();
|
~Font();
|
||||||
|
|
||||||
int getTextWidth(const char *text);
|
int getTextWidth(const std::string& text);
|
||||||
|
|
||||||
int getTextWidth(const std::string& text)
|
|
||||||
{
|
|
||||||
return getTextWidth(text.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
int getLineSpacing()
|
int getLineSpacing()
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user