FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SFML/src/SFML/Graphics/Text.cpp at master · githubhy/SFML · GitHub
githubhy
/
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
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
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
522 lines (419 loc) · 16.5 KB
Breadcrumbs
SFML
/
src
/
SFML
/
Graphics
/
Text.cpp
Copy path
File metadata and controls
522 lines (419 loc) · 16.5 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//
//////////////////////////////////////////////////////////
//
//
SFML - Simple and Fast Multimedia Library
//
Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org)
//
//
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/Font.hpp
>
#
include
<
SFML/Graphics/RenderTarget.hpp
>
#
include
<
SFML/Graphics/Text.hpp
>
#
include
<
SFML/Graphics/Texture.hpp
>
#
include
<
algorithm
>
#
include
<
utility
>
#
include
<
cmath
>
#
include
<
cstddef
>
#
include
<
cstdint
>
namespace
{
//
Add an underline or strikethrough line to the vertex array
void
addLine
(sf::VertexArray& vertices,
float
lineLength,
float
lineTop,
const
sf::Color& color,
float
offset,
float
thickness,
float
outlineThickness =
0
)
{
const
float
top =
std::floor
(lineTop + offset - (thickness /
2
) +
0
.
5f
);
const
float
bottom = top +
std::floor
(thickness +
0
.
5f
);
vertices.
append
({{-outlineThickness, top - outlineThickness}, color, {
1
.
0f
,
1
.
0f
}});
vertices.
append
({{lineLength + outlineThickness, top - outlineThickness}, color, {
1
.
0f
,
1
.
0f
}});
vertices.
append
({{-outlineThickness, bottom + outlineThickness}, color, {
1
.
0f
,
1
.
0f
}});
vertices.
append
({{-outlineThickness, bottom + outlineThickness}, color, {
1
.
0f
,
1
.
0f
}});
vertices.
append
({{lineLength + outlineThickness, top - outlineThickness}, color, {
1
.
0f
,
1
.
0f
}});
vertices.
append
({{lineLength + outlineThickness, bottom + outlineThickness}, color, {
1
.
0f
,
1
.
0f
}});
}
//
Add a glyph quad to the vertex array
void
addGlyphQuad
(sf::VertexArray& vertices, sf::Vector2f position,
const
sf::Color& color,
const
sf::Glyph& glyph,
float
italicShear)
{
const
sf::Vector2f
padding
(
1
.
f
,
1
.
f
);
const
sf::Vector2f p1 = glyph.
bounds
.
position
- padding;
const
sf::Vector2f p2 = glyph.
bounds
.
position
+ glyph.
bounds
.
size
+ padding;
const
auto
uv1 =
sf::Vector2f
(glyph.
textureRect
.
position
) - padding;
const
auto
uv2 =
sf::Vector2f
(glyph.
textureRect
.
position
+ glyph.
textureRect
.
size
) + padding;
vertices.
append
({position +
sf::Vector2f
(p1.
x
- italicShear * p1.
y
, p1.
y
), color, {uv1.
x
, uv1.
y
}});
vertices.
append
({position +
sf::Vector2f
(p2.
x
- italicShear * p1.
y
, p1.
y
), color, {uv2.
x
, uv1.
y
}});
vertices.
append
({position +
sf::Vector2f
(p1.
x
- italicShear * p2.
y
, p2.
y
), color, {uv1.
x
, uv2.
y
}});
vertices.
append
({position +
sf::Vector2f
(p1.
x
- italicShear * p2.
y
, p2.
y
), color, {uv1.
x
, uv2.
y
}});
vertices.
append
({position +
sf::Vector2f
(p2.
x
- italicShear * p1.
y
, p1.
y
), color, {uv2.
x
, uv1.
y
}});
vertices.
append
({position +
sf::Vector2f
(p2.
x
- italicShear * p2.
y
, p2.
y
), color, {uv2.
x
, uv2.
y
}});
}
}
//
namespace
namespace
sf
{
//
//////////////////////////////////////////////////////////
Text::Text
(
const
Font& font, String string,
unsigned
int
characterSize) :
m_string
(std::move(string)),
m_font
(&font),
m_characterSize
(characterSize)
{
}
//
//////////////////////////////////////////////////////////
void
Text::setString
(
const
String& string)
{
if
(m_string != string)
{
m_string = string;
m_geometryNeedUpdate =
true
;
}
}
//
//////////////////////////////////////////////////////////
void
Text::setFont
(
const
Font& font)
{
if
(m_font != &font)
{
m_font = &font;
m_geometryNeedUpdate =
true
;
}
}
//
//////////////////////////////////////////////////////////
void
Text::setCharacterSize
(
unsigned
int
size)
{
if
(m_characterSize != size)
{
m_characterSize = size;
m_geometryNeedUpdate =
true
;
}
}
//
//////////////////////////////////////////////////////////
void
Text::setLetterSpacing
(
float
spacingFactor)
{
if
(m_letterSpacingFactor != spacingFactor)
{
m_letterSpacingFactor = spacingFactor;
m_geometryNeedUpdate =
true
;
}
}
//
//////////////////////////////////////////////////////////
void
Text::setLineSpacing
(
float
spacingFactor)
{
if
(m_lineSpacingFactor != spacingFactor)
{
m_lineSpacingFactor = spacingFactor;
m_geometryNeedUpdate =
true
;
}
}
//
//////////////////////////////////////////////////////////
void
Text::setStyle
(std::
uint32_t
style)
{
if
(m_style != style)
{
m_style = style;
m_geometryNeedUpdate =
true
;
}
}
//
//////////////////////////////////////////////////////////
void
Text::setFillColor
(
const
Color& color)
{
if
(color != m_fillColor)
{
m_fillColor = color;
//
Change vertex colors directly, no need to update whole geometry
//
(if geometry is updated anyway, we can skip this step)
if
(!m_geometryNeedUpdate)
{
for
(std::
size_t
i =
0
; i < m_vertices.
getVertexCount
(); ++i)
m_vertices[i].
color
= m_fillColor;
}
}
}
//
//////////////////////////////////////////////////////////
void
Text::setOutlineColor
(
const
Color& color)
{
if
(color != m_outlineColor)
{
m_outlineColor = color;
//
Change vertex colors directly, no need to update whole geometry
//
(if geometry is updated anyway, we can skip this step)
if
(!m_geometryNeedUpdate)
{
for
(std::
size_t
i =
0
; i < m_outlineVertices.
getVertexCount
(); ++i)
m_outlineVertices[i].
color
= m_outlineColor;
}
}
}
//
//////////////////////////////////////////////////////////
void
Text::setOutlineThickness
(
float
thickness)
{
if
(thickness != m_outlineThickness)
{
m_outlineThickness = thickness;
m_geometryNeedUpdate =
true
;
}
}
//
//////////////////////////////////////////////////////////
const
String&
Text::getString
()
const
{
return
m_string;
}
//
//////////////////////////////////////////////////////////
const
Font&
Text::getFont
()
const
{
return
*m_font;
}
//
//////////////////////////////////////////////////////////
unsigned
int
Text::getCharacterSize
()
const
{
return
m_characterSize;
}
//
//////////////////////////////////////////////////////////
float
Text::getLetterSpacing
()
const
{
return
m_letterSpacingFactor;
}
//
//////////////////////////////////////////////////////////
float
Text::getLineSpacing
()
const
{
return
m_lineSpacingFactor;
}
//
//////////////////////////////////////////////////////////
std::
uint32_t
Text::getStyle
()
const
{
return
m_style;
}
//
//////////////////////////////////////////////////////////
const
Color&
Text::getFillColor
()
const
{
return
m_fillColor;
}
//
//////////////////////////////////////////////////////////
const
Color&
Text::getOutlineColor
()
const
{
return
m_outlineColor;
}
//
//////////////////////////////////////////////////////////
float
Text::getOutlineThickness
()
const
{
return
m_outlineThickness;
}
//
//////////////////////////////////////////////////////////
Vector2f
Text::findCharacterPos
(std::
size_t
index)
const
{
//
Adjust the index if it's out of range
index =
std::min
(index, m_string.
getSize
());
//
Precompute the variables needed by the algorithm
const
bool
isBold = m_style & Bold;
float
whitespaceWidth = m_font->
getGlyph
(U
'
'
, m_characterSize, isBold).
advance
;
const
float
letterSpacing = (whitespaceWidth /
3
.
f
) * (m_letterSpacingFactor -
1
.
f
);
whitespaceWidth += letterSpacing;
const
float
lineSpacing = m_font->
getLineSpacing
(m_characterSize) * m_lineSpacingFactor;
//
Compute the position
Vector2f position;
std::
uint32_t
prevChar =
0
;
for
(std::
size_t
i =
0
; i < index; ++i)
{
const
std::
uint32_t
curChar = m_string[i];
//
Apply the kerning offset
position.
x
+= m_font->
getKerning
(prevChar, curChar, m_characterSize, isBold);
prevChar = curChar;
//
Handle special characters
switch
(curChar)
{
case
U
'
'
:
position.
x
+= whitespaceWidth;
continue
;
case
U
'
\t
'
:
position.
x
+= whitespaceWidth *
4
;
continue
;
case
U
'
\n
'
:
position.
y
+= lineSpacing;
position.
x
=
0
;
continue
;
}
//
For regular characters, add the advance offset of the glyph
position.
x
+= m_font->
getGlyph
(curChar, m_characterSize, isBold).
advance
+ letterSpacing;
}
//
Transform the position to global coordinates
position =
getTransform
().
transformPoint
(position);
return
position;
}
//
//////////////////////////////////////////////////////////
FloatRect
Text::getLocalBounds
()
const
{
ensureGeometryUpdate
();
return
m_bounds;
}
//
//////////////////////////////////////////////////////////
FloatRect
Text::getGlobalBounds
()
const
{
return
getTransform
().
transformRect
(
getLocalBounds
());
}
//
//////////////////////////////////////////////////////////
void
Text::draw
(RenderTarget& target, RenderStates states)
const
{
ensureGeometryUpdate
();
states.
transform
*=
getTransform
();
states.
texture
= &m_font->
getTexture
(m_characterSize);
states.
coordinateType
= CoordinateType::Pixels;
//
Only draw the outline if there is something to draw
if
(m_outlineThickness !=
0
)
target.
draw
(m_outlineVertices, states);
target.
draw
(m_vertices, states);
}
//
//////////////////////////////////////////////////////////
void
Text::ensureGeometryUpdate
()
const
{
//
Do nothing, if geometry has not changed and the font texture has not changed
if
(!m_geometryNeedUpdate && m_font->
getTexture
(m_characterSize).
m_cacheId
== m_fontTextureId)
return
;
//
Save the current fonts texture id
m_fontTextureId = m_font->
getTexture
(m_characterSize).
m_cacheId
;
//
Mark geometry as updated
m_geometryNeedUpdate =
false
;
//
Clear the previous geometry
m_vertices.
clear
();
m_outlineVertices.
clear
();
m_bounds =
FloatRect
();
//
No text: nothing to draw
if
(m_string.
isEmpty
())
return
;
//
Compute values related to the text style
const
bool
isBold = m_style & Bold;
const
bool
isUnderlined = m_style & Underlined;
const
bool
isStrikeThrough = m_style & StrikeThrough;
const
float
italicShear = (m_style & Italic) ?
degrees
(
12
).
asRadians
() :
0
.
f
;
const
float
underlineOffset = m_font->
getUnderlinePosition
(m_characterSize);
const
float
underlineThickness = m_font->
getUnderlineThickness
(m_characterSize);
//
Compute the location of the strike through dynamically
//
We use the center point of the lowercase 'x' glyph as the reference
//
We reuse the underline thickness as the thickness of the strike through as well
const
float
strikeThroughOffset = m_font->
getGlyph
(U
'
x
'
, m_characterSize, isBold).
bounds
.
getCenter
().
y
;
//
Precompute the variables needed by the algorithm
float
whitespaceWidth = m_font->
getGlyph
(U
'
'
, m_characterSize, isBold).
advance
;
const
float
letterSpacing = (whitespaceWidth /
3
.
f
) * (m_letterSpacingFactor -
1
.
f
);
whitespaceWidth += letterSpacing;
const
float
lineSpacing = m_font->
getLineSpacing
(m_characterSize) * m_lineSpacingFactor;
float
x =
0
.
f
;
auto
y =
static_cast
<
float
>(m_characterSize);
//
Create one quad for each character
auto
minX =
static_cast
<
float
>(m_characterSize);
auto
minY =
static_cast
<
float
>(m_characterSize);
float
maxX =
0
.
f
;
float
maxY =
0
.
f
;
std::
uint32_t
prevChar =
0
;
for
(
const
std::
uint32_t
curChar : m_string)
{
//
Skip the \r char to avoid weird graphical issues
if
(curChar == U
'
\r
'
)
continue
;
//
Apply the kerning offset
x += m_font->
getKerning
(prevChar, curChar, m_characterSize, isBold);
//
If we're using the underlined style and there's a new line, draw a line
if
(isUnderlined && (curChar == U
'
\n
'
&& prevChar != U
'
\n
'
))
{
addLine
(m_vertices, x, y, m_fillColor, underlineOffset, underlineThickness);
if
(m_outlineThickness !=
0
)
addLine
(m_outlineVertices, x, y, m_outlineColor, underlineOffset, underlineThickness, m_outlineThickness);
}
//
If we're using the strike through style and there's a new line, draw a line across all characters
if
(isStrikeThrough && (curChar == U
'
\n
'
&& prevChar != U
'
\n
'
))
{
addLine
(m_vertices, x, y, m_fillColor, strikeThroughOffset, underlineThickness);
if
(m_outlineThickness !=
0
)
addLine
(m_outlineVertices, x, y, m_outlineColor, strikeThroughOffset, underlineThickness, m_outlineThickness);
}
prevChar = curChar;
//
Handle special characters
if
((curChar == U
'
'
) || (curChar == U
'
\n
'
) || (curChar == U
'
\t
'
))
{
//
Update the current bounds (min coordinates)
minX =
std::min
(minX, x);
minY =
std::min
(minY, y);
switch
(curChar)
{
case
U
'
'
:
x += whitespaceWidth;
break
;
case
U
'
\t
'
:
x += whitespaceWidth *
4
;
break
;
case
U
'
\n
'
:
y += lineSpacing;
x =
0
;
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
;
}
//
Apply the outline
if
(m_outlineThickness !=
0
)
{
const
Glyph& glyph = m_font->
getGlyph
(curChar, m_characterSize, isBold, m_outlineThickness);
//
Add the outline glyph to the vertices
addGlyphQuad
(m_outlineVertices,
Vector2f
(x, y), m_outlineColor, glyph, italicShear);
}
//
Extract the current glyph's description
const
Glyph& glyph = m_font->
getGlyph
(curChar, m_characterSize, isBold);
//
Add the glyph to the vertices
addGlyphQuad
(m_vertices,
Vector2f
(x, y), m_fillColor, glyph, italicShear);
//
Update the current bounds
const
Vector2f p1 = glyph.
bounds
.
position
;
const
Vector2f p2 = glyph.
bounds
.
position
+ glyph.
bounds
.
size
;
minX =
std::min
(minX, x + p1.
x
- italicShear * p2.
y
);
maxX =
std::max
(maxX, x + p2.
x
- italicShear * p1.
y
);
minY =
std::min
(minY, y + p1.
y
);
maxY =
std::max
(maxY, y + p2.
y
);
//
Advance to the next character
x += glyph.
advance
+ letterSpacing;
}
//
If we're using outline, update the current bounds
if
(m_outlineThickness !=
0
)
{
const
float
outline =
std::abs
(
std::ceil
(m_outlineThickness));
minX -= outline;
maxX += outline;
minY -= outline;
maxY += outline;
}
//
If we're using the underlined style, add the last line
if
(isUnderlined && (x >
0
))
{
addLine
(m_vertices, x, y, m_fillColor, underlineOffset, underlineThickness);
if
(m_outlineThickness !=
0
)
addLine
(m_outlineVertices, x, y, m_outlineColor, underlineOffset, underlineThickness, m_outlineThickness);
}
//
If we're using the strike through style, add the last line across all characters
if
(isStrikeThrough && (x >
0
))
{
addLine
(m_vertices, x, y, m_fillColor, strikeThroughOffset, underlineThickness);
if
(m_outlineThickness !=
0
)
addLine
(m_outlineVertices, x, y, m_outlineColor, strikeThroughOffset, underlineThickness, m_outlineThickness);
}
//
Update the bounding rectangle
m_bounds.
position
=
Vector2f
(minX, minY);
m_bounds.
size
=
Vector2f
(maxX, maxY) -
Vector2f
(minX, minY);
}
}
//
namespace sf
Back
|
FazBrowse Home
|
New Git URL