c39df1aaa324de9fc145177bd10de51f57d36d43
[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
29 enum FontAlignment {
30   LEFT_ALLIGN,
31   CENTER_ALLIGN,
32   RIGHT_ALLIGN
33 };
34
35 class Font
36 {
37 public:
38   Font(const std::string& file, const std::string& shadowfile,
39        int w, int h, int shadowsize = 2);
40   ~Font();
41   
42   /** returns the width of a given text. (Note that I won't add a normal
43    * get_width function here, as we might switch to variable width fonts in the
44    * future.)
45    * Supports breaklines.
46    */
47   float get_text_width(const std::string& text) const;
48   
49   /** returns the height of a given text. This function supports breaklines.
50    * In case, you are positive that your text doesn't use break lines, you can
51    * just use get_height().
52    */
53   float get_text_height(const std::string& text) const;
54   /// returns the height of the font.
55   float get_height() const;
56  
57   /**
58    * returns the given string, truncated (preferrably at whitespace) to be at most max_width pixels long
59    */
60   std::string wrap_to_width(const std::string& text, int max_width, std::string* overflow) const;
61
62   /**
63    * returns the given string, truncated (preferrably at whitespace) to be at most max_chars characters long
64    */
65   static std::string wrap_to_chars(const std::string& text, int max_chars, std::string* overflow);
66   
67   /** Draws the given text to the screen. Also needs the position.
68    * Type of alignment, drawing effect and alpha are optional. */
69   void draw(const std::string& text, const Vector& pos,
70             FontAlignment allignment = LEFT_ALLIGN,
71             DrawingEffect drawing_effect = NO_EFFECT,
72             float alpha = 1.0f) const;
73   
74 private:
75   friend class DrawingContext;
76   
77   void draw_text(const std::string& text, const Vector& pos,
78                  DrawingEffect drawing_effect = NO_EFFECT,
79                  float alpha = 1.0f) const;
80   
81   void draw_chars(Surface* pchars, const std::string& text,
82                   const Vector& position, DrawingEffect drawing_effect,
83                   float alpha) const;
84   
85   Surface* chars;
86   Surface* shadow_chars;
87   int w;
88   int h;
89   int shadowsize;
90   
91   /// the number of the first character that is represented in the font
92   uint32_t first_char;
93   /// the number of the last character that is represented in the font
94   uint32_t char_count;
95 };
96
97 #endif