TrueTypeFont.cpp
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 #include "nomlib/graphics/TrueTypeFont.hpp"
30 
31 namespace nom {
32 
34 {
35 NOM_LOG_TRACE ( NOM );
36 
37  this->font = nullptr;
38  this->coords = Coords ( 0, 0, 0, 0 );
39  this->color = Color ( 0, 0, 0 );
40  this->text_buffer = "\0";
41  this->text_style = FontStyle::Regular; // default text styling effect
42  this->text_alignment = TextAlignment::MiddleLeft;
43  this->rendering = RenderStyle::Solid; // Fast, but ugly
44  this->style_options = 0;
45  this->filename = "\0";
46  this->font_size = 12;
47  this->use_cache = false;
48 
49  if ( TTF_Init () == -1 )
50  {
51 NOM_LOG_ERR ( NOM, TTF_GetError() );
52  }
53 }
54 
56 {
57 NOM_LOG_TRACE ( NOM );
58 
59  // Possible bug -- a segmentation fault occurs here if we do not reset the
60  // smart pointer on destruction.
61  this->font.reset();
62 
63  TTF_Quit();
64 }
65 
66 IFont::SharedPtr TrueTypeFont::clone ( void ) const
67 {
68  return IFont::SharedPtr ( new TrueTypeFont ( *this ), priv::Free_TrueTypeFont );
69 }
70 
71 bool TrueTypeFont::valid ( void ) const
72 {
73  if ( this->font.get() != nullptr )
74  {
75  return true;
76  }
77  else
78  {
79  return false;
80  }
81 }
82 
83 const std::string& TrueTypeFont::getText ( void ) const
84 {
85  return this->text_buffer;
86 }
87 
88 int32 TrueTypeFont::getFontWidth ( void ) const
89 {
90  return this->coords.width;
91 }
92 
93 int32 TrueTypeFont::getFontHeight ( void ) const
94 {
95  return this->coords.height;
96 }
97 
98 IFont::FontStyle TrueTypeFont::getFontStyle ( void ) const
99 {
100  return this->text_style;
101 }
102 
103 const Color& TrueTypeFont::getColor ( void ) const
104 {
105  return this->color;
106 }
107 
108 const Coords& TrueTypeFont::getPosition ( void ) const
109 {
110  return this->coords;
111 }
112 
113 uint32 TrueTypeFont::getNewline ( void ) const
114 {
115  // Not implemented
116  return 0;
117 }
118 
119 uint32 TrueTypeFont::getSpacing ( void ) const
120 {
121  // Not implemented
122  return 0;
123 }
124 
125 IFont::TextAlignment TrueTypeFont::getTextJustification ( void ) const
126 {
127  return this->text_alignment;
128 }
129 
130 void TrueTypeFont::setColor ( const Color& color )
131 {
132  this->color = color;
133 }
134 
135 void TrueTypeFont::setPosition ( const Coords& coords )
136 {
137  this->coords.x = coords.x;
138  this->coords.y = coords.y;
139 }
140 
141 void TrueTypeFont::setFontSize ( int32 point_size )
142 {
143  int32 original_font_size = this->font_size;
144 
145  this->font_size = point_size;
146 
147  if ( this->rebuild() == false )
148  {
149 NOM_LOG_ERR ( NOM, "Could not set new font size." );
150  this->font_size = original_font_size;
151  return;
152  }
153 }
154 
155 void TrueTypeFont::setFontStyle ( int32 style, uint8 options )
156 {
157  switch ( style )
158  {
159  default: break;
160  case FontStyle::Regular:
161  {
162  TTF_SetFontStyle ( this->font.get(), TTF_STYLE_NORMAL );
163  }
164  break;
165 
166  case FontStyle::Bold:
167  {
168  TTF_SetFontStyle ( this->font.get(), TTF_STYLE_BOLD );
169  }
170  break;
171 
172  case FontStyle::Italic:
173  {
174  TTF_SetFontStyle ( this->font.get(), TTF_STYLE_ITALIC );
175  }
176  break;
177 
178  case FontStyle::Underlined:
179  {
180  TTF_SetFontStyle ( this->font.get(), TTF_STYLE_UNDERLINE );
181  }
182  break;
183 
185  case FontStyle::Faded:
186  {
187  this->text_style = FontStyle::Faded;
188  this->style_options = options;
189  break;
190  }
191  }
192 }
193 
194 int32 TrueTypeFont::getFontOutline ( void ) const
195 {
196  return TTF_GetFontOutline ( this->font.get() );
197 }
198 
199 void TrueTypeFont::setFontOutline ( int32 depth )
200 {
201  TTF_SetFontOutline ( this->font.get(), depth );
202 }
203 
205 {
206  return this->rendering;
207 }
208 
210 {
211  this->rendering = style;
212 }
213 
214 void TrueTypeFont::setText ( const std::string& text )
215 {
216  if ( text.length() < 1 ) return;
217 
218  this->text_buffer = text;
219 
220  // Update the width & height of text string, if we can
221  if ( TTF_SizeText ( this->font.get(), this->text_buffer.c_str(), &this->coords.width, &this->coords.height ) == -1 )
222  {
223 NOM_LOG_ERR ( NOM, "Failed to set font width & height." );
224  }
225 }
226 
227 void TrueTypeFont::setSpacing ( uint32 spaces )
228 {
229  // Not implemented
230 }
231 
232 void TrueTypeFont::setTextJustification ( IFont::TextAlignment alignment )
233 {
234  this->text_alignment = alignment;
235 }
236 
237 bool TrueTypeFont::load ( const std::string& filename, const Color& colorkey,
238  bool use_cache
239  )
240 {
241  this->font = std::shared_ptr<TTF_Font> ( TTF_OpenFont ( filename.c_str(), this->font_size ), nom::priv::TTF_FreeFont );
242 
243  if ( this->valid() == false )
244  {
245 NOM_LOG_ERR ( NOM, "Could not load TTF file: " + filename );
246  return false;
247  }
248 
249  // Store the new filename & caching choice for future reference; primarily
250  // used when rebuilding font metrics, such as when we change the font point
251  // size or load a new font.
252  this->filename = filename;
253  this->use_cache = use_cache;
254 
255  return true;
256 }
257 
258 void TrueTypeFont::update ( void )
259 {
260  // Update display coordinates
261  this->font_buffer.set_position ( Point2i ( this->coords.x, this->coords.y ) );
262 
263  // Update the rendered text surface here for drawing
264  if ( this->getText().c_str() == nullptr ) return;
265 
266  if ( this->rendering == RenderStyle::Shaded ) // Moderate-quality
267  {
268  this->font_buffer.initialize ( TTF_RenderText_Shaded
269  (
270  this->font.get(),
271  this->getText().c_str(),
272  SDL_COLOR(this->color),
273  // TODO; implement me -- a second color
274  // possibility means needing two colors in
275  // class
276  SDL_COLOR ( Color ( 97, 97, 97 ) )
277  )
278  );
279  }
280  else if ( this->rendering == RenderStyle::Blended ) // Highest-quality
281  {
282  this->font_buffer.initialize ( TTF_RenderText_Blended
283  (
284  this->font.get(),
285  this->getText().c_str(),
286  SDL_COLOR(this->color)
287  )
288  );
289  }
290  else // Low-quality rendering (the default)
291  {
292  this->font_buffer.initialize ( TTF_RenderText_Solid
293  (
294  this->font.get(),
295  this->getText().c_str(),
296  SDL_COLOR(this->color)
297  )
298  );
299  }
300 
301  if ( this->text_style == FontStyle::Faded )
302  {
303  this->font_buffer.set_alpha ( this->style_options );
304  }
305 }
306 
307 void TrueTypeFont::draw ( SDL_Renderer* target ) const
308 {
309  if ( this->font_buffer.valid() )
310  {
311  this->font_buffer.draw ( target );
312  }
313 }
314 
315 void TrueTypeFont::draw ( const Window& target ) const
316 {
317  if ( this->font_buffer.valid() )
318  {
319  this->font_buffer.draw ( target.renderer() );
320  }
321 }
322 
324 {
325  if ( this->load ( this->filename, Color::Black, this->use_cache ) == false )
326  {
327 NOM_LOG_ERR ( NOM, "Could not rebuild font metrics." );
328  return false;
329  }
330 
331  return true;
332 }
333 
334 namespace priv {
335 
336 void Free_TrueTypeFont ( TrueTypeFont* ptr )
337 {
338  // Do nothing custom deleter
339  //
340  // FIXME; this is a known bug (memory leak).
341 }
342 
343 } // namespace priv
344 } // namespace nom
345 
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 set_position(const Point2i &pos)
Definition: Texture.cpp:201
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)
static const Color Black
Predefined color constants for convenience sake.
Definition: Color.hpp:77
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
void draw(SDL_RENDERER::RawPtr target) const
Definition: Texture.cpp:422
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.
SDL_RENDERER::RawPtr renderer(void) const
Get a raw pointer to the SDL_Renderer in use.
Definition: Renderer.cpp:58
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
bool valid(void) const
Is this object initialized – not nullptr?
Definition: Texture.cpp:162
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 initialize(SDL_SURFACE::RawPtr video_buffer)
Definition: Texture.cpp:97
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.