truetypefont.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 <cassert>
00024 
00025 // 3rd party library includes
00026 #include <SDL.h>
00027 
00028 // FIFE includes
00029 // These includes are split up in two parts, separated by one empty line
00030 // First block: files included from the FIFE root src directory
00031 // Second block: files included from the same folder
00032 #include "util/base/exception.h"
00033 #include "util/structures/rect.h"
00034 #include "util/utf8/utf8.h"
00035 #include "video/image.h"
00036 #include "video/renderbackend.h"
00037 
00038 #include "truetypefont.h"
00039 
00040 namespace FIFE {
00041 
00042     TrueTypeFont::TrueTypeFont(const std::string& filename, int size)
00043         : FIFE::FontBase() {
00044         mFilename = filename;
00045         mFont = NULL;
00046 
00047         mFont = TTF_OpenFont(filename.c_str(), size);
00048 
00049         if (mFont == NULL) {
00050             throw FIFE::CannotOpenFile(filename + " (" + TTF_GetError() + ")");
00051         }
00052         mColor.r = mColor.g = mColor.b = 255;
00053     }
00054 
00055     TrueTypeFont::~TrueTypeFont() {
00056         TTF_CloseFont(mFont);
00057     }
00058 
00059     int TrueTypeFont::getWidth(const std::string& text) const {
00060         int w, h;
00061         assert( utf8::is_valid(text.begin(), text.end()) );
00062         TTF_SizeUTF8(mFont, text.c_str(), &w, &h);
00063         return w;
00064     }
00065 
00066     int TrueTypeFont::getHeight() const {
00067         return TTF_FontHeight(mFont) + getRowSpacing();
00068     }
00069 
00070     SDL_Surface* TrueTypeFont::renderString(const std::string& text) {
00071         if( text.empty() ) {
00072             SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
00073                 1,getHeight(),32,
00074                 RMASK, GMASK, BMASK ,AMASK);
00075             SDL_FillRect(surface,0,0x00000000);
00076             return surface;
00077         }
00078 
00079         SDL_Surface* renderedText = 0;
00080         if (m_antiAlias) {
00081             renderedText = TTF_RenderUTF8_Blended(mFont, text.c_str(), mColor);
00082         } else {
00083             renderedText = TTF_RenderUTF8_Solid(mFont, text.c_str(), mColor);
00084         }
00085         // Workaround for a freetype bug, see here:
00086         // http://www.nabble.com/SDL_ttf-and-DPMSDisable-bug-is-back-or-still-there-to9578884.html
00087         if (renderedText == 0 && !m_antiAlias) {
00088             renderedText = TTF_RenderUTF8_Blended(mFont, text.c_str(), mColor);
00089         }
00090         // Still could not render? Something went horribly wrong!
00091         if (renderedText == 0) {
00092             throw FIFE::SDLException(TTF_GetError());
00093         }
00094         return renderedText;
00095     }
00096 
00097     void TrueTypeFont::setColor(Uint8 r, Uint8 g, Uint8 b) {
00098         mColor.r = r;
00099         mColor.g = g;
00100         mColor.b = b;
00101     }
00102 }