TGUI 1.11
Loading...
Searching...
No Matches
BackendFontRaylib.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_RAYLIB_HPP
26#define TGUI_BACKEND_FONT_RAYLIB_HPP
27
28#include <TGUI/Backend/Font/BackendFont.hpp>
29
30#include <unordered_map>
31
32struct GlyphInfo;
33
35
36namespace tgui
37{
42 class TGUI_API BackendFontRaylib : public BackendFont
43 {
44 public:
45
54 bool loadFromMemory(std::unique_ptr<std::uint8_t[]> data, std::size_t sizeInBytes) override;
56
64 TGUI_NODISCARD bool hasGlyph(char32_t codePoint) const override;
65
79 TGUI_NODISCARD FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) override;
80
95 TGUI_NODISCARD float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override;
96
106 TGUI_NODISCARD float getLineSpacing(unsigned int characterSize) override;
107
115 TGUI_NODISCARD float getFontHeight(unsigned int characterSize) override;
116
124 TGUI_NODISCARD float getAscent(unsigned int characterSize) override;
125
133 TGUI_NODISCARD float getDescent(unsigned int characterSize) override;
134
144 TGUI_NODISCARD float getUnderlinePosition(unsigned int characterSize) override;
145
155 TGUI_NODISCARD float getUnderlineThickness(unsigned int characterSize) override;
156
165 TGUI_NODISCARD std::shared_ptr<BackendTexture> getTexture(unsigned int characterSize, unsigned int& textureVersion) override;
166
174 TGUI_NODISCARD Vector2u getTextureSize(unsigned int characterSize) override;
175
185 void setSmooth(bool smooth) override;
186
194 void setFontScale(float scale) override;
195
197 protected:
198
200 // Converts and caches the loaded glyph
202 FontGlyph loadGlyph(const GlyphInfo& glyphInfo, char32_t codePoint, unsigned int scaledTextSize, bool bold, float scaledOutlineThickness);
203
205 // Reserves space in the texture to place the glyph
207 TGUI_NODISCARD UIntRect findAvailableGlyphRect(unsigned int width, unsigned int height);
208
210 // Text size in TGUI and FreeType are based on the ascent of the text. Raylib uses stb_truetype and specifically the
211 // stbtt_ScaleForPixelHeight function, which considers the font size to be ascent + descent.
212 // This function estimates which font size we need to give to raylib to get the expected result for our text size.
214 int estimateFontSize(unsigned int scaledTextSize);
215
217 protected:
218
219 struct Row
220 {
221 Row(unsigned int rowTop, unsigned int rowHeight) : width(0), top(rowTop), height(rowHeight) {}
222
223 unsigned int width;
224 unsigned int top;
225 unsigned int height;
226 };
227
228 std::unordered_map<std::uint64_t, FontGlyph> m_glyphs;
229 unsigned int m_nextRow = 3;
230 std::vector<Row> m_rows;
231
232 std::unique_ptr<std::uint8_t[]> m_fileContents;
233 std::size_t m_fileSize = 0;
234
235 std::unique_ptr<std::uint8_t[]> m_pixels;
236 std::shared_ptr<BackendTexture> m_texture;
237 unsigned int m_textureSize = 0;
238 unsigned int m_textureVersion = 0;
239
240 std::unordered_map<unsigned int, int> m_cachedAscents; // text size -> font ascent
241 std::unordered_map<unsigned int, int> m_correctedTextSizes; // text size (ascent) -> raylib text size (ascent + descent)
242 };
243
245}
246
248
249#endif // TGUI_BACKEND_FONT_RAYLIB_HPP
Font implementations that uses Raylib to load glyphs.
Definition BackendFontRaylib.hpp:43
unsigned int m_nextRow
Y position of the next new row in the texture (first 2 rows contain pixels for underlining)
Definition BackendFontRaylib.hpp:229
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.
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.
Vector2u getTextureSize(unsigned int characterSize) override
Returns the size of the texture that is used to store glyphs of the given character size.
float getUnderlineThickness(unsigned int characterSize) override
Get the thickness of the underline.
bool hasGlyph(char32_t codePoint) const override
Returns whether a font contains a certain glyph.
float getLineSpacing(unsigned int characterSize) override
Returns the line spacing.
float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override
Returns the kerning offset of two glyphs.
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.
float getAscent(unsigned int characterSize) override
Returns the maximum height of a glyph above the baseline.
bool loadFromMemory(std::unique_ptr< std::uint8_t[]> data, std::size_t sizeInBytes) override
Loads a font from memory.
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
unsigned int width
Current width of the row.
Definition BackendFontRaylib.hpp:223
unsigned int height
Height of the row.
Definition BackendFontRaylib.hpp:225
unsigned int top
Y position of the row into the texture.
Definition BackendFontRaylib.hpp:224
Information about a glyph in the font.
Definition Font.hpp:46