- added support for variable width fonts (not fully finished, needs some more cleanup...
[supertux.git] / src / video / font.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #ifndef SUPERTUX_FONT_H
21 #define SUPERTUX_FONT_H
22
23 #include <string>
24 #include <stdint.h>
25
26 #include "video/surface.hpp"
27 #include "math/vector.hpp"
28 #include "math/rect.hpp"
29
30 enum FontAlignment {
31   LEFT_ALLIGN,
32   CENTER_ALLIGN,
33   RIGHT_ALLIGN
34 };
35
36 class Font
37 {
38 public:
39   /** Construct a fixed-width font 
40    * 
41    *  @param file image file containing the characters
42    *  @param shadowfile image file containing the characters shadows
43    *  @param w width of a character
44    *  @param h height of a character
45    */
46   Font(const std::string& file, const std::string& shadowfile,
47        int w, int h, int shadowsize = 2);
48
49   /** Construct a variable-width font 
50    * 
51    *  @param file image file containing the characters
52    */
53   Font(const std::string& file, int char_width, int char_height);
54
55   ~Font();
56
57   /** returns the width of a given text. (Note that I won't add a normal
58    * get_width function here, as we might switch to variable width fonts in the
59    * future.)
60    * Supports breaklines.
61    */
62   float get_text_width(const std::string& text) const;
63
64   /** returns the height of a given text. This function supports breaklines.
65    * In case, you are positive that your text doesn't use break lines, you can
66    * just use get_height().
67    */
68   float get_text_height(const std::string& text) const;
69
70   /**
71    * returns the height of the font.
72    */
73   float get_height() const;
74
75   /**
76    * returns the given string, truncated (preferrably at whitespace) to be at most max_chars characters long
77    */
78   static std::string wrap_to_chars(const std::string& text, int max_chars, std::string* overflow);
79
80   /** Draws the given text to the screen. Also needs the position.
81    * Type of alignment, drawing effect and alpha are optional. */
82   void draw(const std::string& text, const Vector& pos,
83             FontAlignment allignment = LEFT_ALLIGN,
84             DrawingEffect drawing_effect = NO_EFFECT,
85             float alpha = 1.0f) const;
86
87 private:
88   friend class DrawingContext;
89
90   void draw_text(const std::string& text, const Vector& pos,
91                  DrawingEffect drawing_effect = NO_EFFECT,
92                  float alpha = 1.0f) const;
93
94   void draw_chars(Surface* pchars, const std::string& text,
95                   const Vector& position, DrawingEffect drawing_effect,
96                   float alpha) const;
97
98   Surface* glyph_surface;
99   Surface* shadow_chars;
100   int char_width;
101   int char_height;
102   int shadowsize;
103
104   /// the number of the first character that is represented in the font
105   uint32_t first_char;
106   /// the number of the last character that is represented in the font
107   uint32_t char_count;
108   
109   /** Location of the characters inside the surface */
110   std::vector<Rect> glyphs;
111 };
112
113 #endif