SFML 3.0.2
Loading...
Searching...
No Matches
Text.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2025 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
30#include <SFML/Graphics/Export.hpp>
31
32#include <SFML/Graphics/Color.hpp>
33#include <SFML/Graphics/Drawable.hpp>
34#include <SFML/Graphics/PrimitiveType.hpp>
35#include <SFML/Graphics/Rect.hpp>
36#include <SFML/Graphics/RenderStates.hpp>
37#include <SFML/Graphics/Transformable.hpp>
38#include <SFML/Graphics/VertexArray.hpp>
39
40#include <SFML/System/String.hpp>
41#include <SFML/System/Vector2.hpp>
42
43#include <cstddef>
44#include <cstdint>
45
46
47 namespace sf
48{
49 class Font;
50 class RenderTarget;
51
56 class SFML_GRAPHICS_API Text : public Drawable, public Transformable
57{
58 public:
63 enum Style
64 {
65 Regular = 0,
66 Bold = 1 << 0,
67 Italic = 1 << 1,
68 Underlined = 1 << 2,
69 StrikeThrough = 1 << 3
70 };
71
87 Text(const Font& font, String string = "", unsigned int characterSize = 30);
88
93 Text(const Font&& font, String string = "", unsigned int characterSize = 30) = delete;
94
114 void setString(const String& string);
115
131 void setFont(const Font& font);
132
137 void setFont(const Font&& font) = delete;
138
156 void setCharacterSize(unsigned int size);
157
170 void setLineSpacing(float spacingFactor);
171
189 void setLetterSpacing(float spacingFactor);
190
203 void setStyle(std::uint32_t style);
204
217 void setFillColor(Color color);
218
229 void setOutlineColor(Color color);
230
244 void setOutlineThickness(float thickness);
245
263 [[nodiscard]] const String& getString() const;
264
276 [[nodiscard]] const Font& getFont() const;
277
286 [[nodiscard]] unsigned int getCharacterSize() const;
287
296 [[nodiscard]] float getLetterSpacing() const;
297
306 [[nodiscard]] float getLineSpacing() const;
307
316 [[nodiscard]] std::uint32_t getStyle() const;
317
326 [[nodiscard]] Color getFillColor() const;
327
336 [[nodiscard]] Color getOutlineColor() const;
337
346 [[nodiscard]] float getOutlineThickness() const;
347
363 [[nodiscard]] Vector2f findCharacterPos(std::size_t index) const;
364
377 [[nodiscard]] FloatRect getLocalBounds() const;
378
391 [[nodiscard]] FloatRect getGlobalBounds() const;
392
393 private:
401 void draw(RenderTarget& target, RenderStates states) const override;
402
410 void ensureGeometryUpdate() const;
411
413 // Member data
415 String m_string;
416 const Font* m_font{};
417 unsigned int m_characterSize{30};
418 float m_letterSpacingFactor{1.f};
419 float m_lineSpacingFactor{1.f};
420 std::uint32_t m_style{Regular};
421 Color m_fillColor{Color::White};
422 Color m_outlineColor{Color::Black};
423 float m_outlineThickness{0.f};
424 mutable VertexArray m_vertices{PrimitiveType::Triangles};
425 mutable VertexArray m_outlineVertices{PrimitiveType::Triangles};
426 mutable FloatRect m_bounds;
427 mutable bool m_geometryNeedUpdate{};
428 mutable std::uint64_t m_fontTextureId{};
429};
430
431} // namespace sf
432
433
Color.hpp
Drawable.hpp
Export.hpp
SFML_GRAPHICS_API
#define SFML_GRAPHICS_API
Definition Graphics/Export.hpp:42
PrimitiveType.hpp
Rect.hpp
RenderStates.hpp
String.hpp
Transformable.hpp
Vector2.hpp
VertexArray.hpp
sf::Color
Utility class for manipulating RGBA colors.
Definition Color.hpp:40
sf::Drawable
Abstract base class for objects that can be drawn to a render target.
Definition Drawable.hpp:44
sf::Drawable::RenderTarget
friend class RenderTarget
Definition Drawable.hpp:53
sf::Font
Class for loading and manipulating character fonts.
Definition Font.hpp:58
sf::RenderTarget
Base class for all render targets (window, texture, ...).
Definition RenderTarget.hpp:63
sf::String
Utility string class that automatically handles conversions between types and encodings.
Definition String.hpp:89
sf::Text::getFont
const Font & getFont() const
Get the text's font.
sf::Text::getLetterSpacing
float getLetterSpacing() const
Get the size of the letter spacing factor.
sf::Text::Text
Text(const Font &font, String string="", unsigned int characterSize=30)
Construct the text from a string, font and size.
sf::Text::setFont
void setFont(const Font &font)
Set the text's font.
sf::Text::findCharacterPos
Vector2f findCharacterPos(std::size_t index) const
Return the position of the index-th character.
sf::Text::setFillColor
void setFillColor(Color color)
Set the fill color of the text.
sf::Text::getLocalBounds
FloatRect getLocalBounds() const
Get the local bounding rectangle of the entity.
sf::Text::getCharacterSize
unsigned int getCharacterSize() const
Get the character size.
sf::Text::setFont
void setFont(const Font &&font)=delete
Disallow setting from a temporary font.
sf::Text::getLineSpacing
float getLineSpacing() const
Get the size of the line spacing factor.
sf::Text::setOutlineColor
void setOutlineColor(Color color)
Set the outline color of the text.
sf::Text::setString
void setString(const String &string)
Set the text's string.
sf::Text::getStyle
std::uint32_t getStyle() const
Get the text's style.
sf::Text::setStyle
void setStyle(std::uint32_t style)
Set the text's style.
sf::Text::Style
Style
Enumeration of the string drawing styles.
Definition Text.hpp:64
sf::Text::Regular
@ Regular
Regular characters, no style.
Definition Text.hpp:65
sf::Text::Underlined
@ Underlined
Underlined characters.
Definition Text.hpp:68
sf::Text::StrikeThrough
@ StrikeThrough
Strike through characters.
Definition Text.hpp:69
sf::Text::Italic
@ Italic
Italic characters.
Definition Text.hpp:67
sf::Text::Bold
@ Bold
Bold characters.
Definition Text.hpp:66
sf::Text::getOutlineColor
Color getOutlineColor() const
Get the outline color of the text.
sf::Text::setOutlineThickness
void setOutlineThickness(float thickness)
Set the thickness of the text's outline.
sf::Text::getString
const String & getString() const
Get the text's string.
sf::Text::setLetterSpacing
void setLetterSpacing(float spacingFactor)
Set the letter spacing factor.
sf::Text::getGlobalBounds
FloatRect getGlobalBounds() const
Get the global bounding rectangle of the entity.
sf::Text::Text
Text(const Font &&font, String string="", unsigned int characterSize=30)=delete
Disallow construction from a temporary font.
sf::Text::getFillColor
Color getFillColor() const
Get the fill color of the text.
sf::Text::setCharacterSize
void setCharacterSize(unsigned int size)
Set the character size.
sf::Text::setLineSpacing
void setLineSpacing(float spacingFactor)
Set the line spacing factor.
sf::Text::getOutlineThickness
float getOutlineThickness() const
Get the outline thickness of the text.
sf::Transformable::Transformable
Transformable()=default
Default constructor.
sf
Definition AudioResource.hpp:36
sf::FloatRect
Rect< float > FloatRect
Definition Rect.hpp:147
sf::Vector2f
Vector2< float > Vector2f
Definition Vector2.hpp:210
sf::RenderStates
Define the states used for drawing to a RenderTarget.
Definition RenderStates.hpp:48