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 supported by the current 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 Image font format works like this: The first pixel, the pixal at 68 * coordinate (0,0), tells which color to look for when seperating glyphs. 69 * You create an image with your glyphs and simple separates them with 70 * the seperation color. When you create your image font you supply the 71 * constructor with the glyphs present in your image. 72 * 73 * To create an ImageFont from the first image example above the following 74 * constructor call should be made: 75 * @code gcn::ImageFont imageFont("fixedfont_big.bmp"," abcdefghijklmno\ 76 pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); @endcode 77 * 78 * Note worthy is that the first glyph actually gives the width of space. 79 * Glyphs can, as seen in the second image example above, be seperated with 80 * horizontal lines making it possible to draw glyphs on more then one 81 * line in the image. However, these vertical lines must have a height of 82 * one pixel! 83 */ 84 class GCN_CORE_DECLSPEC ImageFont: public Font 85 { 86 public: 87 88 /** 89 * Constructor. Takes an image file containing the font and 90 * a string containing the glyphs. The glyphs in the string should 91 * be in the same order as they appear in the font image. 92 * 93 * @param filename The filename of the image. 94 * @param glyphs The glyphs found in the image. 95 * @throws Exception when glyph list is incorrect or the font file is 96 * corrupt or if no ImageLoader exists. 97 */ 98 ImageFont(const std::string& filename, const std::string& glyphs); 99 100 /** 101 * Constructor. Takes an image containing the font and 102 * a string containing the glyphs. The glyphs in the string should 103 * be in the same order as they appear in the font image. 104 * The image will be deleted in the destructor. 105 * 106 * @param image The image with font glyphs. 107 * @param glyphs The glyphs found in the image. 108 * @throws Exception when glyph list is incorrect or the font image is 109 * is missing. 110 */ 111 ImageFont(Image* image, const std::string& glyphs); 112 113 /** 114 * Constructor. Takes an image file containing the font and 115 * two boundaries of ASCII values. The font image should include 116 * all glyphs specified with the boundaries in increasing ASCII 117 * order. The boundaries are inclusive. 118 * 119 * @param filename The filename of the image. 120 * @param glyphsFrom The ASCII value of the first glyph found in the 121 * image. 122 * @param glyphsTo The ASCII value of the last glyph found in the 123 * image. 124 * @throws Exception when glyph bondaries are incorrect or the font 125 * file is corrupt or if no ImageLoader exists. 126 */ 127 ImageFont(const std::string& filename, 128 unsigned char glyphsFrom=32, 129 unsigned char glyphsTo=126); 130 131 /** 132 * Destructor. 133 */ 134 virtual ~ImageFont(); 135 136 /** 137 * Draws a glyph. 138 * 139 * NOTE: You normally won't use this function to draw text since 140 * the Graphics class contains better functions for drawing 141 * text. 142 * 143 * @param graphics A graphics object used for drawing. 144 * @param glyph A glyph to draw. 145 * @param x The x coordinate where to draw the glyph. 146 * @param y The y coordinate where to draw the glyph. 147 * @return The width of the glyph in pixels. 148 */ 149 virtual int drawGlyph(Graphics* graphics, unsigned char glyph, 150 int x, int y); 151 152 /** 153 * Sets the space between rows in pixels. Default is 0 pixels. 154 * The space can be negative. 155 * 156 * @param spacing The space between rows in pixels. 157 * @see getRowSpacing 158 */ 159 virtual void setRowSpacing(int spacing); 160 161 /** 162 * Gets the space between rows in pixels. 163 * 164 * @return The space between rows in pixels. 165 * @see setRowSpacing 166 */ 167 virtual int getRowSpacing(); 168 169 /** 170 * Sets the spacing between glyphs in pixels. Default is 0 pixels. 171 * The space can be negative. 172 * 173 * @param spacing The glyph space in pixels. 174 * @see getGlyphSpacing 175 */ 176 virtual void setGlyphSpacing(int spacing); 177 178 /** 179 * Gets the spacing between letters in pixels. 180 * 181 * @return the spacing. 182 * @see setGlyphSpacing 183 */ 184 virtual int getGlyphSpacing(); 185 186 /** 187 * Gets a width of a glyph in pixels. 188 * 189 * @param glyph The glyph which width will be returned. 190 * @return The width of a glyph in pixels. 191 */ 192 virtual int getWidth(unsigned char glyph) const; 193 194 195 // Inherited from Font 196 197 virtual int getWidth(const std::string& text) const; 198 199 virtual void drawString(Graphics* graphics, const std::string& text, 200 int x, int y); 201 202 virtual int getHeight() const; 203 204 virtual int getStringIndexAt(const std::string& text, int x); 205 206 protected: 207 /** 208 * Scans for a certain glyph. 209 * 210 * @param glyph The glyph to scan for. Used for exception messages. 211 * @param x The x coordinate where to begin the scan. The coordinate 212 * will be updated with the end x coordinate of the glyph 213 * when the scan is complete. 214 * @param y The y coordinate where to begin the scan. The coordinate 215 * will be updated with the end y coordinate of the glyph 216 * when the scan is complete. 217 * @param separator The color separator to look for where the glyph ends. 218 * @return A rectangle with the found glyph dimension in the image 219 * with the font. 220 * @throws Exception when no glyph is found. 221 */ 222 Rectangle scanForGlyph(unsigned char glyph, 223 int x, 224 int y, 225 const Color& separator); 226 227 /** 228 * Holds the glyphs areas in the image. 229 */ 230 Rectangle mGlyph[256]; 231 232 /** 233 * Holds the height of the image font. 234 */ 235 int mHeight; 236 237 /** 238 * Holds the glyph spacing of the image font. 239 */ 240 int mGlyphSpacing; 241 242 /** 243 * Holds the row spacing of the image font. 244 */ 245 int mRowSpacing; 246 247 /** 248 * Holds the image with the font data. 249 */ 250 Image* mImage; 251 252 /** 253 * Holds the filename of the image with the font data. 254 */ 255 std::string mFilename; 256 }; 257 } 258 259 #endif // end GCN_IMAGEFONT_HPP 260