FIFE 2008.0
imagefontbase.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 #include <algorithm>
00024 
00025 // 3rd party library includes
00026 #include <boost/filesystem/convenience.hpp>
00027 #include <boost/scoped_array.hpp>
00028 #include <SDL.h>
00029 #include <SDL_image.h>
00030 
00031 // FIFE includes
00032 // These includes are split up in two parts, separated by one empty line
00033 // First block: files included from the FIFE root src directory
00034 // Second block: files included from the same folder
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 }
 All Classes Namespaces Functions Variables Enumerations Enumerator