1 /******************************************************************************
2  *  Warmux is a convivial mass murder game.
3  *  Copyright (C) 2001-2011 Warmux Team.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18  ******************************************************************************/
19 
20 #ifndef FONT_H
21 #define FONT_H
22 #include <SDL.h>
23 #include <SDL_ttf.h>
24 #include <map>
25 #include <WARMUX_base.h>
26 #include "graphic/colors.h"
27 #include "graphic/surface.h"
28 
29 class Font
30 {
31   typedef std::map<std::string, Surface>::value_type txt_sample;
32   typedef std::map<std::string, Surface>::iterator txt_iterator;
33 
34   static bool  LIB_INIT;
35 
36   static std::map<int, Font *> fontMapNormal;
37   static std::map<int, Font *> fontMapBold;
38   static std::map<int, Font *> fontMapItalic;
39 
40   std::map<std::string, Surface> surface_text_table;
41   TTF_Font *m_font;
42   void Write(const Point2i &pos, const Surface &surface) const;
43 
44   Font(int size);
45 
46 public:
47   // Size
48   typedef enum {
49     FONT_HUGE = 40,
50     FONT_LARGE = 32,
51     FONT_BIG = 24,
52     FONT_MEDIUM = 16,
53     FONT_SMALL = 12,
54     FONT_TINY = 10
55   } font_size_t;
56 
57   // Style
58   typedef enum {
59     FONT_NORMAL = 0,
60     FONT_BOLD = 1,
61     FONT_ITALIC = 2,
62   } font_style_t;
63 
64   // type: defined as static consts above
65   static Font * GetInstance(font_size_t size,
66                             font_style_t style = FONT_BOLD);
67   static void ReleaseInstances(void);
68 
69   ~Font();
70 
71   static Surface GenerateSurface(const std::string &txt, const Color &color,
72                                  font_size_t size = FONT_MEDIUM, font_style_t style = FONT_BOLD);
73 
WriteLeft(const Point2i & pos,const std::string & txt,const Color & color)74   void WriteLeft(const Point2i &pos, const std::string &txt, const Color &color)
75   {
76     Surface surface(Render(txt, color, true));
77     Write(pos, surface);
78   }
WriteLeftBottom(const Point2i & pos,const std::string & txt,const Color & color)79   void WriteLeftBottom(const Point2i &pos, const std::string &txt, const Color &color)
80   {
81     Surface surface(Render(txt, color, true));
82     Write(pos - Point2i(0, surface.GetHeight()), surface);
83   }
WriteRight(const Point2i & pos,const std::string & txt,const Color & color)84   void WriteRight(const Point2i &pos, const std::string &txt, const Color &color)
85   {
86     Surface surface(Render(txt, color, true));
87     Write(pos - Point2i(surface.GetWidth(), 0), surface);
88   }
WriteCenterTop(const Point2i & pos,const std::string & txt,const Color & color)89   void WriteCenterTop(const Point2i &pos, const std::string &txt, const Color &color)
90   {
91     Surface surface(Render(txt, color, true));
92     Write(pos - Point2i(surface.GetWidth()>>1, 0), surface);
93   }
WriteCenter(const Point2i & pos,const std::string & txt,const Color & color)94   void WriteCenter(const Point2i &pos, const std::string &txt, const Color &color)
95   {
96     Surface surface(Render(txt, color, true));
97     Write(pos - Point2i(surface.GetWidth()>>1, surface.GetHeight()), surface);
98   }
99 
100   int GetWidth(const std::string &txt) const;
101   int GetHeight() const;
102   int GetHeight(const std::string &txt) const;
GetSize(const std::string & txt)103   Point2i GetSize(const std::string &txt) const { return Point2i(GetWidth(txt), GetHeight(txt)); }
104 
105   Surface Render(const std::string &txt, const Color &color, bool cache=false);
106   Surface CreateSurface(const std::string &txt, const Color &color);
107   void SetBold();
108   void SetItalic();
109 };
110 
111 #endif
112