diff --git a/src/font.cpp b/src/font.cpp index 82c08f8..ce5b35e 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -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, diff --git a/src/font.h b/src/font.h index 68dde24..ff58ac9 100644 --- a/src/font.h +++ b/src/font.h @@ -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() {