FIFE  2008.0
truetypefont.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <cassert>
24 
25 // 3rd party library includes
26 #include <SDL.h>
27 
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src directory
31 // Second block: files included from the same folder
32 #include "util/base/exception.h"
33 #include "util/structures/rect.h"
34 #include "util/utf8/utf8.h"
35 #include "video/image.h"
36 #include "video/renderbackend.h"
37 
38 #include "truetypefont.h"
39 
40 namespace FIFE {
41 
42  TrueTypeFont::TrueTypeFont(const std::string& filename, int32_t size)
43  : FIFE::FontBase() {
44  mFilename = filename;
45  mFont = NULL;
46 
47  mFont = TTF_OpenFont(filename.c_str(), size);
48 
49  if (mFont == NULL) {
50  throw FIFE::CannotOpenFile(filename + " (" + TTF_GetError() + ")");
51  }
52  mColor.r = mColor.g = mColor.b = mColor.unused = 255;
53  }
54 
56  TTF_CloseFont(mFont);
57  }
58 
59  int32_t TrueTypeFont::getWidth(const std::string& text) const {
60  int32_t w, h;
61  assert( utf8::is_valid(text.begin(), text.end()) );
62  TTF_SizeUTF8(mFont, text.c_str(), &w, &h);
63  return w;
64  }
65 
66  int32_t TrueTypeFont::getHeight() const {
67  return TTF_FontHeight(mFont) + getRowSpacing();
68  }
69 
70  SDL_Surface* TrueTypeFont::renderString(const std::string& text) {
71  if( text.empty() ) {
72  SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
73  1,getHeight(),32,
74  RMASK, GMASK, BMASK ,AMASK);
75  SDL_FillRect(surface,0,0x00000000);
76  return surface;
77  }
78 
79  SDL_Surface* renderedText = 0;
80  if (m_antiAlias) {
81  renderedText = TTF_RenderUTF8_Blended(mFont, text.c_str(), mColor);
82  } else {
83  renderedText = TTF_RenderUTF8_Solid(mFont, text.c_str(), mColor);
84  }
85  // Workaround for a freetype bug, see here:
86  // http://www.nabble.com/SDL_ttf-and-DPMSDisable-bug-is-back-or-still-there-to9578884.html
87  if (renderedText == 0 && !m_antiAlias) {
88  renderedText = TTF_RenderUTF8_Blended(mFont, text.c_str(), mColor);
89  }
90  // Still could not render? Something went horribly wrong!
91  if (renderedText == 0) {
92  throw FIFE::SDLException(TTF_GetError());
93  }
94  return renderedText;
95  }
96 
97  void TrueTypeFont::setColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
98  mColor.r = r;
99  mColor.g = g;
100  mColor.b = b;
101  mColor.unused = a;
102  }
103 }
TrueTypeFont(const std::string &filename, int32_t size)
virtual ~TrueTypeFont()
int32_t getRowSpacing() const
Definition: fontbase.cpp:60
virtual int32_t getHeight() const
virtual int32_t getWidth(const std::string &text) const
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...
Definition: soundclip.cpp:39