Support for right-to-left fonts. Set (rtl #t) in
[supertux.git] / src / video / font.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
3 //                     Ingo Ruhnke <grumbel@gmail.com>
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/rectf.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 public:
47   /** Construct a fixed-width font
48    *
49    *  @param glyph_width  VARIABLE for proportional fonts, VARIABLE for monospace ones
50    *  @param fontfile     file in format supertux-font
51    *  @param sgadowsize   offset of shadow
52    */
53   Font(GlyphWidth glyph_width, const std::string& fontfile, int shadowsize = 2);
54   ~Font();
55
56   /** returns the width of a given text. (Note that I won't add a normal
57    * get_width function here, as we might switch to variable width fonts in the
58    * future.)
59    * Supports breaklines.
60    */
61   float get_text_width(const std::string& text) const;
62
63   /** returns the height of a given text. This function supports breaklines.
64    * In case, you are positive that your text doesn't use break lines, you can
65    * just use get_height().
66    */
67   float get_text_height(const std::string& text) const;
68
69   /**
70    * returns the height of the font.
71    */
72   float get_height() const;
73
74   /**
75    * returns the given string, truncated (preferably at whitespace) to be at most max_chars characters long
76    */
77   static std::string wrap_to_chars(const std::string& text, int max_chars, std::string* overflow);
78
79   /**
80    * returns the given string, truncated (preferably at whitespace) to be at most "width" pixels wide
81    */
82   std::string wrap_to_width(const std::string& text, float width, std::string* overflow);
83
84   /** Draws the given text to the screen. Also needs the position.
85    * Type of alignment, drawing effect and alpha are optional. */
86   void draw(Renderer *renderer, const std::string& text, const Vector& pos,
87             FontAlignment alignment = ALIGN_LEFT,
88             DrawingEffect drawing_effect = NO_EFFECT,
89             Color color = Color(1.0,1.0,1.0),
90             float alpha = 1.0f) const;
91
92 private:
93   friend class DrawingContext;
94
95   void draw_text(Renderer *renderer, const std::string& text, const Vector& pos,
96                  DrawingEffect drawing_effect = NO_EFFECT,
97                  Color color = Color(1.0,1.0,1.0),
98                  float alpha = 1.0f) const;
99
100   void draw_chars(Renderer *renderer, bool nonshadow, const std::string& text,
101                   const Vector& position, DrawingEffect drawing_effect, Color color,
102                   float alpha) const;
103
104   void loadFontFile(const std::string &filename);
105   void loadFontSurface(const std::string &glyphimage,
106                        const std::string &shadowimage,
107                        const std::vector<std::string> &chars,
108                        GlyphWidth glyph_width,
109                        int char_width);
110 private:
111   struct Glyph {
112     /** How many pixels should the cursor advance after printing the
113         glyph */
114     float advance;
115
116     /** Offset that is used when drawing the glyph */
117     Vector offset;
118
119     /** index of containing surface */
120     int surface_idx;
121
122     /** Position of the glyph inside the surface */
123     Rectf rect;
124
125     Glyph() :
126       advance(),
127       offset(),
128       surface_idx(),
129       rect()
130     {}
131   };
132
133 private:
134   GlyphWidth glyph_width;
135
136   std::vector<SurfacePtr>  glyph_surfaces;
137   std::vector<SurfacePtr>  shadow_surfaces;
138   int char_height;
139   int shadowsize;
140   int border;
141   bool rtl;
142
143   /** 65536 of glyphs */
144   std::vector<Glyph> glyphs;
145 };
146
147 #endif
148
149 /* EOF */