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