TGUI 1.11
Loading...
Searching...
No Matches
BackendFontFreeType.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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#ifndef TGUI_BACKEND_FONT_FREETYPE_HPP
26#define TGUI_BACKEND_FONT_FREETYPE_HPP
27
28#include <TGUI/Config.hpp>
29#include <TGUI/Backend/Font/BackendFont.hpp>
30
31#include <unordered_map>
32
34
35using FT_Library = struct FT_LibraryRec_*;
36using FT_Face = struct FT_FaceRec_*;
37using FT_Stroker = struct FT_StrokerRec_*;
38
40
41namespace tgui
42{
46 class TGUI_API BackendFontFreeType : public BackendFont
47 {
48 public:
49
54
63 bool loadFromMemory(std::unique_ptr<std::uint8_t[]> data, std::size_t sizeInBytes) override;
65
73 TGUI_NODISCARD bool hasGlyph(char32_t codePoint) const override;
74
88 TGUI_NODISCARD FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) override;
89
104 TGUI_NODISCARD float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override;
105
115 TGUI_NODISCARD float getLineSpacing(unsigned int characterSize) override;
116
124 TGUI_NODISCARD float getFontHeight(unsigned int characterSize) override;
125
133 TGUI_NODISCARD float getAscent(unsigned int characterSize) override;
134
142 TGUI_NODISCARD float getDescent(unsigned int characterSize) override;
143
153 TGUI_NODISCARD float getUnderlinePosition(unsigned int characterSize) override;
154
164 TGUI_NODISCARD float getUnderlineThickness(unsigned int characterSize) override;
165
174 TGUI_NODISCARD std::shared_ptr<BackendTexture> getTexture(unsigned int characterSize, unsigned int& textureVersion) override;
175
183 TGUI_NODISCARD Vector2u getTextureSize(unsigned int characterSize) override;
184
194 void setSmooth(bool smooth) override;
195
203 void setFontScale(float scale) override;
204
206 protected:
207
208 struct Glyph
209 {
210 float advance = 0;
211 float lsbDelta = 0;
212 float rsbDelta = 0;
213 FloatRect bounds;
214 UIntRect textureRect;
215 };
216
218 // Loads a glyph with freetype
220 TGUI_NODISCARD Glyph loadGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness);
221
223 // Returns a cached glyph or calls loadGlyph to load it when this is the first time the glyph is requested
225 TGUI_NODISCARD Glyph getInternalGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness);
226
228 // Reserves space in the texture to place the glyph
230 TGUI_NODISCARD UIntRect findAvailableGlyphRect(unsigned int width, unsigned int height);
231
233 // Sets the character size on which the freetype operations are performed
235 bool setCurrentSize(unsigned int characterSize);
236
238 // Destroys freetype resources
240 void cleanup();
241
243 protected:
244
245 struct Row
246 {
247 Row(unsigned int rowTop, unsigned int rowHeight) : width(0), top(rowTop), height(rowHeight) {}
248
249 unsigned int width;
250 unsigned int top;
251 unsigned int height;
252 };
253
254 FT_Library m_library = nullptr; // Handle to the freetype library
255 FT_Face m_face = nullptr; // Contains the font (typeface and style)
256 FT_Stroker m_stroker = nullptr; // Used for rendering outlines
257
258 std::unordered_map<unsigned int, float> m_cachedLineSpacing;
259 std::unordered_map<unsigned int, float> m_cachedFontHeights;
260 std::unordered_map<unsigned int, float> m_cachedAscents;
261 std::unordered_map<unsigned int, float> m_cachedDescents;
262
263 std::unordered_map<std::uint64_t, Glyph> m_glyphs;
264 unsigned int m_nextRow = 3;
265 std::vector<Row> m_rows;
266
267 std::unique_ptr<std::uint8_t[]> m_fileContents;
268 std::unique_ptr<std::uint8_t[]> m_pixels;
269 std::shared_ptr<BackendTexture> m_texture;
270 unsigned int m_textureSize = 0;
271 unsigned int m_textureVersion = 0;
272 };
273
275
276 using BackendFontFreetype TGUI_DEPRECATED("Use BackendFontFreeType instead (capital T)") = BackendFontFreeType;
277}
278
280
281#endif // TGUI_BACKEND_FONT_FREETYPE_HPP
Font implementations that uses FreeType directly to load glyphs.
Definition BackendFontFreeType.hpp:47
float getDescent(unsigned int characterSize) override
Returns the maximum height of a glyph below the baseline as a negative value.
Vector2u getTextureSize(unsigned int characterSize) override
Returns the size of the texture that is used to store glyphs of the given character size.
float getLineSpacing(unsigned int characterSize) override
Returns the line spacing.
float getUnderlineThickness(unsigned int characterSize) override
Get the thickness of the underline.
~BackendFontFreeType() override
Destructor that cleans up the FreeType resources.
FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness=0) override
Retrieve a glyph of the font.
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.
unsigned int m_nextRow
Y position of the next new row in the texture (first 2 rows contain pixels for underlining)
Definition BackendFontFreeType.hpp:264
float getAscent(unsigned int characterSize) override
Returns the maximum height of a glyph above the baseline.
float getFontHeight(unsigned int characterSize) override
Returns the height required to render a line of text.
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.
void setSmooth(bool smooth) override
Enable or disable the smooth filter.
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.
virtual bool loadFromMemory(std::unique_ptr< std::uint8_t[]> data, std::size_t sizeInBytes)=0
Loads a font from memory.
BackendFont()
Default constructor.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
Definition BackendFontFreeType.hpp:209
float advance
Offset to move horizontally to the next character.
Definition BackendFontFreeType.hpp:210
FloatRect bounds
Bounding rectangle of the glyph, in coordinates relative to the baseline.
Definition BackendFontFreeType.hpp:213
float lsbDelta
Left offset after forced autohint. Internally used by getKerning()
Definition BackendFontFreeType.hpp:211
float rsbDelta
Right offset after forced autohint. Internally used by getKerning()
Definition BackendFontFreeType.hpp:212
UIntRect textureRect
Texture coordinates of the glyph inside the font's texture.
Definition BackendFontFreeType.hpp:214
unsigned int height
Height of the row.
Definition BackendFontFreeType.hpp:251
unsigned int top
Y position of the row into the texture.
Definition BackendFontFreeType.hpp:250
unsigned int width
Current width of the row.
Definition BackendFontFreeType.hpp:249
Information about a glyph in the font.
Definition Font.hpp:46