00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <string>
00024
00025
00026 #include "util/base/fife_stdint.h"
00027
00028
00029 #include <SDL.h>
00030
00031
00032
00033
00034
00035 #include "util/base/exception.h"
00036 #include "util/log/logger.h"
00037 #include "util/structures/rect.h"
00038 #include "util/utf8/utf8.h"
00039 #include "video/image.h"
00040 #include "video/renderbackend.h"
00041 #include "video/imagepool.h"
00042
00043 #include "subimagefont.h"
00044
00045 namespace FIFE {
00046 static Logger _log(LM_GUI);
00047
00048 SubImageFont::SubImageFont(const std::string& filename, const std::string& glyphs, ImagePool& pool)
00049 : ImageFontBase(), m_pool(pool) {
00050
00051 FL_LOG(_log, LMsg("guichan_image_font, loading ") << filename << " glyphs " << glyphs);
00052
00053 int image_id = m_pool.addResourceFromFile(filename);
00054 Image& img = dynamic_cast<Image&>(m_pool.get(image_id));
00055 SDL_Surface* surface = img.getSurface();
00056 m_colorkey = RenderBackend::instance()->getColorKey();
00057
00058 if( !surface ) {
00059 throw CannotOpenFile(filename);
00060 }
00061
00062
00063
00064 SDL_Surface *tmp = SDL_CreateRGBSurface(SDL_SWSURFACE,
00065 surface->w,surface->h,32,
00066 RMASK, GMASK, BMASK ,NULLMASK);
00067
00068 SDL_BlitSurface(surface,0,tmp,0);
00069 surface = tmp;
00070
00071
00072 uint32_t *pixels = reinterpret_cast<uint32_t*>(surface->pixels);
00073
00074 int x, w;
00075 x = 0; w=0;
00076
00077 SDL_Rect src;
00078
00079 src.h = surface->h;
00080 src.y = 0;
00081
00082 uint32_t separator = pixels[0];
00083 uint32_t colorkey = SDL_MapRGB(surface->format, m_colorkey.r, m_colorkey.g, m_colorkey.b);
00084
00085
00086 if (!RenderBackend::instance()->isColorKeyEnabled()) {
00087 while(x < surface->w && pixels[x] == separator) {
00088 ++x;
00089 }
00090
00091 colorkey = pixels[x];
00092 }
00093
00094
00095 SDL_SetAlpha(surface,0,255);
00096 SDL_SetColorKey(surface,SDL_SRCCOLORKEY,colorkey);
00097
00098 FL_DBG(_log, LMsg("image_font")
00099 << " glyph separator is "
00100 << pprint(reinterpret_cast<void*>(separator))
00101 << " transparent color is "
00102 << pprint(reinterpret_cast<void*>(colorkey)));
00103
00104
00105 std::string::const_iterator text_it = glyphs.begin();
00106 while(text_it != glyphs.end()) {
00107 w=0;
00108 while(x < surface->w && pixels[x] == separator)
00109 ++x;
00110 if( x == surface->w )
00111 break;
00112
00113 while(x + w < surface->w && pixels[x + w] != separator)
00114 ++w;
00115
00116 src.x = x;
00117 src.w = w;
00118
00119 tmp = SDL_CreateRGBSurface(SDL_SWSURFACE,
00120 w,surface->h,32,
00121 RMASK, GMASK, BMASK ,NULLMASK);
00122
00123 SDL_FillRect(tmp,0,colorkey);
00124 SDL_BlitSurface(surface,&src,tmp,0);
00125
00126
00127 SDL_SetAlpha(tmp,0,255);
00128 SDL_SetColorKey(tmp,SDL_SRCCOLORKEY,colorkey);
00129
00130
00131 uint32_t codepoint = utf8::next(text_it, glyphs.end());
00132 m_glyphs[ codepoint ].surface = tmp;
00133
00134 x += w;
00135 }
00136
00137
00138
00139 if( m_glyphs.find('?') != m_glyphs.end() ) {
00140 m_placeholder = m_glyphs['?'];
00141 } else {
00142 m_placeholder.surface = 0;
00143 }
00144
00145 mHeight = surface->h;
00146 SDL_FreeSurface(surface);
00147 }
00148
00149
00150 }
00151