summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gui/truetypefont.cpp15
-rw-r--r--src/utils/stringutils.cpp10
-rw-r--r--src/utils/stringutils.h2
3 files changed, 11 insertions, 16 deletions
diff --git a/src/gui/truetypefont.cpp b/src/gui/truetypefont.cpp
index 167a66b2..adb49780 100644
--- a/src/gui/truetypefont.cpp
+++ b/src/gui/truetypefont.cpp
@@ -26,13 +26,22 @@
#include "resources/image.h"
-#include "utils/stringutils.h"
-
#include <guichan/color.hpp>
#include <guichan/exception.hpp>
const unsigned int CACHE_SIZE = 256;
+static const char *getSafeUtf8String(const std::string &text)
+{
+ static int UTF8_MAX_SIZE = 10;
+
+ static char buf[4096];
+ const int len = std::min(text.size(), sizeof(buf) - UTF8_MAX_SIZE);
+ memcpy(buf, text.c_str(), len);
+ memset(buf + len, 0, UTF8_MAX_SIZE);
+ return buf;
+}
+
class TextChunk
{
public:
@@ -62,7 +71,6 @@ class TextChunk
const char *str = getSafeUtf8String(text);
SDL_Surface *surface = TTF_RenderUTF8_Blended(
font, str, sdlCol);
- delete[] str;
if (!surface)
{
@@ -180,7 +188,6 @@ int TrueTypeFont::getWidth(const std::string &text) const
int w, h;
const char *str = getSafeUtf8String(text);
TTF_SizeUTF8(mFont, str, &w, &h);
- delete[] str;
return w;
}
diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp
index 7a951ec6..d9c65dd7 100644
--- a/src/utils/stringutils.cpp
+++ b/src/utils/stringutils.cpp
@@ -28,8 +28,6 @@
#include <cstdarg>
#include <cstdio>
-static int UTF8_MAX_SIZE = 10;
-
std::string &trim(std::string &str)
{
std::string::size_type pos = str.find_last_not_of(' ');
@@ -170,14 +168,6 @@ std::string findSameSubstring(const std::string &str1,
return str1.substr(0, minLength);
}
-const char *getSafeUtf8String(const 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;
-}
-
bool getBoolFromString(const std::string &text, bool def)
{
std::string a = text;
diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h
index 8e5f1102..b394e29f 100644
--- a/src/utils/stringutils.h
+++ b/src/utils/stringutils.h
@@ -136,8 +136,6 @@ bool isWordSeparator(char chr);
std::string findSameSubstring(const std::string &str1,
const std::string &str2);
-const char *getSafeUtf8String(const std::string &text);
-
/**
* Returns a bool value depending on the given string value.
*