TGUI 1.11
Loading...
Searching...
No Matches
Text.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 Bruno Van de Velde (vdv_b@tgui.eu)
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#ifndef TGUI_TEXT_HPP
26#define TGUI_TEXT_HPP
27
28#include <TGUI/Font.hpp>
29#include <TGUI/Color.hpp>
30#include <TGUI/Vector2.hpp>
31#include <TGUI/TextStyle.hpp>
32#include <TGUI/RenderStates.hpp>
33
35
36namespace tgui
37{
38 class BackendText;
39
41
42#if TGUI_COMPILED_WITH_CPP_VER >= 20
43 // Inline is needed here if we want to build a c++20 module
44 inline constexpr unsigned int AutoTextSize = 0xFFFFFFFF;
45#else
46 constexpr unsigned int AutoTextSize = 0xFFFFFFFF;
47#endif
48
52 class TGUI_API Text
53 {
54 public:
55
62 struct Blueprint
63 {
64 unsigned int characterSize = 0;
65 unsigned int style = 0;
66 Color color;
67 String link;
68 String text;
69 Vector2u gapSize;
70 };
71
77 TGUI_NODISCARD static float getExtraHorizontalPadding(const Font& font, unsigned int characterSize);
78
84 TGUI_NODISCARD static float getExtraHorizontalOffset(const Font& font, unsigned int characterSize);
85
89 TGUI_NODISCARD static float getExtraVerticalPadding(unsigned int characterSize);
90
94 TGUI_NODISCARD static float getLineHeight(const Font& font, unsigned int characterSize);
95
99 TGUI_NODISCARD static float getLineWidth(const String &text, const Font& font, unsigned int characterSize, TextStyles textStyle = {});
100
110 TGUI_NODISCARD static unsigned int findBestTextSize(const Font& font, float height, int fit = 0);
111
123 TGUI_NODISCARD static String wordWrap(float maxWidth, const String& text, const Font& font, unsigned int textSize, bool bold);
124
134 TGUI_NODISCARD static std::vector<std::vector<Blueprint>> wordWrap(float maxWidth, const std::vector<std::vector<Blueprint>>& lines, const Font& font);
135
137 public:
138
143
145 // Copy constructor
147 Text(const Text&);
148
150 // Move constructor
152 Text(Text&&) noexcept = default;
153
155 // Overload of copy assignment operator
157 Text& operator=(const Text&);
158
160 // Move assignment operator
162 Text& operator=(Text&&) noexcept = default;
163
169 void setPosition(Vector2f position);
170
176 TGUI_NODISCARD Vector2f getPosition() const;
177
183 TGUI_NODISCARD Vector2f getSize() const;
184
190 void setString(const String& string);
191
197 TGUI_NODISCARD const String& getString() const;
198
204 void setCharacterSize(unsigned int size);
205
211 TGUI_NODISCARD unsigned int getCharacterSize() const;
212
218 void setColor(Color color);
219
225 TGUI_NODISCARD Color getColor() const;
226
232 void setOpacity(float opacity);
233
239 TGUI_NODISCARD float getOpacity() const;
240
246 void setFont(const Font& font);
247
253 TGUI_NODISCARD Font getFont() const;
254
264 void setStyle(TextStyles style);
265
271 TGUI_NODISCARD TextStyles getStyle() const;
272
279
285 TGUI_NODISCARD Color getOutlineColor() const;
286
292 void setOutlineThickness(float thickness);
293
299 TGUI_NODISCARD float getOutlineThickness() const;
300
312 TGUI_NODISCARD Vector2f findCharacterPos(std::size_t index) const;
313
319 TGUI_NODISCARD float getExtraHorizontalPadding() const;
320
326 TGUI_NODISCARD float getExtraHorizontalOffset() const;
327
331 TGUI_NODISCARD float getLineHeight() const;
332
336 TGUI_NODISCARD float getLineWidth() const;
337
342 TGUI_NODISCARD std::shared_ptr<BackendText> getBackendText() const;
343
345 private:
346
347 std::shared_ptr<BackendText> m_backendText;
348 Vector2f m_position;
349 Font m_font;
350 Color m_color;
351 Color m_outlineColor;
352 float m_opacity = 1;
353 };
354
356}
357
359
360#endif // TGUI_TEXT_HPP
Base class for text implementations that depend on the backend.
Definition BackendText.hpp:41
Wrapper for colors.
Definition Color.hpp:71
Wrapper around the backend-specific font. All copies of the font will share the same internal font re...
Definition Font.hpp:56
Wrapper class to store strings.
Definition String.hpp:94
Wrapper for text styles.
Definition TextStyle.hpp:55
Vector2f getPosition() const
Returns the position of the text.
float getOutlineThickness() const
Returns the text outline thickness.
void setCharacterSize(unsigned int size)
Changes the character size of the text.
Color getColor() const
Returns the text fill color.
Font getFont() const
Returns the font of the text.
void setColor(Color color)
Changes the text fill color.
void setOutlineThickness(float thickness)
Changes the text outline thickness.
void setPosition(Vector2f position)
Sets the position of the text.
static float getExtraHorizontalOffset(const Font &font, unsigned int characterSize)
Returns an extra distance that text should be placed from the side of a widget as padding.
TextStyles getStyle() const
Returns the style of the text.
static float getLineWidth(const String &text, const Font &font, unsigned int characterSize, TextStyles textStyle={})
Returns the width of a single line of text.
Text()
Default constructor.
static float getExtraVerticalPadding(unsigned int characterSize)
Returns the distance that text should be placed from the bottom of the widget as padding.
static float getLineHeight(const Font &font, unsigned int characterSize)
Returns the height of a single line of text.
unsigned int getCharacterSize() const
Returns the character size of the text.
void setString(const String &string)
Changes the text.
static std::vector< std::vector< Blueprint > > wordWrap(float maxWidth, const std::vector< std::vector< Blueprint > > &lines, const Font &font)
static String wordWrap(float maxWidth, const String &text, const Font &font, unsigned int textSize, bool bold)
static float getExtraHorizontalPadding(const Font &font, unsigned int characterSize)
Returns a small distance that text should be placed from the side of a widget as padding.
const String & getString() const
Returns the text.
void setOutlineColor(Color color)
Changes the text outline color.
void setFont(const Font &font)
Changes the font used for the text.
Vector2f findCharacterPos(std::size_t index) const
Return the position of the index-th character.
Color getOutlineColor() const
Returns the text outline color.
Vector2f getSize() const
Returns the size of the text.
static unsigned int findBestTextSize(const Font &font, float height, int fit=0)
Finds the best character size for the text.
float getOpacity() const
Returns the opacity of the text.
void setOpacity(float opacity)
Changes the opacity of the text.
std::shared_ptr< BackendText > getBackendText() const
Returns the internal text.
void setStyle(TextStyles style)
Changes the style of the text.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
Describes a text piece, before turning it into an actual Text object.
Definition Text.hpp:63