FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
imagefontbase.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
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 <boost/filesystem/convenience.hpp>
27 #include <boost/scoped_array.hpp>
28 #include <SDL.h>
29 #include <SDL_image.h>
30 
31 // FIFE includes
32 // These includes are split up in two parts, separated by one empty line
33 // First block: files included from the FIFE root src directory
34 // Second block: files included from the same folder
35 #include "util/base/exception.h"
36 #include "util/structures/rect.h"
37 #include "util/utf8/utf8.h"
38 #include "video/image.h"
39 #include "video/renderbackend.h"
40 
41 #include "imagefontbase.h"
42 
43 namespace FIFE {
44 
46  }
47 
49  type_glyphs::iterator i = m_glyphs.begin();
50  for(; i != m_glyphs.end(); ++i) {
51  SDL_FreeSurface(i->second.surface);
52  }
53 
54  }
55 
56  int ImageFontBase::getWidth(const std::string& text) const {
57  int w = 0;
58  std::string::const_iterator text_it = text.begin();
59  while(text_it != text.end()) {
60  uint32_t codepoint = utf8::next(text_it,text.end());
61  type_glyphs::const_iterator it = m_glyphs.find( codepoint );
62 
63  if( it != m_glyphs.end() ) {
64  w += it->second.surface->w + getGlyphSpacing();
65  continue;
66  }
67 
68  if( m_placeholder.surface ) {
69  w += m_placeholder.surface->w + getGlyphSpacing();
70  }
71  }
72  return w;
73  }
74 
76  return mHeight;
77  }
78 
79  SDL_Surface *ImageFontBase::renderString(const std::string& text) {
80  SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
81  getWidth(text),getHeight(),32,
82  RMASK, GMASK, BMASK ,AMASK);
83 
84  SDL_FillRect(surface,0,0x00000000);
85 
86  SDL_Rect dst;
87  dst.x = dst.y = 0;
88  s_glyph *glyph = 0;
89 
90  std::string::const_iterator text_it = text.begin();
91  while(text_it != text.end()) {
92  uint32_t codepoint = utf8::next(text_it,text.end());
93  type_glyphs::iterator it = m_glyphs.find( codepoint );
94 
95  if( it == m_glyphs.end() ) {
96  if( !m_placeholder.surface ) {
97  continue;
98  }
99  glyph = &m_placeholder;
100  } else {
101  glyph = &(it->second);
102  }
103  dst.y = glyph->offset.y;
104  dst.x += glyph->offset.x;
105 
106  SDL_BlitSurface(glyph->surface,0,surface,&dst);
107  dst.x += glyph->surface->w + getGlyphSpacing();
108  }
109 
110  return surface;
111  }
112 
113  void ImageFontBase::setColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
114  }
115 }