FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SFML/src/SFML/Graphics/Text.cpp at master · sharedptr/SFML · GitHub
sharedptr
/
SFML
Public
forked from
SFML/SFML
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
SFML
/
src
/
SFML
/
Graphics
/
Text.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
350 lines (284 loc) · 10.9 KB
Breadcrumbs
SFML
/
src
/
SFML
/
Graphics
/
Text.cpp
Copy path
File metadata and controls
350 lines (284 loc) · 10.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//
//////////////////////////////////////////////////////////
//
//
SFML - Simple and Fast Multimedia Library
//
Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
//
//
This software is provided 'as-is', without any express or implied warranty.
//
In no event will the authors be held liable for any damages arising from the use of this software.
//
//
Permission is granted to anyone to use this software for any purpose,
//
including commercial applications, and to alter it and redistribute it freely,
//
subject to the following restrictions:
//
//
1. The origin of this software must not be misrepresented;
//
you must not claim that you wrote the original software.
//
If you use this software in a product, an acknowledgment
//
in the product documentation would be appreciated but is not required.
//
//
2. Altered source versions must be plainly marked as such,
//
and must not be misrepresented as being the original software.
//
//
3. This notice may not be removed or altered from any source distribution.
//
//
//////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////
//
Headers
//
//////////////////////////////////////////////////////////
#
include
<
SFML/Graphics/Text.hpp
>
#
include
<
SFML/Graphics/Texture.hpp
>
#
include
<
SFML/Graphics/RenderTarget.hpp
>
#
include
<
cassert
>
namespace
sf
{
//
//////////////////////////////////////////////////////////
Text::Text
() :
m_string
(),
m_font
(
NULL
),
m_characterSize
(
30
),
m_style
(Regular),
m_color
(
255
,
255
,
255
),
m_vertices
(Quads),
m_bounds
()
{
}
//
//////////////////////////////////////////////////////////
Text::Text
(
const
String& string,
const
Font& font,
unsigned
int
characterSize) :
m_string
(string),
m_font
(&font),
m_characterSize
(characterSize),
m_style
(Regular),
m_color
(
255
,
255
,
255
),
m_vertices
(Quads),
m_bounds
()
{
updateGeometry
();
}
//
//////////////////////////////////////////////////////////
void
Text::setString
(
const
String& string)
{
m_string = string;
updateGeometry
();
}
//
//////////////////////////////////////////////////////////
void
Text::setFont
(
const
Font& font)
{
if
(m_font != &font)
{
m_font = &font;
updateGeometry
();
}
}
//
//////////////////////////////////////////////////////////
void
Text::setCharacterSize
(
unsigned
int
size)
{
if
(m_characterSize != size)
{
m_characterSize = size;
updateGeometry
();
}
}
//
//////////////////////////////////////////////////////////
void
Text::setStyle
(Uint32 style)
{
if
(m_style != style)
{
m_style = style;
updateGeometry
();
}
}
//
//////////////////////////////////////////////////////////
void
Text::setColor
(
const
Color& color)
{
if
(color != m_color)
{
m_color = color;
for
(
unsigned
int
i =
0
; i < m_vertices.
getVertexCount
(); ++i)
m_vertices[i].
color
= m_color;
}
}
//
//////////////////////////////////////////////////////////
const
String&
Text::getString
()
const
{
return
m_string;
}
//
//////////////////////////////////////////////////////////
const
Font*
Text::getFont
()
const
{
return
m_font;
}
//
//////////////////////////////////////////////////////////
unsigned
int
Text::getCharacterSize
()
const
{
return
m_characterSize;
}
//
//////////////////////////////////////////////////////////
Uint32
Text::getStyle
()
const
{
return
m_style;
}
//
//////////////////////////////////////////////////////////
const
Color&
Text::getColor
()
const
{
return
m_color;
}
//
//////////////////////////////////////////////////////////
Vector2f
Text::findCharacterPos
(std::
size_t
index)
const
{
//
Make sure that we have a valid font
if
(!m_font)
return
Vector2f
();
//
Adjust the index if it's out of range
if
(index > m_string.
getSize
())
index = m_string.
getSize
();
//
Precompute the variables needed by the algorithm
bool
bold = (m_style & Bold) !=
0
;
float
hspace =
static_cast
<
float
>(m_font->
getGlyph
(L
'
'
, m_characterSize, bold).
advance
);
float
vspace =
static_cast
<
float
>(m_font->
getLineSpacing
(m_characterSize));
//
Compute the position
Vector2f position;
Uint32 prevChar =
0
;
for
(std::
size_t
i =
0
; i < index; ++i)
{
Uint32 curChar = m_string[i];
//
Apply the kerning offset
position.
x
+=
static_cast
<
float
>(m_font->
getKerning
(prevChar, curChar, m_characterSize));
prevChar = curChar;
//
Handle special characters
switch
(curChar)
{
case
'
'
: position.
x
+= hspace;
continue
;
case
'
\t
'
: position.
x
+= hspace *
4
;
continue
;
case
'
\n
'
: position.
y
+= vspace; position.
x
=
0
;
continue
;
case
'
\v
'
: position.
y
+= vspace *
4
;
continue
;
}
//
For regular characters, add the advance offset of the glyph
position.
x
+=
static_cast
<
float
>(m_font->
getGlyph
(curChar, m_characterSize, bold).
advance
);
}
//
Transform the position to global coordinates
position =
getTransform
().
transformPoint
(position);
return
position;
}
//
//////////////////////////////////////////////////////////
FloatRect
Text::getLocalBounds
()
const
{
return
m_bounds;
}
//
//////////////////////////////////////////////////////////
FloatRect
Text::getGlobalBounds
()
const
{
return
getTransform
().
transformRect
(
getLocalBounds
());
}
//
//////////////////////////////////////////////////////////
void
Text::draw
(RenderTarget& target, RenderStates states)
const
{
if
(m_font)
{
states.
transform
*=
getTransform
();
states.
texture
= &m_font->
getTexture
(m_characterSize);
target.
draw
(m_vertices, states);
}
}
//
//////////////////////////////////////////////////////////
void
Text::updateGeometry
()
{
//
Clear the previous geometry
m_vertices.
clear
();
m_bounds =
FloatRect
();
//
No font: nothing to draw
if
(!m_font)
return
;
//
No text: nothing to draw
if
(m_string.
isEmpty
())
return
;
//
Compute values related to the text style
bool
bold = (m_style & Bold) !=
0
;
bool
underlined = (m_style & Underlined) !=
0
;
float
italic = (m_style & Italic) ?
0
.
208f
:
0
.
f
;
//
12 degrees
float
underlineOffset = m_characterSize *
0
.
1f
;
float
underlineThickness = m_characterSize * (bold ?
0
.
1f
:
0
.
07f
);
//
Precompute the variables needed by the algorithm
float
hspace =
static_cast
<
float
>(m_font->
getGlyph
(L
'
'
, m_characterSize, bold).
advance
);
float
vspace =
static_cast
<
float
>(m_font->
getLineSpacing
(m_characterSize));
float
x =
0
.
f
;
float
y =
static_cast
<
float
>(m_characterSize);
//
Create one quad for each character
float
minX =
static_cast
<
float
>(m_characterSize);
float
minY =
static_cast
<
float
>(m_characterSize);
float
maxX =
0
.
f
;
float
maxY =
0
.
f
;
Uint32 prevChar =
0
;
for
(std::
size_t
i =
0
; i < m_string.
getSize
(); ++i)
{
Uint32 curChar = m_string[i];
//
Apply the kerning offset
x +=
static_cast
<
float
>(m_font->
getKerning
(prevChar, curChar, m_characterSize));
prevChar = curChar;
//
If we're using the underlined style and there's a new line, draw a line
if
(underlined && (curChar == L
'
\n
'
))
{
float
top = y + underlineOffset;
float
bottom = top + underlineThickness;
m_vertices.
append
(
Vertex
(
Vector2f
(
0
, top), m_color,
Vector2f
(
1
,
1
)));
m_vertices.
append
(
Vertex
(
Vector2f
(x, top), m_color,
Vector2f
(
1
,
1
)));
m_vertices.
append
(
Vertex
(
Vector2f
(x, bottom), m_color,
Vector2f
(
1
,
1
)));
m_vertices.
append
(
Vertex
(
Vector2f
(
0
, bottom), m_color,
Vector2f
(
1
,
1
)));
}
//
Handle special characters
if
((curChar ==
'
'
) || (curChar ==
'
\t
'
) || (curChar ==
'
\n
'
) || (curChar ==
'
\v
'
))
{
//
Update the current bounds (min coordinates)
minX =
std::min
(minX, x);
minY =
std::min
(minY, y);
switch
(curChar)
{
case
'
'
: x += hspace;
break
;
case
'
\t
'
: x += hspace *
4
;
break
;
case
'
\n
'
: y += vspace; x =
0
;
break
;
case
'
\v
'
: y += vspace *
4
;
break
;
}
//
Update the current bounds (max coordinates)
maxX =
std::max
(maxX, x);
maxY =
std::max
(maxY, y);
//
Next glyph, no need to create a quad for whitespace
continue
;
}
//
Extract the current glyph's description
const
Glyph& glyph = m_font->
getGlyph
(curChar, m_characterSize, bold);
int
left = glyph.
bounds
.
left
;
int
top = glyph.
bounds
.
top
;
int
right = glyph.
bounds
.
left
+ glyph.
bounds
.
width
;
int
bottom = glyph.
bounds
.
top
+ glyph.
bounds
.
height
;
float
u1 =
static_cast
<
float
>(glyph.
textureRect
.
left
);
float
v1 =
static_cast
<
float
>(glyph.
textureRect
.
top
);
float
u2 =
static_cast
<
float
>(glyph.
textureRect
.
left
+ glyph.
textureRect
.
width
);
float
v2 =
static_cast
<
float
>(glyph.
textureRect
.
top
+ glyph.
textureRect
.
height
);
//
Add a quad for the current character
m_vertices.
append
(
Vertex
(
Vector2f
(x + left - italic * top, y + top), m_color,
Vector2f
(u1, v1)));
m_vertices.
append
(
Vertex
(
Vector2f
(x + right - italic * top, y + top), m_color,
Vector2f
(u2, v1)));
m_vertices.
append
(
Vertex
(
Vector2f
(x + right - italic * bottom, y + bottom), m_color,
Vector2f
(u2, v2)));
m_vertices.
append
(
Vertex
(
Vector2f
(x + left - italic * bottom, y + bottom), m_color,
Vector2f
(u1, v2)));
//
Update the current bounds
minX =
std::min
(minX, x + left - italic * bottom);
maxX =
std::max
(maxX, x + right - italic * top);
minY =
std::min
(minY, y + top);
maxY =
std::max
(maxY, y + bottom);
//
Advance to the next character
x += glyph.
advance
;
}
//
If we're using the underlined style, add the last line
if
(underlined)
{
float
top = y + underlineOffset;
float
bottom = top + underlineThickness;
m_vertices.
append
(
Vertex
(
Vector2f
(
0
, top), m_color,
Vector2f
(
1
,
1
)));
m_vertices.
append
(
Vertex
(
Vector2f
(x, top), m_color,
Vector2f
(
1
,
1
)));
m_vertices.
append
(
Vertex
(
Vector2f
(x, bottom), m_color,
Vector2f
(
1
,
1
)));
m_vertices.
append
(
Vertex
(
Vector2f
(
0
, bottom), m_color,
Vector2f
(
1
,
1
)));
}
//
Update the bounding rectangle
m_bounds.
left
= minX;
m_bounds.
top
= minY;
m_bounds.
width
= maxX - minX;
m_bounds.
height
= maxY - minY;
}
}
//
namespace sf
Back
|
FazBrowse Home
|
New Git URL