6810d47d9785e2e34df8996ea4334d33079a14bb
[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 "video/color.hpp"
29 #include "math/vector.hpp"
30 #include "math/rect.hpp"
31
32 class Renderer;
33
34 enum FontAlignment {
35   ALIGN_LEFT,
36   ALIGN_CENTER,
37   ALIGN_RIGHT
38 };
39
40 class Font
41 {
42 public:
43   enum GlyphWidth {
44     FIXED,
45     VARIABLE
46   };
47
48   /** Construct a fixed-width font
49    *
50    *  @param glyph_width  VARIABLE for proportional fonts, VARIABLE for monospace ones
51    *  @param fontfile     file in format supertux-font
52    *  @param sgadowsize   offset of shadow
53    */
54   Font(GlyphWidth glyph_width, const std::string& fontfile, int shadowsize = 2);
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 (preferably 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   /**
81    * returns the given string, truncated (preferably at whitespace) to be at most "width" pixels wide
82    */
83   std::string wrap_to_width(const std::string& text, float width, std::string* overflow);
84
85   /** Draws the given text to the screen. Also needs the position.
86    * Type of alignment, drawing effect and alpha are optional. */
87   void draw(Renderer *renderer, const std::string& text, const Vector& pos,
88             FontAlignment alignment = ALIGN_LEFT,
89             DrawingEffect drawing_effect = NO_EFFECT,
90             Color color = Color(1.0,1.0,1.0),
91             float alpha = 1.0f) const;
92
93 private:
94   friend class DrawingContext;
95
96   void draw_text(Renderer *renderer, const std::string& text, const Vector& pos,
97                  DrawingEffect drawing_effect = NO_EFFECT,
98                  Color color = Color(1.0,1.0,1.0),
99                  float alpha = 1.0f) const;
100
101   void draw_chars(Renderer *renderer, bool nonshadow, const std::string& text,
102                   const Vector& position, DrawingEffect drawing_effect, Color color,
103                   float alpha) const;
104
105   GlyphWidth glyph_width;
106
107   std::vector<Surface>  glyph_surfaces;
108   std::vector<Surface>  shadow_surfaces;
109   int char_height;
110   int shadowsize;
111
112   struct Glyph {
113     /** How many pixels should the cursor advance after printing the
114         glyph */
115     float advance;
116
117     /** Offset that is used when drawing the glyph */
118     Vector offset;
119
120     /** index of containing surface */
121     int surface_idx;
122
123     /** Position of the glyph inside the surface */
124     Rect rect;
125   };
126
127   /** 65536 of glyphs */
128   std::vector<Glyph> glyphs;
129  
130   void loadFontFile(const std::string &filename);
131   void loadFontSurface(const std::string &glyphimage,
132                   const std::string &shadowimage,
133                   const std::vector<std::string> &chars,
134                   GlyphWidth glyph_width,
135                   int char_width);
136 };
137
138 #endif