1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
/*
* The Mana Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2012 The Mana Developers
* Copyright (C) 2009 Aethyra Development Team
*
* This file is part of The Mana Client.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gui/truetypefont.h"
#include "graphics.h"
#include "resources/image.h"
#include <guichan/color.hpp>
#include <guichan/exception.hpp>
#include <cmath>
#include <memory>
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;
}
bool operator==(SDL_Color lhs, SDL_Color rhs)
{
return (lhs.r == rhs.r &&
lhs.g == rhs.g &&
lhs.b == rhs.b &&
lhs.a == rhs.a);
}
class TextChunk
{
public:
TextChunk(const std::string &text, SDL_Color color)
: text(text)
, color(color)
{}
void generate(TTF_Font *font)
{
SDL_Surface *surface = TTF_RenderUTF8_Blended(font,
getSafeUtf8String(text),
color);
if (!surface)
return;
img.reset(Image::load(surface));
SDL_FreeSurface(surface);
}
std::unique_ptr<Image> img;
const std::string text;
const SDL_Color color;
};
std::list<TrueTypeFont*> TrueTypeFont::mFonts;
float TrueTypeFont::mScale = 1.0f;
TrueTypeFont::TrueTypeFont(const std::string &filename, int size, int style)
: mFilename(filename)
, mPointSize(size)
{
if (TTF_Init() == -1)
{
throw GCN_EXCEPTION("Unable to initialize SDL_ttf: " +
std::string(TTF_GetError()));
}
mFont = TTF_OpenFont(filename.c_str(), size * mScale);
if (!mFont)
{
throw GCN_EXCEPTION("SDLTrueTypeFont::SDLTrueTypeFont: " +
std::string(TTF_GetError()));
}
TTF_SetFontStyle(mFont, style);
mFonts.push_back(this);
}
TrueTypeFont::~TrueTypeFont()
{
mFonts.remove(this);
if (mFont)
TTF_CloseFont(mFont);
TTF_Quit();
}
void TrueTypeFont::drawString(gcn::Graphics *graphics,
const std::string &text,
int x, int y)
{
if (text.empty())
return;
auto *g = static_cast<Graphics *>(graphics);
const gcn::Color col = g->getColor();
/* The alpha value is ignored at image generation to avoid caching the
* same text with different alpha values.
*/
const SDL_Color color = {
static_cast<Uint8>(col.r),
static_cast<Uint8>(col.g),
static_cast<Uint8>(col.b),
255
};
bool found = false;
for (auto i = mCache.begin(); i != mCache.end(); ++i)
{
auto &chunk = *i;
if (chunk.text == text && chunk.color == color)
{
// Raise priority: move it to front
mCache.splice(mCache.begin(), mCache, i);
found = true;
break;
}
}
if (!found)
{
if (mCache.size() >= CACHE_SIZE)
mCache.pop_back();
mCache.emplace_front(text, color);
mCache.front().generate(mFont);
}
if (auto img = mCache.front().img.get())
{
img->setAlpha(col.a / 255.0f);
g->drawRescaledImageF(img, 0, 0, x, y,
img->getWidth(),
img->getHeight(),
img->getWidth() / mScale,
img->getHeight() / mScale);
}
}
void TrueTypeFont::updateFontScale(float scale)
{
if (mScale == scale)
return;
mScale = scale;
for (auto font : mFonts)
{
#if SDL_TTF_VERSION_ATLEAST(2, 0, 18)
TTF_SetFontSize(font->mFont, font->mPointSize * mScale);
#else
TTF_CloseFont(font->mFont);
font->mFont = TTF_OpenFont(font->mFilename.c_str(), font->mPointSize * mScale);
#endif
font->mCache.clear();
}
}
int TrueTypeFont::getWidth(const std::string &text) const
{
for (auto i = mCache.begin(); i != mCache.end(); i++)
{
if (i->text == text)
{
// Raise priority: move it to front
// Assumption is that TTF::draw will be called next
mCache.splice(mCache.begin(), mCache, i);
if (i->img)
return std::ceil(i->img->getWidth() / mScale);
return 0;
}
}
int w, h;
TTF_SizeUTF8(mFont, getSafeUtf8String(text), &w, &h);
return std::ceil(w / mScale);
}
int TrueTypeFont::getHeight() const
{
return std::ceil(TTF_FontHeight(mFont) / mScale);
}
int TrueTypeFont::getLineHeight() const
{
return std::ceil(TTF_FontLineSkip(mFont) / mScale);
}
|