summaryrefslogtreecommitdiff
path: root/src/gui/fonts/textchunk.cpp
blob: 181e9f87bf78adcd253573802f80ce3ab013720e (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
/*
 *  The ManaVerse Client
 *  Copyright (C) 2004-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2009  Aethyra Development Team
 *  Copyright (C) 2011-2019  The ManaPlus Developers
 *
 *  This file is part of The ManaVerse 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/fonts/textchunk.h"

#include "sdlshared.h"

#include "gui/fonts/font.h"

#include "resources/surfaceimagehelper.h"

#include "resources/image/image.h"

#include "utils/delete2.h"
#include "utils/sdlcheckutils.h"
#include "utils/stringutils.h"

#include "debug.h"

namespace
{
    const int OUTLINE_SIZE = 1;
}  // namespace

char *restrict strBuf = nullptr;

#ifdef UNITTESTS
int textChunkCnt = 0;
#endif  // UNITTESTS

TextChunk::TextChunk() :
    img(nullptr),
    textFont(nullptr),
    text(),
    color(),
    color2(),
    prev(nullptr),
    next(nullptr)
{
#ifdef UNITTESTS
    textChunkCnt ++;
#endif  // UNITTESTS
}

TextChunk::TextChunk(const std::string &restrict text0,
                     const Color &restrict color0,
                     const Color &restrict color1,
                     Font *restrict const font) :
    img(nullptr),
    textFont(font),
    text(text0),
    color(color0),
    color2(color1),
    prev(nullptr),
    next(nullptr)
{
#ifdef UNITTESTS
    textChunkCnt ++;
#endif  // UNITTESTS
}

TextChunk::~TextChunk()
{
    delete2(img)
#ifdef UNITTESTS
    textChunkCnt --;
#endif  // UNITTESTS
}

bool TextChunk::operator==(const TextChunk &restrict chunk) const
{
    return chunk.text == text && chunk.color == color
            && chunk.color2 == color2;
}

void TextChunk::generate(TTF_Font *restrict const font,
                         const float alpha)
{
    BLOCK_START("TextChunk::generate")
    SDL_Color sdlCol;
    sdlCol.b = CAST_U8(color.b);
    sdlCol.r = CAST_U8(color.r);
    sdlCol.g = CAST_U8(color.g);
#ifdef USE_SDL2
    sdlCol.a = 255;
#else  // USE_SDL2

    sdlCol.unused = 0;
#endif  // USE_SDL2

    getSafeUtf8String(text, strBuf);

    SDL_Surface *surface = MTTF_RenderUTF8_Blended(
        font, strBuf, sdlCol);

    if (surface == nullptr)
    {
        img = nullptr;
        BLOCK_END("TextChunk::generate")
        return;
    }

    const int width = surface->w;
    const int height = surface->h;

    if (color.r != color2.r || color.g != color2.g
        || color.b != color2.b)
    {   // outlining
        SDL_Color sdlCol2;
        SDL_Surface *const background = imageHelper->create32BitSurface(
            width, height);
        if (background == nullptr)
        {
            img = nullptr;
            MSDL_FreeSurface(surface);
            BLOCK_END("TextChunk::generate")
            return;
        }
        sdlCol2.b = CAST_U8(color2.b);
        sdlCol2.r = CAST_U8(color2.r);
        sdlCol2.g = CAST_U8(color2.g);
#ifdef USE_SDL2
        sdlCol2.a = 255;
#else  // USE_SDL2

        sdlCol2.unused = 0;
#endif  // USE_SDL2

        SDL_Surface *const surface2 = MTTF_RenderUTF8_Blended(
            font, strBuf, sdlCol2);
        if (surface2 == nullptr)
        {
            img = nullptr;
            MSDL_FreeSurface(surface);
            BLOCK_END("TextChunk::generate")
            return;
        }
        SDL_Rect rect =
        {
            OUTLINE_SIZE,
            0,
            static_cast<Uint16>(surface->w),
            static_cast<Uint16>(surface->h)
        };
        SurfaceImageHelper::combineSurface(surface2, nullptr,
            background, &rect);
        rect.x = -OUTLINE_SIZE;
        SurfaceImageHelper::combineSurface(surface2, nullptr,
            background, &rect);
        rect.x = 0;
        rect.y = -OUTLINE_SIZE;
        SurfaceImageHelper::combineSurface(surface2, nullptr,
            background, &rect);
        rect.y = OUTLINE_SIZE;
        SurfaceImageHelper::combineSurface(surface2, nullptr,
            background, &rect);
        rect.x = 0;
        rect.y = 0;
        SurfaceImageHelper::combineSurface(surface, nullptr,
            background, &rect);
        MSDL_FreeSurface(surface);
        MSDL_FreeSurface(surface2);
        surface = background;
    }
    img = imageHelper->createTextSurface(
        surface, width, height, alpha);
    MSDL_FreeSurface(surface);

    BLOCK_END("TextChunk::generate")
}

void TextChunk::deleteImage()
{
    if (textFont != nullptr)
    {
        textFont->insertChunk(this);
        img = nullptr;
    }
    else
    {
        delete2(img)
    }
}