TGUI  0.9-dev
BackendFontSDLttf.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2021 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
26#ifndef TGUI_BACKEND_FONT_SDL_TTF_HPP
27#define TGUI_BACKEND_FONT_SDL_TTF_HPP
28
29#include <TGUI/Backend/Font/BackendFont.hpp>
30#include <SDL_ttf.h>
31#include <unordered_map>
32
34
35namespace tgui
36{
41 {
42 public:
43
48
49
58 virtual bool loadFromMemory(std::unique_ptr<std::uint8_t[]> data, std::size_t sizeInBytes) override;
60
61
69 bool hasGlyph(char32_t codePoint) const override;
70
71
85 virtual FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) override;
86
87
102 virtual float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override;
103#ifndef TGUI_REMOVE_DEPRECATED_CODE
104 using BackendFont::getKerning; // Import version without bold parameter
105#endif
106
107
117 virtual float getLineSpacing(unsigned int characterSize) override;
118
119
129 float getUnderlinePosition(unsigned int characterSize) override;
130
131
141 float getUnderlineThickness(unsigned int characterSize) override;
142
143
151 std::shared_ptr<BackendTexture> getTexture(unsigned int characterSize) override;
152
153
163 void setSmooth(bool smooth) override;
164
165
172 TTF_Font* getInternalFont(unsigned int characterSize);
173
174
175#ifndef TGUI_REMOVE_DEPRECATED_CODE
180 TGUI_DEPRECATED("loadInternalFont should no longer be accessed!") TTF_Font* loadInternalFont(unsigned int characterSize) const;
181#endif
182
183
185 private:
186
188 // Finds the location and thickness of the underline, for the getUnderlinePosition and getUnderlineThickness functions
190 std::pair<int, int> getUnderlineInfo(unsigned int characterSize);
191
192
194 // Reserves space in the texture to place the glyph
196 IntRect findAvailableGlyphRect(unsigned int width, unsigned int height);
197
198
200 private:
201
202 struct Row
203 {
204 Row(unsigned int rowTop, unsigned int rowHeight) : width(0), top(rowTop), height(rowHeight) {}
205
206 unsigned int width;
207 unsigned int top;
208 unsigned int height;
209 };
210
211 std::unique_ptr<std::uint8_t[]> m_fileContents;
212 std::size_t m_fileSize = 0;
213
214 // TTF_Font needs a character size, so we store one font per character size
215 std::unordered_map<unsigned int, TTF_Font*> m_fonts;
216 std::unordered_map<unsigned int, bool> m_lastBoldFlag;
217 std::unordered_map<unsigned int, float> m_lastOutlineThickness;
218
219 std::unordered_map<unsigned int, std::pair<int, int>> m_cachedUnderlineInfo; // character size -> (underline vertical offset, underline thickness)
220
221 std::unordered_map<std::uint64_t, FontGlyph> m_glyphs;
222 unsigned int m_nextRow = 3;
223 std::vector<Row> m_rows;
224
225 std::unique_ptr<std::uint8_t[]> m_pixels;
226 std::shared_ptr<BackendTexture> m_texture;
227 unsigned int m_textureSize = 0;
228 bool m_textureUpToDate = false;
229 };
230
232
233#ifndef TGUI_REMOVE_DEPRECATED_CODE
234 using BackendFontSDL TGUI_DEPRECATED("BackendFontSDL was renamed to BackendFontSDLttf") = BackendFontSDLttf;
235#endif
236}
237
239
240#endif // TGUI_BACKEND_FONT_SDL_TTF_HPP
Font implementations that uses SDL_ttf to load glyphs.
Definition: BackendFontSDLttf.hpp:41
float getUnderlinePosition(unsigned int characterSize) override
Get the position of the underline.
float getUnderlineThickness(unsigned int characterSize) override
Get the thickness of the underline.
virtual float getLineSpacing(unsigned int characterSize) override
Returns the line spacing.
std::shared_ptr< BackendTexture > getTexture(unsigned int characterSize) override
Returns the texture that is used to store glyphs of the given character size.
virtual 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.
virtual float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold) override
Returns the kerning offset of two glyphs.
virtual FontGlyph getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness=0) override
Retrieve a glyph of the font.
bool hasGlyph(char32_t codePoint) const override
Returns whether a font contains a certain glyph.
~BackendFontSDLttf()
Destructor that cleans up the SDL resources.
Base class for font implementations that depend on the backend.
Definition: BackendFont.hpp:43
virtual bool loadFromMemory(const void *data, std::size_t sizeInBytes)
Loads a font from memory.
virtual float getKerning(char32_t first, char32_t second, unsigned int characterSize)
Returns the kerning offset of two glyphs.
Definition: BackendFont.hpp:129
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Information about a glyph in the font.
Definition: Font.hpp:47