nomlib
Loading...
Searching...
No Matches
Text.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_GRAPHICS_TEXT_HPP
30#define NOMLIB_GRAPHICS_TEXT_HPP
31
32#include <iostream>
33#include <string>
34
35#include "export.hpp"
36#include "nomlib/config.hpp"
37#include "nomlib/graphics/IDrawable.hpp"
38#include "nomlib/math/Transformable.hpp"
39#include "nomlib/math/Point2.hpp"
40#include "nomlib/math/Size2.hpp"
41#include "nomlib/graphics/Texture.hpp"
42#include "nomlib/graphics/fonts/Font.hpp"
43
44namespace nom {
45
46class NOM_EXPORT Text: public Transformable
47{
48 public:
49 typedef Text self_type;
50 typedef Transformable derived_type;
51
59 enum Style: uint32
60 {
61 Normal = 0,
62 Bold = 2,
63 Italic = 4,
64 Underline = 8,
65 Strikethrough = 16
66 };
67
69 Text( void );
70
72 ~Text( void );
73
77 const std::string& text,
78 const Font& font,
79 uint character_size = nom::DEFAULT_FONT_SIZE,
80 const Color4i& text_color = Color4i::White
81 );
82
86 const std::string& text,
87 Font* font,
88 uint character_size = nom::DEFAULT_FONT_SIZE,
89 const Color4i& text_color = Color4i::White
90 );
91
98 Text(const self_type& rhs);
99
106 self_type& operator =(const self_type& rhs);
107
111 virtual void set_position(const Point2i& pos) override;
112
114 Text* clone() const;
115
119 ObjectTypeInfo type() const override;
120
121 const Font& font() const;
122
129
131 bool valid ( void ) const;
132
133 // enum IFont::FontType font_type( void ) const;
134
144 int text_width(const std::string& text_buffer) const;
145
155 sint text_height ( const std::string& text_string ) const;
156
158 const std::string& text ( void ) const;
159
161 const Color4i& color( void ) const;
162
164 uint32 style( void ) const;
165
167 uint text_size ( void ) const;
168
173 void set_font(const Font& font);
174
183 void set_font(Font* font);
184
186 void set_text( const std::string& text );
187
195 void set_text_size ( uint character_size );
196
198 void set_color( const Color4i& text_color );
199
203 void set_style( uint32 style );
204
207 void set_text_kerning(bool state);
208
212 void draw(RenderTarget& target) const override;
213
214 private:
215 void render_text(RenderTarget& target) const;
216
224 void update() override;
225
227 int width() const;
228
230 int height() const;
231
232 bool update_cache();
233
234 int
235 multiline_width(const std::string& text_buffer, nom::size_type pos) const;
236
237 Font font_;
238
244
248 std::unique_ptr<Texture> rendered_text_;
249
251 std::string text_;
252 uint text_size_;
253 Color4i color_;
254
256 uint32 style_;
257
259 bool dirty_;
260};
261
262} // namespace nom
263
264#endif // include guard defined
265
Font interface wrapper for nom::IFont derived objects.
Definition Font.hpp:42
const RenderWindow RenderTarget
Definition IDrawable.hpp:60
Run-time type information (RTTI) container class.
uint32 style(void) const
Get text style.
virtual void set_position(const Point2i &pos) override
Set the position of the rendered text.
const Color4i & color(void) const
Get text color.
std::unique_ptr< Texture > rendered_text_
The texture containing the rendered text.
Definition Text.hpp:248
sint text_height(const std::string &text_string) const
Obtain the text height in pixels of the set text.
int width() const
Get the current text width.
bool valid(void) const
Obtain validity of the Text object.
void draw(RenderTarget &target) const override
uint text_size(void) const
Get text character size (in pixels?).
void set_text(const std::string &text)
Set the text string to be rendered.
const std::string & text(void) const
Get text string.
void set_text_kerning(bool state)
Set the use of rendering glyphs with respect to kerning offsets, when supported by the underlying fon...
Text(const self_type &rhs)
Copy constructor.
bool dirty_
Definition Text.hpp:259
void set_text_size(uint character_size)
Set the text's font point size.
void set_font(const Font &font)
Set the font to use in rendering text.
void set_color(const Color4i &text_color)
Set text's font color.
Texture * clone_texture() const
Get a deep-copy instance of the underlying texture used for the text rendering.
void update() override
Apply requested transformations, styles, etc.
~Text(void)
Destructor.
Text(void)
Default constructor.
uint32 style_
Current text effect set.
Definition Text.hpp:256
ObjectTypeInfo type() const override
Re-implements the IObject::type method.
Text * clone() const
Implements the required IDrawable::clone method.
Text(const std::string &text, Font *font, uint character_size=nom::DEFAULT_FONT_SIZE, const Color4i &text_color=Color4i::White)
Construct a Text, initializing it with a text string, a nom::Font object pointer and character size.
Style
Font face style; multiple styles can be combined from bitwise masks.
Definition Text.hpp:60
@ Bold
Default.
Definition Text.hpp:62
std::string text_
Holds contents of text as a string buffer.
Definition Text.hpp:251
void set_style(uint32 style)
Set the text's rendering style for the font.
Texture glyphs_texture_
A texture atlas created from the nom::Font instance that is referred to in rendering a text.
Definition Text.hpp:243
int height() const
Get the current text height.
void set_font(Font *font)
Set the font to use in rendering text.
int text_width(const std::string &text_buffer) const
Obtain the text width (in pixels) of the set text.
Text(const std::string &text, const Font &font, uint character_size=nom::DEFAULT_FONT_SIZE, const Color4i &text_color=Color4i::White)
Construct a Text, initializing it with a text string, a nom::Font object reference and character size...