File: font.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (232 lines) | stat: -rw-r--r-- 6,120 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************/

#include <iostream>

#include "include/app.h"
#include "game/config.h"
#include "graphic/font.h"
#include "graphic/video.h"
#include "map/map.h"
#include <WARMUX_file_tools.h>

bool  Font::LIB_INIT = false;

std::map<int, Font *> Font::fontMapNormal;
std::map<int, Font *> Font::fontMapBold;
std::map<int, Font *> Font::fontMapItalic;

Font* Font::GetInstance(font_size_t _fontSize,
                        font_style_t fontStyle)
{
  int fontSize = (int)_fontSize;

  if (!LIB_INIT && TTF_Init() == -1) {
    Error(Format("Initialisation of TTF library failed: %s", TTF_GetError()));
    exit(1);
  }
  LIB_INIT = true;

  Font * font = NULL;

  switch (fontStyle) {
    case FONT_BOLD :
      if (!fontMapBold.count(fontSize)) {
        font = new Font(fontSize);
        font->SetBold();
        fontMapBold[fontSize] = font;
      } else {
        return fontMapBold[fontSize];
      }
      break;
    case FONT_ITALIC :
      if (!fontMapItalic.count(fontSize)) {
        font = new Font(fontSize);
        font->SetItalic();
        fontMapItalic[fontSize] = font;
      } else {
        return fontMapItalic[fontSize];
      }
      break;
    case FONT_NORMAL :
      if (!fontMapNormal.count(fontSize)) {
        font = new Font(fontSize);
        fontMapNormal[fontSize] = font;
      } else {
        return fontMapNormal[fontSize];
      }
      break;
  }
  return font;
}

Font::Font(int size):
  surface_text_table(),
  m_font(NULL)
{
  const std::string filename = Config::GetConstInstance()->GetTtfFilename();

  if (DoesFileExist(filename)) {
    m_font = TTF_OpenFont(filename.c_str(), size);
    if (!m_font)
      Error(Format("Error in font file %s (size:%d): %s", filename.c_str(), size, TTF_GetError()));
  } else {
    Error(Format("Can't find font file '%s'", filename.c_str()));
  }

  TTF_SetFontStyle(m_font, TTF_STYLE_NORMAL);
}

Font::~Font()
{
  if (m_font != NULL) {
    TTF_CloseFont(m_font);
    m_font = NULL;
  }

  txt_iterator it;

  // Fix bug #10866 and also fix memory leak.
  surface_text_table.clear();
}

void Font::ReleaseInstances(void)
{
  std::map<int, Font *>::iterator fontMapIte;

  for (fontMapIte = fontMapNormal.begin();
       fontMapIte != fontMapNormal.end();
       ++fontMapIte) {
    delete fontMapIte->second;
  }
  fontMapNormal.clear();

  for (fontMapIte = fontMapBold.begin();
       fontMapIte != fontMapBold.end();
       ++fontMapIte) {
    delete fontMapIte->second;
  }
  fontMapBold.clear();

  for (fontMapIte = fontMapItalic.begin();
       fontMapIte != fontMapItalic.end();
       ++fontMapIte) {
    delete fontMapIte->second;
  }
  fontMapItalic.clear();

  TTF_Quit();
  LIB_INIT = false;
}

void Font::SetBold()
{
  TTF_SetFontStyle(m_font, TTF_STYLE_BOLD);
}

void Font::SetItalic()
{
  TTF_SetFontStyle(m_font, TTF_STYLE_ITALIC);
}

void Font::Write(const Point2i & pos,
                 const Surface & surface) const
{
  GetMainWindow().Blit(surface, pos);

  // TODO: Remove this line! (and use GameFont instead of Font)
  GetWorld().ToRedrawOnScreen( Rectanglei(pos, surface.GetSize()) );
}

Surface Font::CreateSurface(const std::string & txt,
                            const Color & color)
{
#ifdef HAVE_HANDHELD // would be HAVE_HANDHELD if ANDROID didn't misteriously crash because of it
  SDL_Surface *surf = TTF_RenderUTF8_Solid(m_font, txt.c_str(), color.GetSDLColor());
#else
  SDL_Surface *surf = TTF_RenderUTF8_Blended(m_font, txt.c_str(), color.GetSDLColor());
#endif

  if (!surf) {
    // SDL_ttf or freetype might be missing some feature, report it
    Error(Format("Unable to render text: %s", TTF_GetError()));
  }
  
#ifdef HAVE_HANDHELD
  return Surface(surf).DisplayFormat();
#else
  return Surface(surf).DisplayFormatAlpha();
#endif
}

Surface Font::Render(const std::string & txt,
                     const Color & color,
                     bool cache)
{
  Surface surface;

  if (cache) {
    txt_iterator p = surface_text_table.find(txt);
    if( p == surface_text_table.end() ){
      if( surface_text_table.size() > 5 ){
        //SDL_FreeSurface( surface_text_table.begin()->second );
        surface_text_table.erase( surface_text_table.begin() );
      }
      surface = CreateSurface(txt, color);
      surface_text_table.insert( txt_sample(txt, surface) );
    } else {
      txt_iterator p2 = surface_text_table.find( txt );
      surface = p2->second;
    }
  } else {
    surface = CreateSurface(txt, color);
  }

  ASSERT( !surface.IsNull() );
  return surface;
}

int Font::GetWidth(const std::string & txt) const
{
  int width=-1;

  TTF_SizeUTF8(m_font, txt.c_str(), &width, NULL);

  return width;
}

int Font::GetHeight() const
{
  return TTF_FontHeight(m_font);
}

int Font::GetHeight(const std::string & str) const
{
  int height = -1;
  TTF_SizeUTF8(m_font, str.c_str(), NULL, &height);
  return height;
}

Surface Font::GenerateSurface(const std::string & txt,
                              const Color & color,
                              font_size_t font_size,
                              font_style_t font_style)
{
  return Surface(Font::GetInstance(font_size, font_style)->CreateSurface(txt, color));
}