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_BITMAP_FONT_HPP
19#define HEADER_SUPERTUX_VIDEO_BITMAP_FONT_HPP
20
21#include <string>
22
23#include "math/rectf.hpp"
24#include "math/vector.hpp"
25#include "video/color.hpp"
26#include "video/font.hpp"
27#include "video/surface_ptr.hpp"
28#include "video/texture.hpp"
29
30class Painter;
31
32class BitmapFont final : public Font
33{
34public:
35 enum GlyphWidth {
36 FIXED,
37 VARIABLE
38 };
39
40public:
41 /** Construct a fixed-width font
42 *
43 * @param glyph_width VARIABLE for proportional fonts, VARIABLE for monospace ones
44 * @param fontfile file in format supertux-font
45 * @param sgadowsize offset of shadow
46 */
47 BitmapFont(GlyphWidth glyph_width, const std::string& fontfile, int shadowsize = 2);
48 virtual ~BitmapFont();
49
50 int get_shadow_size() const { return shadowsize; }
51
52 /** returns the width of a given text. (Note that I won't add a normal
53 * get_width function here, as we might switch to variable width fonts in the
54 * future.)
55 * Supports breaklines.
56 */
57 virtual float get_text_width(const std::string& text) const override;
58
59 /** returns the height of a given text. This function supports breaklines.
60 * In case, you are positive that your text doesn't use break lines, you can
61 * just use get_height().
62 */
63 virtual float get_text_height(const std::string& text) const override;
64
65 /**
66 * returns the height of the font.
67 */
68 virtual float get_height() const override;
69
70 /**
71 * returns the given string, truncated (preferably at whitespace) to be at most "width" pixels wide
72 */
73 virtual std::string wrap_to_width(const std::string& text, float width, std::string* overflow) override;
74
75 virtual void draw_text(Canvas& canvas, const std::string& text,
76 const Vector& pos, FontAlignment alignment, int layer, const Color& color) override;
77
78private:
79 friend class DrawingContext;
80
81 void draw_text(Canvas& painter, const std::string& text, const Vector& pos, int layer,
82 Color color = Color(1.0,1.0,1.0)) const;
83
84 void draw_chars(Canvas& painter, bool nonshadow, const std::string& text,
85 const Vector& position, int layer, Color color) const;
86
87 void loadFontFile(const std::string &filename);
88 void loadFontSurface(const std::string &glyphimage,
89 const std::string &shadowimage,
90 const std::vector<std::string> &chars,
91 GlyphWidth glyph_width,
92 int char_width);
93private:
94 struct Glyph {
95 /** How many pixels should the cursor advance after printing the
96 glyph */
97 float advance;
98
99 /** Offset that is used when drawing the glyph */
100 Vector offset;
101
102 /** index of containing surface */
103 int surface_idx;
104
105 /** Position of the glyph inside the surface */
106 Rectf rect;
107
108 Glyph() :
109 advance(),
110 offset(),
111 surface_idx(),
112 rect()
113 {}
114 };
115
116private:
117 GlyphWidth glyph_width;
118
119 std::vector<SurfacePtr> glyph_surfaces;
120 std::vector<SurfacePtr> shadow_surfaces;
121 int char_height;
122 int shadowsize;
123 int border;
124 bool rtl;
125
126 /** 65536 of glyphs */
127 std::vector<Glyph> glyphs;
128};
129
130#endif
131
132/* EOF */
133