TGUI  1.6.1
Loading...
Searching...
No Matches
BackendFontRaylib.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_RAYLIB_HPP
26#define TGUI_BACKEND_FONT_RAYLIB_HPP
27
28#include <TGUI/Config.hpp>
29#if !TGUI_BUILD_AS_CXX_MODULE
30 #include <TGUI/Backend/Font/BackendFont.hpp>
31
32 struct GlyphInfo;
33#endif
34
35#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
36 #include <unordered_map>
37#endif
38
40
41TGUI_MODULE_EXPORT namespace tgui
42{
47 class TGUI_API BackendFontRaylib : public BackendFont
48 {
49 public:
50
59 bool loadFromMemory(std::unique_ptr<std::uint8_t[]> data, std::size_t sizeInBytes) override;
60 using BackendFont::loadFromMemory;
61
69 TGUI_NODISCARD bool hasGlyph(char32_t codePoint) const override;
70
84 TGUI_NODISCARD FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) override;
85
100 TGUI_NODISCARD float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override;
101
111 TGUI_NODISCARD float getLineSpacing(unsigned int characterSize) override;
112
120 TGUI_NODISCARD float getFontHeight(unsigned int characterSize) override;
121
129 TGUI_NODISCARD float getAscent(unsigned int characterSize) override;
130
138 TGUI_NODISCARD float getDescent(unsigned int characterSize) override;
139
149 TGUI_NODISCARD float getUnderlinePosition(unsigned int characterSize) override;
150
160 TGUI_NODISCARD float getUnderlineThickness(unsigned int characterSize) override;
161
170 TGUI_NODISCARD std::shared_ptr<BackendTexture> getTexture(unsigned int characterSize, unsigned int& textureVersion) override;
171
179 TGUI_NODISCARD Vector2u getTextureSize(unsigned int characterSize) override;
180
190 void setSmooth(bool smooth) override;
191
199 void setFontScale(float scale) override;
200
202 protected:
203
205 // Converts and caches the loaded glyph
207 FontGlyph loadGlyph(const GlyphInfo& glyphInfo, char32_t codePoint, unsigned int scaledTextSize, bool bold, float scaledOutlineThickness);
208
210 // Reserves space in the texture to place the glyph
212 TGUI_NODISCARD UIntRect findAvailableGlyphRect(unsigned int width, unsigned int height);
213
215 // Text size in TGUI and FreeType are based on the ascent of the text. Raylib uses stb_truetype and specifically the
216 // stbtt_ScaleForPixelHeight function, which considers the font size to be ascent + descent.
217 // This function estimates which font size we need to give to raylib to get the expected result for our text size.
219 int estimateFontSize(unsigned int scaledTextSize);
220
222 protected:
223
224 struct Row
225 {
226 Row(unsigned int rowTop, unsigned int rowHeight) : width(0), top(rowTop), height(rowHeight) {}
227
228 unsigned int width;
229 unsigned int top;
230 unsigned int height;
231 };
232
233 std::unordered_map<std::uint64_t, FontGlyph> m_glyphs;
234 unsigned int m_nextRow = 3;
235 std::vector<Row> m_rows;
236
237 std::unique_ptr<std::uint8_t[]> m_fileContents;
238 std::size_t m_fileSize = 0;
239
240 std::unique_ptr<std::uint8_t[]> m_pixels;
241 std::shared_ptr<BackendTexture> m_texture;
242 unsigned int m_textureSize = 0;
243 unsigned int m_textureVersion = 0;
244
245 std::unordered_map<unsigned int, int> m_cachedAscents; // text size -> font ascent
246 std::unordered_map<unsigned int, int> m_correctedTextSizes; // text size (ascent) -> raylib text size (ascent + descent)
247 };
248
250}
251
253
254#endif // TGUI_BACKEND_FONT_RAYLIB_HPP
Font implementations that uses Raylib to load glyphs.
Definition BackendFontRaylib.hpp:48
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.
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.
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 BackendFontRaylib.hpp:225
unsigned int width
Current width of the row.
Definition BackendFontRaylib.hpp:228
unsigned int height
Height of the row.
Definition BackendFontRaylib.hpp:230
unsigned int top
Y position of the row into the texture.
Definition BackendFontRaylib.hpp:229
Information about a glyph in the font.
Definition Font.hpp:48