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/rect.hpp"
25 #include "math/vector.hpp"
26 #include "video/color.hpp"
27 #include "video/surface.hpp"
28 #include "video/texture.hpp"
46 /** Construct a fixed-width font
48 * @param glyph_width VARIABLE for proportional fonts, VARIABLE for monospace ones
49 * @param fontfile file in format supertux-font
50 * @param sgadowsize offset of shadow
52 Font(GlyphWidth glyph_width, const std::string& fontfile, int shadowsize = 2);
55 /** returns the width of a given text. (Note that I won't add a normal
56 * get_width function here, as we might switch to variable width fonts in the
58 * Supports breaklines.
60 float get_text_width(const std::string& text) const;
62 /** returns the height of a given text. This function supports breaklines.
63 * In case, you are positive that your text doesn't use break lines, you can
64 * just use get_height().
66 float get_text_height(const std::string& text) const;
69 * returns the height of the font.
71 float get_height() const;
74 * returns the given string, truncated (preferably at whitespace) to be at most max_chars characters long
76 static std::string wrap_to_chars(const std::string& text, int max_chars, std::string* overflow);
79 * returns the given string, truncated (preferably at whitespace) to be at most "width" pixels wide
81 std::string wrap_to_width(const std::string& text, float width, std::string* overflow);
83 /** Draws the given text to the screen. Also needs the position.
84 * Type of alignment, drawing effect and alpha are optional. */
85 void draw(Renderer *renderer, const std::string& text, const Vector& pos,
86 FontAlignment alignment = ALIGN_LEFT,
87 DrawingEffect drawing_effect = NO_EFFECT,
88 Color color = Color(1.0,1.0,1.0),
89 float alpha = 1.0f) const;
92 friend class DrawingContext;
94 void draw_text(Renderer *renderer, const std::string& text, const Vector& pos,
95 DrawingEffect drawing_effect = NO_EFFECT,
96 Color color = Color(1.0,1.0,1.0),
97 float alpha = 1.0f) const;
99 void draw_chars(Renderer *renderer, bool nonshadow, const std::string& text,
100 const Vector& position, DrawingEffect drawing_effect, Color color,
103 GlyphWidth glyph_width;
105 std::vector<Surface> glyph_surfaces;
106 std::vector<Surface> shadow_surfaces;
111 /** How many pixels should the cursor advance after printing the
115 /** Offset that is used when drawing the glyph */
118 /** index of containing surface */
121 /** Position of the glyph inside the surface */
125 /** 65536 of glyphs */
126 std::vector<Glyph> glyphs;
128 void loadFontFile(const std::string &filename);
129 void loadFontSurface(const std::string &glyphimage,
130 const std::string &shadowimage,
131 const std::vector<std::string> &chars,
132 GlyphWidth glyph_width,