GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/fonts/textchunk.cpp Lines: 60 70 85.7 %
Date: 2021-03-17 Branches: 12 24 50.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2009  Aethyra Development Team
6
 *  Copyright (C) 2011-2019  The ManaPlus Developers
7
 *  Copyright (C) 2019-2021  Andrei Karas
8
 *
9
 *  This file is part of The ManaPlus Client.
10
 *
11
 *  This program is free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  any later version.
15
 *
16
 *  This program is distributed in the hope that it will be useful,
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *  GNU General Public License for more details.
20
 *
21
 *  You should have received a copy of the GNU General Public License
22
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
#include "gui/fonts/textchunk.h"
26
27
#include "sdlshared.h"
28
29
#include "gui/fonts/font.h"
30
31
#include "resources/surfaceimagehelper.h"
32
33
#include "resources/image/image.h"
34
35
#include "utils/delete2.h"
36
#include "utils/sdlcheckutils.h"
37
#include "utils/stringutils.h"
38
39
#include "debug.h"
40
41
namespace
42
{
43
    const int OUTLINE_SIZE = 1;
44
}  // namespace
45
46
char *restrict strBuf = nullptr;
47
48
#ifdef UNITTESTS
49
int textChunkCnt = 0;
50
#endif  // UNITTESTS
51
52
1459
TextChunk::TextChunk() :
53
    img(nullptr),
54
    textFont(nullptr),
55
    text(),
56
    color(),
57
    color2(),
58
    prev(nullptr),
59
5836
    next(nullptr)
60
{
61
#ifdef UNITTESTS
62
1459
    textChunkCnt ++;
63
#endif  // UNITTESTS
64
1459
}
65
66
242
TextChunk::TextChunk(const std::string &restrict text0,
67
                     const Color &restrict color0,
68
                     const Color &restrict color1,
69
242
                     Font *restrict const font) :
70
    img(nullptr),
71
    textFont(font),
72
    text(text0),
73
    color(color0),
74
    color2(color1),
75
    prev(nullptr),
76
484
    next(nullptr)
77
{
78
#ifdef UNITTESTS
79
242
    textChunkCnt ++;
80
#endif  // UNITTESTS
81
242
}
82
83
5103
TextChunk::~TextChunk()
84
{
85
1701
    delete2(img)
86
#ifdef UNITTESTS
87
1701
    textChunkCnt --;
88
#endif  // UNITTESTS
89
1701
}
90
91
bool TextChunk::operator==(const TextChunk &restrict chunk) const
92
{
93
    return chunk.text == text && chunk.color == color
94
            && chunk.color2 == color2;
95
}
96
97
244
void TextChunk::generate(TTF_Font *restrict const font,
98
                         const float alpha)
99
{
100
    BLOCK_START("TextChunk::generate")
101
    SDL_Color sdlCol;
102
244
    sdlCol.b = CAST_U8(color.b);
103
244
    sdlCol.r = CAST_U8(color.r);
104
244
    sdlCol.g = CAST_U8(color.g);
105
#ifdef USE_SDL2
106
244
    sdlCol.a = 255;
107
#else  // USE_SDL2
108
109
    sdlCol.unused = 0;
110
#endif  // USE_SDL2
111
112
732
    getSafeUtf8String(text, strBuf);
113
114
244
    SDL_Surface *surface = MTTF_RenderUTF8_Blended(
115
        font, strBuf, sdlCol);
116
117
244
    if (surface == nullptr)
118
    {
119
        img = nullptr;
120
        BLOCK_END("TextChunk::generate")
121
        return;
122
    }
123
124
244
    const int width = surface->w;
125
244
    const int height = surface->h;
126
127
244
    if (color.r != color2.r || color.g != color2.g
128
114
        || color.b != color2.b)
129
    {   // outlining
130
        SDL_Color sdlCol2;
131
130
        SDL_Surface *const background = imageHelper->create32BitSurface(
132
130
            width, height);
133
130
        if (background == nullptr)
134
        {
135
            img = nullptr;
136
            MSDL_FreeSurface(surface);
137
            BLOCK_END("TextChunk::generate")
138
            return;
139
        }
140
130
        sdlCol2.b = CAST_U8(color2.b);
141
130
        sdlCol2.r = CAST_U8(color2.r);
142
130
        sdlCol2.g = CAST_U8(color2.g);
143
#ifdef USE_SDL2
144
130
        sdlCol2.a = 255;
145
#else  // USE_SDL2
146
147
        sdlCol2.unused = 0;
148
#endif  // USE_SDL2
149
150
130
        SDL_Surface *const surface2 = MTTF_RenderUTF8_Blended(
151
            font, strBuf, sdlCol2);
152
130
        if (surface2 == nullptr)
153
        {
154
            img = nullptr;
155
            MSDL_FreeSurface(surface);
156
            BLOCK_END("TextChunk::generate")
157
            return;
158
        }
159
        SDL_Rect rect =
160
        {
161
            OUTLINE_SIZE,
162
            0,
163
130
            static_cast<Uint16>(surface->w),
164
130
            static_cast<Uint16>(surface->h)
165
390
        };
166
        SurfaceImageHelper::combineSurface(surface2, nullptr,
167
130
            background, &rect);
168
130
        rect.x = -OUTLINE_SIZE;
169
        SurfaceImageHelper::combineSurface(surface2, nullptr,
170
130
            background, &rect);
171
130
        rect.x = 0;
172
130
        rect.y = -OUTLINE_SIZE;
173
        SurfaceImageHelper::combineSurface(surface2, nullptr,
174
130
            background, &rect);
175
130
        rect.y = OUTLINE_SIZE;
176
        SurfaceImageHelper::combineSurface(surface2, nullptr,
177
130
            background, &rect);
178
130
        rect.x = 0;
179
130
        rect.y = 0;
180
        SurfaceImageHelper::combineSurface(surface, nullptr,
181
130
            background, &rect);
182
130
        MSDL_FreeSurface(surface);
183
130
        MSDL_FreeSurface(surface2);
184
130
        surface = background;
185
    }
186
488
    img = imageHelper->createTextSurface(
187
244
        surface, width, height, alpha);
188
244
    MSDL_FreeSurface(surface);
189
190
    BLOCK_END("TextChunk::generate")
191
}
192
193
1494
void TextChunk::deleteImage()
194
{
195
1494
    if (textFont != nullptr)
196
    {
197
431
        textFont->insertChunk(this);
198
431
        img = nullptr;
199
    }
200
    else
201
    {
202
1063
        delete2(img)
203
    }
204
1494
}