Gamedev Framework (gf)  0.8.0
A C++14 framework for 2D games
Font.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 
130  class GF_API Font {
131  public:
137  Font();
138 
142  ~Font();
143 
147  Font(const Font&) = delete;
148 
152  Font& operator=(const Font&) = delete;
153 
157  Font(Font&& other) noexcept;
158 
162  Font& operator=(Font&& other) noexcept;
163 
182  bool loadFromFile(const Path& filename);
183 
199  bool loadFromStream(InputStream& stream);
200 
201 
218  bool loadFromMemory(const uint8_t *data, std::size_t length);
219 
220 
234  const Glyph& getGlyph(char32_t codepoint, unsigned characterSize, float outlineThickness = 0.0f);
235 
251  float getKerning(char32_t left, char32_t right, unsigned characterSize);
252 
263  float getLineSpacing(unsigned characterSize);
264 
276  const AlphaTexture *getTexture(unsigned characterSize);
277 
286  void generateTexture(unsigned characterSize);
287 
288  private:
289  struct Packing {
290  unsigned top = 0;
291  unsigned bottom = 0;
292  unsigned right = 0;
293  };
294 
295  struct GlyphCache {
296  AlphaTexture texture;
297  std::map<uint64_t, Glyph> glyphs;
298  Packing packing;
299  };
300 
301  private:
302  GlyphCache createCache(unsigned characterSize);
303  Glyph createGlyph(char32_t codepoint, unsigned characterSize, float outlineThickness, GlyphCache& cache);
304 
305  bool setCurrentCharacterSize(unsigned characterSize);
306 
307  private:
308  void *m_library;
309  void *m_stroker;
310  void *m_face;
311  unsigned m_currentCharacterSize;
312  std::map<unsigned, GlyphCache> m_cache;
313  };
314 
315 #ifndef DOXYGEN_SHOULD_SKIP_THIS
316 }
317 #endif
318 }
319 
320 #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:34
Abstract class for custom file input streams.
Definition: InputStream.h:51
A character font.
Definition: Font.h:130
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:41
A texture with a single alpha channel.
Definition: Texture.h:474