FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
imagefontbase.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <algorithm>
24 
25 // 3rd party library includes
26 #include <SDL.h>
27 #include <SDL_image.h>
28 
29 // FIFE includes
30 // These includes are split up in two parts, separated by one empty line
31 // First block: files included from the FIFE root src directory
32 // Second block: files included from the same folder
33 #include "util/base/exception.h"
34 #include "util/structures/rect.h"
35 #include "util/utf8/utf8.h"
36 #include "video/image.h"
37 #include "video/renderbackend.h"
38 
39 #include "imagefontbase.h"
40 
41 namespace FIFE {
42 
44  }
45 
47  type_glyphs::iterator i = m_glyphs.begin();
48  for(; i != m_glyphs.end(); ++i) {
49  SDL_FreeSurface(i->second.surface);
50  }
51 
52  }
53 
54  int32_t ImageFontBase::getWidth(const std::string& text) const {
55  int32_t w = 0;
56  std::string::const_iterator text_it = text.begin();
57  while(text_it != text.end()) {
58  uint32_t codepoint = utf8::next(text_it,text.end());
59  type_glyphs::const_iterator it = m_glyphs.find( codepoint );
60 
61  if( it != m_glyphs.end() ) {
62  w += it->second.surface->w + getGlyphSpacing();
63  continue;
64  }
65 
66  if( m_placeholder.surface ) {
68  }
69  }
70  return w;
71  }
72 
73  int32_t ImageFontBase::getHeight() const {
74  return m_height;
75  }
76 
77  SDL_Surface *ImageFontBase::renderString(const std::string& text) {
78  SDL_Surface *surface = SDL_CreateRGBSurface(0,
79  getWidth(text),getHeight(),32,
80  RMASK, GMASK, BMASK ,AMASK);
81 
82  SDL_FillRect(surface,0,0x00000000);
83 
84  SDL_Rect dst;
85  dst.x = dst.y = 0;
86  s_glyph *glyph = 0;
87 
88  std::string::const_iterator text_it = text.begin();
89  while(text_it != text.end()) {
90  uint32_t codepoint = utf8::next(text_it,text.end());
91  type_glyphs::iterator it = m_glyphs.find( codepoint );
92 
93  if( it == m_glyphs.end() ) {
94  if( !m_placeholder.surface ) {
95  continue;
96  }
97  glyph = &m_placeholder;
98  } else {
99  glyph = &(it->second);
100  }
101  dst.y = glyph->offset.y;
102  dst.x += glyph->offset.x;
103 
104  SDL_BlitSurface(glyph->surface,0,surface,&dst);
105  dst.x += glyph->surface->w + getGlyphSpacing();
106  }
107 
108  return surface;
109  }
110 
112  }
113 }
type_glyphs m_glyphs
Definition: imagefontbase.h:82
virtual int32_t getWidth(const std::string &text) const
Get the width in pixels a given text would occupy.
virtual void setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Set the color the text should be rendered in.
int32_t getGlyphSpacing() const
Gets the spacing between letters in pixels.
Definition: fontbase.cpp:73
uint32_t next(octet_iterator &it, octet_iterator end)
Definition: checked.h:137
virtual ~ImageFontBase()
Destructor.
const uint32_t RMASK
Definition: fife_stdint.h:53
const uint32_t AMASK
Definition: fife_stdint.h:56
Abstract Font Base Class Uses a pool for rendered strings.
Definition: fontbase.h:48
unsigned char uint8_t
Definition: core.h:38
const uint32_t GMASK
Definition: fife_stdint.h:54
const uint32_t BMASK
Definition: fife_stdint.h:55
virtual SDL_Surface * renderString(const std::string &text)
virtual int32_t getHeight() const
Get the height in pixels a text line would occupy.
unsigned int uint32_t
Definition: core.h:40
ImageFontBase()
Constructor.