imagefontbase.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <algorithm>
00024
00025
00026 #include <boost/filesystem/convenience.hpp>
00027 #include <boost/scoped_array.hpp>
00028 #include <SDL.h>
00029 #include <SDL_image.h>
00030
00031
00032
00033
00034
00035 #include "util/base/exception.h"
00036 #include "util/structures/rect.h"
00037 #include "util/utf8/utf8.h"
00038 #include "video/image.h"
00039 #include "video/renderbackend.h"
00040
00041 #include "imagefontbase.h"
00042
00043 namespace FIFE {
00044
00045 ImageFontBase::ImageFontBase() : FontBase() {
00046 }
00047
00048 ImageFontBase::~ImageFontBase() {
00049 type_glyphs::iterator i = m_glyphs.begin();
00050 for(; i != m_glyphs.end(); ++i) {
00051 SDL_FreeSurface(i->second.surface);
00052 }
00053
00054 }
00055
00056 int ImageFontBase::getWidth(const std::string& text) const {
00057 int w = 0;
00058 std::string::const_iterator text_it = text.begin();
00059 while(text_it != text.end()) {
00060 uint32_t codepoint = utf8::next(text_it,text.end());
00061 type_glyphs::const_iterator it = m_glyphs.find( codepoint );
00062
00063 if( it != m_glyphs.end() ) {
00064 w += it->second.surface->w + getGlyphSpacing();
00065 continue;
00066 }
00067
00068 if( m_placeholder.surface ) {
00069 w += m_placeholder.surface->w + getGlyphSpacing();
00070 }
00071 }
00072 return w;
00073 }
00074
00075 int ImageFontBase::getHeight() const {
00076 return mHeight;
00077 }
00078
00079 SDL_Surface *ImageFontBase::renderString(const std::string& text) {
00080 SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
00081 getWidth(text),getHeight(),32,
00082 RMASK, GMASK, BMASK ,AMASK);
00083
00084 SDL_FillRect(surface,0,0x00000000);
00085
00086 SDL_Rect dst;
00087 dst.x = dst.y = 0;
00088 s_glyph *glyph = 0;
00089
00090 std::string::const_iterator text_it = text.begin();
00091 while(text_it != text.end()) {
00092 uint32_t codepoint = utf8::next(text_it,text.end());
00093 type_glyphs::iterator it = m_glyphs.find( codepoint );
00094
00095 if( it == m_glyphs.end() ) {
00096 if( !m_placeholder.surface ) {
00097 continue;
00098 }
00099 glyph = &m_placeholder;
00100 } else {
00101 glyph = &(it->second);
00102 }
00103 dst.y = glyph->offset.y;
00104 dst.x += glyph->offset.x;
00105
00106 SDL_BlitSurface(glyph->surface,0,surface,&dst);
00107 dst.x += glyph->surface->w + getGlyphSpacing();
00108 }
00109
00110 return surface;
00111 }
00112
00113 void ImageFontBase::setColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
00114 }
00115 }