TGUI  0.8.8
Text.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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/Vector2f.hpp>
32 #include <TGUI/TextStyle.hpp>
33 #include <SFML/Graphics/Text.hpp>
34 #include <SFML/Graphics/Transformable.hpp>
35 
37 
38 namespace tgui
39 {
41 
42  class TGUI_API Text : public sf::Transformable
43  {
44  public:
45 
46 #ifndef TGUI_REMOVE_DEPRECATED_CODE
47  TGUI_DEPRECATED("Use as (non-static) member function instead") static float getExtraHorizontalPadding(const Text& text);
51 #endif
52 
58  static float getExtraHorizontalPadding(Font font, unsigned int characterSize, TextStyle textStyle = {});
59 
60 
66  static float getExtraHorizontalOffset(Font font, unsigned int characterSize, TextStyle textStyle = {});
67 
68 
72  static float getExtraVerticalPadding(unsigned int characterSize);
73 
74 #ifndef TGUI_REMOVE_DEPRECATED_CODE
75  TGUI_DEPRECATED("Use as (non-static) member function instead") static float getLineHeight(const Text& text);
79 #endif
80 
84  static float getLineHeight(Font font, unsigned int characterSize, TextStyle textStyle = {});
85 
86 #ifndef TGUI_REMOVE_DEPRECATED_CODE
87  TGUI_DEPRECATED("Use as (non-static) member function instead") static float getLineWidth(const Text& text);
91 #endif
92 
96  static float getLineWidth(const sf::String &text, Font font, unsigned int characterSize, TextStyle textStyle = {});
97 
98 
107  static unsigned int findBestTextSize(Font font, float height, int fit = 0);
108 
109 
115  static float calculateExtraVerticalSpace(Font font, unsigned int characterSize, TextStyle style = {});
116 
117 
129  static sf::String wordWrap(float maxWidth, const sf::String& text, Font font, unsigned int textSize, bool bold, bool dropLeadingSpace = true);
130 
131 
133  public:
134 
141  Vector2f getSize() const;
142 
143 
150  void setString(const sf::String& string);
151 
152 
159  const sf::String& getString() const;
160 
161 
168  void setCharacterSize(unsigned int size);
169 
170 
177  unsigned int getCharacterSize() const;
178 
179 
186  void setColor(Color color);
187 
188 
195  Color getColor() const;
196 
197 
204  void setOpacity(float opacity);
205 
206 
213  float getOpacity() const;
214 
215 
222  void setFont(Font font);
223 
224 
231  Font getFont() const;
232 
233 
244  void setStyle(TextStyle style);
245 
246 
254 
255 
261  void setOutlineColor(Color color);
262 
263 
270 
271 
277  void setOutlineThickness(float thickness);
278 
279 
285  float getOutlineThickness() const;
286 
287 
300  Vector2f findCharacterPos(std::size_t index) const;
301 
302 
310  void draw(sf::RenderTarget& target, sf::RenderStates states) const;
311 
312 
319 
320 
327 
328 
332  float getLineHeight() const;
333 
334 
338  float getLineWidth() const;
339 
340 
342  private:
343 
345  // Calculates the size of the text again after something changed.
347  void recalculateSize();
348 
349 
351  private:
352 
353  sf::Text m_text;
354  Vector2f m_size;
355  Font m_font;
356  Color m_color;
357  Color m_outlineColor;
358  float m_opacity = 1;
359  };
360 
362 }
363 
365 
366 #endif // TGUI_TEXT_HPP
void draw(sf::RenderTarget &target, sf::RenderStates states) const
Draw the text to a render target.
Definition: Font.hpp:43
void setColor(Color color)
Changes the text fill color.
static float getLineWidth(const sf::String &text, Font font, unsigned int characterSize, TextStyle textStyle={})
Returns the width of a single line of text.
const sf::String & getString() const
Returns the text.
void setFont(Font font)
Changes the font used for the text.
float getLineHeight() const
Returns the height of a single line of text.
static float calculateExtraVerticalSpace(Font font, unsigned int characterSize, TextStyle style={})
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
Definition: Text.hpp:43
static unsigned int findBestTextSize(Font font, float height, int fit=0)
Finds the best character size for the text.
float getLineWidth() const
Returns the width of a single line of text.
Wrapper for colors.
Definition: Color.hpp:49
float getOutlineThickness() const
Returns the text outline thickness.
Vector2f getSize() const
Returns the size of the text.
static sf::String wordWrap(float maxWidth, const sf::String &text, Font font, unsigned int textSize, bool bold, bool dropLeadingSpace=true)
float getExtraHorizontalPadding() const
Returns a small distance that text should be placed from the side of a widget as padding.
float getOpacity() const
Returns the opacity of the text.
void setOutlineThickness(float thickness)
Changes the text outline thickness.
void setOpacity(float opacity)
Changes the opacity of the text.
static float getExtraVerticalPadding(unsigned int characterSize)
Returns the distance that text should be placed from the bottom of the widget as padding.
void setOutlineColor(Color color)
Changes the text outline color.
unsigned int getCharacterSize() const
Returns the character size of the text.
void setString(const sf::String &string)
Changes the text.
Color getColor() const
Returns the text fill color.
TextStyle getStyle() const
Returns the style of the text.
void setStyle(TextStyle style)
Changes the style of the text.
Definition: Vector2f.hpp:39
static float getExtraHorizontalOffset(Font font, unsigned int characterSize, TextStyle textStyle={})
Returns an extra distance that text should be placed from the side of a widget as padding.
Font getFont() const
Returns the font of the text.
Vector2f findCharacterPos(std::size_t index) const
Return the position of the index-th character.
static float getLineHeight(Font font, unsigned int characterSize, TextStyle textStyle={})
Returns the height of a single line of text.
Color getOutlineColor() const
Returns the text outline color.
static float getExtraHorizontalPadding(Font font, unsigned int characterSize, TextStyle textStyle={})
Returns a small distance that text should be placed from the side of a widget as padding.
float getExtraHorizontalOffset() const
Returns an extra distance that text should be placed from the side of a widget as padding.
void setCharacterSize(unsigned int size)
Changes the character size of the text.
Wrapper for text styles.
Definition: TextStyle.hpp:47