2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
3 // Ingo Ruhnke <grumbel@gmx.de>
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.
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.
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/>.
18 #ifndef HEADER_SUPERTUX_VIDEO_FONT_HPP
19 #define HEADER_SUPERTUX_VIDEO_FONT_HPP
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"
47 /** Construct a fixed-width font
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
53 Font(GlyphWidth glyph_width, const std::string& fontfile, int shadowsize = 2);
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
59 * Supports breaklines.
61 float get_text_width(const std::string& text) const;
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().
67 float get_text_height(const std::string& text) const;
70 * returns the height of the font.
72 float get_height() const;
75 * returns the given string, truncated (preferably at whitespace) to be at most max_chars characters long
77 static std::string wrap_to_chars(const std::string& text, int max_chars, std::string* overflow);
80 * returns the given string, truncated (preferably at whitespace) to be at most "width" pixels wide
82 std::string wrap_to_width(const std::string& text, float width, std::string* overflow);
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;
93 friend class DrawingContext;
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;
100 void draw_chars(Renderer *renderer, bool nonshadow, const std::string& text,
101 const Vector& position, DrawingEffect drawing_effect, Color color,
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,
112 /** How many pixels should the cursor advance after printing the
116 /** Offset that is used when drawing the glyph */
119 /** index of containing surface */
122 /** Position of the glyph inside the surface */
134 GlyphWidth glyph_width;
136 std::vector<SurfacePtr> glyph_surfaces;
137 std::vector<SurfacePtr> shadow_surfaces;
141 /** 65536 of glyphs */
142 std::vector<Glyph> glyphs;