1 /***************************************************************************
2  *      Mechanized Assault and Exploration Reloaded Projectfile            *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 #ifndef unifontsH
20 #define unifontsH
21 
22 #include "utility/autosurface.h"
23 #include <SDL.h>
24 #include <string>
25 #include "defines.h"
26 
27 class cPosition;
28 
29 /** different fonttypes*/
30 enum eUnicodeFontType
31 {
32 	FONT_LATIN_NORMAL,
33 	FONT_LATIN_NORMAL_RED,
34 	FONT_LATIN_BIG,
35 	FONT_LATIN_BIG_GOLD,
36 	FONT_LATIN_SMALL_WHITE,
37 	FONT_LATIN_SMALL_RED,
38 	FONT_LATIN_SMALL_GREEN,
39 	FONT_LATIN_SMALL_YELLOW,
40 };
41 
42 /** different sizes that fonttypes can have*/
43 enum eUnicodeFontSize
44 {
45 	FONT_SIZE_NORMAL,
46 	FONT_SIZE_BIG,
47 	FONT_SIZE_SMALL,
48 };
49 
50 /** different ISO-8559-X charsets*/
51 enum eUnicodeFontCharset
52 {
53 	CHARSET_ISO8559_ALL, // main part of the charsets which is the same in all charsets
54 	CHARSET_ISO8559_1,
55 	CHARSET_ISO8559_2,
56 	CHARSET_ISO8559_3,
57 	CHARSET_ISO8559_4,
58 	CHARSET_ISO8559_5,
59 	CHARSET_ISO8559_6,
60 	CHARSET_ISO8559_7,
61 	CHARSET_ISO8559_8,
62 	CHARSET_ISO8559_9,
63 	CHARSET_ISO8559_10,
64 	CHARSET_ISO8559_11,
65 	CHARSET_ISO8559_12, // doesn't exists but is just a placeholder that the enum-numbers are the same as the iso-numbers
66 	CHARSET_ISO8559_13,
67 	CHARSET_ISO8559_14,
68 	CHARSET_ISO8559_15,
69 	CHARSET_ISO8559_16,
70 };
71 
72 /**
73  * @author alzi alias DoctorDeath
74  * Loads the fontbitmaps from a ISO-8859 structure to a unicode structure
75  * and handles theire output to the screen
76 */
77 class cUnicodeFont
78 {
79 public:
80 	cUnicodeFont();
81 
82 	/**
83 	 * Wrapper for showText for easy use of SDL_Rects
84 	 * @author beko
85 	 * @param rdest destination to start drawing
86 	 * @param sText text to draw
87 	 * @param eBitmapFontType enum of fonttype. LATIN_NORMAL is default
88 	 */
89 	void showText (SDL_Rect rDest, const std::string& sText,
90 				   eUnicodeFontType fonttype = FONT_LATIN_NORMAL);
91 	/**
92 	 * Displays a text
93 	 * @author beko
94 	 * @param x position x to start drawing
95 	 * @param y position y to start drawing
96 	 * @param sText text to draw
97 	 * @param eBitmapFontType enum of fonttype. LATIN_NORMAL is default
98 	 */
99 	void showText (int x, int y, const std::string& sText,
100 				   eUnicodeFontType fonttype = FONT_LATIN_NORMAL);
101 
102 	void showText (const cPosition& position, const std::string& sText,
103 				   eUnicodeFontType fonttype = FONT_LATIN_NORMAL);
104 
105 	/**
106 	 * Displays a text as block.<br><br>
107 	 * This does <b>not</b> allow blanks in line. Linebreaks are interpreted.
108 	 * Unneeded blanks will be snipped.<br><br>
109 	 * Example:
110 	 * "Headline\n\n This is my text for a textblock that get's linebreaked automagically"!
111 	 * @author beko
112 	 * @param rDest SDL_Rect for position and wide of textbox.
113 	 *        Height is not taken care of!
114 	 * @param sText text to draw
115 	 * @param eBitmapFontType enum of fonttype. LATIN_NORMAL is default
116 	 */
117 	int showTextAsBlock (SDL_Rect rDest, const std::string& sText,
118 						 eUnicodeFontType fonttype = FONT_LATIN_NORMAL);
119 	/**
120 	 * Displays a text centered on given X
121 	 * @author beko
122 	 * @param rDest DL_Rect for position.<br>Use X for position to center on.
123 	 *              <br>Y is not taken care of!
124 	 * @param sText text to draw
125 	 * @param eBitmapFontType enum of fonttype. LATIN_NORMAL is default
126 	 */
127 	void showTextCentered (SDL_Rect rDest, const std::string& sText,
128 						   eUnicodeFontType fonttype = FONT_LATIN_NORMAL);
129 	/**
130 	 * Displays a text centered on given X
131 	 * @author beko
132 	 * @param x Use X for position to center on.<br>Y is not taken care of!
133 	 * @param y position y to start drawing
134 	 * @param sText text to draw
135 	 * @param eBitmapFontType enum of fonttype. LATIN_NORMAL is default
136 	 */
137 	void showTextCentered (int x, int y, const std::string& sText,
138 						   eUnicodeFontType fonttype = FONT_LATIN_NORMAL);
139 
140 	void showTextCentered (const cPosition& pos, const std::string& sText,
141 						   eUnicodeFontType fonttype = FONT_LATIN_NORMAL);
142 	/**
143 	 * Calculates the needed width for a text in pixels
144 	 * @author beko
145 	 * @param sText text to check
146 	 * @param eBitmapFontType enum of fonttype. LATIN_NORMAL is default
147 	 * @return needed width for text
148 	 */
149 	int getTextWide (const std::string& sText,
150 					 eUnicodeFontType fonttype = FONT_LATIN_NORMAL);
151 	/**
152 	 * Calculates the needed space for a text in pixels
153 	 * @author beko
154 	 * @param sText text to check
155 	 * @param eBitmapFontType enum of fonttype. LATIN_NORMAL is default
156 	 * @return SDL_Rect with needed width and height for text
157 	 */
158 	SDL_Rect getTextSize (const std::string& sText,
159 						  eUnicodeFontType fonttype = FONT_LATIN_NORMAL);
160 	/**
161 	 * Holds information of font height
162 	 * @author beko
163 	 * @param eBitmapFontType enum of fonttype. LATIN_NORMAL is default
164 	 * @return Height of fonttype in pixels
165 	 */
166 	int getFontHeight (eUnicodeFontType fonttype = FONT_LATIN_NORMAL);
167 	/**
168 	 * Holds information of font size
169 	 * @author alzi
170 	 * @param eBitmapFontType enum of fonttype.
171 	 * @return eUnicodeFontSize enum size of fonttype
172 	 */
173 	eUnicodeFontSize getFontSize (eUnicodeFontType fonttype) const;
174 
175 	std::string shortenStringToSize (const std::string& str, int size,
176 									 eUnicodeFontType fonttype);
177 
setTargetSurface(SDL_Surface * surface)178 	void setTargetSurface (SDL_Surface* surface) { this->surface = surface; }
getTargetSurface()179 	SDL_Surface* getTargetSurface() { return surface; }
180 
181 	/**
182 	 * encodes a UTF-8 character to its unicode position
183 	 * @author alzi alias DoctorDeath
184 	 * @param pch pointer to the character string
185 	 * @param increase number which will be changed to the value
186 	 *        how much bytes the character has taken in UTF-8
187 	 * @return unicode position
188 	 */
189 	Uint16 encodeUTF8Char (const char* pch, int& increase) const;
190 
191 	bool isUtf8Space (const char* pch) const;
192 
193 	int getUnicodeCharacterWidth (Uint16 unicodeCharacter, eUnicodeFontType fonttype) /*const*/;
194 private:
195 	typedef AutoSurface FontTypeSurfaces[0xFFFF];
196 	// character surfaces.
197 	// Since SDL maximal gives us the unicodes
198 	// from BMP we need 0xFFFF surfaces at maximum
199 	AutoSurface charsNormal[0xFFFF];
200 	AutoSurface charsNormalRed[0xFFFF];
201 	AutoSurface charsSmallWhite[0xFFFF];
202 	AutoSurface charsSmallGreen[0xFFFF];
203 	AutoSurface charsSmallRed[0xFFFF];
204 	AutoSurface charsSmallYellow[0xFFFF];
205 	AutoSurface charsBig[0xFFFF];
206 	AutoSurface charsBigGold[0xFFFF];
207 
208 	// target surface where to draw.
209 	SDL_Surface* surface;
210 
211 	/**
212 	 * loads all characters of a ISO table and fonttype.
213 	 * @author beko
214 	 * @param charset the charset which should be loaded.
215 	 * @param fonttype the fonttype which should be loaded.
216 	 */
217 	void loadChars (eUnicodeFontCharset charset, eUnicodeFontType fonttype);
218 	/**
219 	 * returns the character array of a fonttype.
220 	 * @author alzi alias DoctorDeath
221 	 * @param fonttype the fonttype of which the character
222 	 *        array should be returned.
223 	 * @return the character array for the fonttype.
224 	 */
225 	FontTypeSurfaces* getFontTypeSurfaces (eUnicodeFontType fonttype);
226 	/**
227 	 * loads the ISO-8859 bitmap font surface
228 	 * @author alzi alias DoctorDeath
229 	 * @param charset the charset which bitmap should be loaded.
230 	 * @param fonttype the fonttype which bitmap should be loaded.
231 	 * @return the bitmap surface
232 	 */
233 	AutoSurface loadCharsetSurface (eUnicodeFontCharset charset, eUnicodeFontType fonttype);
234 	/**
235 	 * returns the iso page with the unicode positions of the characters
236 	 * in a ISO-8859 font
237 	 * @author alzi alias DoctorDeath
238 	 * @param charset the charset for that the iso page should be returned.
239 	 * @return the iso page
240 	 */
241 	const unsigned short* getIsoPage (eUnicodeFontCharset charset);
242 	int drawWithBreakLines (SDL_Rect rDest, const std::string& sText,
243 							eUnicodeFontType fonttype);
244 };
245 
246 EX cUnicodeFont* font;
247 
248 #endif
249