TGUI  0.9-dev
BackendFontFreeType.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2021 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages 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 freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25
26#ifndef TGUI_BACKEND_FONT_FREETYPE_HPP
27#define TGUI_BACKEND_FONT_FREETYPE_HPP
28
29#include <TGUI/Backend/Font/BackendFont.hpp>
30#include <unordered_map>
31
33
34typedef struct FT_LibraryRec_* FT_Library;
35typedef struct FT_FaceRec_* FT_Face;
36typedef struct FT_StrokerRec_* FT_Stroker;
37
39
40namespace tgui
41{
46 {
47 public:
48
53
54
63 virtual bool loadFromMemory(std::unique_ptr<std::uint8_t[]> data, std::size_t sizeInBytes) override;
65
66
74 bool hasGlyph(char32_t codePoint) const override;
75
76
90 virtual FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) override;
91
92
107 virtual float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override;
108#ifndef TGUI_REMOVE_DEPRECATED_CODE
109 using BackendFont::getKerning; // Import version without bold parameter
110#endif
111
121 virtual float getLineSpacing(unsigned int characterSize) override;
122
123
133 float getUnderlinePosition(unsigned int characterSize) override;
134
135
145 float getUnderlineThickness(unsigned int characterSize) override;
146
147
155 std::shared_ptr<BackendTexture> getTexture(unsigned int characterSize) override;
156
157
167 void setSmooth(bool smooth) override;
168
169
171 protected:
172
173 struct Glyph
174 {
175 float advance = 0;
176 float lsbDelta = 0;
177 float rsbDelta = 0;
180 };
181
182
184 // Loads a glyph with freetype
186 Glyph loadGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness);
187
188
190 // Returns a cached glyph or calls loadGlyph to load it when this is the first time the glyph is requested
192 Glyph getInternalGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness);
193
194
196 // Reserves space in the texture to place the glyph
198 IntRect findAvailableGlyphRect(unsigned int width, unsigned int height);
199
200
202 // Sets the character size on which the freetype operations are performed
204 bool setCurrentSize(unsigned int characterSize);
205
206
208 // Destroys freetype resources
210 void cleanup();
211
212
214 protected:
215
216 struct Row
217 {
218 Row(unsigned int rowTop, unsigned int rowHeight) : width(0), top(rowTop), height(rowHeight) {}
219
220 unsigned int width;
221 unsigned int top;
222 unsigned int height;
223 };
224
225 FT_Library m_library = nullptr; // Handle to the freetype library
226 FT_Face m_face = nullptr; // Contains the font (typeface and style)
227 FT_Stroker m_stroker = nullptr; // Used for rendering outlines
228
229 std::unordered_map<unsigned int, float> m_cachedLineSpacing;
230
231 std::unordered_map<std::uint64_t, Glyph> m_glyphs;
232 unsigned int m_nextRow = 3;
233 std::vector<Row> m_rows;
234
235 std::unique_ptr<std::uint8_t[]> m_fileContents;
236 std::unique_ptr<std::uint8_t[]> m_pixels;
237 std::shared_ptr<BackendTexture> m_texture;
238 unsigned int m_textureSize = 0;
239 bool m_textureUpToDate = false;
240 };
241
243}
244
246
247#endif // TGUI_BACKEND_FONT_FREETYPE_HPP
Font implementations that uses FreeType directly to load glyphs.
Definition: BackendFontFreeType.hpp:46
virtual FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness=0) override
Retrieve a glyph of the font.
virtual float getLineSpacing(unsigned int characterSize) override
Returns the line spacing.
float getUnderlineThickness(unsigned int characterSize) override
Get the thickness of the underline.
virtual float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override
Returns the kerning offset of two glyphs.
float getUnderlinePosition(unsigned int characterSize) override
Get the position of the underline.
~BackendFontFreetype()
Destructor that cleans up the FreeType resources.
unsigned int m_nextRow
Y position of the next new row in the texture (first 2 rows contain pixels for underlining)
Definition: BackendFontFreeType.hpp:232
std::shared_ptr< BackendTexture > getTexture(unsigned int characterSize) override
Returns the texture that is used to store glyphs of the given character size.
void setSmooth(bool smooth) override
Enable or disable the smooth filter.
virtual bool loadFromMemory(std::unique_ptr< std::uint8_t[]> data, std::size_t sizeInBytes) override
Loads a font from memory.
bool hasGlyph(char32_t codePoint) const override
Returns whether a font contains a certain glyph.
Base class for font implementations that depend on the backend.
Definition: BackendFont.hpp:43
virtual bool loadFromMemory(const void *data, std::size_t sizeInBytes)
Loads a font from memory.
virtual float getKerning(char32_t first, char32_t second, unsigned int characterSize)
Returns the kerning offset of two glyphs.
Definition: BackendFont.hpp:129
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: BackendFontFreeType.hpp:174
IntRect textureRect
Texture coordinates of the glyph inside the font's texture.
Definition: BackendFontFreeType.hpp:179
float rsbDelta
Right offset after forced autohint. Internally used by getKerning()
Definition: BackendFontFreeType.hpp:177
float lsbDelta
Left offset after forced autohint. Internally used by getKerning()
Definition: BackendFontFreeType.hpp:176
float advance
Offset to move horizontally to the next character.
Definition: BackendFontFreeType.hpp:175
FloatRect bounds
Bounding rectangle of the glyph, in coordinates relative to the baseline.
Definition: BackendFontFreeType.hpp:178
Definition: BackendFontFreeType.hpp:217
unsigned int height
Height of the row.
Definition: BackendFontFreeType.hpp:222
unsigned int width
Current width of the row.
Definition: BackendFontFreeType.hpp:220
unsigned int top
Y position of the row into the texture.
Definition: BackendFontFreeType.hpp:221
Information about a glyph in the font.
Definition: Font.hpp:47