TGUI 1.11
Loading...
Searching...
No Matches
BackendFontSDLttf.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_SDL_TTF_HPP
26#define TGUI_BACKEND_FONT_SDL_TTF_HPP
27
28#include <TGUI/extlibs/IncludeSDL.hpp>
29#if SDL_MAJOR_VERSION >= 3
30 #include <SDL3_ttf/SDL_ttf.h>
31#else
32 #include <SDL_ttf.h>
33
34 // To keep code compatible with both SDL2_ttf and SDL3_ttf, renamed SDL2_ttf functions are mapped on their SDL3_ttf equivalent
35 #define TTF_GetFontAscent TTF_FontAscent
36 #define TTF_GetFontDescent TTF_FontDescent
37 #define TTF_GetFontHeight TTF_FontHeight
38 #define TTF_GetFontLineSkip TTF_FontLineSkip
39#endif
40
41#include <TGUI/Backend/Font/BackendFont.hpp>
42
43#include <unordered_map>
44
46
47namespace tgui
48{
52 class TGUI_API BackendFontSDLttf : public BackendFont
53 {
54 public:
55
60
69 bool loadFromMemory(std::unique_ptr<std::uint8_t[]> data, std::size_t sizeInBytes) override;
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
217 TGUI_NODISCARD TTF_Font* getInternalFont(unsigned int characterSize);
218
220 private:
221
223 // Finds the location and thickness of the underline, for the getUnderlinePosition and getUnderlineThickness functions
225 std::pair<int, int> getUnderlineInfo(unsigned int characterSize);
226
228 // Reserves space in the texture to place the glyph
230 UIntRect findAvailableGlyphRect(unsigned int width, unsigned int height);
231
233 private:
234
235 struct Row
236 {
237 Row(unsigned int rowTop, unsigned int rowHeight) : width(0), top(rowTop), height(rowHeight) {}
238
239 unsigned int width;
240 unsigned int top;
241 unsigned int height;
242 };
243
244 std::unique_ptr<std::uint8_t[]> m_fileContents;
245 std::size_t m_fileSize = 0;
246
247 // TTF_Font needs a character size, so we store one font per character size
248 std::unordered_map<unsigned int, TTF_Font*> m_fonts;
249 std::unordered_map<unsigned int, bool> m_lastBoldFlag;
250 std::unordered_map<unsigned int, int> m_lastOutlineThickness;
251
252 std::unordered_map<unsigned int, std::pair<int, int>> m_cachedUnderlineInfo; // character size -> (underline vertical offset, underline thickness)
253
254 std::unordered_map<std::uint64_t, FontGlyph> m_glyphs;
255 unsigned int m_nextRow = 3;
256 std::vector<Row> m_rows;
257
258 std::unique_ptr<std::uint8_t[]> m_pixels;
259 std::shared_ptr<BackendTexture> m_texture;
260 unsigned int m_textureSize = 0;
261 unsigned int m_textureVersion = 0;
262 };
263}
264
266
267#endif // TGUI_BACKEND_FONT_SDL_TTF_HPP
Font implementations that uses SDL_ttf to load glyphs.
Definition BackendFontSDLttf.hpp:53
float getFontHeight(unsigned int characterSize) override
Returns the height required to render a line of text.
float getUnderlinePosition(unsigned int characterSize) override
Get the position of the underline.
float getLineSpacing(unsigned int characterSize) override
Returns the line spacing.
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.
float getUnderlineThickness(unsigned int characterSize) override
Get the thickness of the underline.
float getAscent(unsigned int characterSize) override
Returns the maximum height of a glyph above the baseline.
float getDescent(unsigned int characterSize) override
Returns the maximum height of a glyph below the baseline as a negative value.
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.
FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness=0) override
Retrieve a glyph of the font.
bool hasGlyph(char32_t codePoint) const override
Returns whether a font contains a certain glyph.
float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override
Returns the kerning offset of two glyphs.
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
Information about a glyph in the font.
Definition Font.hpp:46