comparison ext/guichan-0.8.2/include/guichan/imagefont.hpp @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 /* _______ __ __ __ ______ __ __ _______ __ __
2 * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
3 * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
4 * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
5 * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
6 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
7 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
8 *
9 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
10 *
11 *
12 * Per Larsson a.k.a finalman
13 * Olof Naessén a.k.a jansem/yakslem
14 *
15 * Visit: http://guichan.sourceforge.net
16 *
17 * License: (BSD)
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
26 * distribution.
27 * 3. Neither the name of Guichan nor the names of its contributors may
28 * be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
37 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 #ifndef GCN_IMAGEFONT_HPP
45 #define GCN_IMAGEFONT_HPP
46
47 #include <string>
48
49 #include "guichan/font.hpp"
50 #include "guichan/platform.hpp"
51 #include "guichan/rectangle.hpp"
52
53 namespace gcn
54 {
55 class Color;
56 class Graphics;
57 class Image;
58
59 /**
60 * A font using an image containing the font data. ImageFont can be used
61 * with any image supported by the currently used ImageLoader.
62 *
63 * These are two examples of an image containing a font.
64 * \image html imagefontexample.bmp
65 * \image html imagefontexample2.bmp
66 *
67 * The first pixel at coordinate (0,0) tells which color the image font
68 * looks for when seperating glyphs. The glyphs in the image is provided
69 * to the image font's constructor in the order they appear in the image.
70 *
71 * To create an ImageFont from the first image example above the following
72 * constructor call should be made:
73 * @code gcn::ImageFont imageFont("fixedfont_big.bmp"," abcdefghijklmno\
74 pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); @endcode
75 *
76 * Noteworthy is that the first glyph actually gives the width of space.
77 * Glyphs can, as seen in the second image example above, be seperated with
78 * horizontal lines making it possible to draw glyphs on more then one
79 * line in the image. However, these horizontal lines must have a height of
80 * one pixel!
81 */
82 class GCN_CORE_DECLSPEC ImageFont: public Font
83 {
84 public:
85
86 /**
87 * Constructor. Takes an image file containing the font and
88 * a string containing the glyphs. The glyphs in the string should
89 * be in the same order as they appear in the font image.
90 *
91 * @param filename The filename of the image.
92 * @param glyphs The glyphs found in the image.
93 * @throws Exception when glyph list is incorrect or the font file is
94 * corrupt or if no ImageLoader exists.
95 */
96 ImageFont(const std::string& filename, const std::string& glyphs);
97
98 /**
99 * Constructor. Takes an image containing the font and
100 * a string containing the glyphs. The glyphs in the string should
101 * be in the same order as they appear in the font image.
102 * The image will be deleted in the destructor.
103 *
104 * @param image The image with font glyphs.
105 * @param glyphs The glyphs found in the image.
106 * @throws Exception when glyph list is incorrect or the font image is
107 * is missing.
108 */
109 ImageFont(Image* image, const std::string& glyphs);
110
111 /**
112 * Constructor. Takes an image file containing the font and
113 * two boundaries of ASCII values. The font image should include
114 * all glyphs specified with the boundaries in increasing ASCII
115 * order. The boundaries are inclusive.
116 *
117 * @param filename The filename of the image.
118 * @param glyphsFrom The ASCII value of the first glyph found in the
119 * image.
120 * @param glyphsTo The ASCII value of the last glyph found in the
121 * image.
122 * @throws Exception when glyph bondaries are incorrect or the font
123 * file is corrupt or if no ImageLoader exists.
124 */
125 ImageFont(const std::string& filename,
126 unsigned char glyphsFrom=32,
127 unsigned char glyphsTo=126);
128
129 /**
130 * Destructor.
131 */
132 virtual ~ImageFont();
133
134 /**
135 * Draws a glyph.
136 *
137 * NOTE: You normally won't use this function to draw text since
138 * the Graphics class contains better functions for drawing
139 * text.
140 *
141 * @param graphics A graphics object used for drawing.
142 * @param glyph A glyph to draw.
143 * @param x The x coordinate where to draw the glyph.
144 * @param y The y coordinate where to draw the glyph.
145 * @return The width of the glyph in pixels.
146 */
147 virtual int drawGlyph(Graphics* graphics, unsigned char glyph,
148 int x, int y);
149
150 /**
151 * Sets the space between rows in pixels. Default is 0 pixels.
152 * The space can be negative.
153 *
154 * @param spacing The space between rows in pixels.
155 * @see getRowSpacing
156 */
157 virtual void setRowSpacing(int spacing);
158
159 /**
160 * Gets the space between rows in pixels.
161 *
162 * @return The space between rows in pixels.
163 * @see setRowSpacing
164 */
165 virtual int getRowSpacing();
166
167 /**
168 * Sets the spacing between glyphs in pixels. Default is 0 pixels.
169 * The space can be negative.
170 *
171 * @param spacing The glyph space in pixels.
172 * @see getGlyphSpacing
173 */
174 virtual void setGlyphSpacing(int spacing);
175
176 /**
177 * Gets the spacing between letters in pixels.
178 *
179 * @return the spacing.
180 * @see setGlyphSpacing
181 */
182 virtual int getGlyphSpacing();
183
184 /**
185 * Gets a width of a glyph in pixels.
186 *
187 * @param glyph The glyph which width will be returned.
188 * @return The width of a glyph in pixels.
189 */
190 virtual int getWidth(unsigned char glyph) const;
191
192
193 // Inherited from Font
194
195 virtual int getWidth(const std::string& text) const;
196
197 virtual void drawString(Graphics* graphics, const std::string& text,
198 int x, int y);
199
200 virtual int getHeight() const;
201
202 virtual int getStringIndexAt(const std::string& text, int x) const;
203
204 protected:
205 /**
206 * Scans for a certain glyph.
207 *
208 * @param glyph The glyph to scan for. Used for exception messages.
209 * @param x The x coordinate where to begin the scan. The coordinate
210 * will be updated with the end x coordinate of the glyph
211 * when the scan is complete.
212 * @param y The y coordinate where to begin the scan. The coordinate
213 * will be updated with the end y coordinate of the glyph
214 * when the scan is complete.
215 * @param separator The color separator to look for where the glyph ends.
216 * @return A rectangle with the found glyph dimension in the image
217 * with the font.
218 * @throws Exception when no glyph is found.
219 */
220 Rectangle scanForGlyph(unsigned char glyph,
221 int x,
222 int y,
223 const Color& separator);
224
225 /**
226 * Holds the glyphs areas in the image.
227 */
228 Rectangle mGlyph[256];
229
230 /**
231 * Holds the height of the image font.
232 */
233 int mHeight;
234
235 /**
236 * Holds the glyph spacing of the image font.
237 */
238 int mGlyphSpacing;
239
240 /**
241 * Holds the row spacing of the image font.
242 */
243 int mRowSpacing;
244
245 /**
246 * Holds the image with the font data.
247 */
248 Image* mImage;
249
250 /**
251 * Holds the filename of the image with the font data.
252 */
253 std::string mFilename;
254 };
255 }
256
257 #endif // end GCN_IMAGEFONT_HPP