4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
5 // Ingo Ruhnke <grumbel@gmx.de>
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.
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.
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.
21 #ifndef SUPERTUX_FONT_H
22 #define SUPERTUX_FONT_H
27 #include "video/surface.hpp"
28 #include "math/vector.hpp"
29 #include "math/rect.hpp"
45 /** Construct a fixed-width font
47 * @param file image file containing the characters
48 * @param shadowfile image file containing the characters shadows
49 * @param w width of a character
50 * @param h height of a character
52 Font(GlyphWidth glyph_width, const std::string& file, const std::string& shadowfile,
53 int w, int h, int shadowsize = 2);
55 /** Construct a variable-width font
57 * @param file image file containing the characters
59 Font(GlyphWidth glyph_width, const std::string& filename,
60 int char_width, int char_height);
64 /** returns the width of a given text. (Note that I won't add a normal
65 * get_width function here, as we might switch to variable width fonts in the
67 * Supports breaklines.
69 float get_text_width(const std::string& text) const;
71 /** returns the height of a given text. This function supports breaklines.
72 * In case, you are positive that your text doesn't use break lines, you can
73 * just use get_height().
75 float get_text_height(const std::string& text) const;
78 * returns the height of the font.
80 float get_height() const;
83 * returns the given string, truncated (preferrably at whitespace) to be at most max_chars characters long
85 static std::string wrap_to_chars(const std::string& text, int max_chars, std::string* overflow);
87 /** Draws the given text to the screen. Also needs the position.
88 * Type of alignment, drawing effect and alpha are optional. */
89 void draw(const std::string& text, const Vector& pos,
90 FontAlignment allignment = ALIGN_LEFT,
91 DrawingEffect drawing_effect = NO_EFFECT,
92 float alpha = 1.0f) const;
95 friend class DrawingContext;
97 void draw_text(const std::string& text, const Vector& pos,
98 DrawingEffect drawing_effect = NO_EFFECT,
99 float alpha = 1.0f) const;
101 void draw_chars(Surface* pchars, const std::string& text,
102 const Vector& position, DrawingEffect drawing_effect,
105 /** Convert a Unicode character code to the index of its glyph */
106 int chr2glyph(uint32_t chr) const;
108 GlyphWidth glyph_width;
109 Surface* glyph_surface;
110 Surface* shadow_chars;
114 /// the number of the first character that is represented in the font
116 /// the number of the last character that is represented in the font
119 /** Location of the characters inside the surface */
120 std::vector<Rect> glyphs;