Gamedev Framework (gf)  0.2.0
A C++11 framework for 2D games
Text.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * 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
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_TEXT_H
25 #define GF_TEXT_H
26 
27 #include <string>
28 
29 #include "Alignment.h"
30 #include "Portability.h"
31 #include "Transformable.h"
32 #include "Vector.h"
33 #include "VertexArray.h"
34 #include "VertexBuffer.h"
35 
36 namespace gf {
37 #ifndef DOXYGEN_SHOULD_SKIP_THIS
38 inline namespace v1 {
39 #endif
40 
41  class Font;
42 
43  /**
44  * @brief Graphical text that can be drawn to a render target
45  *
46  * gf::Text is a drawable class that allows to easily display
47  * some text with custom style and color on a render target.
48  *
49  * It inherits all the functions from gf::Transformable:
50  * position, rotation, scale, origin. It also adds text-specific
51  * properties such as the font to use, the character size, the
52  * global color and the text to display of course.
53  * It also provides convenience functions to calculate the
54  * graphical size of the text.
55  *
56  * gf::Text works in combination with the gf::Font class, which
57  * loads and provides the glyphs (visual characters) of a given font.
58  *
59  * The separation of gf::Font and gf::Text allows more flexibility
60  * and better performances: indeed a gf::Font is a heavy resource,
61  * and any operation on it is slow (often too slow for real-time
62  * applications). On the other side, a gf::Text is a lightweight
63  * object which can combine the glyphs data and metrics of a gf::Font
64  * to display any text on a render target.
65  *
66  * It is important to note that the gf::Text instance doesn't
67  * copy the font that it uses, it only keeps a reference to it.
68  * Thus, a gf::Font must not be destructed while it is
69  * used by a gf::Text (i.e. never write a function that
70  * uses a local gf::Font instance for creating a text).
71  *
72  * Usage example:
73  *
74  * ~~~{.cc}
75  * // Declare and load a font
76  * gf::Font font;
77  * font.loadFromFile("arial.ttf");
78  *
79  * // Create a text
80  * gf::Text text("hello", font);
81  * text.setCharacterSize(30);
82  * text.setColor(gf::Color::Red);
83  *
84  * // Draw it
85  * rendered.draw(text);
86  * ~~~
87  *
88  * @sa gf::Font
89  */
90  class GF_API Text : public Transformable {
91  public:
92  /**
93  * @brief Default constructor
94  *
95  * Creates an empty text.
96  */
97  Text();
98 
99  /**
100  * @brief Construct the text from a string, font and size
101  *
102  * @param string Text assigned to the string in UTF-8 format
103  * @param font Font used to draw the string
104  * @param characterSize Base size of characters, in pixels
105  */
106  Text(std::string string, Font& font, unsigned characterSize = 30);
107 
108  /**
109  * @brief Set the text's string
110  *
111  * The text string is in UTF-8 format.
112  * A text's string is empty by default.
113  *
114  * @param string New string in UTF-8 format
115  *
116  * @sa getString()
117  */
118  void setString(std::string string);
119 
120  /**
121  * @brief Get the text's string
122  *
123  * The text string is in UTF-8 format.
124  *
125  * @return Text's string
126  *
127  * @sa setString()
128  */
129  const std::string& getString() const {
130  return m_string;
131  }
132 
133  /**
134  * @brief Set the character size
135  *
136  * The default size is 30.
137  *
138  * @param characterSize New character size, in pixels
139  *
140  * @sa getCharacterSize()
141  */
142  void setCharacterSize(unsigned characterSize);
143 
144  /**
145  * @brief Get the character size
146  *
147  * @return Size of the characters, in pixels
148  *
149  * @sa setCharacterSize()
150  */
151  unsigned getCharacterSize() const {
152  return m_characterSize;
153  }
154 
155  /**
156  * @brief Set the text's font
157  *
158  * The `font` argument refers to a font that must
159  * exist as long as the text uses it. Indeed, the text
160  * doesn't store its own copy of the font, but rather keeps
161  * a pointer to the one that you passed to this function.
162  * If the font is destroyed and the text tries to
163  * use it, the behavior is undefined.
164  *
165  * @param font New font
166  *
167  * @sa getFont()
168  */
169  void setFont(Font& font);
170 
171  /**
172  * @brief Get the text's font
173  *
174  * If the text has no font attached, a `nullptr` pointer is returned.
175  * The returned pointer is const, which means that you
176  * cannot modify the font when you get it from this function.
177  *
178  * @return Pointer to the text's font
179  *
180  * @sa setFont()
181  */
182  const Font *getFont() const {
183  return m_font;
184  }
185 
186  /**
187  * @brief Set the fill color of the text
188  *
189  * By default, the text's fill color is opaque black.
190  * Setting the fill color to a transparent color with an outline
191  * will cause the outline to be displayed in the fill area of the text.
192  *
193  * @param color New fill color of the text
194  *
195  * @sa getColor()
196  */
197  void setColor(const Color4f& color);
198 
199  /**
200  * @brief Get the fill color of the text
201  *
202  * @return Fill color of the text
203  *
204  * @sa setColor()
205  */
206  const Color4f& getColor() const {
207  return m_color;
208  }
209 
210  /**
211  * @brief Set the outline color of the text
212  *
213  * By default, the text's outline color is opaque black.
214  *
215  * @param color New outline color of the text
216  *
217  * @sa getOutlineColor()
218  */
219  void setOutlineColor(const Color4f& color);
220 
221  /**
222  * @brief Get the outline color of the text
223  *
224  * @return Outline color of the text
225  *
226  * @sa setOutlineColor()
227  */
228  const Color4f& getOutlineColor() const {
229  return m_outlineColor;
230  }
231 
232  /**
233  * @brief Set the thickness of the text's outline
234  *
235  * By default, the outline thickness is 0.
236  *
237  * @param thickness New outline thickness, in pixels
238  *
239  * @sa getOutlineThickness()
240  */
241  void setOutlineThickness(float thickness);
242 
243  /**
244  * @brief Get the outline thickness of the text
245  *
246  * @return Outline thickness of the text, in pixels
247  *
248  * @sa setOutlineThickness()
249  */
251  return m_outlineThickness;
252  }
253 
254  /**
255  * @brief Set the paragraph width for aligned text
256  *
257  * By default, the paragraph width is 0.
258  *
259  * @param paragraphWidth New paragraph width in pixels
260  * @sa getParagraphWidth()
261  */
262  void setParagraphWidth(float paragraphWidth);
263 
264  /**
265  * @brief Get the paragraph width
266  *
267  * @return Paragraph width in pixels
268  * @sa setParagraphWidth()
269  */
270  float getParagraphWidth() const {
271  return m_paragraphWidth;
272  }
273 
274  /**
275  * @brief Set the alignement of the text
276  *
277  * By default, the text is not aligned.
278  *
279  * @param align New alignement
280  * @sa getAlignment()
281  */
282  void setAlignment(Alignment align);
283 
284  /**
285  * @brief Get the alignment of the text
286  *
287  * @return Current alignment of the text
288  * @sa setAlignment()
289  */
291  return m_align;
292  }
293 
294  /**
295  * @brief Get the local bounding rectangle of the entity
296  *
297  * The returned rectangle is in local coordinates, which means
298  * that it ignores the transformations (translation, rotation,
299  * scale, ...) that are applied to the entity.
300  * In other words, this function returns the bounds of the
301  * entity in the entity's coordinate system.
302  *
303  * @return Local bounding rectangle of the entity
304  */
305  RectF getLocalBounds() const {
306  return m_bounds;
307  }
308 
309  /**
310  * @brief Set the anchor origin of the entity
311  *
312  * Compute the origin of the entity based on the local bounds and
313  * the specified anchor. Internally, this function calls
314  * `Transformable::setOrigin()`.
315  *
316  * @param anchor The anchor of the entity
317  * @sa getLocalBounds(), Transformable::setOrigin()
318  */
319  void setAnchor(Anchor anchor);
320 
321  /**
322  * @brief Create a buffer with the current geometry
323  *
324  * The geometry is uploaded in the graphics memory so that it's faster
325  * to draw.
326  *
327  * @return A buffer with the current geometry
328  */
330 
331  /**
332  * @brief Create a buffer with the current outline geometry
333  *
334  * The geometry is uploaded in the graphics memory so that it's faster
335  * to draw.
336  *
337  * @return A buffer with the current outline geometry
338  */
340 
341  virtual void draw(RenderTarget& target, RenderStates states) override;
342 
343  private:
344  void updateGeometry();
345 
346  private:
347  struct Line {
348  std::vector<std::u32string> words;
349  float indent = 0.0f;
350  float spacing = 0.0f;
351  };
352 
353  struct Paragraph {
354  std::vector<Line> lines;
355  };
356 
357  float getWordWidth(const std::u32string& word);
358 
359  std::vector<Paragraph> makeParagraphs(const std::string& str, float spaceWidth);
360 
361  private:
362  std::string m_string;
363  Font *m_font;
364  unsigned m_characterSize;
365  Color4f m_color;
366  VertexArray m_vertices;
367  RectF m_bounds;
368 
369  Color4f m_outlineColor;
370  float m_outlineThickness;
371  VertexArray m_outlineVertices;
372 
373  float m_paragraphWidth;
374  Alignment m_align;
375  };
376 
377 #ifndef DOXYGEN_SHOULD_SKIP_THIS
378 }
379 #endif
380 }
381 
382 #endif // GF_TEXT_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:109
void setAnchor(Anchor anchor)
Set the anchor origin of the entity.
float getOutlineThickness()
Get the outline thickness of the text.
Definition: Text.h:250
void setColor(const Color4f &color)
Set the fill color of the text.
void setFont(Font &font)
Set the text's font.
A set of primitives.
Definition: VertexArray.h:65
Text()
Default constructor.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:65
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
const Color4f & getColor() const
Get the fill color of the text.
Definition: Text.h:206
unsigned getCharacterSize() const
Get the character size.
Definition: Text.h:151
virtual void draw(RenderTarget &target, RenderStates states) override
Draw the object to a render target.
Data in the graphics memory.
Definition: VertexBuffer.h:70
void setOutlineThickness(float thickness)
Set the thickness of the text's outline.
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Definition: Text.h:305
void setOutlineColor(const Color4f &color)
Set the outline color of the text.
void setAlignment(Alignment align)
Set the alignement of the text.
Alignment getAlignment() const
Get the alignment of the text.
Definition: Text.h:290
Graphical text that can be drawn to a render target.
Definition: Text.h:90
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
const std::string & getString() const
Get the text's string.
Definition: Text.h:129
void setParagraphWidth(float paragraphWidth)
Set the paragraph width for aligned text.
Definition: Action.h:34
void setString(std::string string)
Set the text's string.
Text(std::string string, Font &font, unsigned characterSize=30)
Construct the text from a string, font and size.
A character font.
Definition: Font.h:130
Alignment
The alignement of a text.
Definition: Alignment.h:32
float getParagraphWidth() const
Get the paragraph width.
Definition: Text.h:270
const Color4f & getOutlineColor() const
Get the outline color of the text.
Definition: Text.h:228
Anchor
The origin anchor of the transformable object.
Definition: Transformable.h:45
const Font * getFont() const
Get the text's font.
Definition: Text.h:182
#define GF_API
Definition: Portability.h:35
VertexBuffer commitOutlineGeometry() const
Create a buffer with the current outline geometry.
void setCharacterSize(unsigned characterSize)
Set the character size.