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 //
11 /**@name include/font.h - The font headerfile. */
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 
33 //@{
34 
35 /*----------------------------------------------------------------------------
36 --  Documentation
37 ----------------------------------------------------------------------------*/
38 
39 /**
40 **  @class CFont font.h
41 **
42 **  \#include "font.h"
43 **
44 **  Defines the fonts used in the Stratagus engine. We support
45 **  proportional multicolor fonts of 9 colors.
46 **  (Currently the fonts aren't packed)
47 **
48 **  CFont::CharWidth[]
49 **
50 **    The width of each font glyph in pixels. The index 0 is the
51 **    width of the SPACE (' ', 0x20).
52 **
53 **  CFont::G
54 **
55 **    Contains the graphics of the font, Only 9 colors are supported.
56 */
57 
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 
72 /// Font definition
73 class CFont : public gcn::Font
74 {
75 private:
CFont(const std::string & ident)76 	explicit CFont(const std::string &ident) :
77 		Ident(ident),
78 		CharWidth(nullptr),
79 		G(nullptr)
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 
getHeight()93 	virtual int getHeight() const { return Height(); }
getWidth(const std::string & text)94 	virtual int getWidth(const std::string &text) const { return Width(text); }
95 	//Wyrmgus start
96 //	virtual void drawString(gcn::Graphics *graphics, const std::string &text, int x, int y);
97 	virtual void drawString(gcn::Graphics *graphics, const std::string &text, int x, int y, bool is_normal = true);
98 	//Wyrmgus end
99 
100 	void Load();
101 	void Reload() const;
102 #if defined(USE_OPENGL) || defined(USE_GLES)
103 	void FreeOpenGL();
104 #endif
105 	void Clean();
106 
107 	CGraphic *GetFontColorGraphic(const CFontColor &fontColor) const;
108 
109 	template<bool CLIP>
110 	unsigned int DrawChar(CGraphic &g, int utf8, int x, int y, const CFontColor &fc) const;
111 
112 	void DynamicLoad() const;
113 
114 private:
115 #if defined(USE_OPENGL) || defined(USE_GLES)
116 	void MakeFontColorTextures() const;
117 #endif
118 	void MeasureWidths();
119 
120 private:
121 	std::string Ident;    /// Ident of the font.
122 	char *CharWidth;      /// Real font width (starting with ' ')
123 	CGraphic *G;          /// Graphic object used to draw
124 };
125 
126 #define MaxFontColors 9
127 
128 /// Font color definition
129 class CFontColor
130 {
131 public:
132 	explicit CFontColor(const std::string &ident);
133 	~CFontColor();
134 
135 	static CFontColor *New(const std::string &ident);
136 	static CFontColor *Get(const std::string &ident);
137 
138 	std::string Ident;
139 	CColor Colors[MaxFontColors];
140 };
141 
142 /*----------------------------------------------------------------------------
143 --  Definitions
144 ----------------------------------------------------------------------------*/
145 
146 /**
147 **  FIXME: should be moved to lua
148 */
149 #define FontRed "red"
150 #define FontGreen "green"
151 #define FontYellow "yellow"
152 #define FontWhite "white"
153 #define FontGrey "grey"
154 
155 /*----------------------------------------------------------------------------
156 --  Functions
157 ----------------------------------------------------------------------------*/
158 
159 /**
160 **  Font selector for the font functions.
161 **  FIXME: should be moved to lua
162 */
163 extern CFont &GetSmallFont();  /// Small font used in stats
164 extern CFont &GetGameFont();   /// Normal font used in game
165 extern bool IsGameFontReady(); /// true when GameFont is provided
166 
167 
168 /// Set the default text colors for normal and reverse text
169 extern void SetDefaultTextColors(const std::string &normal, const std::string &reverse);
170 /// Get the default text colors for normal and reverse text
171 extern void GetDefaultTextColors(std::string &normalp, std::string &reversep);
172 ///  Return the 'line' line of the string 's'.
173 extern std::string GetLineFont(unsigned int line, const std::string &s, unsigned int maxlen, const CFont *font);
174 
175 /// Get the hot key from a string
176 extern int GetHotKey(const std::string &text);
177 
178 /// Load and initialize the fonts
179 extern void LoadFonts();
180 
181 #if defined(USE_OPENGL) || defined(USE_GLES)
182 /// Free OpenGL fonts
183 extern void FreeOpenGLFonts();
184 /// Reload OpenGL fonts
185 extern void ReloadFonts();
186 #endif
187 
188 /// Cleanup the font module
189 extern void CleanFonts();
190 
191 class CLabel
192 {
193 public:
CLabel(const CFont & f,const std::string & nc,const std::string & rc)194 	CLabel(const CFont &f, const std::string &nc, const std::string &rc): font(&f)
195 	{
196 		normal = CFontColor::Get(nc);
197 		reverse = CFontColor::Get(rc);
198 	}
199 	explicit CLabel(const CFont &f);
200 
Height()201 	int Height() const { return font->Height(); }
202 
SetFont(const CFont & f)203 	void SetFont(const CFont &f) { font = &f; }
204 
SetNormalColor(const std::string & nc)205 	void SetNormalColor(const std::string &nc) { normal = CFontColor::Get(nc); }
206 
207 	/// Draw text/number unclipped
208 	int Draw(int x, int y, const char *const text) const;
209 	int Draw(int x, int y, const std::string &text) const;
210 	int Draw(int x, int y, int number) const;
211 	/// Draw text/number clipped
212 	int DrawClip(int x, int y, const char *const text) const;
213 	//Wyrmgus start
214 //	int DrawClip(int x, int y, const std::string &text) const;
215 	int DrawClip(int x, int y, const std::string &text, bool is_normal = true) const;
216 	//Wyrmgus end
217 	int DrawClip(int x, int y, int number) const;
218 	/// Draw reverse text/number unclipped
219 	int DrawReverse(int x, int y, const char *const text) const;
220 	int DrawReverse(int x, int y, const std::string &text) const;
221 	int DrawReverse(int x, int y, int number) const ;
222 	/// Draw reverse text/number clipped
223 	int DrawReverseClip(int x, int y, const char *const text) const;
224 	int DrawReverseClip(int x, int y, const std::string &text) const;
225 	int DrawReverseClip(int x, int y, int number) const;
226 
227 	int DrawCentered(int x, int y, const std::string &text) const;
228 	int DrawReverseCentered(int x, int y, const std::string &text) const;
229 private:
230 	template <const bool CLIP>
231 	int DoDrawText(int x, int y, const char *const text,
232 				   const size_t len, const CFontColor *fc) const;
233 private:
234 	const CFontColor *normal;
235 	const CFontColor *reverse;
236 	const CFont *font;
237 };
238 
239 //@}
240 
241 #endif // !__FONT_H__
242