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