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
31
40
43
44#include <functional>
45#include <memory>
46
47#include <cstddef>
48#include <cstdint>
49
50
51namespace sf
52{
53class Font;
54class RenderTarget;
55
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 {
106 LeftToRight,
107 RightToLeft,
108 TopToBottom,
109 BottomToTop
110 };
111
117 {
118 Default,
119 TopToBottom,
120 BottomToTop
121 };
122
128 {
130
132 std::uint32_t cluster{};
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
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
467
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
719
731
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
#define SFML_GRAPHICS_API
Utility class for manipulating RGBA colors.
Definition Color.hpp:40
Abstract base class for objects that can be drawn to a render target.
Definition Drawable.hpp:44
friend class RenderTarget
Definition Drawable.hpp:53
Class for loading and manipulating character fonts.
Definition Font.hpp:58
Base class for all render targets (window, texture, ...).
Utility string class that automatically handles conversions between types and encodings.
Definition String.hpp:91
const Font & getFont() const
Get the text's font.
float getLetterSpacing() const
Get the size of the letter spacing factor.
LineAlignment getLineAlignment() const
Get the line alignment for a multi-line text.
Text(const Font &font, String string="", unsigned int characterSize=30)
Construct the text from a string, font and size.
void setFont(const Font &font)
Set the text's font.
VertexArray & getOutlineVertexData() const
Get a reference to the outline vertex data of this text.
Vector2f findCharacterPos(std::size_t index) const
Return the position of the index-th character.
TextOrientation getTextOrientation() const
Get the text orientation.
void setFillColor(Color color)
Set the fill color of the text.
FloatRect getLocalBounds() const
Get the local bounding rectangle of the entity.
ClusterGrouping getClusterGrouping() const
Return the cluster grouping algorithm in use.
unsigned int getCharacterSize() const
Get the character size.
void setFont(const Font &&font)=delete
Disallow setting from a temporary font.
void setLineAlignment(LineAlignment lineAlignment)
Set the line alignment for a multi-line text.
float getLineSpacing() const
Get the size of the line spacing factor.
void setOutlineColor(Color color)
Set the outline color of the text.
void setGlyphPreProcessor(GlyphPreProcessor glyphPreProcessor)
Set the glyph pre-processor to be called per glyph.
void setString(const String &string)
Set the text's string.
std::uint32_t getStyle() const
Get the text's style.
TextOrientation
Text Orientation.
Definition Text.hpp:117
void setStyle(std::uint32_t style)
Set the text's style.
TextDirection
Text Direction.
Definition Text.hpp:104
const std::vector< ShapedGlyph > & getShapedGlyphs() const
Return a list of shaped glyphs that make up the text.
void setClusterGrouping(ClusterGrouping clusterGrouping)
Set the cluster grouping algorithm to use.
Style
Enumeration of the string drawing styles.
Definition Text.hpp:68
@ Regular
Regular characters, no style.
Definition Text.hpp:69
@ Underlined
Underlined characters.
Definition Text.hpp:72
@ StrikeThrough
Strike through characters.
Definition Text.hpp:73
@ Italic
Italic characters.
Definition Text.hpp:71
@ Bold
Bold characters.
Definition Text.hpp:70
Color getOutlineColor() const
Get the outline color of the text.
LineAlignment
Enumeration of the text alignment options.
Definition Text.hpp:81
void setOutlineThickness(float thickness)
Set the thickness of the text's outline.
const String & getString() const
Get the text's string.
void setLetterSpacing(float spacingFactor)
Set the letter spacing factor.
VertexArray & getVertexData() const
Get a reference to the vertex data of this text.
FloatRect getGlobalBounds() const
Get the global bounding rectangle of the entity.
Text(const Font &&font, String string="", unsigned int characterSize=30)=delete
Disallow construction from a temporary font.
Color getFillColor() const
Get the fill color of the text.
void setCharacterSize(unsigned int size)
Set the character size.
void setTextOrientation(TextOrientation textOrientation)
Set the text orientation.
void setLineSpacing(float spacingFactor)
Set the line spacing factor.
float getOutlineThickness() const
Get the outline thickness of the text.
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
ClusterGrouping
Cluster Grouping.
Definition Text.hpp:93
Transformable()=default
Default constructor.
Set of one or more 2D primitives.
Rect< float > FloatRect
Definition Rect.hpp:147
Vector2< float > Vector2f
Definition Vector2.hpp:213
Structure describing a glyph.
Definition Glyph.hpp:42
Define the states used for drawing to a RenderTarget.
Structure describing a glyph after shaping.
Definition Text.hpp:128
std::size_t vertexOffset
Starting offset of the vertex data belonging to this glyph.
Definition Text.hpp:135
TextDirection textDirection
Text direction.
Definition Text.hpp:133
Vector2f position
Position of the glyph within a text.
Definition Text.hpp:131
std::uint32_t cluster
Cluster ID.
Definition Text.hpp:132
std::size_t vertexCount
Count of vertices belonging to this glyph.
Definition Text.hpp:136
float baseline
The baseline position of the line this glyph is a part of.
Definition Text.hpp:134