TGUI  1.1
Loading...
Searching...
No Matches
Text.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2023 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
26#ifndef TGUI_TEXT_HPP
27#define TGUI_TEXT_HPP
28
29#include <TGUI/Font.hpp>
30#include <TGUI/Color.hpp>
31#include <TGUI/Vector2.hpp>
32#include <TGUI/TextStyle.hpp>
33#include <TGUI/RenderStates.hpp>
34
36
37TGUI_MODULE_EXPORT namespace tgui
38{
39 class BackendText;
40
42
43 constexpr unsigned int AutoTextSize = 0xFFFFFFFF;
44
45
49 class TGUI_API Text
50 {
51 public:
52
59 struct Blueprint
60 {
61 unsigned int characterSize;
62 unsigned int style;
63 Color color;
64 String text;
65 Vector2u gapSize;
66 };
67
73 TGUI_NODISCARD static float getExtraHorizontalPadding(const Font& font, unsigned int characterSize);
74
75
81 TGUI_NODISCARD static float getExtraHorizontalOffset(const Font& font, unsigned int characterSize);
82
83
87 TGUI_NODISCARD static float getExtraVerticalPadding(unsigned int characterSize);
88
89
93 TGUI_NODISCARD static float getLineHeight(const Font& font, unsigned int characterSize);
94
95
99 TGUI_NODISCARD static float getLineWidth(const String &text, const Font& font, unsigned int characterSize, TextStyles textStyle = {});
100
101
111 TGUI_NODISCARD static unsigned int findBestTextSize(const Font& font, float height, int fit = 0);
112
113
125 TGUI_NODISCARD static String wordWrap(float maxWidth, const String& text, const Font& font, unsigned int textSize, bool bold);
126
127
139 TGUI_NODISCARD static std::vector<std::vector<Blueprint>> wordWrap(float maxWidth, const std::vector<std::vector<Blueprint>>& lines, const Font& font);
140
141
143 public:
144
145
150
151
153 // Copy constructor
155 Text(const Text&);
156
158 // Move constructor
160 Text(Text&&) noexcept = default;
161
163 // Overload of copy assignment operator
165 Text& operator=(const Text&);
166
168 // Move assignment operator
170 Text& operator=(Text&&) noexcept = default;
171
172
178 void setPosition(Vector2f position);
179
180
186 TGUI_NODISCARD Vector2f getPosition() const;
187
188
195 TGUI_NODISCARD Vector2f getSize() const;
196
197
204 void setString(const String& string);
205
206
213 TGUI_NODISCARD const String& getString() const;
214
215
222 void setCharacterSize(unsigned int size);
223
224
231 TGUI_NODISCARD unsigned int getCharacterSize() const;
232
233
240 void setColor(Color color);
241
242
249 TGUI_NODISCARD Color getColor() const;
250
251
258 void setOpacity(float opacity);
259
260
267 TGUI_NODISCARD float getOpacity() const;
268
269
276 void setFont(const Font& font);
277
278
285 TGUI_NODISCARD Font getFont() const;
286
287
298 void setStyle(TextStyles style);
299
300
307 TGUI_NODISCARD TextStyles getStyle() const;
308
309
315 void setOutlineColor(Color color);
316
317
323 TGUI_NODISCARD Color getOutlineColor() const;
324
325
331 void setOutlineThickness(float thickness);
332
333
339 TGUI_NODISCARD float getOutlineThickness() const;
340
341
354 TGUI_NODISCARD Vector2f findCharacterPos(std::size_t index) const;
355
356
362 TGUI_NODISCARD float getExtraHorizontalPadding() const;
363
364
370 TGUI_NODISCARD float getExtraHorizontalOffset() const;
371
372
376 TGUI_NODISCARD float getLineHeight() const;
377
378
382 TGUI_NODISCARD float getLineWidth() const;
383
384
389 TGUI_NODISCARD std::shared_ptr<BackendText> getBackendText() const;
390
391
393 private:
394
395 std::shared_ptr<BackendText> m_backendText;
396 Vector2f m_position;
397 Font m_font;
398 Color m_color;
399 Color m_outlineColor;
400 float m_opacity = 1;
401 };
402
404}
405
407
408#endif // TGUI_TEXT_HPP
Base class for text implementations that depend on the backend.
Definition BackendText.hpp:42
Wrapper for colors.
Definition Color.hpp:72
Wrapper around the backend-specific font. All copies of the font will share the same internal font re...
Definition Font.hpp:61
Wrapper class to store strings.
Definition String.hpp:101
Wrapper for text styles.
Definition TextStyle.hpp:57
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:50
static TGUI_NODISCARD float getLineHeight(const Font &font, unsigned int characterSize)
Returns the height of a single line of text.
static TGUI_NODISCARD std::vector< std::vector< Blueprint > > wordWrap(float maxWidth, const std::vector< std::vector< Blueprint > > &lines, const Font &font)
static TGUI_NODISCARD float getLineWidth(const String &text, const Font &font, unsigned int characterSize, TextStyles textStyle={})
Returns the width of a single line of text.
static TGUI_NODISCARD String wordWrap(float maxWidth, const String &text, const Font &font, unsigned int textSize, bool bold)
Text()
Default constructor.
static TGUI_NODISCARD float getExtraVerticalPadding(unsigned int characterSize)
Returns the distance that text should be placed from the bottom of the widget as padding.
static TGUI_NODISCARD 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.
static TGUI_NODISCARD 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.
static TGUI_NODISCARD unsigned int findBestTextSize(const Font &font, float height, int fit=0)
Finds the best character size for the text.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
Describes a text piece, before turning it into an actual Text object.
Definition Text.hpp:60