Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Font.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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 "GraphicsApi.h"
31#include "Path.h"
32#include "Texture.h"
33
34namespace gf {
35#ifndef DOXYGEN_SHOULD_SKIP_THIS
36inline namespace v1 {
37#endif
38
39 class InputStream;
40
57 struct GF_GRAPHICS_API Glyph {
60 float advance = 0.0f;
61 };
62
109 class GF_GRAPHICS_API Font {
110 public:
117
133 Font(const Path& filename);
134
148
162
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();
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 texture with a single alpha channel.
Definition: Texture.h:427
A character font.
Definition: Font.h:109
Font(Span< const uint8_t > content)
Load the font from a file in memory.
void generateTexture(unsigned characterSize)
Generate the texture for a given character size.
const Glyph & getGlyph(char32_t codepoint, unsigned characterSize, float outlineThickness=0.0f)
Retrieve a glyph of the font.
Font(const Font &)=delete
Deleted copy constructor.
float getKerning(char32_t left, char32_t right, unsigned characterSize)
Get the kerning offset of two glyphs.
Font()
Default constructor.
float getLineSpacing(unsigned characterSize)
Get the line spacing.
Font(const Path &filename)
Load the font from a file.
Font & operator=(const Font &)=delete
Deleted copy assignement.
Font & operator=(Font &&other) noexcept
Move assignement.
const AlphaTexture * getTexture(unsigned characterSize)
Retrieve the texture containing the loaded glyphs of a certain size.
Font(Font &&other) noexcept
Move constructor.
~Font()
Destructor.
Font(InputStream &stream)
Load the font from a custom stream.
Abstract class for custom file input streams.
Definition: Stream.h:54
std::filesystem::path Path
A path in the filesystem.
Definition: Path.h:40
The namespace for gf classes.
A glyph.
Definition: Font.h:57
RectF textureRect
Texture coordinates of the glyph inside the font's texture.
Definition: Font.h:59
RectF bounds
Bouding rectangle of the glyph, in coordinates relative to the baseline.
Definition: Font.h:58