TGUI  1.3.0
Loading...
Searching...
No Matches
BackendFontFreeType.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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/Config.hpp>
30#if !TGUI_BUILD_AS_CXX_MODULE
31 #include <TGUI/Backend/Font/BackendFont.hpp>
32#endif
33
34#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
35 #include <unordered_map>
36#endif
37
39
40#if !TGUI_BUILD_AS_CXX_MODULE
41 using FT_Library = struct FT_LibraryRec_*;
42 using FT_Face = struct FT_FaceRec_*;
43 using FT_Stroker = struct FT_StrokerRec_*;
44#endif
45
47
48TGUI_MODULE_EXPORT namespace tgui
49{
53 class TGUI_API BackendFontFreetype : public BackendFont
54 {
55 public:
56
61
62
71 bool loadFromMemory(std::unique_ptr<std::uint8_t[]> data, std::size_t sizeInBytes) override;
72 using BackendFont::loadFromMemory;
73
74
82 TGUI_NODISCARD bool hasGlyph(char32_t codePoint) const override;
83
84
98 TGUI_NODISCARD FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) override;
99
100
115 TGUI_NODISCARD float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override;
116
117
127 TGUI_NODISCARD float getLineSpacing(unsigned int characterSize) override;
128
129
137 TGUI_NODISCARD float getFontHeight(unsigned int characterSize) override;
138
139
147 TGUI_NODISCARD float getAscent(unsigned int characterSize) override;
148
149
157 TGUI_NODISCARD float getDescent(unsigned int characterSize) override;
158
159
169 TGUI_NODISCARD float getUnderlinePosition(unsigned int characterSize) override;
170
171
181 TGUI_NODISCARD float getUnderlineThickness(unsigned int characterSize) override;
182
183
192 TGUI_NODISCARD std::shared_ptr<BackendTexture> getTexture(unsigned int characterSize, unsigned int& textureVersion) override;
193
194
202 TGUI_NODISCARD Vector2u getTextureSize(unsigned int characterSize) override;
203
204
214 void setSmooth(bool smooth) override;
215
216
224 void setFontScale(float scale) override;
225
226
228 protected:
229
230 struct Glyph
231 {
232 float advance = 0;
233 float lsbDelta = 0;
234 float rsbDelta = 0;
237 };
238
240 // Loads a glyph with freetype
242 TGUI_NODISCARD Glyph loadGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness);
243
245 // Returns a cached glyph or calls loadGlyph to load it when this is the first time the glyph is requested
247 TGUI_NODISCARD Glyph getInternalGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness);
248
250 // Reserves space in the texture to place the glyph
252 TGUI_NODISCARD UIntRect findAvailableGlyphRect(unsigned int width, unsigned int height);
253
255 // Sets the character size on which the freetype operations are performed
257 bool setCurrentSize(unsigned int characterSize);
258
260 // Destroys freetype resources
262 void cleanup();
263
265 protected:
266
267 struct Row
268 {
269 Row(unsigned int rowTop, unsigned int rowHeight) : width(0), top(rowTop), height(rowHeight) {}
270
271 unsigned int width;
272 unsigned int top;
273 unsigned int height;
274 };
275
276 FT_Library m_library = nullptr; // Handle to the freetype library
277 FT_Face m_face = nullptr; // Contains the font (typeface and style)
278 FT_Stroker m_stroker = nullptr; // Used for rendering outlines
279
280 std::unordered_map<unsigned int, float> m_cachedLineSpacing;
281 std::unordered_map<unsigned int, float> m_cachedFontHeights;
282 std::unordered_map<unsigned int, float> m_cachedAscents;
283 std::unordered_map<unsigned int, float> m_cachedDescents;
284
285 std::unordered_map<std::uint64_t, Glyph> m_glyphs;
286 unsigned int m_nextRow = 3;
287 std::vector<Row> m_rows;
288
289 std::unique_ptr<std::uint8_t[]> m_fileContents;
290 std::unique_ptr<std::uint8_t[]> m_pixels;
291 std::shared_ptr<BackendTexture> m_texture;
292 unsigned int m_textureSize = 0;
293 unsigned int m_textureVersion = 0;
294 };
295
297}
298
300
301#endif // TGUI_BACKEND_FONT_FREETYPE_HPP
Font implementations that uses FreeType directly to load glyphs.
Definition BackendFontFreeType.hpp:54
TGUI_NODISCARD float getLineSpacing(unsigned int characterSize) override
Returns the line spacing.
TGUI_NODISCARD bool hasGlyph(char32_t codePoint) const override
Returns whether a font contains a certain glyph.
TGUI_NODISCARD std::shared_ptr< BackendTexture > getTexture(unsigned int characterSize, unsigned int &textureVersion) override
Returns the texture that is used to store glyphs of the given character size.
TGUI_NODISCARD float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override
Returns the kerning offset of two glyphs.
~BackendFontFreetype() override
Destructor that cleans up the FreeType resources.
TGUI_NODISCARD FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness=0) override
Retrieve a glyph of the font.
TGUI_NODISCARD float getAscent(unsigned int characterSize) override
Returns the maximum height of a glyph above the baseline.
void setSmooth(bool smooth) override
Enable or disable the smooth filter.
TGUI_NODISCARD float getUnderlineThickness(unsigned int characterSize) override
Get the thickness of the underline.
TGUI_NODISCARD float getDescent(unsigned int characterSize) override
Returns the maximum height of a glyph below the baseline.
TGUI_NODISCARD Vector2u getTextureSize(unsigned int characterSize) override
Returns the size of the texture that is used to store glyphs of the given character size.
bool loadFromMemory(std::unique_ptr< std::uint8_t[]> data, std::size_t sizeInBytes) override
Loads a font from memory.
TGUI_NODISCARD float getUnderlinePosition(unsigned int characterSize) override
Get the position of the underline.
TGUI_NODISCARD float getFontHeight(unsigned int characterSize) override
Returns the height required to render a line of text.
Base class for font implementations that depend on the backend.
Definition BackendFont.hpp:46
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
Definition BackendFontFreeType.hpp:231
UIntRect textureRect
Texture coordinates of the glyph inside the font's texture.
Definition BackendFontFreeType.hpp:236
FloatRect bounds
Bounding rectangle of the glyph, in coordinates relative to the baseline.
Definition BackendFontFreeType.hpp:235
Definition BackendFontFreeType.hpp:268
unsigned int height
Height of the row.
Definition BackendFontFreeType.hpp:273
unsigned int width
Current width of the row.
Definition BackendFontFreeType.hpp:271
unsigned int top
Y position of the row into the texture.
Definition BackendFontFreeType.hpp:272
Information about a glyph in the font.
Definition Font.hpp:50