TGUI 1.13
Loading...
Searching...
No Matches
BackendFontRaylib.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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:
53 [[nodiscard]] bool loadFromMemory(std::unique_ptr<std::uint8_t[]> data, std::size_t sizeInBytes) override;
55
63 [[nodiscard]] bool hasGlyph(char32_t codePoint) const override;
64
78 [[nodiscard]] FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) override;
79
94 [[nodiscard]] float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override;
95
105 [[nodiscard]] float getLineSpacing(unsigned int characterSize) override;
106
114 [[nodiscard]] float getFontHeight(unsigned int characterSize) override;
115
123 [[nodiscard]] float getAscent(unsigned int characterSize) override;
124
132 [[nodiscard]] float getDescent(unsigned int characterSize) override;
133
143 [[nodiscard]] float getUnderlinePosition(unsigned int characterSize) override;
144
154 [[nodiscard]] float getUnderlineThickness(unsigned int characterSize) override;
155
164 [[nodiscard]] std::shared_ptr<BackendTexture> getTexture(unsigned int characterSize, unsigned int& textureVersion) override;
165
173 [[nodiscard]] Vector2u getTextureSize(unsigned int characterSize) override;
174
184 void setSmooth(bool smooth) override;
185
193 void setFontScale(float scale) override;
194
196
197 protected:
199 // Converts and caches the loaded glyph
201 FontGlyph loadGlyph(const GlyphInfo& glyphInfo, char32_t codePoint, unsigned int scaledTextSize, bool bold, float scaledOutlineThickness);
202
204 // Reserves space in the texture to place the glyph
206 [[nodiscard]] UIntRect findAvailableGlyphRect(unsigned int width, unsigned int height);
207
209 // Text size in TGUI and FreeType are based on the ascent of the text. Raylib uses stb_truetype and specifically the
210 // stbtt_ScaleForPixelHeight function, which considers the font size to be ascent + descent.
211 // This function estimates which font size we need to give to raylib to get the expected result for our text size.
213 int estimateFontSize(unsigned int scaledTextSize);
214
216
217 protected:
218 struct Row
219 {
220 Row(unsigned int rowTop, unsigned int rowHeight) :
221 width(0),
222 top(rowTop),
223 height(rowHeight)
224 {
225 }
226
227 unsigned int width;
228 unsigned int top;
229 unsigned int height;
230 };
231
232 std::unordered_map<std::uint64_t, FontGlyph> m_glyphs;
233 unsigned int m_nextRow = 3;
234 std::vector<Row> m_rows;
235
236 std::unique_ptr<std::uint8_t[]> m_fileContents;
237 std::size_t m_fileSize = 0;
238
239 std::unique_ptr<std::uint8_t[]> m_pixels;
240 std::shared_ptr<BackendTexture> m_texture;
241 unsigned int m_textureSize = 0;
242 unsigned int m_textureVersion = 0;
243
244 std::unordered_map<unsigned int, int> m_cachedAscents; // text size -> font ascent
245 std::unordered_map<unsigned int, int> m_correctedTextSizes; // text size (ascent) -> raylib text size (ascent + descent)
246 };
247} // namespace tgui
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:233
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:37
unsigned int width
Current width of the row.
Definition BackendFontRaylib.hpp:227
unsigned int height
Height of the row.
Definition BackendFontRaylib.hpp:229
unsigned int top
Y position of the row into the texture.
Definition BackendFontRaylib.hpp:228
Information about a glyph in the font.
Definition Font.hpp:44