SFML - Simple and Fast Multimedia Library

SFML 3.1.0
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-2026 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/Glyph.hpp>
35#include <SFML/Graphics/PrimitiveType.hpp>
36#include <SFML/Graphics/Rect.hpp>
37#include <SFML/Graphics/RenderStates.hpp>
38#include <SFML/Graphics/Transformable.hpp>
39#include <SFML/Graphics/VertexArray.hpp>
40
41#include <SFML/System/String.hpp>
42#include <SFML/System/Vector2.hpp>
43
44#include <functional>
45#include <memory>
46
47#include <cstddef>
48#include <cstdint>
49
50
51namespace sf
52{
53class Font;
54class RenderTarget;
55
60 class SFML_GRAPHICS_API Text : public Drawable, public Transformable
61{
62public:
67 enum Style
68 {
69 Regular = 0,
70 Bold = 1 << 0,
71 Italic = 1 << 1,
72 Underlined = 1 << 2,
73 StrikeThrough = 1 << 3
74 };
75
80 enum class LineAlignment
81 {
82 Default,
83 Left,
84 Center,
85 Right
86 };
87
92 enum class ClusterGrouping
93 {
94 Grapheme,
95 Character,
96 None
97 };
98
103 enum class TextDirection
104 {
105 Unspecified,
106 LeftToRight,
107 RightToLeft,
108 TopToBottom,
109 BottomToTop
110 };
111
116 enum class TextOrientation
117 {
118 Default,
119 TopToBottom,
120 BottomToTop
121 };
122
127 struct ShapedGlyph
128 {
129 Glyph glyph;
130
131 Vector2f position;
132 std::uint32_t cluster{};
133 TextDirection textDirection{};
134 float baseline{};
135 std::size_t vertexOffset{};
136 std::size_t vertexCount{};
137 };
138
154 Text(const Font& font, String string = "", unsigned int characterSize = 30);
155
160 Text(const Font&& font, String string = "", unsigned int characterSize = 30) = delete;
161
181 void setString(const String& string);
182
198 void setFont(const Font& font);
199
204 void setFont(const Font&& font) = delete;
205
223 void setCharacterSize(unsigned int size);
224
237 void setLineSpacing(float spacingFactor);
238
256 void setLetterSpacing(float spacingFactor);
257
270 void setStyle(std::uint32_t style);
271
284 void setFillColor(Color color);
285
296 void setOutlineColor(Color color);
297
311 void setOutlineThickness(float thickness);
312
329 void setLineAlignment(LineAlignment lineAlignment);
330
354 void setTextOrientation(TextOrientation textOrientation);
355
373 [[nodiscard]] const String& getString() const;
374
386 [[nodiscard]] const Font& getFont() const;
387
396 [[nodiscard]] unsigned int getCharacterSize() const;
397
406 [[nodiscard]] float getLetterSpacing() const;
407
416 [[nodiscard]] float getLineSpacing() const;
417
426 [[nodiscard]] std::uint32_t getStyle() const;
427
436 [[nodiscard]] Color getFillColor() const;
437
446 [[nodiscard]] Color getOutlineColor() const;
447
456 [[nodiscard]] float getOutlineThickness() const;
457
466 LineAlignment getLineAlignment() const;
467
476 TextOrientation getTextOrientation() const;
477
495 [[deprecated("Use getShapedGlyphs() instead")]] [[nodiscard]] Vector2f findCharacterPos(std::size_t index) const;
496
542 [[nodiscard]] const std::vector<ShapedGlyph>& getShapedGlyphs() const;
543
550 [[nodiscard]] ClusterGrouping getClusterGrouping() const;
551
566 void setClusterGrouping(ClusterGrouping clusterGrouping);
567
706 using GlyphPreProcessor = std::function<
707 void(const ShapedGlyph& shapedGlyph, std::uint32_t& style, Color& fillColor, Color& outlineColor, float& outlineThickness)>;
708
718 void setGlyphPreProcessor(GlyphPreProcessor glyphPreProcessor);
719
730 VertexArray& getVertexData() const;
731
742 VertexArray& getOutlineVertexData() const;
743
756 [[nodiscard]] FloatRect getLocalBounds() const;
757
770 [[nodiscard]] FloatRect getGlobalBounds() const;
771
772private:
780 void draw(RenderTarget& target, RenderStates states) const override;
781
789 void ensureGeometryUpdate() const;
790
791 struct ShaperImpl;
792
794 // Member data
796 String m_string;
797 const Font* m_font{};
798 unsigned int m_characterSize{30};
799 float m_letterSpacingFactor{1.f};
800 float m_lineSpacingFactor{1.f};
801 std::uint32_t m_style{Regular};
802 Color m_fillColor{Color::White};
803 Color m_outlineColor{Color::Black};
804 float m_outlineThickness{0.f};
805 LineAlignment m_lineAlignment{LineAlignment::Default};
806 TextOrientation m_textOrientation{TextOrientation::Default};
807 ClusterGrouping m_clusterGrouping{ClusterGrouping::Character};
808 GlyphPreProcessor m_glyphPreProcessor;
809 mutable VertexArray m_vertices{PrimitiveType::Triangles};
810 mutable VertexArray m_outlineVertices{PrimitiveType::Triangles};
811 mutable FloatRect m_bounds;
812 mutable bool m_geometryNeedUpdate{};
813 mutable std::uint64_t m_fontTextureId{};
814 mutable std::vector<ShapedGlyph> m_glyphs;
815 mutable std::shared_ptr<ShaperImpl> m_shaper;
816};
817
818} // namespace sf
819
820
Color.hpp
Drawable.hpp
Glyph.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:91
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::getLineAlignment
LineAlignment getLineAlignment() const
Get the line alignment for a multi-line text.
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::getOutlineVertexData
VertexArray & getOutlineVertexData() const
Get a reference to the outline vertex data of this text.
sf::Text::findCharacterPos
Vector2f findCharacterPos(std::size_t index) const
Return the position of the index-th character.
sf::Text::getTextOrientation
TextOrientation getTextOrientation() const
Get the text orientation.
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::getClusterGrouping
ClusterGrouping getClusterGrouping() const
Return the cluster grouping algorithm in use.
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::setLineAlignment
void setLineAlignment(LineAlignment lineAlignment)
Set the line alignment for a multi-line text.
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::setGlyphPreProcessor
void setGlyphPreProcessor(GlyphPreProcessor glyphPreProcessor)
Set the glyph pre-processor to be called per glyph.
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::TextOrientation
TextOrientation
Text Orientation.
Definition Text.hpp:117
sf::Text::setStyle
void setStyle(std::uint32_t style)
Set the text's style.
sf::Text::TextDirection
TextDirection
Text Direction.
Definition Text.hpp:104
sf::Text::getShapedGlyphs
const std::vector< ShapedGlyph > & getShapedGlyphs() const
Return a list of shaped glyphs that make up the text.
sf::Text::setClusterGrouping
void setClusterGrouping(ClusterGrouping clusterGrouping)
Set the cluster grouping algorithm to use.
sf::Text::Style
Style
Enumeration of the string drawing styles.
Definition Text.hpp:68
sf::Text::Regular
@ Regular
Regular characters, no style.
Definition Text.hpp:69
sf::Text::Underlined
@ Underlined
Underlined characters.
Definition Text.hpp:72
sf::Text::StrikeThrough
@ StrikeThrough
Strike through characters.
Definition Text.hpp:73
sf::Text::Italic
@ Italic
Italic characters.
Definition Text.hpp:71
sf::Text::Bold
@ Bold
Bold characters.
Definition Text.hpp:70
sf::Text::getOutlineColor
Color getOutlineColor() const
Get the outline color of the text.
sf::Text::LineAlignment
LineAlignment
Enumeration of the text alignment options.
Definition Text.hpp:81
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::getVertexData
VertexArray & getVertexData() const
Get a reference to the vertex data of this text.
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::setTextOrientation
void setTextOrientation(TextOrientation textOrientation)
Set the text orientation.
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::Text::GlyphPreProcessor
std::function< void(const ShapedGlyph &shapedGlyph, std::uint32_t &style, Color &fillColor, Color &outlineColor, float &outlineThickness)> GlyphPreProcessor
Callable that is provided with glyph data for pre-processing.
Definition Text.hpp:706
sf::Text::ClusterGrouping
ClusterGrouping
Cluster Grouping.
Definition Text.hpp:93
sf::Transformable::Transformable
Transformable()=default
Default constructor.
sf::VertexArray
Set of one or more 2D primitives.
Definition VertexArray.hpp:52
sf::SoundChannel::Unspecified
@ Unspecified
Definition SoundChannel.hpp:43
sf
Definition AudioResource.hpp:36
sf::FloatRect
Rect< float > FloatRect
Definition Rect.hpp:147
sf::Vector2f
Vector2< float > Vector2f
Definition Vector2.hpp:213
sf::Glyph
Structure describing a glyph.
Definition Glyph.hpp:42
sf::RenderStates
Define the states used for drawing to a RenderTarget.
Definition RenderStates.hpp:48
sf::Text::ShapedGlyph
Structure describing a glyph after shaping.
Definition Text.hpp:128
sf::Text::ShapedGlyph::glyph
Glyph glyph
Definition Text.hpp:129
sf::Text::ShapedGlyph::vertexOffset
std::size_t vertexOffset
Starting offset of the vertex data belonging to this glyph.
Definition Text.hpp:135
sf::Text::ShapedGlyph::textDirection
TextDirection textDirection
Text direction.
Definition Text.hpp:133
sf::Text::ShapedGlyph::position
Vector2f position
Position of the glyph within a text.
Definition Text.hpp:131
sf::Text::ShapedGlyph::cluster
std::uint32_t cluster
Cluster ID.
Definition Text.hpp:132
sf::Text::ShapedGlyph::vertexCount
std::size_t vertexCount
Count of vertices belonging to this glyph.
Definition Text.hpp:136
sf::Text::ShapedGlyph::baseline
float baseline
The baseline position of the line this glyph is a part of.
Definition Text.hpp:134