--- --- TGUI: include/TGUI/Backend/Font/FreeType/BackendFontFreeType.hpp Source File
TGUI  1.x-dev
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#ifndef TGUI_BACKEND_FONT_FREETYPE_HPP
26#define TGUI_BACKEND_FONT_FREETYPE_HPP
27
28#include <TGUI/Config.hpp>
29#if !TGUI_BUILD_AS_CXX_MODULE
30 #include <TGUI/Backend/Font/BackendFont.hpp>
31#endif
32
33#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
34 #include <unordered_map>
35#endif
36
38
39#if !TGUI_BUILD_AS_CXX_MODULE
40 using FT_Library = struct FT_LibraryRec_*;
41 using FT_Face = struct FT_FaceRec_*;
42 using FT_Stroker = struct FT_StrokerRec_*;
43#endif
44
46
47TGUI_MODULE_EXPORT namespace tgui
48{
52 class TGUI_API BackendFontFreetype : public BackendFont
53 {
54 public:
55
60
69 bool loadFromMemory(std::unique_ptr<std::uint8_t[]> data, std::size_t sizeInBytes) override;
70 using BackendFont::loadFromMemory;
71
79 TGUI_NODISCARD bool hasGlyph(char32_t codePoint) const override;
80
94 TGUI_NODISCARD FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) override;
95
110 TGUI_NODISCARD float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override;
111
121 TGUI_NODISCARD float getLineSpacing(unsigned int characterSize) override;
122
130 TGUI_NODISCARD float getFontHeight(unsigned int characterSize) override;
131
139 TGUI_NODISCARD float getAscent(unsigned int characterSize) override;
140
148 TGUI_NODISCARD float getDescent(unsigned int characterSize) override;
149
159 TGUI_NODISCARD float getUnderlinePosition(unsigned int characterSize) override;
160
170 TGUI_NODISCARD float getUnderlineThickness(unsigned int characterSize) override;
171
180 TGUI_NODISCARD std::shared_ptr<BackendTexture> getTexture(unsigned int characterSize, unsigned int& textureVersion) override;
181
189 TGUI_NODISCARD Vector2u getTextureSize(unsigned int characterSize) override;
190
200 void setSmooth(bool smooth) override;
201
209 void setFontScale(float scale) override;
210
212 protected:
213
214 struct Glyph
215 {
216 float advance = 0;
217 float lsbDelta = 0;
218 float rsbDelta = 0;
221 };
222
224 // Loads a glyph with freetype
226 TGUI_NODISCARD Glyph loadGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness);
227
229 // Returns a cached glyph or calls loadGlyph to load it when this is the first time the glyph is requested
231 TGUI_NODISCARD Glyph getInternalGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness);
232
234 // Reserves space in the texture to place the glyph
236 TGUI_NODISCARD UIntRect findAvailableGlyphRect(unsigned int width, unsigned int height);
237
239 // Sets the character size on which the freetype operations are performed
241 bool setCurrentSize(unsigned int characterSize);
242
244 // Destroys freetype resources
246 void cleanup();
247
249 protected:
250
251 struct Row
252 {
253 Row(unsigned int rowTop, unsigned int rowHeight) : width(0), top(rowTop), height(rowHeight) {}
254
255 unsigned int width;
256 unsigned int top;
257 unsigned int height;
258 };
259
260 FT_Library m_library = nullptr; // Handle to the freetype library
261 FT_Face m_face = nullptr; // Contains the font (typeface and style)
262 FT_Stroker m_stroker = nullptr; // Used for rendering outlines
263
264 std::unordered_map<unsigned int, float> m_cachedLineSpacing;
265 std::unordered_map<unsigned int, float> m_cachedFontHeights;
266 std::unordered_map<unsigned int, float> m_cachedAscents;
267 std::unordered_map<unsigned int, float> m_cachedDescents;
268
269 std::unordered_map<std::uint64_t, Glyph> m_glyphs;
270 unsigned int m_nextRow = 3;
271 std::vector<Row> m_rows;
272
273 std::unique_ptr<std::uint8_t[]> m_fileContents;
274 std::unique_ptr<std::uint8_t[]> m_pixels;
275 std::shared_ptr<BackendTexture> m_texture;
276 unsigned int m_textureSize = 0;
277 unsigned int m_textureVersion = 0;
278 };
279
281}
282
284
285#endif // TGUI_BACKEND_FONT_FREETYPE_HPP
Font implementations that uses FreeType directly to load glyphs.
Definition BackendFontFreeType.hpp:53
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:45
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Definition BackendFontFreeType.hpp:215
UIntRect textureRect
Texture coordinates of the glyph inside the font's texture.
Definition BackendFontFreeType.hpp:220
FloatRect bounds
Bounding rectangle of the glyph, in coordinates relative to the baseline.
Definition BackendFontFreeType.hpp:219
Definition BackendFontFreeType.hpp:252
unsigned int height
Height of the row.
Definition BackendFontFreeType.hpp:257
unsigned int width
Current width of the row.
Definition BackendFontFreeType.hpp:255
unsigned int top
Y position of the row into the texture.
Definition BackendFontFreeType.hpp:256
Information about a glyph in the font.
Definition Font.hpp:48