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-2024 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
39
42
43#include <cstddef>
44#include <cstdint>
45
46
47namespace sf
48{
49class Font;
50class RenderTarget;
51
57{
58public:
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
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
393private:
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
#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
Class for loading and manipulating character fonts.
Definition Font.hpp:64
Base class for all render targets (window, texture, ...)
Utility string class that automatically handles conversions between types and encodings.
Definition String.hpp:89
Graphical text that can be drawn to a render target.
Definition Text.hpp:57
const Font & getFont() const
Get the text's font.
float getLetterSpacing() const
Get the size of the letter spacing factor.
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.
Vector2f findCharacterPos(std::size_t index) const
Return the position of the index-th character.
void setFillColor(Color color)
Set the fill color of the text.
FloatRect getLocalBounds() const
Get the local bounding rectangle of the entity.
unsigned int getCharacterSize() const
Get the character size.
void setFont(const Font &&font)=delete
Disallow setting from a temporary font.
float getLineSpacing() const
Get the size of the line spacing factor.
void setOutlineColor(Color color)
Set the outline color of the text.
void setString(const String &string)
Set the text's string.
std::uint32_t getStyle() const
Get the text's style.
void setStyle(std::uint32_t style)
Set the text's style.
Style
Enumeration of the string drawing styles.
Definition Text.hpp:64
Color getOutlineColor() const
Get the outline color of the text.
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.
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 setLineSpacing(float spacingFactor)
Set the line spacing factor.
float getOutlineThickness() const
Get the outline thickness of the text.
Decomposed transform defined by a position, a rotation and a scale.
Rect< float > FloatRect
Definition Rect.hpp:147
Define the states used for drawing to a RenderTarget