TrueTypeFont.hpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. 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 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON 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
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #ifndef NOMLIB_SDL2_TRUETYPE_FONT_HEADERS
30 #define NOMLIB_SDL2_TRUETYPE_FONT_HEADERS
31 
32 #include <iostream>
33 #include <string>
34 #include <memory>
35 
36 #include <SDL_ttf.h>
37 
38 #include "nomlib/config.hpp"
39 #include "nomlib/graphics/IFont.hpp"
40 #include "nomlib/math/Coords.hpp"
41 #include "nomlib/math/Rect-inl.hpp"
42 #include "nomlib/math/Color.hpp"
43 #include "nomlib/graphics/Texture.hpp"
44 #include "nomlib/graphics/Window.hpp"
45 #include "nomlib/SDL_helpers.hpp"
46 
47 namespace nom {
48 
51  public IFont
52 {
53  public:
55  TrueTypeFont ( void );
56 
58  ~TrueTypeFont ( void );
59 
61  IFont::SharedPtr clone ( void ) const;
62 
64  bool valid ( void ) const;
65 
67  const std::string& getText ( void ) const;
68 
70  int32 getFontWidth ( void ) const;
71 
73  int32 getFontHeight ( void ) const;
74 
75  IFont::FontStyle getFontStyle ( void ) const;
76 
77  const Color& getColor ( void ) const;
78  const Coords& getPosition ( void ) const;
79 
81  uint32 getNewline ( void ) const;
82 
84  uint32 getSpacing ( void ) const;
85 
86  IFont::TextAlignment getTextJustification ( void ) const;
87 
88  void setFontStyle ( int32 style, uint8 options = 150 );
89 
91  void setFontSize ( int32 point_size );
92 
96  void setText ( const std::string& text );
97 
98  void setColor ( const Color& color );
99  void setPosition ( const Coords& coords );
100 
102  void setSpacing ( uint32 spaces );
103 
105  void setTextJustification ( IFont::TextAlignment alignment );
106 
108  int32 getFontOutline ( void ) const;
109 
111  void setFontOutline ( int32 depth );
112 
114  IFont::RenderStyle getRenderingStyle ( void ) const;
115 
118 
123  bool load ( const std::string& filename, const Color& colorkey,
124  bool use_cache = false
125  );
126 
127  void update ( void );
128 
130  void draw ( SDL_Renderer* target ) const;
131 
132  void draw ( const Window& target ) const;
133 
134  private:
137  bool rebuild ( void );
138 
141 
143  std::shared_ptr<TTF_Font> font;
144 
147 
150 
152  std::string text_buffer;
153 
156  uint8 style_options;
157 
159  std::string filename;
160 
162  int32 font_size;
163 
165  bool use_cache;
166 
167  enum IFont::TextAlignment text_alignment;
168 
169  enum IFont::RenderStyle rendering;
170 };
171 
172 namespace priv {
173 
176 void Free_TrueTypeFont ( TrueTypeFont* ptr );
177 
178 } // namespace priv
179 } // namespace nom
180 
181 #endif // include guard defined
182 
bool load(const std::string &filename, const Color &colorkey, bool use_cache=false)
Load a new font in from a file.
std::shared_ptr< TTF_Font > font
Font file data, used by SDL_ttf extension.
void draw(SDL_Renderer *target) const
Draw the set text string to the video surface.
void setText(const std::string &text)
Set a new text string for drawing; defaults to \0.
int32 getFontHeight(void) const
Compute the height in pixels of the set text string; defaults to zero (0)
void setFontOutline(int32 depth)
Set a new vector outline – in pixels – for the loaded font.
const std::string & getText(void) const
Obtains set text string buffer; defaults to \0.
void setFontStyle(int32 style, uint8 options=150)
IFont::SharedPtr clone(void) const
Return a std::shared_ptr copy of this instance.
int32 getFontOutline(void) const
Getter for obtaining the vector outline of the loaded font.
RenderStyle
Text rendering qualities.
Definition: IFont.hpp:71
int32 getFontWidth(void) const
Compute the width in pixels of the set text string; defaults to zero (0)
FontStyle text_style
Current text effect set.
~TrueTypeFont(void)
Default destructor; we shutdown the SDL_ttf extension here.
TrueType fonts renderer.
std::string filename
Store the file path so we can change font sizes on the fly.
Texture font_buffer
Surface where font for drawing is rendered to.
Color color
Text color.
FontStyle
Text effect styling.
Definition: IFont.hpp:48
void setSpacing(uint32 spaces)
Not implemented.
uint32 getSpacing(void) const
Not implemented.
Coords coords
Positioning coordinates (including width and height)
IFont::RenderStyle getRenderingStyle(void) const
Getter for obtaining the current font rendering style in use.
uint32 getNewline(void) const
Not implemented.
std::string text_buffer
holds contents of text as a string buffer
TrueTypeFont(void)
Default constructor; we initialize the SDL_ttf extension here.
bool valid(void) const
Is this object initialized – not nullptr?
Coordinate values (x, y, z, width, height ) wrapper.
Definition: Coords.hpp:44
bool use_cache
Whether or not to use caching features of nom::ObjectCache.
int32 font_size
Font point (pixel) size; defaults to 12.
void setTextJustification(IFont::TextAlignment alignment)
Not implemented.
Utility class container for RGBA colors.
Definition: Color.hpp:43
void setRenderingStyle(IFont::RenderStyle)
Set a new font rendering style.
void setFontSize(int32 point_size)
Set a new text point size.