FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
subimagefont.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 <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/imagemanager.h"
41 #include "video/renderbackend.h"
42 
43 #include "subimagefont.h"
44 
45 namespace FIFE {
49  static Logger _log(LM_GUI);
50 
51  SubImageFont::SubImageFont(const std::string& filename, const std::string& glyphs)
52  : ImageFontBase() {
53 
54  FL_LOG(_log, LMsg("fifechan_image_font, loading ") << filename << " glyphs " << glyphs);
55 
56  ImagePtr img = ImageManager::instance()->load(filename);
57  SDL_Surface* surface = img->getSurface();
59 
60  if( !surface ) {
61  throw CannotOpenFile(filename);
62  }
63 
64  // Make sure we get 32bit RGB
65  // and copy the Pixelbuffers surface
66  SDL_Surface *tmp = SDL_CreateRGBSurface(0,
67  surface->w,surface->h,32,
69 
70  SDL_BlitSurface(surface,0,tmp,0);
71  surface = tmp;
72 
73  // Prepare the data for extracting the glyphs.
74  uint32_t *pixels = reinterpret_cast<uint32_t*>(surface->pixels);
75 
76  int32_t x = 0;
77 
78  SDL_Rect src;
79 
80  src.h = surface->h;
81  src.y = 0;
82 
83  uint32_t separator = pixels[0];
84  uint32_t colorkey = SDL_MapRGB(surface->format, m_colorkey.r, m_colorkey.g, m_colorkey.b);
85 
86  // if colorkey feature is not enabled then manually find the color key in the font
87  if (!RenderBackend::instance()->isColorKeyEnabled()) {
88  while(x < surface->w && pixels[x] == separator) {
89  ++x;
90  }
91 
92  colorkey = pixels[x];
93  }
94 
95  // Disable alpha blending, so that we use color keying
96  //SDL_SetAlpha(surface,0,255);
97  //SDL_SetColorKey(surface,SDL_SRCCOLORKEY,colorkey);
98 
99  FL_DBG(_log, LMsg("image_font")
100  << " glyph separator is "
101  << pprint(reinterpret_cast<void*>(separator))
102  << " transparent color is "
103  << pprint(reinterpret_cast<void*>(colorkey)));
104 
105  // Finally extract all glyphs
106  std::string::const_iterator text_it = glyphs.begin();
107  while(text_it != glyphs.end()) {
108  int32_t w = 0;
109  while(x < surface->w && pixels[x] == separator)
110  ++x;
111  if( x == surface->w )
112  break;
113 
114  while(x + w < surface->w && pixels[x + w] != separator)
115  ++w;
116 
117  src.x = x;
118  src.w = w;
119 
120  tmp = SDL_CreateRGBSurface(0,
121  w,surface->h,32,
123 
124  SDL_FillRect(tmp,0,colorkey);
125  SDL_BlitSurface(surface,&src,tmp,0);
126 
127  // Disable alpha blending, so that we use colorkeying
128  //SDL_SetAlpha(tmp,0,255);
129  //SDL_SetColorKey(tmp,SDL_SRCCOLORKEY,colorkey);
130  SDL_SetSurfaceBlendMode(tmp, SDL_BLENDMODE_NONE);
131  SDL_SetColorKey(tmp, SDL_TRUE, colorkey);
132 
133  uint32_t codepoint = utf8::next(text_it, glyphs.end());
134  m_glyphs[ codepoint ].surface = tmp;
135 
136  x += w;
137  }
138 
139  // Set placeholder glyph
140  // This should actually work with utf8.
141  if( m_glyphs.find('?') != m_glyphs.end() ) {
142  m_placeholder = m_glyphs['?'];
143  } else {
145  }
146 
147  m_height = surface->h;
148  SDL_FreeSurface(surface);
149  }
150 
151 
152 }
153 
Definition: modules.h:41
type_glyphs m_glyphs
Definition: imagefontbase.h:82
const uint32_t NULLMASK
Definition: fife_stdint.h:63
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
uint32_t next(octet_iterator &it, octet_iterator end)
Definition: checked.h:137
const uint32_t RMASK
Definition: fife_stdint.h:53
static Logger _log(LM_AUDIO)
static ImageManager * instance()
Definition: singleton.h:84
const uint32_t GMASK
Definition: fife_stdint.h:54
SDL_Surface * getSurface()
Definition: image.h:96
const SDL_Color & getColorKey() const
Gets the global colorkey setting.
const uint32_t BMASK
Definition: fife_stdint.h:55
#define FL_LOG(logger, msg)
Definition: logger.h:71
Helper for printing a pointer.
Definition: logger.h:247
unsigned int uint32_t
Definition: core.h:40
SDL_Color m_colorkey
Definition: subimagefont.h:55
SubImageFont(const std::string &filename, const std::string &glyphs)
Constructor.
#define FL_DBG(logger, msg)
Definition: logger.h:70
virtual ImagePtr load(const std::string &name, IResourceLoader *loader=0)
Creates a blank resource and loads it from disk.
ImageFont base class.
Definition: imagefontbase.h:46