29#include <guichan/color.hpp>
30#include <guichan/exception.hpp>
37static const char *getSafeUtf8String(
const std::string &text)
39 static int UTF8_MAX_SIZE = 10;
41 static char buf[4096];
42 const int len = std::min(text.size(),
sizeof(buf) - UTF8_MAX_SIZE);
43 memcpy(buf, text.c_str(), len);
44 memset(buf + len, 0, UTF8_MAX_SIZE);
59 std::unique_ptr<Image> &img,
70 std::unique_ptr<Image> &img,
76 constexpr SDL_Color white = { 255, 255, 255, 255 };
77 SDL_Surface *surface = TTF_RenderUTF8_Blended(font,
78 getSafeUtf8String(
text),
84 SDL_FreeSurface(surface);
93 img->getWidth() / scale,
94 img->getHeight() / scale,
true);
104 : mFilename(filename)
108 if (TTF_Init() == -1)
110 throw GCN_EXCEPTION(
"Unable to initialize SDL_ttf: " +
111 std::string(TTF_GetError()));
119 throw GCN_EXCEPTION(
"SDLTrueTypeFont::SDLTrueTypeFont: " +
120 std::string(TTF_GetError()));
136 TTF_CloseFont(
mFont);
145 const std::string &text,
158 const std::string &text,
160 const std::optional<gcn::Color> &outlineColor,
161 const std::optional<gcn::Color> &shadowColor)
172 g->setColor(*shadowColor);
181 g->setColor(*outlineColor);
198#if SDL_TTF_VERSION_ATLEAST(2, 0, 18)
199 TTF_SetFontSize(font->mFont, font->mPointSize *
mScale);
200 TTF_SetFontSize(font->mFontOutline, font->mPointSize *
mScale);
201 TTF_SetFontOutline(font->mFontOutline,
mScale);
203 TTF_CloseFont(font->mFont);
204 TTF_CloseFont(font->mFontOutline);
205 font->mFont = TTF_OpenFont(font->mFilename.c_str(), font->mPointSize *
mScale);
206 font->mFontOutline = TTF_OpenFont(font->mFilename.c_str(), font->mPointSize *
mScale);
207 TTF_SetFontStyle(font->mFont, font->mStyle);
208 TTF_SetFontStyle(font->mFontOutline, font->mStyle);
209 TTF_SetFontOutline(font->mFontOutline,
mScale);
212 font->mCache.clear();
219 if (
auto img = chunk.
regular.get())
220 return std::ceil(img->getWidth() /
mScale);
224 TTF_SizeUTF8(
mFont, getSafeUtf8String(text), &w, &h);
225 return std::ceil(w /
mScale);
235 return std::ceil(TTF_FontLineSkip(
mFont) /
mScale);
253 return mCache.emplace_front(text);
A central point of control for graphics.
const gcn::Color & getColor() const final
virtual bool drawRescaledImageF(const Image *image, int srcX, int srcY, float dstX, float dstY, int width, int height, float desiredWidth, float desiredHeight, bool useColor=false)
Draws a rescaled version of the image.
static Resource * load(SDL_RWops *rw)
Loads an image from an SDL_RWops structure.
std::unique_ptr< Image > regular
TextChunk(const std::string &text)
std::unique_ptr< Image > outlined
void render(Graphics *graphics, int x, int y, TTF_Font *font, std::unique_ptr< Image > &img, float scale)
int getWidth(const std::string &text) const override
static std::list< TrueTypeFont * > mFonts
static void updateFontScale(float scale)
TextChunk & getChunk(const std::string &text) const
int getLineHeight() const
Returns the height of a line of text.
const std::string & filename() const
TrueTypeFont(const std::string &filename, int size, int style=0)
Constructor.
std::list< TextChunk > mCache
int getHeight() const override
void drawString(gcn::Graphics *graphics, const std::string &text, int x, int y) override
const unsigned int CACHE_SIZE