Luanti 5.11.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 "IGUIEnvironment.h"
40#include "IGUIFont.h"
41#include "IVideoDriver.h"
42#include "IrrlichtDevice.h"
44#include "util/basic_macros.h"
45
46#include <map>
47#include <optional>
48
49namespace irr
50{
51namespace gui
52{
53 // Manages the FT_Face cache.
54 struct SGUITTFace : public irr::IReferenceCounted
55 {
56 private:
57
58 static std::map<io::path, SGUITTFace*> faces;
59 static FT_Library freetype_library;
60 static std::size_t n_faces;
61
62 static FT_Library getFreeTypeLibrary();
63
64 public:
65
66 SGUITTFace(std::string &&buffer);
67
69
70 std::optional<std::string> filename;
71
72 FT_Face face;
74 std::string face_buffer;
75
76 static SGUITTFace* createFace(std::string &&buffer);
77
78 static SGUITTFace* loadFace(const io::path &filename);
79
80 void dropFilename();
81 };
82 class CGUITTFont;
83
86 {
89 glyph_page(0),
91 offset(),
92 advance(),
93 surface(0)
94 {}
95
97
99 SGUITTGlyph(SGUITTGlyph &&other) noexcept :
100 glyph_page(other.glyph_page),
101 source_rect(other.source_rect),
102 offset(other.offset),
103 advance(other.advance),
104 surface(other.surface)
105 {
106 other.surface = 0;
107 }
108
111
113 inline bool isLoaded() const {
114 return source_rect != core::recti();
115 }
116
122 void preload(u32 char_index, FT_Face face, CGUITTFont *parent, u32 font_size, const FT_Int32 loadFlags);
123
125 void unload();
126
128 video::IImage* createGlyphImage(const FT_Bitmap& bits, video::IVideoDriver* driver) const;
129
132
134 core::recti source_rect;
135
137 core::vector2di offset;
138
140 core::vector2di advance;
141
144 mutable video::IImage* surface;
145 };
146
149 {
150 public:
151 CGUITTGlyphPage(video::IVideoDriver* Driver, const io::path& texture_name) :texture(0), available_slots(0), used_slots(0), dirty(false), driver(Driver), name(texture_name) {}
153 {
154 if (texture)
155 {
156 if (driver)
157 driver->removeTexture(texture);
158 else
159 texture->drop();
160 }
161 }
162
164 bool createPageTexture(const u8& pixel_mode, const core::dimension2du& texture_size)
165 {
166 if( texture )
167 return false;
168
169 bool flgmip = driver->getTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS);
170 driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
171 bool flgcpy = driver->getTextureCreationFlag(video::ETCF_ALLOW_MEMORY_COPY);
172 driver->setTextureCreationFlag(video::ETCF_ALLOW_MEMORY_COPY, true);
173
174 // Set the texture color format.
175 switch (pixel_mode)
176 {
177 case FT_PIXEL_MODE_MONO:
178 texture = driver->addTexture(texture_size, name, video::ECF_A1R5G5B5);
179 break;
180 case FT_PIXEL_MODE_GRAY:
181 default:
182 texture = driver->addTexture(texture_size, name, video::ECF_A8R8G8B8);
183 break;
184 }
185
186 // Restore our texture creation flags.
187 driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, flgmip);
188 driver->setTextureCreationFlag(video::ETCF_ALLOW_MEMORY_COPY, flgcpy);
189
190 return texture ? true : false;
191 }
192
196 {
197 glyph_to_be_paged.push_back(glyph);
198 }
199
202 {
203 if (!dirty) return;
204
205 void* ptr = texture->lock();
206 video::ECOLOR_FORMAT format = texture->getColorFormat();
207 core::dimension2du size = texture->getOriginalSize();
208 video::IImage* pageholder = driver->createImageFromData(format, size, ptr, true, false);
209
210 for (u32 i = 0; i < glyph_to_be_paged.size(); ++i)
211 {
212 const SGUITTGlyph* glyph = glyph_to_be_paged[i];
213 if (glyph && glyph->surface)
214 {
215 glyph->surface->copyTo(pageholder, glyph->source_rect.UpperLeftCorner);
216 glyph->surface->drop();
217 glyph->surface = 0;
218 }
219 }
220
221 pageholder->drop();
222 texture->unlock();
223 glyph_to_be_paged.clear();
224 dirty = false;
225 }
226
227 video::ITexture* texture;
230 bool dirty;
231
232 core::array<core::vector2di> render_positions;
233 core::array<core::recti> render_source_rects;
234 core::array<video::SColor> render_colors;
235
236 private:
237 core::array<const SGUITTGlyph*> glyph_to_be_paged;
238 video::IVideoDriver* driver;
239 io::path name;
240 };
241
243 class CGUITTFont : public IGUIFont
244 {
245 public:
252 static CGUITTFont* createTTFont(IGUIEnvironment *env,
253 SGUITTFace *face, u32 size, bool antialias = true,
254 bool transparency = true, u32 shadow = 0, u32 shadow_alpha = 255);
255
257 virtual ~CGUITTFont();
258
260 void setBatchLoadSize(u32 batch_size) { batch_load_size = batch_size; }
261
263 void setMaxPageTextureSize(const core::dimension2du& texture_size) { max_page_texture_size = texture_size; }
264
266 u32 getFontSize() const { return size; }
267
269 bool isTransparent() const { return use_transparency; }
270
273 bool useAutoHinting() const { return use_auto_hinting; }
274
276 bool useHinting() const { return use_hinting; }
277
280 bool useMonochrome() const { return use_monochrome; }
281
285 void setTransparency(const bool flag);
286
290 void setMonochrome(const bool flag);
291
296 void setFontHinting(const bool enable, const bool enable_auto_hinting = true);
297
299 virtual void draw(const core::stringw& text, const core::rect<s32>& position,
300 video::SColor color, bool hcenter=false, bool vcenter=false,
301 const core::rect<s32>* clip=0) override;
302
303 void draw(const EnrichedString& text, const core::rect<s32>& position,
304 bool hcenter=false, bool vcenter=false,
305 const core::rect<s32>* clip=0);
306
308 virtual core::dimension2du getDimension(const wchar_t* text) const override;
309
311 virtual s32 getCharacterFromPos(const wchar_t* text, s32 pixel_x) const override;
312
314 virtual void setKerningWidth(s32 kerning) override;
315
317 virtual void setKerningHeight(s32 kerning) override;
318
320 virtual core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const override;
321
323 virtual void setInvisibleCharacters(const wchar_t *s) override;
324
328
331 //should be better typed. fix later.
332 CGUITTGlyphPage* createGlyphPage(const u8 pixel_mode);
333
335 u32 getLastGlyphPageIndex() const { return Glyph_Pages.size() - 1; }
336
338 void setFallback(gui::IGUIFont* font) { fallback = font; }
339
343 video::IImage* createTextureFromChar(const char32_t& ch);
344
347 video::ITexture* getPageTextureByIndex(const u32& page_index) const;
348
349 inline video::IVideoDriver *getDriver() const { return Driver; }
350
351 inline s32 getAscender() const { return font_metrics.ascender; }
352
353 protected:
358 u32 size;
360 core::dimension2du max_page_texture_size;
361
362 private:
363 // Helper functions for the same-named public member functions above
364 // (Since std::u32string is nicer to work with than wchar_t *)
365 core::dimension2d<u32> getDimension(const std::u32string& text) const;
366 s32 getCharacterFromPos(const std::u32string& text, s32 pixel_x) const;
367
368 // Helper function for the above helper functions :P
369 std::u32string convertWCharToU32String(const wchar_t* const) const;
370
371 CGUITTFont(IGUIEnvironment *env);
372 bool load(SGUITTFace *face, const u32 size, const bool antialias, const bool transparency);
373 void reset_images();
374 void update_glyph_pages() const;
376 {
377 // Set up our loading flags.
378 load_flags = FT_LOAD_DEFAULT | FT_LOAD_RENDER;
379 if (!useHinting()) load_flags |= FT_LOAD_NO_HINTING;
380 if (!useAutoHinting()) load_flags |= FT_LOAD_NO_AUTOHINT;
381 if (useMonochrome()) load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO;
382 else load_flags |= FT_LOAD_TARGET_NORMAL;
383 }
384 u32 getWidthFromCharacter(char32_t c) const;
385 u32 getHeightFromCharacter(char32_t c) const;
386 u32 getGlyphIndexByChar(char32_t c) const;
387 core::vector2di getKerning(const char32_t thisLetter, const char32_t previousLetter) const;
388
389 video::IVideoDriver* Driver;
390 std::optional<io::path> filename;
391 FT_Face tt_face;
392 FT_Size_Metrics font_metrics;
393 FT_Int32 load_flags;
394
395 mutable core::array<CGUITTGlyphPage*> Glyph_Pages;
396 mutable core::array<SGUITTGlyph> Glyphs;
397
400 std::u32string Invisible;
403
404 gui::IGUIFont* fallback;
405 };
406
407} // end namespace gui
408} // end namespace irr
Definition enriched_string.h:29
Class representing a TrueType font.
Definition CGUITTFont.h:244
s32 getAscender() const
Definition CGUITTFont.h:351
video::ITexture * getPageTextureByIndex(const u32 &page_index) const
This function is for debugging mostly.
Definition CGUITTFont.cpp:915
s32 GlobalKerningHeight
Definition CGUITTFont.h:399
u32 shadow_offset
Definition CGUITTFont.h:401
void reset_images()
Definition CGUITTFont.cpp:353
video::IVideoDriver * getDriver() const
Definition CGUITTFont.h:349
u32 getHeightFromCharacter(char32_t c) const
Definition CGUITTFont.cpp:720
void setMonochrome(const bool flag)
Tells the font to use monochrome rendering.
Definition CGUITTFont.cpp:449
static CGUITTFont * createTTFont(IGUIEnvironment *env, SGUITTFace *face, u32 size, bool antialias=true, bool transparency=true, u32 shadow=0, u32 shadow_alpha=255)
Creates a new TrueType font and returns a pointer to it.
Definition CGUITTFont.cpp:268
void setBatchLoadSize(u32 batch_size)
Sets the amount of glyphs to batch load.
Definition CGUITTFont.h:260
virtual void draw(const core::stringw &text, 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:462
bool isTransparent() const
Check the font's transparency.
Definition CGUITTFont.h:269
bool load(SGUITTFace *face, const u32 size, const bool antialias, const bool transparency)
Definition CGUITTFont.cpp:306
core::array< CGUITTGlyphPage * > Glyph_Pages
Definition CGUITTFont.h:395
void update_load_flags()
Definition CGUITTFont.h:375
bool useAutoHinting() const
Check if the font auto-hinting is enabled.
Definition CGUITTFont.h:273
video::IImage * createTextureFromChar(const char32_t &ch)
Create corresponding character's software image copy from the font, so you can use this data just lik...
Definition CGUITTFont.cpp:880
core::dimension2du max_page_texture_size
Definition CGUITTFont.h:360
bool useHinting() const
Check if the font hinting is enabled.
Definition CGUITTFont.h:276
FT_Face tt_face
Definition CGUITTFont.h:391
core::array< SGUITTGlyph > Glyphs
Definition CGUITTFont.h:396
u32 getGlyphIndexByChar(char32_t c) const
Definition CGUITTFont.cpp:740
void setFallback(gui::IGUIFont *font)
Set font that should be used for glyphs not present in ours.
Definition CGUITTFont.h:338
void update_glyph_pages() const
Definition CGUITTFont.cpp:368
FT_Int32 load_flags
Definition CGUITTFont.h:393
FT_Size_Metrics font_metrics
Definition CGUITTFont.h:392
u32 getFontSize() const
Get the font size.
Definition CGUITTFont.h:266
CGUITTFont(IGUIEnvironment *env)
Constructor.
Definition CGUITTFont.cpp:289
u32 batch_load_size
Definition CGUITTFont.h:359
s32 GlobalKerningWidth
Definition CGUITTFont.h:398
u32 shadow_alpha
Definition CGUITTFont.h:402
void setFontHinting(const bool enable, const bool enable_auto_hinting=true)
Enables or disables font hinting.
Definition CGUITTFont.cpp:455
virtual core::dimension2du getDimension(const wchar_t *text) const override
Returns the dimension of a text string.
Definition CGUITTFont.cpp:635
u32 getLastGlyphPageIndex() const
Get the last glyph page's index.
Definition CGUITTFont.h:335
gui::IGUIFont * fallback
Definition CGUITTFont.h:404
CGUITTGlyphPage * getLastGlyphPage() const
Get the last glyph page if there's still available slots.
Definition CGUITTFont.cpp:377
std::u32string Invisible
Definition CGUITTFont.h:400
bool useMonochrome() const
Check if the font is being loaded as a monochrome font.
Definition CGUITTFont.h:280
bool use_auto_hinting
Definition CGUITTFont.h:357
u32 getWidthFromCharacter(char32_t c) const
Definition CGUITTFont.cpp:701
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:783
bool use_transparency
Definition CGUITTFont.h:355
bool use_hinting
Definition CGUITTFont.h:356
virtual ~CGUITTFont()
Destructor.
Definition CGUITTFont.cpp:342
bool use_monochrome
Definition CGUITTFont.h:354
virtual void setKerningHeight(s32 kerning) override
Sets global kerning height for the font.
Definition CGUITTFont.cpp:820
virtual void setKerningWidth(s32 kerning) override
Sets global kerning width for the font.
Definition CGUITTFont.cpp:815
std::u32string convertWCharToU32String(const wchar_t *const) const
Definition CGUITTFont.cpp:923
void setMaxPageTextureSize(const core::dimension2du &texture_size)
Sets the maximum texture size for a page of glyphs.
Definition CGUITTFont.h:263
video::IVideoDriver * Driver
Definition CGUITTFont.h:389
std::optional< io::path > filename
Definition CGUITTFont.h:390
CGUITTGlyphPage * createGlyphPage(const u8 pixel_mode)
Create a new glyph page texture.
Definition CGUITTFont.cpp:391
void setTransparency(const bool flag)
Tells the font to allow transparency when rendering.
Definition CGUITTFont.cpp:443
u32 size
Definition CGUITTFont.h:358
virtual void setInvisibleCharacters(const wchar_t *s) override
Define which characters should not be drawn by the font.
Definition CGUITTFont.cpp:875
virtual core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const override
Returns the distance between letters.
Definition CGUITTFont.cpp:825
Holds a sheet of glyphs.
Definition CGUITTFont.h:149
bool dirty
Definition CGUITTFont.h:230
core::array< core::recti > render_source_rects
Definition CGUITTFont.h:233
u32 used_slots
Definition CGUITTFont.h:229
void pushGlyphToBePaged(const SGUITTGlyph *glyph)
Add the glyph to a list of glyphs to be paged.
Definition CGUITTFont.h:195
io::path name
Definition CGUITTFont.h:239
u32 available_slots
Definition CGUITTFont.h:228
video::IVideoDriver * driver
Definition CGUITTFont.h:238
core::array< video::SColor > render_colors
Definition CGUITTFont.h:234
core::array< const SGUITTGlyph * > glyph_to_be_paged
Definition CGUITTFont.h:237
bool createPageTexture(const u8 &pixel_mode, const core::dimension2du &texture_size)
Create the actual page texture,.
Definition CGUITTFont.h:164
core::array< core::vector2di > render_positions
Definition CGUITTFont.h:232
~CGUITTGlyphPage()
Definition CGUITTFont.h:152
video::ITexture * texture
Definition CGUITTFont.h:227
CGUITTGlyphPage(video::IVideoDriver *Driver, const io::path &texture_name)
Definition CGUITTFont.h:151
void updateTexture()
Updates the texture atlas with new glyphs.
Definition CGUITTFont.h:201
Definition clientmap.h:30
Definition CGUITTFont.h:55
~SGUITTFace()
Definition CGUITTFont.cpp:72
void dropFilename()
Definition CGUITTFont.cpp:119
static FT_Library getFreeTypeLibrary()
Definition CGUITTFont.cpp:55
static SGUITTFace * createFace(std::string &&buffer)
Definition CGUITTFont.cpp:84
static SGUITTFace * loadFace(const io::path &filename)
Definition CGUITTFont.cpp:96
static std::size_t n_faces
Definition CGUITTFont.h:60
static FT_Library freetype_library
Definition CGUITTFont.h:59
static std::map< io::path, SGUITTFace * > faces
Definition CGUITTFont.h:58
std::string face_buffer
Must not be deallocated until we are done with the face!
Definition CGUITTFont.h:74
SGUITTFace(std::string &&buffer)
Definition CGUITTFont.cpp:66
FT_Face face
Definition CGUITTFont.h:72
std::optional< std::string > filename
Definition CGUITTFont.h:70
Structure representing a single TrueType glyph.
Definition CGUITTFont.h:86
void unload()
Unloads the glyph.
Definition CGUITTFont.cpp:255
video::IImage * surface
This is just the temporary image holder.
Definition CGUITTFont.h:144
core::recti source_rect
The source rectangle for the glyph.
Definition CGUITTFont.h:134
SGUITTGlyph()
Constructor.
Definition CGUITTFont.h:88
void preload(u32 char_index, FT_Face face, CGUITTFont *parent, u32 font_size, const FT_Int32 loadFlags)
Preload the glyph.
Definition CGUITTFont.cpp:207
video::IImage * createGlyphImage(const FT_Bitmap &bits, video::IVideoDriver *driver) const
Creates the IImage object from the FT_Bitmap.
Definition CGUITTFont.cpp:134
u32 glyph_page
The page the glyph is on.
Definition CGUITTFont.h:131
SGUITTGlyph(SGUITTGlyph &&other) noexcept
This class would be trivially copyable except for the reference count on surface.
Definition CGUITTFont.h:99
core::vector2di offset
The offset of glyph when drawn.
Definition CGUITTFont.h:137
bool isLoaded() const
If true, the glyph has been loaded.
Definition CGUITTFont.h:113
DISABLE_CLASS_COPY(SGUITTGlyph)
core::vector2di advance
Glyph advance information.
Definition CGUITTFont.h:140
~SGUITTGlyph()
Destructor.
Definition CGUITTFont.h:110