1 /*      _______   __   __   __   ______   __   __   _______   __   __
2  *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
3  *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
4  *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
5  *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
6  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
7  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
8  *
9  * Copyright (c) 2004, 2005 darkbits                        Js_./
10  * Per Larsson a.k.a finalman                          _RqZ{a<^_aa
11  * Olof Naessén a.k.a jansem/yakslem                _asww7!uY`>  )\a//
12  *                                                 _Qhm`] _f "'c  1!5m
13  * Visit: http://guichan.darkbits.org             )Qk<P ` _: :+' .'  "{[
14  *                                               .)j(] .d_/ '-(  P .   S
15  * License: (BSD)                                <Td/Z <fP"5(\"??"\a.  .L
16  * Redistribution and use in source and          _dV>ws?a-?'      ._/L  #'
17  * binary forms, with or without                 )4d[#7r, .   '     )d`)[
18  * modification, are permitted provided         _Q-5'5W..j/?'   -?!\)cam'
19  * that the following conditions are met:       j<<WP+k/);.        _W=j f
20  * 1. Redistributions of source code must       .$%w\/]Q  . ."'  .  mj$
21  *    retain the above copyright notice,        ]E.pYY(Q]>.   a     J@\
22  *    this list of conditions and the           j(]1u<sE"L,. .   ./^ ]{a
23  *    following disclaimer.                     4'_uomm\.  )L);-4     (3=
24  * 2. Redistributions in binary form must        )_]X{Z('a_"a7'<a"a,  ]"[
25  *    reproduce the above copyright notice,       #}<]m7`Za??4,P-"'7. ).m
26  *    this list of conditions and the            ]d2e)Q(<Q(  ?94   b-  LQ/
27  *    following disclaimer in the                <B!</]C)d_, '(<' .f. =C+m
28  *    documentation and/or other materials      .Z!=J ]e []('-4f _ ) -.)m]'
29  *    provided with the distribution.          .w[5]' _[ /.)_-"+?   _/ <W"
30  * 3. Neither the name of Guichan nor the      :$we` _! + _/ .        j?
31  *    names of its contributors may be used     =3)= _f  (_yQmWW$#(    "
32  *    to endorse or promote products derived     -   W,  sQQQQmZQ#Wwa]..
33  *    from this software without specific        (js, \[QQW$QWW#?!V"".
34  *    prior written permission.                    ]y:.<\..          .
35  *                                                 -]n w/ '         [.
36  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT       )/ )/           !
37  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY         <  (; sac    ,    '
38  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,               ]^ .-  %
39  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF            c <   r
40  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR            aga<  <La
41  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE          5%  )P'-3L
42  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR        _bQf` y`..)a
43  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,          ,J?4P'.P"_(\?d'.,
44  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES               _Pa,)!f/<[]/  ?"
45  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT      _2-..:. .r+_,.. .
46  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     ?a.<%"'  " -'.a_ _,
47  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION)                     ^
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
49  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
51  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
52  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53  */
54 
55 #ifndef GCN_IMAGEFONT_HPP
56 #define GCN_IMAGEFONT_HPP
57 
58 #include <map>
59 #include <string>
60 
61 #include "guichan/font.h"
62 #include "guichan/graphics.h"
63 #include "guichan/image.h"
64 #include "guichan/platform.h"
65 
66 namespace gcn
67 {
68     /**
69      * A font using an image containing the font data. It implements the font
70      * class. You can use any filetype for the font data as long as it can be
71      * loaded with your ImageLoader.
72      *
73      * This are two examples of an image containing a font.
74      *  \image html imagefontexample.bmp
75      *  \image html imagefontexample2.bmp
76      *
77      * The Image font format works like this: The first pixel, the pixal at
78      * coordinate (0,0), tells which color to look for when seperating glyphs.
79      * You create an image with your glyphs and simple separates them with
80      * the seperation color. When you create your ImageFont you supply the
81      * constructor with the glyphs present in your image. When creating an
82      * ImageFont for the image data in the first example above, the following
83      * constructor call would be used.
84      * @code gcn::ImageFont imageFont("fixedfont_big.bmp"," abcdefghijklmno\
85 pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); @endcode
86      * Noteworthy is that the first glyph actually gives the width of space.
87      * Glyphs can, as seen in the second example above, be seperated with
88      * horizontal lines making it possible to draw glyphs on more then one
89      * line in the image. However, these vertical lines must be of one pixel
90      * size!
91      */
92     class GCN_CORE_DECLSPEC ImageFont: public Font
93     {
94     public:
95 
96         /**
97          * Constructor which takes an image file containing the font and
98          * a string containing the glyphs. The glyphs in the string should
99          * be in the same order as they appear in the font image.
100          *
101          * @param filename the filename of the image.
102          * @param glyphs the glyphs found in the image.
103          * @throws Exception when glyph list is incorrect or the font file is
104          *                   corrupt or if no ImageLoader exists.
105          */
106         ImageFont(const std::string& filename, const std::string& glyphs);
107 
108         /**
109          * Constructor which takes an image file containing the font and
110          * two boundaries of ASCII values. The font image should include
111          * all glyphs specified with the boundaries in increasing ASCII
112          * order. The boundaries are inclusive.
113          *
114          * @param filename the filename of the image.
115          * @param glyphsFrom the ASCII value of the first glyph found in the
116          *                   image.
117          * @param glyphsTo the ASCII value of the last glyph found in the
118          *                 image.
119          * @throws Exception when glyph bondaries are incorrect or the font
120          *                   file is corrupt or if no ImageLoader exists.
121          */
122         ImageFont(const std::string& filename, unsigned char glyphsFrom=32, unsigned char glyphsTo=126);
123 
124         /**
125          * Destructor.
126          */
127         virtual ~ImageFont();
128 
129         /**
130          * Draws a glyph.
131          *
132          * NOTE: You normally won't use this function to draw text since
133          *       the Graphics class contains better functions for drawing
134          *       text.
135          *
136          * @param graphics a graphics object to be used for drawing.
137          * @param glyph a glyph to draw.
138          * @param x the x coordinate where to draw the glyph.
139          * @param y the y coordinate where to draw the glyph.
140          * @return the width of the glyph in pixels.
141          * @see Graphics
142          */
143         virtual int drawGlyph(Graphics* graphics, unsigned char glyph, int x, int y);
144 
145         /**
146          * Sets the spacing between rows in pixels.  Default is 0 pixels.
147          * The spacing can be negative.
148          *
149          * @param spacing the spacing in pixels.
150          */
151         virtual void setRowSpacing(int spacing);
152 
153         /**
154          * Gets the spacing between rows in pixels.
155          *
156          * @return the spacing.
157          */
158         virtual int getRowSpacing();
159 
160         /**
161          * Sets the spacing between letters in pixels. Default is 0 pixels.
162          * The spacing can be negative.
163          *
164          * @param spacing the spacing in pixels
165          */
166         virtual void setGlyphSpacing(int spacing);
167 
168         /**
169          * Gets the spacing between letters in pixels.
170          *
171          * @return the spacing.
172          */
173         virtual int getGlyphSpacing();
174 
175         /**
176          * Gets a width of a glyph.
177          *
178          * @param glyph the glyph which width will be returned
179          * @return the width of a glyph
180          */
181         virtual int getWidth(unsigned char glyph) const;
182 
183 
184         // Inherited from Font
185 
186         virtual int getWidth(const std::string& text) const;
187 
188         virtual void drawString(Graphics* graphics, const std::string& text, int x, int y, bool is_normal = true);
189 
190         virtual int getHeight() const;
191 
192         virtual int getStringIndexAt(const std::string& text, int x);
193 
194     protected:
195         void addGlyph(unsigned char c, int &x, int &y, const Color& separator);
196 
197         Rectangle mGlyph[256];
198         int mHeight;
199         int mGlyphSpacing;
200         int mRowSpacing;
201         Image* mImage;
202         std::string mFilename;
203     };
204 }
205 
206 #endif // end GCN_IMAGEFONT_HPP
207