TGUI 1.11
Loading...
Searching...
No Matches
BackendFontSFML.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_SFML_HPP
26#define TGUI_BACKEND_FONT_SFML_HPP
27
28#include <TGUI/Backend/Font/BackendFont.hpp>
29
30#include <SFML/Graphics/Font.hpp>
31
32#include <unordered_set>
33#include <memory>
34#include <map>
35
37
38namespace tgui
39{
43 class TGUI_API BackendFontSFML : public BackendFont
44 {
45 public:
46
55 bool loadFromMemory(std::unique_ptr<std::uint8_t[]> data, std::size_t sizeInBytes) override;
57
65 TGUI_NODISCARD bool hasGlyph(char32_t codePoint) const override;
66
80 TGUI_NODISCARD FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) override;
81
96 TGUI_NODISCARD float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override;
97
107 TGUI_NODISCARD float getLineSpacing(unsigned int characterSize) override;
108
116 TGUI_NODISCARD float getFontHeight(unsigned int characterSize) override;
117
125 TGUI_NODISCARD float getAscent(unsigned int characterSize) override;
126
134 TGUI_NODISCARD float getDescent(unsigned int characterSize) override;
135
145 TGUI_NODISCARD float getUnderlinePosition(unsigned int characterSize) override;
146
156 TGUI_NODISCARD float getUnderlineThickness(unsigned int characterSize) override;
157
166 TGUI_NODISCARD std::shared_ptr<BackendTexture> getTexture(unsigned int characterSize, unsigned int& textureVersion) override;
167
175 TGUI_NODISCARD Vector2u getTextureSize(unsigned int characterSize) override;
176
186 void setSmooth(bool smooth) override;
187
195 void setFontScale(float scale) override;
196
202 TGUI_NODISCARD sf::Font* getInternalFont();
203
205 protected:
206
207 std::unique_ptr<sf::Font> m_font;
208 std::unique_ptr<std::uint8_t[]> m_fileContents;
209
210 std::unordered_set<std::uint64_t> m_loadedGlyphKeys;
211
212 // We keep one texture per character size. Other font backends don't do this, but this is to prevent existing code
213 // from breaking since sf::Font does the same.
214 std::map<unsigned int, std::shared_ptr<BackendTexture>> m_textures;
215 std::map<unsigned int, unsigned int> m_textureVersions;
216
217 // We use a single version that is unique across all text sizes. Otherwise switching from size by changing the font scale
218 // can result in the same version being accidentally returned and the text not realizing that the texture changed.
219 unsigned int m_lastTextureVersion = 0;
220 };
221}
222
224
225#endif // TGUI_BACKEND_FONT_SFML_HPP
Font implementation that makes use of SFML.
Definition BackendFontSFML.hpp:44
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.
bool hasGlyph(char32_t codePoint) const override
Returns whether a font contains a certain glyph.
FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness=0) override
Retrieve a glyph of the font.
float getUnderlineThickness(unsigned int characterSize) override
Get the thickness of the underline.
bool loadFromMemory(std::unique_ptr< std::uint8_t[]> data, std::size_t sizeInBytes) override
Loads a font from memory.
void setSmooth(bool smooth) override
Enable or disable the smooth filter.
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.
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.
Vector2u getTextureSize(unsigned int characterSize) override
Returns the size of the texture that is used to store glyphs of the given character size.
float getDescent(unsigned int characterSize) override
Returns the maximum height of a glyph below the baseline as a negative value.
float getAscent(unsigned int characterSize) override
Returns the maximum height of a glyph above the baseline.
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