_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

font.h
Go to the documentation of this file.
1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
12 //
13 // (c) Copyright 1998-2005 by Lutz Sammer and Jimmy Salmon
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29 
30 #ifndef __FONT_H__
31 #define __FONT_H__
32 
34 
35 /*----------------------------------------------------------------------------
36 -- Documentation
37 ----------------------------------------------------------------------------*/
38 
58 /*----------------------------------------------------------------------------
59 -- Includes
60 ----------------------------------------------------------------------------*/
61 
62 #include <string>
63 #include "color.h"
64 #include "guichan/font.h"
65 
66 /*----------------------------------------------------------------------------
67 -- Declarations
68 ----------------------------------------------------------------------------*/
69 class CGraphic;
70 class CFontColor;
71 
73 class CFont : public gcn::Font
74 {
75 private:
76  explicit CFont(const std::string &ident) :
77  Ident(ident),
78  CharWidth(NULL),
79  G(NULL)
80  {}
81 
82 public:
83  virtual ~CFont();
84 
85  static CFont *New(const std::string &ident, CGraphic *g);
86  static CFont *Get(const std::string &ident);
87 
88  int Height() const;
89  int Width(const std::string &text) const;
90  int Width(const int number) const;
91  bool IsLoaded() const;
92 
93  virtual int getHeight() const { return Height(); }
94  virtual int getWidth(const std::string &text) const { return Width(text); }
95  virtual void drawString(gcn::Graphics *graphics, const std::string &text, int x, int y, bool is_normal = true);
96 
97  void Load();
98  void Reload() const;
99  void Clean();
100 
101  CGraphic *GetFontColorGraphic(const CFontColor &fontColor) const;
102 
103  template<bool CLIP>
104  unsigned int DrawChar(CGraphic &g, int utf8, int x, int y, const CFontColor &fc) const;
105 
106  void DynamicLoad() const;
107 
108 private:
109  void MeasureWidths();
110 
111 private:
112  std::string Ident;
113  char *CharWidth;
114  CGraphic *G;
115 };
116 
117 #define MaxFontColors 9
118 
121 {
122 public:
123  explicit CFontColor(const std::string &ident);
124  ~CFontColor();
125 
126  static CFontColor *New(const std::string &ident);
127  static CFontColor *Get(const std::string &ident);
128 
129  std::string Ident;
130  SDL_Color *Colors;
131 };
132 
133 /*----------------------------------------------------------------------------
134 -- Definitions
135 ----------------------------------------------------------------------------*/
136 
140 #define FontRed "red"
141 #define FontGreen "green"
142 #define FontYellow "yellow"
143 #define FontWhite "white"
144 #define FontGrey "grey"
145 
146 /*----------------------------------------------------------------------------
147 -- Functions
148 ----------------------------------------------------------------------------*/
149 
154 extern CFont &GetSmallFont();
155 extern CFont &GetGameFont();
156 extern bool IsGameFontReady();
157 
158 extern int FontCodePage;
159 
161 extern void SetDefaultTextColors(const std::string &normal, const std::string &reverse);
163 extern void GetDefaultTextColors(std::string &normalp, std::string &reversep);
165 extern std::string GetLineFont(unsigned int line, const std::string &s, unsigned int maxlen, const CFont *font);
166 
168 extern int GetHotKey(const std::string &text);
169 
171 extern void LoadFonts();
172 
174 extern void CleanFonts();
175 
176 class CLabel
177 {
178 public:
179  CLabel(const CFont &f, const std::string &nc, const std::string &rc): font(&f)
180  {
181  normal = CFontColor::Get(nc);
182  reverse = CFontColor::Get(rc);
183  }
184  explicit CLabel(const CFont &f);
185 
186  int Height() const { return font->Height(); }
187 
188  void SetFont(const CFont &f) { font = &f; }
189 
190  void SetNormalColor(const std::string &nc) { normal = CFontColor::Get(nc); }
191 
193  int Draw(int x, int y, const char *const text) const;
194  int Draw(int x, int y, const std::string &text) const;
195  int Draw(int x, int y, int number) const;
197  int DrawClip(int x, int y, const char *const text) const;
198  int DrawClip(int x, int y, const std::string &text, bool is_normal = true) const;
199  int DrawClip(int x, int y, int number) const;
201  int DrawReverse(int x, int y, const char *const text) const;
202  int DrawReverse(int x, int y, const std::string &text) const;
203  int DrawReverse(int x, int y, int number) const ;
205  int DrawReverseClip(int x, int y, const char *const text) const;
206  int DrawReverseClip(int x, int y, const std::string &text) const;
207  int DrawReverseClip(int x, int y, int number) const;
208 
209  int DrawCentered(int x, int y, const std::string &text) const;
210  int DrawReverseCentered(int x, int y, const std::string &text) const;
211 private:
212  template <const bool CLIP>
213  int DoDrawText(int x, int y, const char *const text,
214  const size_t len, const CFontColor *fc) const;
215 private:
216  const CFontColor *normal;
217  const CFontColor *reverse;
218  const CFont *font;
219 };
220 
222 
223 #endif // !__FONT_H__
SetDefaultTextColors
void SetDefaultTextColors(const std::string &normal, const std::string &reverse)
Set the default text colors for normal and reverse text.
Definition: font.cpp:152
LoadFonts
void LoadFonts()
Load and initialize the fonts.
Definition: font.cpp:932
CFont::getWidth
virtual int getWidth(const std::string &text) const
Definition: font.h:94
CLabel::DrawReverse
int DrawReverse(int x, int y, const char *const text) const
Draw reverse text/number unclipped.
Definition: font.cpp:709
gcn::Graphics
Definition: graphics.h:97
CFont::drawString
virtual void drawString(gcn::Graphics *graphics, const std::string &text, int x, int y, bool is_normal=true)
Definition: font.cpp:105
CLabel::SetFont
void SetFont(const CFont &f)
Definition: font.h:188
CleanFonts
void CleanFonts()
Cleanup the font module.
Definition: font.cpp:1066
CFont::Get
static CFont * Get(const std::string &ident)
Definition: font.cpp:999
CFontColor::Get
static CFontColor * Get(const std::string &ident)
Definition: font.cpp:1050
CLabel::SetNormalColor
void SetNormalColor(const std::string &nc)
Definition: font.h:190
CFontColor::Ident
std::string Ident
Definition: font.h:129
CFont::IsLoaded
bool IsLoaded() const
Definition: font.cpp:392
GetLineFont
std::string GetLineFont(unsigned int line, const std::string &s, unsigned int maxlen, const CFont *font)
Return the 'line' line of the string 's'.
Definition: font.cpp:845
CLabel::DrawCentered
int DrawCentered(int x, int y, const std::string &text) const
Definition: font.cpp:744
CFont::getHeight
virtual int getHeight() const
Definition: font.h:93
CFont::New
static CFont * New(const std::string &ident, CGraphic *g)
Definition: font.cpp:978
FontCodePage
int FontCodePage
true when GameFont is provided
Definition: font.cpp:218
CLabel
Definition: font.h:176
CFontColor::Colors
SDL_Color * Colors
Definition: font.h:130
GetGameFont
CFont & GetGameFont()
Small font used in stats.
Definition: font.cpp:91
CFontColor::CFontColor
CFontColor(const std::string &ident)
Definition: font.cpp:1014
CFontColor::~CFontColor
~CFontColor()
Definition: font.cpp:1021
CFontColor::New
static CFontColor * New(const std::string &ident)
Definition: font.cpp:1033
CFont::Width
int Width(const std::string &text) const
Definition: font.cpp:428
IsGameFontReady
bool IsGameFontReady()
Normal font used in game.
Definition: font.cpp:86
CFont::Clean
void Clean()
Definition: font.cpp:1059
CFont::GetFontColorGraphic
CGraphic * GetFontColorGraphic(const CFontColor &fontColor) const
Definition: font.cpp:539
GetDefaultTextColors
void GetDefaultTextColors(std::string &normalp, std::string &reversep)
Get the default text colors for normal and reverse text.
Definition: font.cpp:166
CLabel::DrawClip
int DrawClip(int x, int y, const char *const text) const
Draw text/number clipped.
Definition: font.cpp:683
CFont::DrawChar
unsigned int DrawChar(CGraphic &g, int utf8, int x, int y, const CFontColor &fc) const
Definition: font.cpp:518
CLabel::CLabel
CLabel(const CFont &f, const std::string &nc, const std::string &rc)
Definition: font.h:179
CFont::Load
void Load()
Definition: font.cpp:908
gcn::Font
Definition: font.h:69
CFont::Reload
void Reload() const
Definition: font.cpp:944
CFont::~CFont
virtual ~CFont()
Definition: font.cpp:486
GetSmallFont
CFont & GetSmallFont()
Definition: font.cpp:77
CFont::DynamicLoad
void DynamicLoad() const
Definition: font.cpp:920
CLabel::Draw
int Draw(int x, int y, const char *const text) const
Draw text/number unclipped.
Definition: font.cpp:665
CLabel::DrawReverseCentered
int DrawReverseCentered(int x, int y, const std::string &text) const
Definition: font.cpp:751
CLabel::Height
int Height() const
Definition: font.h:186
GetHotKey
int GetHotKey(const std::string &text)
Get the hot key from a string.
Definition: font.cpp:472
font.h
color.h
CFontColor
Font color definition.
Definition: font.h:120
CLabel::DrawReverseClip
int DrawReverseClip(int x, int y, const char *const text) const
Draw reverse text/number clipped.
Definition: font.cpp:727
CFont
Font definition.
Definition: font.h:73
CGraphic
Definition: video.h:91
CFont::Height
int Height() const
Definition: font.cpp:386
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.