summaryrefslogtreecommitdiff
path: root/src/gui/truetypefont.cpp
blob: 2dd7b817b3dda6ba12b3fd2208f0b08488e1e0c4 (plain) (blame)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 *  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;
}


class TextChunk
{
public:
    TextChunk(const std::string &text)
        : text(text)
    {}

    void render(Graphics *graphics,
                int x, int y,
                TTF_Font *font,
                std::unique_ptr<Image> &img,
                float scale);

    const std::string text;
    std::unique_ptr<Image> regular;
    std::unique_ptr<Image> outlined;
};

void TextChunk::render(Graphics *graphics,
                       int x, int y,
                       TTF_Font *font,
                       std::unique_ptr<Image> &img,
                       float scale)
{
    if (!img)
    {
        // Always render in white, we'll use color modulation when rendering
        constexpr SDL_Color white = { 255, 255, 255, 255 };
        SDL_Surface *surface = TTF_RenderUTF8_Blended(font,
                                                      getSafeUtf8String(text),
                                                      white);

        if (surface)
        {
            img.reset(Image::load(surface));
            SDL_FreeSurface(surface);
        }
    }

    if (img)
    {
        graphics->drawRescaledImageF(img.get(), 0, 0, x, y,
                                     img->getWidth(),
                                     img->getHeight(),
                                     img->getWidth() / scale,
                                     img->getHeight() / scale, true);

    }
}


std::list<TrueTypeFont*> TrueTypeFont::mFonts;
float TrueTypeFont::mScale = 1.0f;

TrueTypeFont::TrueTypeFont(const std::string &filename, int size, int style)
    : mFilename(filename)
    , mPointSize(size)
    , mStyle(style)
{
    if (TTF_Init() == -1)
    {
        throw GCN_EXCEPTION("Unable to initialize SDL_ttf: " +
            std::string(TTF_GetError()));
    }

    mFont = TTF_OpenFont(filename.c_str(), size * mScale);
    mFontOutline = TTF_OpenFont(filename.c_str(), size * mScale);

    if (!mFont || !mFontOutline)
    {
        throw GCN_EXCEPTION("SDLTrueTypeFont::SDLTrueTypeFont: " +
            std::string(TTF_GetError()));
    }

    TTF_SetFontStyle(mFont, style);
    TTF_SetFontStyle(mFontOutline, style);

    TTF_SetFontOutline(mFontOutline, static_cast<int>(mScale));

    mFonts.push_back(this);
}

TrueTypeFont::~TrueTypeFont()
{
    mFonts.remove(this);

    if (mFont)
        TTF_CloseFont(mFont);

    if (mFontOutline)
        TTF_CloseFont(mFontOutline);

    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);
    TextChunk &chunk = getChunk(text);

    chunk.render(g, x, y, mFont, chunk.regular, mScale);
}

void TrueTypeFont::drawString(Graphics *graphics,
                              const std::string &text,
                              int x, int y,
                              const std::optional<gcn::Color> &outlineColor,
                              const std::optional<gcn::Color> &shadowColor)
{
    if (text.empty())
        return;

    auto *g = static_cast<Graphics *>(graphics);
    auto color = graphics->getColor();
    TextChunk &chunk = getChunk(text);

    if (shadowColor)
    {
        g->setColor(*shadowColor);
        if (outlineColor)
            chunk.render(g, x, y, mFontOutline, chunk.outlined, mScale);
        else
            chunk.render(g, x + 1, y + 1, mFont, chunk.regular, mScale);
    }

    if (outlineColor)
    {
        g->setColor(*outlineColor);
        chunk.render(g, x - 1, y - 1, mFontOutline, chunk.outlined, mScale);
    }

    g->setColor(color);
    chunk.render(g, x, y, mFont, chunk.regular, 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);
        TTF_SetFontSize(font->mFontOutline, font->mPointSize * mScale);
        TTF_SetFontOutline(font->mFontOutline, mScale);
#else
        TTF_CloseFont(font->mFont);
        TTF_CloseFont(font->mFontOutline);
        font->mFont = TTF_OpenFont(font->mFilename.c_str(), font->mPointSize * mScale);
        font->mFontOutline = TTF_OpenFont(font->mFilename.c_str(), font->mPointSize * mScale);
        TTF_SetFontStyle(font->mFont, font->mStyle);
        TTF_SetFontStyle(font->mFontOutline, font->mStyle);
        TTF_SetFontOutline(font->mFontOutline, mScale);
#endif

        font->mCache.clear();
    }
}

int TrueTypeFont::getWidth(const std::string &text) const
{
    TextChunk &chunk = getChunk(text);
    if (auto img = chunk.regular.get())
        return std::ceil(img->getWidth() / mScale);

    // If the image wasn't created yet, just calculate the width of the text
    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);
}

TextChunk &TrueTypeFont::getChunk(const std::string &text) const
{
    for (auto i = mCache.begin(); i != mCache.end(); i++)
    {
        if (i->text == text)
        {
            // Raise priority: move it to front
            mCache.splice(mCache.begin(), mCache, i);
            return *i;
        }
    }

    if (mCache.size() >= CACHE_SIZE)
        mCache.pop_back();

    return mCache.emplace_front(text);
}