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 "nomlib/config.hpp"
36#include "nomlib/graphics/IDrawable.hpp"
37#include "nomlib/math/Transformable.hpp"
38#include "nomlib/math/Point2.hpp"
39#include "nomlib/math/Size2.hpp"
40#include "nomlib/graphics/Texture.hpp"
41#include "nomlib/graphics/fonts/Font.hpp"
42
43namespace nom {
44
45class Text: public Transformable
46{
47 public:
48 typedef Text self_type;
49 typedef Transformable derived_type;
50
58 enum Style: uint32
59 {
60 Normal = 0,
61 Bold = 2,
62 Italic = 4,
63 Underline = 8,
64 Strikethrough = 16
65 };
66
68 Text( void );
69
71 ~Text( void );
72
76 const std::string& text,
77 const Font& font,
78 uint character_size = nom::DEFAULT_FONT_SIZE,
79 const Color4i& text_color = Color4i::White
80 );
81
85 const std::string& text,
86 Font* font,
87 uint character_size = nom::DEFAULT_FONT_SIZE,
88 const Color4i& text_color = Color4i::White
89 );
90
97 Text(const self_type& rhs);
98
105 self_type& operator =(const self_type& rhs);
106
110 virtual void set_position(const Point2i& pos) override;
111
113 Text* clone() const;
114
118 ObjectTypeInfo type() const override;
119
120 const Font& font() const;
121
128
130 bool valid ( void ) const;
131
132 // enum IFont::FontType font_type( void ) const;
133
143 int text_width(const std::string& text_buffer) const;
144
154 sint text_height ( const std::string& text_string ) const;
155
157 const std::string& text ( void ) const;
158
160 const Color4i& color( void ) const;
161
163 uint32 style( void ) const;
164
166 uint text_size ( void ) const;
167
172 void set_font(const Font& font);
173
182 void set_font(Font* font);
183
185 void set_text( const std::string& text );
186
194 void set_text_size ( uint character_size );
195
197 void set_color( const Color4i& text_color );
198
202 void set_style( uint32 style );
203
206 void set_text_kerning(bool state);
207
211 void draw(RenderTarget& target) const override;
212
213 private:
214 void render_text(RenderTarget& target) const;
215
223 void update() override;
224
226 int width() const;
227
229 int height() const;
230
231 bool update_cache();
232
233 int
234 multiline_width(const std::string& text_buffer, nom::size_type pos) const;
235
236 Font font_;
237
243
247 std::unique_ptr<Texture> rendered_text_;
248
250 std::string text_;
251 uint text_size_;
252 Color4i color_;
253
255 uint32 style_;
256
258 bool dirty_;
259};
260
261} // namespace nom
262
263#endif // include guard defined
264
Font interface wrapper for nom::IFont derived objects.
Definition Font.hpp:41
const RenderWindow RenderTarget
Definition IDrawable.hpp:59
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:247
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:258
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:255
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:59
@ Bold
Default.
Definition Text.hpp:61
std::string text_
Holds contents of text as a string buffer.
Definition Text.hpp:250
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:242
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...
self_type & operator=(const self_type &rhs)
Copy assignment operator.