FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
subimagefont.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 <string>
24 
25 // Platform specific includes
26 #include "util/base/fife_stdint.h"
27 
28 // 3rd party library includes
29 #include <SDL.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/log/logger.h"
37 #include "util/structures/rect.h"
38 #include "util/utf8/utf8.h"
39 #include "video/image.h"
40 #include "video/renderbackend.h"
41 #include "video/imagepool.h"
42 
43 #include "subimagefont.h"
44 
45 namespace FIFE {
46  static Logger _log(LM_GUI);
47 
48  SubImageFont::SubImageFont(const std::string& filename, const std::string& glyphs, ImagePool& pool)
49  : ImageFontBase(), m_pool(pool) {
50 
51  FL_LOG(_log, LMsg("guichan_image_font, loading ") << filename << " glyphs " << glyphs);
52 
53  int image_id = m_pool.addResourceFromFile(filename);
54  Image& img = dynamic_cast<Image&>(m_pool.get(image_id));
55  SDL_Surface* surface = img.getSurface();
56  m_colorkey = RenderBackend::instance()->getColorKey();
57 
58  if( !surface ) {
59  throw CannotOpenFile(filename);
60  }
61 
62  // Make sure we get 32bit RGB
63  // and copy the Pixelbuffers surface
64  SDL_Surface *tmp = SDL_CreateRGBSurface(SDL_SWSURFACE,
65  surface->w,surface->h,32,
66  RMASK, GMASK, BMASK ,NULLMASK);
67 
68  SDL_BlitSurface(surface,0,tmp,0);
69  surface = tmp;
70 
71  // Prepare the data for extracting the glyphs.
72  uint32_t *pixels = reinterpret_cast<uint32_t*>(surface->pixels);
73 
74  int x, w;
75  x = 0; w=0;
76 
77  SDL_Rect src;
78 
79  src.h = surface->h;
80  src.y = 0;
81 
82  uint32_t separator = pixels[0];
83  uint32_t colorkey = SDL_MapRGB(surface->format, m_colorkey.r, m_colorkey.g, m_colorkey.b);
84 
85  // if colorkey feature is not enabled then manually find the color key in the font
86  if (!RenderBackend::instance()->isColorKeyEnabled()) {
87  while(x < surface->w && pixels[x] == separator) {
88  ++x;
89  }
90 
91  colorkey = pixels[x];
92  }
93 
94  // Disable alpha blending, so that we use color keying
95  SDL_SetAlpha(surface,0,255);
96  SDL_SetColorKey(surface,SDL_SRCCOLORKEY,colorkey);
97 
98  FL_DBG(_log, LMsg("image_font")
99  << " glyph separator is "
100  << pprint(reinterpret_cast<void*>(separator))
101  << " transparent color is "
102  << pprint(reinterpret_cast<void*>(colorkey)));
103 
104  // Finally extract all glyphs
105  std::string::const_iterator text_it = glyphs.begin();
106  while(text_it != glyphs.end()) {
107  w=0;
108  while(x < surface->w && pixels[x] == separator)
109  ++x;
110  if( x == surface->w )
111  break;
112 
113  while(x + w < surface->w && pixels[x + w] != separator)
114  ++w;
115 
116  src.x = x;
117  src.w = w;
118 
119  tmp = SDL_CreateRGBSurface(SDL_SWSURFACE,
120  w,surface->h,32,
121  RMASK, GMASK, BMASK ,NULLMASK);
122 
123  SDL_FillRect(tmp,0,colorkey);
124  SDL_BlitSurface(surface,&src,tmp,0);
125 
126  // Disable alpha blending, so that we use colorkeying
127  SDL_SetAlpha(tmp,0,255);
128  SDL_SetColorKey(tmp,SDL_SRCCOLORKEY,colorkey);
129 
130 
131  uint32_t codepoint = utf8::next(text_it, glyphs.end());
132  m_glyphs[ codepoint ].surface = tmp;
133 
134  x += w;
135  }
136 
137  // Set placeholder glyph
138  // This should actually work ith utf8.
139  if( m_glyphs.find('?') != m_glyphs.end() ) {
140  m_placeholder = m_glyphs['?'];
141  } else {
142  m_placeholder.surface = 0;
143  }
144 
145  mHeight = surface->h;
146  SDL_FreeSurface(surface);
147  }
148 
149 
150 }
151