BitmapFont.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 Portions Copyright (c) 2004-2013 Lazy Foo' Productions [1]
9 All rights reserved.
10 
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 
14 1. Redistributions of source code must retain the above copyright notice, this
15  list of conditions and the following disclaimer.
16 2. Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 1. http://lazyfoo.net/SDL_tutorials/lesson30/index.php
32 
33 ******************************************************************************/
34 #include "nomlib/graphics/BitmapFont.hpp"
35 
36 namespace nom {
37 
38 BitmapFont::BitmapFont ( void ) : sheet_width ( 16 ), sheet_height ( 16 )
39 {
40 NOM_LOG_TRACE ( NOM );
41 
42  this->text_buffer = "\0";
43  this->text_style = FontStyle::Regular;
44  this->text_alignment = TextAlignment::MiddleLeft;
45  this->rendering = RenderStyle::Solid; // Fast, but ugly
46  this->newline = 0;
47  this->spacing = 0;
48  this->color = Color ( 0, 0, 0 );
49  this->coords.setPosition ( 0, 0 );
50 
51  for ( unsigned int idx = 0; idx < 256; idx++ )
52  {
53  this->chars[idx] = Coords ( 0, 0, 0, 0 );
54  }
55 }
56 
58 {
59 NOM_LOG_TRACE ( NOM );
60 }
61 
62 IFont::SharedPtr BitmapFont::clone ( void ) const
63 {
64  return IFont::SharedPtr ( new BitmapFont ( *this ), priv::Free_BitmapFont );
65 }
66 
67 const std::string& BitmapFont::getText ( void ) const
68 {
69  return this->text_buffer;
70 }
71 
72 int32 BitmapFont::getFontWidth ( void ) const
73 {
74  int32 text_width = 0;
75 
76  for ( uint32 char_pos = 0; char_pos < this->text_buffer.length(); char_pos++ )
77  {
78  if ( this->text_buffer [ char_pos ] == ' ' )
79  {
80  text_width += this->spacing;
81 
82 // Dump each character's table used for calculation
83 #if defined ( NOM_DEBUG_SDL_BITMAP_FONT )
84 NOM_DUMP_VAR ( char_pos );
85 NOM_DUMP_VAR ( this->text_buffer [ char_pos ] );
86 NOM_DUMP_VAR ( text_width );
87 #endif
88 
89  }
90  else if ( this->text_buffer [ char_pos ] == '\n' ) // TODO
91  {
92  text_width = 0;
93  }
94  else
95  {
96  uint8 ascii_char = static_cast<uchar> ( this->text_buffer [ char_pos ] );
97  text_width += this->chars [ ascii_char ].width + 1;
98 
99 // Dump each character's table used for calculation
100 #if defined ( NOM_DEBUG_SDL_BITMAP_FONT )
101 NOM_DUMP_VAR ( char_pos );
102 NOM_DUMP_VAR ( this->text_buffer [ char_pos ] );
103 NOM_DUMP_VAR ( this->chars [ ascii_char ].width + 1 );
104 NOM_DUMP_VAR ( text_width );
105 #endif
106 
107  }
108  } // end for loop
109 
110  return text_width;
111 }
112 
113 int32 BitmapFont::getFontHeight ( void ) const
114 {
115  int32 text_height = 0;
116 
117  for ( uint32 char_pos = 0; char_pos < this->text_buffer.length(); char_pos++ )
118  {
119  if ( this->text_buffer [ char_pos ] == '\n' )
120  {
121  text_height += this->newline;
122 
123 // Dump each character's table used for calculation
124 #if defined ( NOM_DEBUG_SDL_BITMAP_FONT )
125 NOM_DUMP_VAR ( char_pos );
126 NOM_DUMP_VAR ( this->text_buffer [ char_pos ] );
127 NOM_DUMP_VAR ( text_height );
128 #endif
129 
130  }
131  else
132  {
133  uint8 ascii_char = static_cast<uchar> ( this->text_buffer [ char_pos ] );
134 
135  text_height = this->chars [ ascii_char ].height;
136 
137 // Dump each character's table used for calculation
138 #if defined ( NOM_DEBUG_SDL_BITMAP_FONT )
139 NOM_DUMP_VAR ( char_pos );
140 NOM_DUMP_VAR ( this->text_buffer [ char_pos ] );
141 NOM_DUMP_VAR ( this->chars [ ascii_char ].height );
142 NOM_DUMP_VAR ( text_height );
143 #endif
144 
145  }
146  }
147 
148  return text_height;
149 }
150 
151 const Color& BitmapFont::getColor ( void ) const
152 {
153  return this->color;
154 }
155 
156 const Coords& BitmapFont::getPosition ( void ) const
157 {
158  return this->coords;
159 }
160 
161 void BitmapFont::setColor ( const Color& color )
162 {
163  this->color = color;
164 }
165 
166 void BitmapFont::setPosition ( const Coords& coords )
167 {
168  this->coords = coords;
169 }
170 
171 void BitmapFont::setText ( const std::string& text )
172 {
173  this->text_buffer = text;
174 }
175 
176 uint32 BitmapFont::getSpacing ( void ) const
177 {
178  return this->spacing;
179 }
180 
181 void BitmapFont::setSpacing ( uint32 spaces )
182 {
183  this->spacing = spaces;
184 }
185 
186 uint32 BitmapFont::getNewline ( void ) const
187 {
188  return this->newline;
189 }
190 
191 IFont::TextAlignment BitmapFont::getTextJustification ( void ) const
192 {
193  return this->text_alignment;
194 }
195 
196 void BitmapFont::setNewline ( uint32 newline )
197 {
198  this->newline = newline;
199 }
200 
201 IFont::FontStyle BitmapFont::getFontStyle ( void ) const
202 {
203  return this->text_style;
204 }
205 
206 void BitmapFont::setFontStyle ( int32 style, uint8 options )
207 {
208  switch ( style )
209  {
210  default:
211  break;
212  case FontStyle::Regular:
213  case FontStyle::Bold:
214  case FontStyle::Italic:
215  case FontStyle::Underlined:
216  // Do nothing stub
217  break;
218 
220  case FontStyle::Faded:
221  {
222  if ( this->bitmap_font.valid() )
223  {
224  if ( this->bitmap_font.set_alpha ( options ) == true )
225  {
226  this->text_style = FontStyle::Faded;
227  }
228  }
229  break;
230  }
231  }
232 }
233 
234 /*
235 RenderStyle BitmapFont::getRenderingStyle ( void )
236 {
237  return this->rendering; // Not implemented
238 }
239 
240 void BitmapFont::setRenderingStyle ( enum RenderStyle style )
241 {
242  this->rendering = style; // Not implemented
243 }
244 */
245 
246 bool BitmapFont::rebuild ( void )
247 {
248  uint32 tile_width = 0;
249  uint32 tile_height = 0;
250  uint32 top = 0;
251  uint32 baseA = 0;
252  uint32 currentChar = 0;
253  uint32 background_color = 0;
254 
255 NOM_ASSERT ( this->bitmap_font.valid() );
256 
257  if ( this->bitmap_font.lock() == false )
258  {
259 NOM_LOG_ERR ( NOM, "Could not lock Texture." );
260  return false;
261  }
262 
263  background_color = RGBA ( this->bitmap_font.colorkey(),
264  this->bitmap_font.pixel_format()
265  );
266 
267  tile_width = this->bitmap_font.width() / this->sheet_width;
268  tile_height = this->bitmap_font.height() / this->sheet_height;
269  top = tile_height;
270  baseA = tile_height;
271 
272  for ( int32 rows = 0; rows < this->sheet_width; rows++ )
273  {
274  for ( int32 cols = 0; cols < this->sheet_height; cols++ )
275  {
276  // Set character offsets
277  this->chars[ currentChar ].setPosition ( tile_width * cols, tile_height * rows );
278  this->chars[ currentChar ].setSize ( tile_width, tile_height );
279 
280  //Find Left Side; go through pixel columns
281  for ( uint32 pCol = 0; pCol < tile_width; pCol++ )
282  {
283  //Go through pixel rows
284  for ( uint32 pRow = 0; pRow < tile_height; pRow++ )
285  {
286  //Get the pixel offsets
287  int pX = ( tile_width * cols ) + pCol;
288  int pY = ( tile_height * rows ) + pRow;
289 
290  //If a non colorkey pixel is found
291  if( this->bitmap_font.pixel ( pX, pY ) != background_color )
292  {
293  //Set the x offset
294  this->chars[ currentChar ].x = pX;
295 
296  //Break the loops
297  pCol = tile_width;
298  pRow = tile_height;
299  }
300  }
301  }
302 
303  //Find Right Side; go through pixel columns
304  for ( int32 pCol_w = tile_width - 1; pCol_w >= 0; pCol_w-- )
305  {
306  //Go through pixel rows
307  for ( uint32 pRow_w = 0; pRow_w < tile_height; pRow_w++ )
308  {
309  //Get the pixel offsets
310  uint32 pX = ( tile_width * cols ) + pCol_w;
311  uint32 pY = ( tile_height * rows ) + pRow_w;
312 
313  //If a non colorkey pixel is found
314  if ( this->bitmap_font.pixel ( pX, pY ) != background_color )
315  {
316  //Set the width
317  this->chars[ currentChar ].width = ( pX - this->chars[ currentChar ].x ) + 1;
318 
319  //Break the loops
320  pCol_w = -1;
321  pRow_w = tile_height;
322  }
323  }
324  }
325 
326  //Find Top; go through pixel rows
327  for ( uint32 pRow = 0; pRow < tile_height; pRow++ )
328  {
329  //Go through pixel columns
330  for ( uint32 pCol = 0; pCol < tile_width; pCol++ )
331  {
332  //Get the pixel offsets
333  uint32 pX = ( tile_width * cols ) + pCol;
334  uint32 pY = ( tile_height * rows ) + pRow;
335 
336  // If a non colorkey pixel is found
337  if( this->bitmap_font.pixel ( pX, pY ) != background_color )
338  {
339  // If new top is found
340  if ( pRow < top )
341  {
342  top = pRow;
343  }
344 
345  // Break the loops
346  pCol = tile_width;
347  pRow = tile_height;
348  }
349  }
350  }
351 
352  // Calculate the baseline of 'A' so that we can use this in our
353  // newline offset calculation
354  if ( currentChar == 'A' )
355  {
356  // Go through pixel rows
357  for ( int32 pRow = tile_height - 1; pRow >= 0; pRow-- )
358  {
359  // Go through pixel columns
360  for ( uint32 pCol = 0; pCol < tile_width; pCol++ )
361  {
362  // Get the pixel offsets
363  unsigned int pX = ( tile_width * cols ) + pCol;
364  unsigned int pY = ( tile_height * rows ) + pRow;
365 
366  // If a non colorkey pixel is found
367  if ( this->bitmap_font.pixel ( pX, pY ) != background_color )
368  {
369  // Bottom of a is found
370  baseA = pRow;
371 
372  //Break the loops
373  pCol = tile_width;
374  pRow = -1;
375  }
376  } // end for pixel columns loop
377  } // end for pixel rows loop
378  } // end if currentChar
379 
380  // Go to the next character
381  currentChar++;
382  }
383  }
384  this->bitmap_font.unlock(); // Finished uploading to texture
385 
386  // Calculate space
387  this->spacing = tile_width / 2;
388 
389  // Calculate new line
390  this->newline = baseA - top;
391 
392  // Loop off excess top pixels
393  for ( uint32 t = 0; t < 256; t++ )
394  {
395  this->chars[ t ].y += top;
396  this->chars[ t ].height -= top;
397  }
398 
399 // Dump table of calculated bitmap character positions
400 #if defined ( NOM_DEBUG_SDL_BITMAP_FONT )
401  for ( uint32 pos = 0; pos < 256; pos++ )
402  {
403 NOM_DUMP_VAR ( pos );
404 NOM_DUMP_VAR ( static_cast<uchar> ( pos ) );
405 NOM_DUMP_VAR ( this->chars [ pos ].x );
406 NOM_DUMP_VAR ( this->chars [ pos ].y );
407 NOM_DUMP_VAR ( this->chars [ pos ].width );
408 NOM_DUMP_VAR ( this->chars [ pos ].height );
409  }
410 #endif
411 
412  return true;
413 }
414 
415 bool BitmapFont::load ( const std::string& filename, const Color& colorkey,
416  bool use_cache
417  )
418 {
419  if ( this->bitmap_font.load ( filename, 0, use_cache ) == false )
420  {
421 NOM_LOG_ERR ( NOM, "Could not load bitmap font image file: " + filename );
422  return false;
423  }
424 
425  if ( colorkey != Color::null )
426  {
427  this->bitmap_font.set_colorkey ( colorkey );
428  }
429 
430  // Attempt to rebuild font metrics
431  if ( this->rebuild() == false )
432  {
433 NOM_LOG_ERR ( NOM, "Could not rebuild bitmap font metrics" );
434  return false;
435  }
436 
437  return true;
438 }
439 
440 void BitmapFont::setTextJustification ( enum TextAlignment alignment )
441 {
442  this->text_alignment = alignment;
443 }
444 
445 const Coords BitmapFont::findGlyph ( const std::string& glyph )
446 {
447  uint8 ascii = 0;
448  std::istringstream i ( glyph );
449 
450  i >> ascii;
451 
452  return this->chars[ascii];
453 }
454 
455 void BitmapFont::update ( void )
456 {
457  // Stub
458 }
459 
460 void BitmapFont::draw ( RenderTarget target ) const
461 {
462  // Use coordinates provided by interface user as our starting origin
463  // coordinates to compute from
464  int32 x_offset = this->coords.x;
465  int32 y_offset = this->coords.y;
466 
467  switch ( this->text_alignment )
468  {
469  default:
470  case TextAlignment::MiddleLeft:
471  {
472  x_offset = this->coords.x;
473  y_offset = this->coords.y;
474  }
475  break;
476 
477  case TextAlignment::MiddleCenter:
478  {
479  x_offset = this->coords.x + ( this->coords.width / 2 ) - ( this->getFontWidth() / 2 );
480  y_offset = ( this->coords.height / 2 ) + this->coords.y - ( this->getFontHeight() / 2 );
481  }
482  break;
483 
484  case TextAlignment::MiddleRight:
485  {
486  // TODO
487  }
488  break;
489  } // end switch
490 
491  //If the font has been built
492  if ( this->bitmap_font.valid() )
493  {
494  for ( uint32 show = 0; show < this->text_buffer.length(); show++ )
495  {
496  //If the current character is a space
497  if ( this->text_buffer[show] == ' ' )
498  {
499  //Move over
500  x_offset += this->spacing;
501  }
502  // If the current character is a newline
503  else if( this->text_buffer[show] == '\n' )
504  {
505  //Move down and back over to the beginning of line
506  y_offset += this->newline;
507  x_offset = this->coords.x;
508  }
509  // If the current character is a tab
510  else if( this->text_buffer[show] == '\t' )
511  {
512  x_offset += this->spacing * 4;
513  }
514  else
515  {
516  //Get the ASCII value of the character
517  uint8 ascii = static_cast<uchar>( this->text_buffer[show] );
518 
519  this->bitmap_font.set_position ( Point2i ( x_offset, y_offset ) );
520  this->bitmap_font.set_bounds ( this->chars[ascii] );
521  this->bitmap_font.draw ( target.renderer() );
522 
523  // Move over the width of the character with one pixel of padding
524  x_offset += ( this->chars[ascii].width ) + 1;
525  } // end else
526  } // end for loop
527  } // end if this->bitmap_font != nullptr
528 }
529 
530 bool BitmapFont::resize ( enum ResizeAlgorithm scaling_algorithm )
531 {
532  if ( this->bitmap_font.valid() == false )
533  {
534 NOM_LOG_ERR ( NOM, "Video surface is invalid." );
535  return false;
536  }
537 
538  if ( this->bitmap_font.resize ( scaling_algorithm ) == false )
539  {
540 NOM_LOG_ERR ( NOM, "Failed to resize the video surface." );
541  return false;
542  }
543 
544  if ( this->rebuild() == false )
545  {
546 NOM_LOG_ERR ( NOM, "Could not rebuild bitmap font metrics" );
547  return false;
548  }
549 
550  return true;
551 }
552 
553 namespace priv {
554 
555 void Free_BitmapFont ( BitmapFont* ptr )
556 {
557  // Do nothing custom deleter
558  //
559  // FIXME; this is a known bug (memory leak).
560 }
561 
562 } // namespace priv
563 } // namespace nom
564 
int32 width(void) const
Definition: Texture.cpp:212
uint32 getNewline(void) const
Definition: BitmapFont.cpp:186
~BitmapFont(void)
Default destructor.
Definition: BitmapFont.cpp:57
std::string text_buffer
holds contents of text as a string
Definition: BitmapFont.hpp:177
BitmapFont(void)
Default constructor.
Definition: BitmapFont.cpp:38
uint32 getSpacing(void) const
Definition: BitmapFont.cpp:176
void set_position(const Point2i &pos)
Definition: Texture.cpp:201
bool rebuild(void)
Definition: BitmapFont.cpp:246
uint32 pixel(int32 x, int32 y)
Definition: Texture.cpp:519
const Color & colorkey(void) const
Definition: Texture.cpp:299
void setTextJustification(IFont::TextAlignment alignment)
Definition: BitmapFont.cpp:440
bool set_colorkey(const Color &colorkey)
Definition: Texture.cpp:870
static const Color null
Convenience object that will always contain a value of -1.
Definition: Color.hpp:74
Color color
Not implemented (yet)
Definition: BitmapFont.hpp:183
enum IFont::FontStyle text_style
Current text effect set.
Definition: BitmapFont.hpp:180
void setText(const std::string &text)
Set a new text string for drawing; defaults to \0.
Definition: BitmapFont.cpp:171
Coords chars[256]
individual chars positioning offsets within bitmap_font
Definition: BitmapFont.hpp:161
void draw(SDL_RENDERER::RawPtr target) const
Definition: Texture.cpp:422
bool load(const std::string &filename, uint32 flags, bool use_cache=false)
Definition: Texture.cpp:353
void setColor(const Color &color)
Not implemented.
Definition: BitmapFont.cpp:161
Texture bitmap_font
pointer reference holding our bitmap font image sheet
Definition: BitmapFont.hpp:158
const std::string & getText(void) const
Obtains set text string buffer; defaults to \0.
Definition: BitmapFont.cpp:67
IFont::SharedPtr clone(void) const
Return a std::shared_ptr copy of this instance.
Definition: BitmapFont.cpp:62
Coords coords
Positioning coordinates.
Definition: BitmapFont.hpp:164
FontStyle
Text effect styling.
Definition: IFont.hpp:48
void unlock(void)
Definition: Texture.cpp:340
SDL_RENDERER::RawPtr renderer(void) const
Get a raw pointer to the SDL_Renderer in use.
Definition: Renderer.cpp:58
void update(void)
Definition: BitmapFont.cpp:455
int32 height(void) const
Definition: Texture.cpp:225
int32 getFontHeight(void) const
Definition: BitmapFont.cpp:113
bool valid(void) const
Is this object initialized – not nullptr?
Definition: Texture.cpp:162
void draw(RenderTarget target) const
Definition: BitmapFont.cpp:460
Coordinate values (x, y, z, width, height ) wrapper.
Definition: Coords.hpp:44
bool resize(enum ResizeAlgorithm scaling_algorithm)
Definition: BitmapFont.cpp:530
void setSpacing(uint32 spaces)
Definition: BitmapFont.cpp:181
uint32 newline
height (in pixels) to offset when newline carriage char is encountered
Definition: BitmapFont.hpp:167
void setNewline(uint32_t newline)
Set new text character spacing height offsets in pixels.
Definition: BitmapFont.cpp:196
int32 getFontWidth(void) const
Definition: BitmapFont.cpp:72
bool lock(void)
Definition: Texture.cpp:304
bool load(const std::string &filename, const Color &colorkey, bool use_cache=false)
Definition: BitmapFont.cpp:415
void setFontStyle(int32 style, uint8 options=150)
Definition: BitmapFont.cpp:206
Utility class container for RGBA colors.
Definition: Color.hpp:43
bool resize(enum ResizeAlgorithm scaling_algorithm)
Definition: Texture.cpp:559