GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/fonts/textchunk.cpp Lines: 61 71 85.9 %
Date: 2017-11-29 Branches: 12 18 66.7 %

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