ManaPlus
src
gui
fonts
textchunk.cpp
Go to the documentation of this file.
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
TextChunk::TextChunk
() :
53
img(
nullptr
),
54
textFont(
nullptr
),
55
text(),
56
color(),
57
color2(),
58
prev(
nullptr
),
59
next(
nullptr
)
60
{
61
#ifdef UNITTESTS
62
textChunkCnt ++;
63
#endif
// UNITTESTS
64
}
65
66
TextChunk::TextChunk
(
const
std::string &
restrict
text0,
67
const
Color
&
restrict
color0,
68
const
Color
&
restrict
color1,
69
Font
*
restrict
const
font) :
70
img(
nullptr
),
71
textFont(font),
72
text(text0),
73
color(color0),
74
color2(color1),
75
prev(
nullptr
),
76
next(
nullptr
)
77
{
78
#ifdef UNITTESTS
79
textChunkCnt ++;
80
#endif
// UNITTESTS
81
}
82
83
TextChunk::~TextChunk
()
84
{
85
delete2
(
img
)
86
#ifdef UNITTESTS
87
textChunkCnt --;
88
#endif
// UNITTESTS
89
}
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
void
TextChunk::generate
(TTF_Font *
restrict
const
font,
98
const
float
alpha)
99
{
100
BLOCK_START
(
"TextChunk::generate"
)
101
SDL_Color sdlCol;
102
sdlCol.b =
CAST_U8
(
color
.
b
);
103
sdlCol.r =
CAST_U8
(
color
.
r
);
104
sdlCol.g =
CAST_U8
(
color
.
g
);
105
#ifdef USE_SDL2
106
sdlCol.a = 255;
107
#else
// USE_SDL2
108
109
sdlCol.unused = 0;
110
#endif
// USE_SDL2
111
112
getSafeUtf8String
(
text
,
strBuf
);
113
114
SDL_Surface *surface =
MTTF_RenderUTF8_Blended
(
115
font,
strBuf
, sdlCol);
116
117
if
(surface ==
nullptr
)
118
{
119
img
=
nullptr
;
120
BLOCK_END
(
"TextChunk::generate"
)
121
return
;
122
}
123
124
const
int
width = surface->w;
125
const
int
height = surface->h;
126
127
if
(
color
.
r
!=
color2
.
r
||
color
.
g
!=
color2
.
g
128
||
color
.
b
!=
color2
.
b
)
129
{
// outlining
130
SDL_Color sdlCol2;
131
SDL_Surface *
const
background =
imageHelper
->
create32BitSurface
(
132
width, height);
133
if
(background ==
nullptr
)
134
{
135
img
=
nullptr
;
136
MSDL_FreeSurface
(surface);
137
BLOCK_END
(
"TextChunk::generate"
)
138
return
;
139
}
140
sdlCol2.b =
CAST_U8
(
color2
.
b
);
141
sdlCol2.r =
CAST_U8
(
color2
.
r
);
142
sdlCol2.g =
CAST_U8
(
color2
.
g
);
143
#ifdef USE_SDL2
144
sdlCol2.a = 255;
145
#else
// USE_SDL2
146
147
sdlCol2.unused = 0;
148
#endif
// USE_SDL2
149
150
SDL_Surface *
const
surface2 =
MTTF_RenderUTF8_Blended
(
151
font,
strBuf
, sdlCol2);
152
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
static_cast<
Uint16
>
(surface->w),
164
static_cast<
Uint16
>
(surface->h)
165
};
166
SurfaceImageHelper::combineSurface(surface2,
nullptr
,
167
background, &rect);
168
rect.x = -
OUTLINE_SIZE
;
169
SurfaceImageHelper::combineSurface(surface2,
nullptr
,
170
background, &rect);
171
rect.x = 0;
172
rect.y = -
OUTLINE_SIZE
;
173
SurfaceImageHelper::combineSurface(surface2,
nullptr
,
174
background, &rect);
175
rect.y =
OUTLINE_SIZE
;
176
SurfaceImageHelper::combineSurface(surface2,
nullptr
,
177
background, &rect);
178
rect.x = 0;
179
rect.y = 0;
180
SurfaceImageHelper::combineSurface(surface,
nullptr
,
181
background, &rect);
182
MSDL_FreeSurface
(surface);
183
MSDL_FreeSurface
(surface2);
184
surface = background;
185
}
186
img
=
imageHelper
->
createTextSurface
(
187
surface, width, height, alpha);
188
MSDL_FreeSurface
(surface);
189
190
BLOCK_END
(
"TextChunk::generate"
)
191
}
192
193
void
TextChunk::deleteImage
()
194
{
195
if
(
textFont
!=
nullptr
)
196
{
197
textFont
->
insertChunk
(
this
);
198
img
=
nullptr
;
199
}
200
else
201
{
202
delete2
(
img
)
203
}
204
}
CAST_U8
#define CAST_U8
Definition:
cast.h:27
Color
Definition:
color.h:76
Color::b
unsigned int b
Definition:
color.h:245
Color::r
unsigned int r
Definition:
color.h:235
Color::g
unsigned int g
Definition:
color.h:240
Font
Definition:
font.h:90
Font::insertChunk
void insertChunk(TextChunk *const chunk)
Definition:
font.cpp:478
ImageHelper::createTextSurface
virtual Image * createTextSurface(SDL_Surface *const tmpImage, const int width, const int height, const float alpha) const
Definition:
imagehelper.h:76
ImageHelper::create32BitSurface
virtual SDL_Surface * create32BitSurface(int width, int height) const
Definition:
imagehelper.cpp:239
TextChunk
Definition:
textchunk.h:43
TextChunk::deleteImage
void deleteImage()
Definition:
textchunk.cpp:193
TextChunk::operator==
bool operator==(const TextChunk &chunk) const
Definition:
textchunk.cpp:91
TextChunk::textFont
Font * textFont
Definition:
textchunk.h:64
TextChunk::img
Image * img
Definition:
textchunk.h:63
TextChunk::color
Color color
Definition:
textchunk.h:66
TextChunk::TextChunk
TextChunk()
Definition:
textchunk.cpp:52
TextChunk::color2
Color color2
Definition:
textchunk.h:67
TextChunk::~TextChunk
~TextChunk()
Definition:
textchunk.cpp:83
TextChunk::text
std::string text
Definition:
textchunk.h:65
TextChunk::generate
void generate(TTF_Font *const font, const float alpha)
Definition:
textchunk.cpp:97
debug.h
MTTF_RenderUTF8_Blended
#define MTTF_RenderUTF8_Blended(font, text, fg)
Definition:
debug.h:59
MSDL_FreeSurface
#define MSDL_FreeSurface(surface)
Definition:
debug.h:54
delete2.h
delete2
#define delete2(var)
Definition:
delete2.h:25
font.h
image.h
imageHelper
ImageHelper * imageHelper
Definition:
imagehelper.cpp:44
restrict
#define restrict
Definition:
localconsts.h:165
nullptr
#define nullptr
Definition:
localconsts.h:45
anonymous_namespace{textchunk.cpp}::OUTLINE_SIZE
const int OUTLINE_SIZE
Definition:
textchunk.cpp:43
BLOCK_END
#define BLOCK_END(name)
Definition:
perfomance.h:80
BLOCK_START
#define BLOCK_START(name)
Definition:
perfomance.h:79
sdlcheckutils.h
sdlshared.h
getSafeUtf8String
const char * getSafeUtf8String(const std::string &text)
Definition:
stringutils.cpp:308
stringutils.h
surfaceimagehelper.h
strBuf
char * strBuf
Definition:
textchunk.cpp:46
textchunk.h
Generated on Wed Mar 17 2021 19:19:06 for ManaPlus by
1.9.1