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 <SDL.h>
00027 #include <SDL_image.h>
00028 
00029 // FIFE includes
00030 // These includes are split up in two parts, separated by one empty line
00031 // First block: files included from the FIFE root src directory
00032 // Second block: files included from the same folder
00033 #include "util/base/exception.h"
00034 #include "util/structures/rect.h"
00035 #include "util/utf8/utf8.h"
00036 #include "video/image.h"
00037 #include "video/renderbackend.h"
00038 
00039 #include "imagefontbase.h"
00040 
00041 namespace FIFE {
00042 
00043     ImageFontBase::ImageFontBase() : FontBase() {
00044     }
00045 
00046     ImageFontBase::~ImageFontBase() {
00047         type_glyphs::iterator i = m_glyphs.begin();
00048         for(; i != m_glyphs.end(); ++i) {
00049             SDL_FreeSurface(i->second.surface);
00050         }
00051         
00052     }
00053 
00054     int32_t ImageFontBase::getWidth(const std::string& text) const {
00055         int32_t w = 0;
00056         std::string::const_iterator text_it = text.begin();
00057         while(text_it != text.end()) {
00058             uint32_t codepoint = utf8::next(text_it,text.end());
00059             type_glyphs::const_iterator it = m_glyphs.find( codepoint );
00060 
00061             if( it != m_glyphs.end() ) {
00062                 w += it->second.surface->w + getGlyphSpacing();
00063                 continue;
00064             }
00065 
00066             if( m_placeholder.surface ) {
00067                 w += m_placeholder.surface->w + getGlyphSpacing();
00068             }
00069         }
00070         return w;
00071     }
00072 
00073     int32_t ImageFontBase::getHeight() const {
00074         return mHeight;
00075     }
00076 
00077     SDL_Surface *ImageFontBase::renderString(const std::string& text) {
00078         SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
00079             getWidth(text),getHeight(),32,
00080             RMASK, GMASK, BMASK ,AMASK);
00081 
00082         SDL_FillRect(surface,0,0x00000000);
00083 
00084         SDL_Rect dst;
00085         dst.x = dst.y = 0;
00086         s_glyph *glyph = 0;
00087 
00088         std::string::const_iterator text_it = text.begin();
00089         while(text_it != text.end()) {
00090             uint32_t codepoint = utf8::next(text_it,text.end());
00091             type_glyphs::iterator it = m_glyphs.find( codepoint );
00092 
00093             if( it == m_glyphs.end() ) {
00094                 if( !m_placeholder.surface ) {
00095                     continue;
00096                 }
00097                 glyph = &m_placeholder;
00098             } else {
00099                 glyph = &(it->second);
00100             }
00101             dst.y  = glyph->offset.y;
00102             dst.x += glyph->offset.x;
00103 
00104             SDL_BlitSurface(glyph->surface,0,surface,&dst);
00105             dst.x += glyph->surface->w + getGlyphSpacing();
00106         }
00107 
00108         return surface;
00109     }
00110 
00111     void ImageFontBase::setColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
00112     }
00113 }