restore trunk
[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 class Renderer;
32
33 enum FontAlignment {
34   ALIGN_LEFT,
35   ALIGN_CENTER,
36   ALIGN_RIGHT
37 };
38
39 class Font
40 {
41 public:
42   enum GlyphWidth {
43     FIXED,
44     VARIABLE
45   };
46
47   /** Construct a fixed-width font
48    *
49    *  @param glyph_width  VARIABLE for proportional fonts, VARIABLE for monospace ones
50    *  @param filename     image file containing the characters
51    *  @param shadowfile   image file containing the characters shadows
52    *  @param char_width   width of a character
53    *  @param char_height  height of a character
54    */
55   Font(GlyphWidth glyph_width,
56        const std::string& filename, const std::string& shadowfile,
57        int char_width, int char_height, int shadowsize = 2);
58   ~Font();
59
60   /** returns the width of a given text. (Note that I won't add a normal
61    * get_width function here, as we might switch to variable width fonts in the
62    * future.)
63    * Supports breaklines.
64    */
65   float get_text_width(const std::string& text) const;
66
67   /** returns the height of a given text. This function supports breaklines.
68    * In case, you are positive that your text doesn't use break lines, you can
69    * just use get_height().
70    */
71   float get_text_height(const std::string& text) const;
72
73   /**
74    * returns the height of the font.
75    */
76   float get_height() const;
77
78   /**
79    * returns the given string, truncated (preferrably at whitespace) to be at most max_chars characters long
80    */
81   static std::string wrap_to_chars(const std::string& text, int max_chars, std::string* overflow);
82
83   /**
84    * returns the given string, truncated (preferrably at whitespace) to be at most "width" pixels wide
85    */
86   std::string wrap_to_width(const std::string& text, float width, std::string* overflow);
87
88   /** Draws the given text to the screen. Also needs the position.
89    * Type of alignment, drawing effect and alpha are optional. */
90   void draw(Renderer *renderer, const std::string& text, const Vector& pos,
91             FontAlignment allignment = ALIGN_LEFT,
92             DrawingEffect drawing_effect = NO_EFFECT,
93             float alpha = 1.0f) const;
94
95 private:
96   friend class DrawingContext;
97
98   void draw_text(Renderer *renderer, const std::string& text, const Vector& pos,
99                  DrawingEffect drawing_effect = NO_EFFECT,
100                  float alpha = 1.0f) const;
101
102   void draw_chars(Renderer *renderer, Surface* pchars, const std::string& text,
103                   const Vector& position, DrawingEffect drawing_effect,
104                   float alpha) const;
105
106   /** Convert a Unicode character code to the index of its glyph */
107   int chr2glyph(uint32_t chr) const;
108
109   GlyphWidth glyph_width;
110   Surface*   glyph_surface;
111   Surface*   shadow_glyph_surface;
112   int char_height;
113   int shadowsize;
114
115   /// the number of the first character that is represented in the font
116   uint32_t first_char;
117   /// the number of the last character that is represented in the font
118   uint32_t char_count;
119
120   struct Glyph {
121     /** How many pixels should the cursor advance after printing the
122         glyph */
123     float advance;
124
125     /** Offset that is used when drawing the glyph */
126     Vector offset;
127
128     /** Position of the glyph inside the surface */
129     Rect rect;
130   };
131
132   /** Location of the characters inside the surface */
133   std::vector<Glyph> glyphs;
134   std::vector<Glyph> shadow_glyphs;
135 };
136
137 #endif