TGUI  1.1
Loading...
Searching...
No Matches
BackendFontSDLttf.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2023 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_SDL_TTF_HPP
27#define TGUI_BACKEND_FONT_SDL_TTF_HPP
28
29#include <TGUI/extlibs/IncludeSDL.hpp>
30#if SDL_MAJOR_VERSION >= 3
31 #include <SDL3_ttf/SDL_ttf.h>
32#else
33 #include <SDL_ttf.h>
34#endif
35
36#include <TGUI/Config.hpp>
37#if TGUI_BUILD_AS_CXX_MODULE
38 import tgui;
39#else
40 #include <TGUI/Backend/Font/BackendFont.hpp>
41#endif
42
43#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
44 #include <unordered_map>
45#endif
46
48
49TGUI_MODULE_EXPORT namespace tgui
50{
55 {
56 public:
57
62
63
72 bool loadFromMemory(std::unique_ptr<std::uint8_t[]> data, std::size_t sizeInBytes) override;
74
75
83 TGUI_NODISCARD bool hasGlyph(char32_t codePoint) const override;
84
85
99 TGUI_NODISCARD FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) override;
100
101
116 TGUI_NODISCARD float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override;
117
118
128 TGUI_NODISCARD float getLineSpacing(unsigned int characterSize) override;
129
130
138 TGUI_NODISCARD float getFontHeight(unsigned int characterSize) override;
139
140
148 TGUI_NODISCARD float getAscent(unsigned int characterSize) override;
149
150
158 TGUI_NODISCARD float getDescent(unsigned int characterSize) override;
159
160
170 TGUI_NODISCARD float getUnderlinePosition(unsigned int characterSize) override;
171
172
182 TGUI_NODISCARD float getUnderlineThickness(unsigned int characterSize) override;
183
184
193 TGUI_NODISCARD std::shared_ptr<BackendTexture> getTexture(unsigned int characterSize, unsigned int& textureVersion) override;
194
195
203 TGUI_NODISCARD Vector2u getTextureSize(unsigned int characterSize) override;
204
205
215 void setSmooth(bool smooth) override;
216
217
225 void setFontScale(float scale) override;
226
227
234 TGUI_NODISCARD TTF_Font* getInternalFont(unsigned int characterSize);
235
236
238 private:
239
241 // Finds the location and thickness of the underline, for the getUnderlinePosition and getUnderlineThickness functions
243 std::pair<int, int> getUnderlineInfo(unsigned int characterSize);
244
245
247 // Reserves space in the texture to place the glyph
249 UIntRect findAvailableGlyphRect(unsigned int width, unsigned int height);
250
251
253 private:
254
255 struct Row
256 {
257 Row(unsigned int rowTop, unsigned int rowHeight) : width(0), top(rowTop), height(rowHeight) {}
258
259 unsigned int width;
260 unsigned int top;
261 unsigned int height;
262 };
263
264 std::unique_ptr<std::uint8_t[]> m_fileContents;
265 std::size_t m_fileSize = 0;
266
267 // TTF_Font needs a character size, so we store one font per character size
268 std::unordered_map<unsigned int, TTF_Font*> m_fonts;
269 std::unordered_map<unsigned int, bool> m_lastBoldFlag;
270 std::unordered_map<unsigned int, int> m_lastOutlineThickness;
271
272 std::unordered_map<unsigned int, std::pair<int, int>> m_cachedUnderlineInfo; // character size -> (underline vertical offset, underline thickness)
273
274 std::unordered_map<std::uint64_t, FontGlyph> m_glyphs;
275 unsigned int m_nextRow = 3;
276 std::vector<Row> m_rows;
277
278 std::unique_ptr<std::uint8_t[]> m_pixels;
279 std::shared_ptr<BackendTexture> m_texture;
280 unsigned int m_textureSize = 0;
281 unsigned int m_textureVersion = 0;
282 };
283}
284
286
287#endif // TGUI_BACKEND_FONT_SDL_TTF_HPP
Font implementations that uses SDL_ttf to load glyphs.
Definition BackendFontSDLttf.hpp:55
TGUI_NODISCARD float getUnderlineThickness(unsigned int characterSize) override
Get the thickness of the underline.
TGUI_NODISCARD bool hasGlyph(char32_t codePoint) const override
Returns whether a font contains a certain glyph.
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.
~BackendFontSDLttf() override
Destructor that cleans up the SDL resources.
TGUI_NODISCARD float getDescent(unsigned int characterSize) override
Returns the maximum height of a glyph below the baseline.
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 getUnderlinePosition(unsigned int characterSize) override
Get the position of the underline.
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 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.
TGUI_NODISCARD float getFontHeight(unsigned int characterSize) override
Returns the height required to render a line of text.
TGUI_NODISCARD float getLineSpacing(unsigned int characterSize) override
Returns the line spacing.
Base class for font implementations that depend on the backend.
Definition BackendFont.hpp:46
virtual bool loadFromMemory(std::unique_ptr< std::uint8_t[]> data, std::size_t sizeInBytes)=0
Loads a font from memory.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
Information about a glyph in the font.
Definition Font.hpp:50