TGUI  1.3-dev
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#endif
32
33#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
34 #include <unordered_map>
35#endif
36
37struct GlyphInfo;
38
40
41TGUI_MODULE_EXPORT namespace tgui
42{
47 {
48 public:
49
58 bool loadFromMemory(std::unique_ptr<std::uint8_t[]> data, std::size_t sizeInBytes) override;
60
68 TGUI_NODISCARD bool hasGlyph(char32_t codePoint) const override;
69
83 TGUI_NODISCARD FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) override;
84
99 TGUI_NODISCARD float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override;
100
110 TGUI_NODISCARD float getLineSpacing(unsigned int characterSize) override;
111
119 TGUI_NODISCARD float getFontHeight(unsigned int characterSize) override;
120
128 TGUI_NODISCARD float getAscent(unsigned int characterSize) override;
129
137 TGUI_NODISCARD float getDescent(unsigned int characterSize) override;
138
148 TGUI_NODISCARD float getUnderlinePosition(unsigned int characterSize) override;
149
159 TGUI_NODISCARD float getUnderlineThickness(unsigned int characterSize) override;
160
169 TGUI_NODISCARD std::shared_ptr<BackendTexture> getTexture(unsigned int characterSize, unsigned int& textureVersion) override;
170
178 TGUI_NODISCARD Vector2u getTextureSize(unsigned int characterSize) override;
179
189 void setSmooth(bool smooth) override;
190
198 void setFontScale(float scale) override;
199
201 protected:
202
204 // Converts and caches the loaded glyph
206 FontGlyph loadGlyph(const GlyphInfo& glyphInfo, char32_t codePoint, unsigned int scaledTextSize, bool bold, float scaledOutlineThickness);
207
209 // Reserves space in the texture to place the glyph
211 TGUI_NODISCARD UIntRect findAvailableGlyphRect(unsigned int width, unsigned int height);
212
214 // Text size in TGUI and FreeType are based on the ascent of the text. Raylib uses stb_truetype and specifically the
215 // stbtt_ScaleForPixelHeight function, which considers the font size to be ascent + descent.
216 // This function estimates which font size we need to give to raylib to get the expected result for our text size.
218 int estimateFontSize(unsigned int scaledTextSize);
219
221 protected:
222
223 struct Row
224 {
225 Row(unsigned int rowTop, unsigned int rowHeight) : width(0), top(rowTop), height(rowHeight) {}
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
249}
250
252
253#endif // TGUI_BACKEND_FONT_RAYLIB_HPP
Font implementations that uses Raylib to load glyphs.
Definition BackendFontRaylib.hpp:47
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
TGUI_NODISCARD float getLineSpacing(unsigned int characterSize) override
Returns the line spacing.
TGUI_NODISCARD bool hasGlyph(char32_t codePoint) const override
Returns whether a font contains a certain glyph.
TGUI_NODISCARD float getDescent(unsigned int characterSize) override
Returns the maximum height of a glyph below the baseline.
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.
void setSmooth(bool smooth) override
Enable or disable the smooth filter.
TGUI_NODISCARD float getUnderlineThickness(unsigned int characterSize) override
Get the thickness of the underline.
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 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 getAscent(unsigned int characterSize) override
Returns the maximum height of a glyph above the baseline.
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.
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
Definition BackendFontRaylib.hpp:224
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:50