FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
truetypefont.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
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  mFontStyle = TTF_STYLE_NORMAL;
47 
48  mFont = TTF_OpenFont(filename.c_str(), size);
49 
50  if (mFont == NULL) {
51  throw FIFE::CannotOpenFile(filename + " (" + TTF_GetError() + ")");
52  }
53 
54  mColor.r = mColor.g = mColor.b = mColor.a = 255;
55 
56  // Maybe we should add an setting for that
57  // TTF_HINTING_NORMAL // default
58  // TTF_HINTING_LIGHT
59  // TTF_HINTING_MONO
60  // TTF_HINTING_NONE
61  //TTF_SetFontHinting(mFont, TTF_HINTING_LIGHT);
62 
63  }
64 
66  TTF_CloseFont(mFont);
67  }
68 
69  int32_t TrueTypeFont::getWidth(const std::string& text) const {
70  int32_t w, h;
71  assert( utf8::is_valid(text.begin(), text.end()) );
72  TTF_SizeUTF8(mFont, text.c_str(), &w, &h);
73  return w;
74  }
75 
76  int32_t TrueTypeFont::getHeight() const {
77  return TTF_FontHeight(mFont) + getRowSpacing();
78  }
79 
80  void TrueTypeFont::setBoldStyle(bool style) {
81  if (style != m_boldStyle) {
82  if (style) {
83  mFontStyle |= TTF_STYLE_BOLD;
84  } else {
85  mFontStyle &= ~TTF_STYLE_BOLD;
86  }
87  m_boldStyle = style;
88  TTF_SetFontStyle(mFont, mFontStyle);
89  }
90  }
91 
92  void TrueTypeFont::setItalicStyle(bool style) {
93  if (style != m_italicStyle) {
94  if (style) {
95  mFontStyle |= TTF_STYLE_ITALIC;
96  } else {
97  mFontStyle &= ~TTF_STYLE_ITALIC;
98  }
99  m_italicStyle = style;
100  TTF_SetFontStyle(mFont, mFontStyle);
101  }
102  }
103 
105  if (style != m_underlineStyle) {
106  if (style) {
107  mFontStyle |= TTF_STYLE_UNDERLINE;
108  } else {
109  mFontStyle &= ~TTF_STYLE_UNDERLINE;
110  }
111  m_underlineStyle = style;
112  TTF_SetFontStyle(mFont, mFontStyle);
113  }
114  }
115 
117  // ToDo: It's currently not available.
118  if (style != m_strikeStyle) {
119  m_strikeStyle = style;
120  }
121  }
122 
124  return mFontStyle;
125  }
126 
127  SDL_Surface* TrueTypeFont::renderString(const std::string& text) {
128  if( text.empty() ) {
129  SDL_Surface *surface = SDL_CreateRGBSurface(0,
130  1,getHeight(),32,
131  RMASK, GMASK, BMASK ,AMASK);
132  SDL_FillRect(surface,0,0x00000000);
133  return surface;
134  }
135 
136  SDL_Surface* renderedText = 0;
137  if (m_antiAlias) {
138  renderedText = TTF_RenderUTF8_Blended(mFont, text.c_str(), mColor);
139  } else {
140  renderedText = TTF_RenderUTF8_Solid(mFont, text.c_str(), mColor);
141  }
142  // Workaround for a freetype bug, see here:
143  // http://www.nabble.com/SDL_ttf-and-DPMSDisable-bug-is-back-or-still-there-to9578884.html
144  if (renderedText == 0 && !m_antiAlias) {
145  renderedText = TTF_RenderUTF8_Blended(mFont, text.c_str(), mColor);
146  }
147  // Still could not render? Something went horribly wrong!
148  if (renderedText == 0) {
149  throw FIFE::SDLException(TTF_GetError());
150  }
151  return renderedText;
152  }
153 
155  mColor.r = r;
156  mColor.g = g;
157  mColor.b = b;
158  mColor.a = a;
159  }
160 }
virtual void setItalicStyle(bool style)
Sets the use of italic style.
virtual SDL_Surface * renderString(const std::string &text)
virtual void setStrikethroughStyle(bool style)
Sets the use of strikethrough style.
virtual int32_t getFontStyleMask() const
const uint32_t RMASK
Definition: fife_stdint.h:53
TrueTypeFont(const std::string &filename, int32_t size)
Constructor.
SDL_Color mColor
Definition: fontbase.h:86
bool m_italicStyle
Definition: fontbase.h:93
const uint32_t AMASK
Definition: fife_stdint.h:56
Abstract Font Base Class Uses a pool for rendered strings.
Definition: fontbase.h:48
unsigned char uint8_t
Definition: core.h:38
const uint32_t GMASK
Definition: fife_stdint.h:54
virtual ~TrueTypeFont()
Destructor.
virtual void setBoldStyle(bool style)
Sets the use of bold style.
bool is_valid(octet_iterator start, octet_iterator end)
Definition: core.h:300
virtual void setUnderlineStyle(bool style)
Sets the use of underline style.
int32_t getRowSpacing() const
Gets the spacing between rows in pixels.
Definition: fontbase.cpp:65
bool m_strikeStyle
Definition: fontbase.h:95
TTF_Font * mFont
Definition: truetypefont.h:84
const uint32_t BMASK
Definition: fife_stdint.h:55
virtual int32_t getHeight() const
gets height of this font
bool m_antiAlias
Definition: fontbase.h:91
bool m_underlineStyle
Definition: fontbase.h:94
std::string mFilename
Definition: fontbase.h:90
virtual void setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Set the color the text should be rendered in.
bool m_boldStyle
Definition: fontbase.h:92
virtual int32_t getWidth(const std::string &text) const
gets width of given text