diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/truetypefont.cpp | 11 | ||||
-rw-r--r-- | src/utils/stringutils.cpp | 11 | ||||
-rw-r--r-- | src/utils/stringutils.h | 2 |
3 files changed, 21 insertions, 3 deletions
diff --git a/src/gui/truetypefont.cpp b/src/gui/truetypefont.cpp index 3bf1febe..55d1a18a 100644 --- a/src/gui/truetypefont.cpp +++ b/src/gui/truetypefont.cpp @@ -26,9 +26,11 @@ #include "resources/image.h" +#include "utils/stringutils.h" + #include <guichan/exception.hpp> -#define CACHE_SIZE 256 +const unsigned int CACHE_SIZE = 256; class TextChunk { @@ -55,8 +57,10 @@ class TextChunk sdlCol.r = color.r; sdlCol.g = color.g; + const char* str = getSafeUtf8String(text); SDL_Surface *surface = TTF_RenderUTF8_Blended( - font, text.c_str(), sdlCol); + font, str, sdlCol); + delete[] str; if (!surface) { @@ -175,7 +179,8 @@ int TrueTypeFont::getWidth(const std::string &text) const } int w, h; - TTF_SizeUTF8(mFont, text.c_str(), &w, &h); + const char* str = getSafeUtf8String(text); + TTF_SizeUTF8(mFont, str, &w, &h); return w; } diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 01bf0d3c..9fe3de14 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -21,10 +21,13 @@ #include "utils/stringutils.h" +#include <string.h> #include <algorithm> #include <cstdarg> #include <cstdio> +const int UTF8_MAX_SIZE = 10; + std::string &trim(std::string &str) { std::string::size_type pos = str.find_last_not_of(' '); @@ -164,3 +167,11 @@ const std::string findSameSubstring(const std::string &str1, const std::string & } return str1.substr(0, minLength); } + +const char* getSafeUtf8String(std::string text) +{ + char* buf = new char[text.size() + UTF8_MAX_SIZE]; + memcpy(buf, text.c_str(), text.size()); + memset(buf + text.size(), 0, UTF8_MAX_SIZE); + return buf; +}
\ No newline at end of file diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 5e4718c0..ec82e240 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -123,4 +123,6 @@ bool isWordSeparator(char chr); const std::string findSameSubstring(const std::string &str1, const std::string &str2); +const char* getSafeUtf8String(std::string text); + #endif // UTILS_STRINGUTILS_H |