ScummVM API documentation
font.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
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.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef TWP_FONT_H
23 #define TWP_FONT_H
24 
25 #include "common/rect.h"
26 #include "common/hashmap.h"
27 #include "common/str.h"
28 #include "twp/spritesheet.h"
29 #include "twp/gfx.h"
30 
31 namespace Common {
32 template<>
33 struct Hash<Common::u32char_type_t> : public UnaryFunction<Common::u32char_type_t, uint> {
34  uint operator()(Common::u32char_type_t val) const { return (uint)val; }
35 };
36 } // namespace Common
37 
38 namespace Twp {
39 
40 struct KerningKey {
41  int first;
42  int second;
43 };
44 bool operator==(const KerningKey &l, const KerningKey &r);
45 
46 } // namespace Twp
47 
48 namespace Common {
49 
50 template<>
51 struct Hash<Twp::KerningKey> : public Common::UnaryFunction<Twp::KerningKey, uint> {
52  uint operator()(Twp::KerningKey val) const { return (uint)(val.first ^ val.second); }
53 };
54 
55 } // namespace Common
56 
57 namespace Twp {
58 typedef Common::u32char_type_t CodePoint;
59 
60 // represents a glyph: a part of an image for a specific font character
61 struct Glyph {
62  int advance; // Offset to move horizontally to the next character.
63  Common::Rect bounds; // Bounding rectangle of the glyph, in coordinates relative to the baseline.
64  Common::Rect textureRect; // Texture coordinates of the glyph inside the font's texture.
65 };
66 
67 class Font {
68 public:
69  virtual ~Font() = default;
70 
71  virtual int getLineHeight() = 0;
72  virtual float getKerning(CodePoint prev, CodePoint next) = 0;
73  virtual Glyph getGlyph(CodePoint chr) = 0;
74  virtual Common::String getName() = 0;
75 };
76 
77 // Represents a bitmap font from a spritesheet.
78 class GGFont : public Font {
79 public:
80  ~GGFont() override;
81  void load(const Common::String &path);
82 
83  int getLineHeight() final { return _lineHeight; }
84  float getKerning(CodePoint prev, CodePoint next) final { return 0.f; }
85  Glyph getGlyph(CodePoint chr) final;
86  Common::String getName() final { return _name; }
87 
88 private:
90  int _lineHeight = 0;
91  Common::String _name;
92 };
93 
94 struct Char {
95  int id = 0;
96  int x = 0;
97  int y = 0;
98  int w = 0;
99  int h = 0;
100  int xoff = 0;
101  int yoff = 0;
102  int xadv = 0;
103  int page = 0;
104  int chnl = 0;
105  Common::String _letter;
106 };
107 
108 class BmFont : public Font {
109 public:
110  ~BmFont() override;
111  void load(const Common::String &path);
112 
113  int getLineHeight() final { return _lnHeight; }
114  float getKerning(CodePoint prev, CodePoint next) final;
115  Glyph getGlyph(CodePoint chr) final;
116  Common::String getName() final { return _name; }
117 
118 private:
121  int _lnHeight = 0;
122  int _base = 0;
123  int _scaleW = 0;
124  int _scaleH = 0;
125  int _pages = 0;
126  int _packed = 0;
127  Common::String _name;
128 };
129 
130 enum TextHAlignment {
131  thLeft,
132  thCenter,
133  thRight
134 };
135 enum TextVAlignment {
136  tvTop,
137  tvCenter,
138  tvBottom
139 };
140 
141 // This class allows to render a text.
142 //
143 // A text can contains color in hexadecimal with this format: #RRGGBB
144 class Text {
145 public:
146  Text();
147  Text(const Common::String &fontName, const Common::String &text, TextHAlignment hAlign = thCenter, TextVAlignment vAlign = tvCenter, float maxWidth = 0.0f, const Color &color = Color());
148 
149  void setText(const Common::String &text) {
150  _txt = text;
151  _dirty = true;
152  }
153  Common::String getText() const { return _txt; }
154 
155  void setColor(const Color &c) {
156  _col = c;
157  _dirty = true;
158  }
159  Color getColor() const { return _col; }
160 
161  void setMaxWidth(float maxW) {
162  _maxW = maxW;
163  _dirty = true;
164  }
165  float getMaxWidth() const { return _maxW; }
166 
167  void setHAlign(TextHAlignment align) {
168  _hAlign = align;
169  _dirty = true;
170  }
171  TextHAlignment getHAlign() const { return _hAlign; }
172 
173  void setVAlign(TextVAlignment align) {
174  _vAlign = align;
175  _dirty = true;
176  }
177  TextVAlignment getVAlign() const { return _vAlign; }
178 
179  void setFont(const Common::String &fontName);
180  Common::SharedPtr<Font> getFont() { return _font; }
181  Math::Vector2d getBounds();
182 
183  void draw(Gfx &gfx, const Math::Matrix4 &trsf = Math::Matrix4());
184 
185 private:
186  void update();
187 
188 private:
190  Common::String _fontName;
191  Texture *_texture = nullptr;
192  Common::String _txt;
193  Color _col;
194  TextHAlignment _hAlign = TextHAlignment::thLeft;
195  TextVAlignment _vAlign = TextVAlignment::tvCenter;
196  Common::Array<Vertex> _vertices;
197  Math::Vector2d _bnds;
198  float _maxW = 0.f;
200  bool _dirty = false;
201 };
202 
203 } // namespace Twp
204 
205 #endif
Definition: font.h:67
Definition: str.h:59
Definition: font.h:78
Definition: array.h:52
Definition: font.h:144
Definition: rect.h:144
Definition: func.h:527
Definition: gfx.h:35
Definition: hashmap.h:85
Definition: font.h:108
Definition: algorithm.h:29
char32_t u32char_type_t
Definition: ustr.h:41
Definition: font.h:94
Definition: font.h:40
Definition: ptr.h:159
Definition: func.h:43
Definition: font.h:61
Definition: gfx.h:101
Definition: achievements_tables.h:27
Definition: gfx.h:161