Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
CGUITTFont.h
Go to the documentation of this file.
1/*
2 CGUITTFont FreeType class for Irrlicht
3 Copyright (c) 2009-2010 John Norman
4 Copyright (c) 2016 Nathanaƫlle Courant
5 Copyright (c) 2023 Caleb Butler
6 Copyright (c) 2024 Luanti contributors
7
8 This software is provided 'as-is', without any express or implied
9 warranty. In no event will the authors be held liable for any
10 damages arising from the use of this software.
11
12 Permission is granted to anyone to use this software for any
13 purpose, including commercial applications, and to alter it and
14 redistribute it freely, subject to the following restrictions:
15
16 1. The origin of this software must not be misrepresented; you
17 must not claim that you wrote the original software. If you use
18 this software in a product, an acknowledgment in the product
19 documentation would be appreciated but is not required.
20
21 2. Altered source versions must be plainly marked as such, and
22 must not be misrepresented as being the original software.
23
24 3. This notice may not be removed or altered from any source
25 distribution.
26
27 The original version of this class can be located at:
28 http://irrlicht.suckerfreegames.com/
29
30 John Norman
31 john@suckerfreegames.com
32*/
33
34#pragma once
35
36#include <ft2build.h>
37#include <freetype/freetype.h>
38
39#include "irr_ptr.h"
40#include "IGUIEnvironment.h"
41#include "IGUIFont.h"
42#include "IVideoDriver.h"
44#include "util/basic_macros.h"
45
46#include <map>
47
48namespace gui
49{
50 class CGUITTFont;
51
52 // Manages the FT_Face cache.
53 struct SGUITTFace : public IReferenceCounted
54 {
55 private:
56
57 static FT_Library freetype_library;
58 static size_t n_faces;
59
60 static FT_Library getFreeTypeLibrary();
61
62 // This holds the font file data for this face.
63 // Must not be deallocated until we are done with the face!
64 std::string face_buffer;
65
66 public:
67 SGUITTFace(std::string &&buffer);
69
70 FT_Face face;
71
72 static SGUITTFace* createFace(std::string &&buffer);
73
74 static SGUITTFace* loadFace(const io::path &filename);
75 };
76
79 {
81 glyph_page(0),
83 offset(),
84 advance()
85 {}
87
89 inline bool isLoaded() const {
90 return source_rect != core::recti();
91 }
92
94 void preload(u32 char_index, FT_Face face, CGUITTFont *parent, u32 font_size, const FT_Int32 loadFlags);
95
97 void unload();
98
100 video::IImage* createGlyphImage(const FT_Bitmap &bits, video::IVideoDriver *driver) const;
101
104
106 core::recti source_rect;
107
109 core::vector2di offset;
110
112 core::vector2di advance;
113 };
114
117 SGUITTGlyphPending(const SGUITTGlyph *glyph, video::IImage *surface) noexcept :
119 {}
121 if (surface)
122 surface->drop();
123 }
124
126
128 glyph(other.glyph), surface(other.surface)
129 {
130 other.surface = nullptr;
131 }
132
134 video::IImage *surface;
135 };
136
139 {
140 public:
141 CGUITTGlyphPage(video::IVideoDriver *Driver, const io::path &texture_name) :
143 driver(Driver), name(texture_name)
144 {}
146 {
147 if (texture)
148 driver->removeTexture(texture);
149 }
150
152 bool createPageTexture(u8 pixel_mode, core::dimension2du texture_size);
153
156 void pushGlyphToBePaged(const SGUITTGlyph *glyph, video::IImage *surface)
157 {
158 if (!glyph || !surface)
159 return;
160 glyph_to_be_paged.emplace_back(glyph, surface);
161 }
162
163 inline bool isDirty() const
164 {
165 return !glyph_to_be_paged.empty();
166 }
167
169 void updateTexture();
170
171 video::ITexture* texture;
174
175 std::vector<core::vector2di> render_positions;
176 std::vector<core::recti> render_source_rects;
177 std::vector<video::SColor> render_colors;
178
179 private:
180 std::vector<SGUITTGlyphPending> glyph_to_be_paged;
181 video::IVideoDriver* driver;
182 io::path name;
183 };
184
186 class CGUITTFont final : public IGUIFont
187 {
188 public:
195 static CGUITTFont* createTTFont(IGUIEnvironment *env,
196 SGUITTFace *face, u32 size, bool antialias = true,
197 bool preload = true, u32 shadow = 0, u32 shadow_alpha = 255);
198
200 virtual ~CGUITTFont();
201
203 void setBatchLoadSize(u32 batch_size) { batch_load_size = batch_size; }
204
206 u32 getFontSize() const { return size; }
207
210 bool useAutoHinting() const { return use_auto_hinting; }
211
213 bool useHinting() const { return use_hinting; }
214
217 bool useMonochrome() const { return use_monochrome; }
218
222 void setMonochrome(const bool flag);
223
228 void setFontHinting(const bool enable, const bool enable_auto_hinting = true);
229
231 virtual void draw(const core::stringw& utext, const core::rect<s32>& position,
232 video::SColor color, bool hcenter=false, bool vcenter=false,
233 const core::rect<s32>* clip=0) override;
234
235 void draw(const EnrichedString& text, const core::rect<s32>& position,
236 bool hcenter=false, bool vcenter=false,
237 const core::rect<s32>* clip=0);
238
240 virtual core::dimension2du getDimension(const wchar_t* text) const override;
241
243 virtual s32 getCharacterFromPos(const wchar_t* text, s32 pixel_x) const override;
244
246 virtual void setKerningWidth(s32 kerning) override;
247
249 virtual void setKerningHeight(s32 kerning) override;
250
252 virtual core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const override;
253
255 virtual void setInvisibleCharacters(const wchar_t *s) override;
256
260
263 //should be better typed. fix later.
264 CGUITTGlyphPage* createGlyphPage(const u8 pixel_mode);
265
267 u32 getLastGlyphPageIndex() const { return Glyph_Pages.size() - 1; }
268
270 void setFallback(gui::IGUIFont *font);
271
272 inline s32 getAscender() const { return font_metrics.ascender; }
273
274 protected:
278 u32 size;
280
281 private:
282 // Helper functions for the same-named public member functions above
283 // (Since std::u32string is nicer to work with than wchar_t *)
284 core::dimension2d<u32> getDimension(const std::u32string& text) const;
285 s32 getCharacterFromPos(const std::u32string& text, s32 pixel_x) const;
286
287 // Helper function for the above helper functions :P
288 std::u32string convertWCharToU32String(const wchar_t* const) const;
289
290 CGUITTFont(IGUIEnvironment *env);
291 bool load(SGUITTFace *face, const u32 size, const bool antialias,
292 const bool transparency, const bool preload);
293
294 void reset_images();
295 void update_glyph_pages() const;
297 {
298 // Set up our loading flags.
299 load_flags = FT_LOAD_DEFAULT | FT_LOAD_RENDER;
300 if (!useHinting())
301 load_flags |= FT_LOAD_NO_HINTING;
302 if (!useAutoHinting())
303 load_flags |= FT_LOAD_NO_AUTOHINT;
304 if (useMonochrome())
305 load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO;
306 else
307 load_flags |= FT_LOAD_TARGET_NORMAL;
308 }
309
311 inline u32 getLineHeight() const { return font_metrics.height / 64 + 1; }
312 u32 getWidthFromCharacter(char32_t c) const;
313 u32 getHeightFromCharacter(char32_t c) const;
316 u32 getGlyphIndexByChar(char32_t c) const;
317 core::vector2di getKerning(const char32_t thisLetter, const char32_t previousLetter) const;
318
319 video::IVideoDriver* Driver = nullptr;
320 FT_Face tt_face;
321 FT_Size_Metrics font_metrics;
322 FT_Int32 load_flags;
323
324 mutable core::array<CGUITTGlyphPage*> Glyph_Pages;
325 mutable core::array<SGUITTGlyph> Glyphs;
326
329 std::u32string InvisibleChars;
332
333 irr_ptr<gui::IGUIFont> fallback;
334 };
335
336} // end namespace gui
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:26
Definition enriched_string.h:28
Class representing a TrueType font.
Definition CGUITTFont.h:187
static CGUITTFont * createTTFont(IGUIEnvironment *env, SGUITTFace *face, u32 size, bool antialias=true, bool preload=true, u32 shadow=0, u32 shadow_alpha=255)
Creates a new TrueType font and returns a pointer to it.
Definition CGUITTFont.cpp:283
video::IVideoDriver * Driver
Definition CGUITTFont.h:319
bool use_hinting
Definition CGUITTFont.h:276
u32 size
Definition CGUITTFont.h:278
virtual ~CGUITTFont()
Destructor.
Definition CGUITTFont.cpp:357
void setBatchLoadSize(u32 batch_size)
Sets the amount of glyphs to batch load.
Definition CGUITTFont.h:203
void setMonochrome(const bool flag)
Tells the font to use monochrome rendering.
Definition CGUITTFont.cpp:453
void setFontHinting(const bool enable, const bool enable_auto_hinting=true)
Enables or disables font hinting.
Definition CGUITTFont.cpp:459
virtual void draw(const core::stringw &utext, const core::rect< s32 > &position, video::SColor color, bool hcenter=false, bool vcenter=false, const core::rect< s32 > *clip=0) override
Draws some text and clips it to the specified rectangle if wanted.
Definition CGUITTFont.cpp:466
virtual void setInvisibleCharacters(const wchar_t *s) override
Define which characters should not be drawn by the font.
Definition CGUITTFont.cpp:879
bool use_auto_hinting
Definition CGUITTFont.h:277
FT_Face tt_face
Definition CGUITTFont.h:320
CGUITTGlyphPage * getLastGlyphPage() const
Get the last glyph page if there's still available slots.
Definition CGUITTFont.cpp:392
CGUITTGlyphPage * createGlyphPage(const u8 pixel_mode)
Create a new glyph page texture.
Definition CGUITTFont.cpp:402
bool useMonochrome() const
Check if the font is being loaded as a monochrome font.
Definition CGUITTFont.h:217
virtual core::dimension2du getDimension(const wchar_t *text) const override
Returns the dimension of a text string.
Definition CGUITTFont.cpp:642
void setFallback(gui::IGUIFont *font)
Set font that should be used for glyphs not present in ours.
Definition CGUITTFont.cpp:447
CGUITTFont(IGUIEnvironment *env)
Constructor.
Definition CGUITTFont.cpp:303
u32 getFontSize() const
Get the font size.
Definition CGUITTFont.h:206
u32 shadow_offset
Definition CGUITTFont.h:330
virtual void setKerningHeight(s32 kerning) override
Sets global kerning height for the font.
Definition CGUITTFont.cpp:824
bool use_monochrome
Definition CGUITTFont.h:275
u32 getLastGlyphPageIndex() const
Get the last glyph page's index.
Definition CGUITTFont.h:267
void update_load_flags()
Definition CGUITTFont.h:296
std::u32string InvisibleChars
Definition CGUITTFont.h:329
virtual core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const override
Returns the distance between letters.
Definition CGUITTFont.cpp:829
u32 getWidthFromCharacter(char32_t c) const
Definition CGUITTFont.cpp:703
FT_Size_Metrics font_metrics
Definition CGUITTFont.h:321
u32 getGlyphIndexByChar(char32_t c) const
Returns (index + 1) of this->Glyphs Returns 0 if no such glyph is provided by the font.
Definition CGUITTFont.cpp:742
FT_Int32 load_flags
Definition CGUITTFont.h:322
s32 GlobalKerningWidth
Definition CGUITTFont.h:327
u32 getHeightFromCharacter(char32_t c) const
Definition CGUITTFont.cpp:722
virtual s32 getCharacterFromPos(const wchar_t *text, s32 pixel_x) const override
Calculates the index of the character in the text which is on a specific position.
Definition CGUITTFont.cpp:787
core::array< CGUITTGlyphPage * > Glyph_Pages
Definition CGUITTFont.h:324
void reset_images()
Definition CGUITTFont.cpp:368
u32 batch_load_size
Definition CGUITTFont.h:279
s32 GlobalKerningHeight
Definition CGUITTFont.h:328
core::array< SGUITTGlyph > Glyphs
Definition CGUITTFont.h:325
u32 shadow_alpha
Definition CGUITTFont.h:331
bool load(SGUITTFace *face, const u32 size, const bool antialias, const bool transparency, const bool preload)
Definition CGUITTFont.cpp:319
virtual void setKerningWidth(s32 kerning) override
Sets global kerning width for the font.
Definition CGUITTFont.cpp:819
u32 getLineHeight() const
Gets the overall font height, including a line gap of 1 px.
Definition CGUITTFont.h:311
void update_glyph_pages() const
Definition CGUITTFont.cpp:383
s32 getAscender() const
Definition CGUITTFont.h:272
std::u32string convertWCharToU32String(const wchar_t *const) const
Definition CGUITTFont.cpp:884
irr_ptr< gui::IGUIFont > fallback
Definition CGUITTFont.h:333
bool useAutoHinting() const
Check if the font auto-hinting is enabled.
Definition CGUITTFont.h:210
bool useHinting() const
Check if the font hinting is enabled.
Definition CGUITTFont.h:213
Holds a sheet of glyphs.
Definition CGUITTFont.h:139
io::path name
Definition CGUITTFont.h:182
video::ITexture * texture
Definition CGUITTFont.h:171
u32 available_slots
Definition CGUITTFont.h:172
std::vector< video::SColor > render_colors
Definition CGUITTFont.h:177
void pushGlyphToBePaged(const SGUITTGlyph *glyph, video::IImage *surface)
Add the glyph to a list of glyphs to be paged.
Definition CGUITTFont.h:156
bool isDirty() const
Definition CGUITTFont.h:163
video::IVideoDriver * driver
Definition CGUITTFont.h:181
std::vector< SGUITTGlyphPending > glyph_to_be_paged
Definition CGUITTFont.h:180
bool createPageTexture(u8 pixel_mode, core::dimension2du texture_size)
Create the actual page texture,.
Definition CGUITTFont.cpp:230
~CGUITTGlyphPage()
Definition CGUITTFont.h:145
void updateTexture()
Updates the texture atlas with new glyphs.
Definition CGUITTFont.cpp:260
std::vector< core::recti > render_source_rects
Definition CGUITTFont.h:176
std::vector< core::vector2di > render_positions
Definition CGUITTFont.h:175
u32 used_slots
Definition CGUITTFont.h:173
CGUITTGlyphPage(video::IVideoDriver *Driver, const io::path &texture_name)
Definition CGUITTFont.h:141
Definition fontengine.h:16
Definition CGUITTFont.h:54
static FT_Library getFreeTypeLibrary()
Definition CGUITTFont.cpp:50
std::string face_buffer
Definition CGUITTFont.h:64
static SGUITTFace * createFace(std::string &&buffer)
Definition CGUITTFont.cpp:79
static size_t n_faces
Definition CGUITTFont.h:58
static SGUITTFace * loadFace(const io::path &filename)
Definition CGUITTFont.cpp:91
FT_Face face
Definition CGUITTFont.h:70
static FT_Library freetype_library
Definition CGUITTFont.h:57
SGUITTFace(std::string &&buffer)
Definition CGUITTFont.cpp:61
~SGUITTFace()
Definition CGUITTFont.cpp:67
Wrapper struct for a preloaded glyph.
Definition CGUITTFont.h:116
video::IImage * surface
Definition CGUITTFont.h:134
SGUITTGlyphPending(const SGUITTGlyph *glyph, video::IImage *surface) noexcept
Definition CGUITTFont.h:117
const SGUITTGlyph * glyph
Definition CGUITTFont.h:133
~SGUITTGlyphPending()
Definition CGUITTFont.h:120
Structure representing a single TrueType glyph.
Definition CGUITTFont.h:79
core::vector2di offset
The offset of glyph when drawn.
Definition CGUITTFont.h:109
video::IImage * createGlyphImage(const FT_Bitmap &bits, video::IVideoDriver *driver) const
Creates the IImage object from the FT_Bitmap.
Definition CGUITTFont.cpp:103
SGUITTGlyph()
Definition CGUITTFont.h:80
~SGUITTGlyph()
Definition CGUITTFont.h:86
void preload(u32 char_index, FT_Face face, CGUITTFont *parent, u32 font_size, const FT_Int32 loadFlags)
Preload the glyph.
Definition CGUITTFont.cpp:176
void unload()
Unloads the glyph.
Definition CGUITTFont.cpp:224
core::recti source_rect
The source rectangle for the glyph.
Definition CGUITTFont.h:106
bool isLoaded() const
If true, the glyph has been loaded.
Definition CGUITTFont.h:89
core::vector2di advance
Glyph advance information.
Definition CGUITTFont.h:112
u32 glyph_page
The page the glyph is on.
Definition CGUITTFont.h:103