Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Font.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_FONT_H
25 #define GF_FONT_H
26 
27 #include <cstdint>
28 #include <map>
29 
30 #include "Path.h"
31 #include "Portability.h"
32 #include "Texture.h"
33 
34 namespace gf {
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 inline namespace v1 {
37 #endif
38 
39  class InputStream;
40 
57  struct GF_API Glyph {
60  float advance = 0.0f;
61  };
62 
109  class GF_API Font {
110  public:
116  Font();
117 
133  Font(const Path& filename);
134 
147  Font(InputStream& stream);
148 
161  Font(ArrayRef<uint8_t> content);
162 
166  ~Font();
167 
171  Font(const Font&) = delete;
172 
176  Font& operator=(const Font&) = delete;
177 
181  Font(Font&& other) noexcept;
182 
186  Font& operator=(Font&& other) noexcept;
187 
188 
202  const Glyph& getGlyph(char32_t codepoint, unsigned characterSize, float outlineThickness = 0.0f);
203 
219  float getKerning(char32_t left, char32_t right, unsigned characterSize);
220 
231  float getLineSpacing(unsigned characterSize);
232 
244  const AlphaTexture *getTexture(unsigned characterSize);
245 
254  void generateTexture(unsigned characterSize);
255 
256  private:
257  struct Packing {
258  int top = 0;
259  int bottom = 0;
260  int right = 0;
261  };
262 
263  struct GlyphCache {
264  AlphaTexture texture;
265  std::map<uint64_t, Glyph> glyphs;
266  Packing packing;
267  };
268 
269  private:
270  GlyphCache createCache(unsigned characterSize);
271  Glyph createGlyph(char32_t codepoint, unsigned characterSize, float outlineThickness, GlyphCache& cache);
272 
273  bool setCurrentCharacterSize(unsigned characterSize);
274 
275  private:
276  void *m_library;
277  void *m_stroker;
278  void *m_face;
279  unsigned m_currentCharacterSize;
280  std::map<unsigned, GlyphCache> m_cache;
281  };
282 
283 #ifndef DOXYGEN_SHOULD_SKIP_THIS
284 }
285 #endif
286 }
287 
288 #endif // GF_FONT_H
A glyph.
Definition: Font.h:57
RectF bounds
Bouding rectangle of the glyph, in coordinates relative to the baseline.
Definition: Font.h:58
The namespace for gf classes.
Definition: Action.h:35
Abstract class for custom file input streams.
Definition: Stream.h:55
A character font.
Definition: Font.h:109
RectF textureRect
Texture coordinates of the glyph inside the font&#39;s texture.
Definition: Font.h:59
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:44
A texture with a single alpha channel.
Definition: Texture.h:391