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
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any
9 damages arising from the use of this software.
10
11 Permission is granted to anyone to use this software for any
12 purpose, including commercial applications, and to alter it and
13 redistribute it freely, subject to the following restrictions:
14
15 1. The origin of this software must not be misrepresented; you
16 must not claim that you wrote the original software. If you use
17 this software in a product, an acknowledgment in the product
18 documentation would be appreciated but is not required.
19
20 2. Altered source versions must be plainly marked as such, and
21 must not be misrepresented as being the original software.
22
23 3. This notice may not be removed or altered from any source
24 distribution.
25
26 The original version of this class can be located at:
27 http://irrlicht.suckerfreegames.com/
28
29 John Norman
30 john@suckerfreegames.com
31*/
32
33#pragma once
34
35#include <map>
36#include <ft2build.h>
37#include FT_FREETYPE_H
38
39#include "IGUIEnvironment.h"
40#include "IGUIFont.h"
41#include "IVideoDriver.h"
42#include "IrrlichtDevice.h"
43#include "SMesh.h"
45#include "util/basic_macros.h"
46
47namespace irr
48{
49namespace gui
50{
51 struct SGUITTFace;
52 class CGUITTFont;
53
56 {
59 glyph_page(0),
61 offset(),
62 advance(),
63 surface(0)
64 {}
65
67
69 SGUITTGlyph(SGUITTGlyph &&other) noexcept :
70 glyph_page(other.glyph_page),
71 source_rect(other.source_rect),
72 offset(other.offset),
73 advance(other.advance),
74 surface(other.surface)
75 {
76 other.surface = 0;
77 }
78
81
83 inline bool isLoaded() const {
84 return source_rect != core::recti();
85 }
86
92 void preload(u32 char_index, FT_Face face, CGUITTFont *parent, u32 font_size, const FT_Int32 loadFlags);
93
95 void unload();
96
98 video::IImage* createGlyphImage(const FT_Bitmap& bits, video::IVideoDriver* driver) const;
99
102
104 core::recti source_rect;
105
107 core::vector2di offset;
108
110 core::vector2di advance;
111
114 mutable video::IImage* surface;
115 };
116
119 {
120 public:
121 CGUITTGlyphPage(video::IVideoDriver* Driver, const io::path& texture_name) :texture(0), available_slots(0), used_slots(0), dirty(false), driver(Driver), name(texture_name) {}
123 {
124 if (texture)
125 {
126 if (driver)
127 driver->removeTexture(texture);
128 else
129 texture->drop();
130 }
131 }
132
134 bool createPageTexture(const u8& pixel_mode, const core::dimension2du& texture_size)
135 {
136 if( texture )
137 return false;
138
139 bool flgmip = driver->getTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS);
140 driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
141 bool flgcpy = driver->getTextureCreationFlag(video::ETCF_ALLOW_MEMORY_COPY);
142 driver->setTextureCreationFlag(video::ETCF_ALLOW_MEMORY_COPY, true);
143
144 // Set the texture color format.
145 switch (pixel_mode)
146 {
147 case FT_PIXEL_MODE_MONO:
148 texture = driver->addTexture(texture_size, name, video::ECF_A1R5G5B5);
149 break;
150 case FT_PIXEL_MODE_GRAY:
151 default:
152 texture = driver->addTexture(texture_size, name, video::ECF_A8R8G8B8);
153 break;
154 }
155
156 // Restore our texture creation flags.
157 driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, flgmip);
158 driver->setTextureCreationFlag(video::ETCF_ALLOW_MEMORY_COPY, flgcpy);
159
160 return texture ? true : false;
161 }
162
166 {
167 glyph_to_be_paged.push_back(glyph);
168 }
169
172 {
173 if (!dirty) return;
174
175 void* ptr = texture->lock();
176 video::ECOLOR_FORMAT format = texture->getColorFormat();
177 core::dimension2du size = texture->getOriginalSize();
178 video::IImage* pageholder = driver->createImageFromData(format, size, ptr, true, false);
179
180 for (u32 i = 0; i < glyph_to_be_paged.size(); ++i)
181 {
182 const SGUITTGlyph* glyph = glyph_to_be_paged[i];
183 if (glyph && glyph->surface)
184 {
185 glyph->surface->copyTo(pageholder, glyph->source_rect.UpperLeftCorner);
186 glyph->surface->drop();
187 glyph->surface = 0;
188 }
189 }
190
191 pageholder->drop();
192 texture->unlock();
193 glyph_to_be_paged.clear();
194 dirty = false;
195 }
196
197 video::ITexture* texture;
200 bool dirty;
201
202 core::array<core::vector2di> render_positions;
203 core::array<core::recti> render_source_rects;
204 core::array<video::SColor> render_colors;
205
206 private:
207 core::array<const SGUITTGlyph*> glyph_to_be_paged;
208 video::IVideoDriver* driver;
209 io::path name;
210 };
211
213 class CGUITTFont : public IGUIFont
214 {
215 public:
223 static CGUITTFont* createTTFont(IGUIEnvironment *env, const io::path& filename, const u32 size, const bool antialias = true, const bool transparency = true, const u32 shadow = 0, const u32 shadow_alpha = 255);
224
226 virtual ~CGUITTFont();
227
229 void setBatchLoadSize(u32 batch_size) { batch_load_size = batch_size; }
230
232 void setMaxPageTextureSize(const core::dimension2du& texture_size) { max_page_texture_size = texture_size; }
233
235 u32 getFontSize() const { return size; }
236
238 bool isTransparent() const { return use_transparency; }
239
242 bool useAutoHinting() const { return use_auto_hinting; }
243
245 bool useHinting() const { return use_hinting; }
246
249 bool useMonochrome() const { return use_monochrome; }
250
254 void setTransparency(const bool flag);
255
259 void setMonochrome(const bool flag);
260
265 void setFontHinting(const bool enable, const bool enable_auto_hinting = true);
266
268 virtual void draw(const core::stringw& text, const core::rect<s32>& position,
269 video::SColor color, bool hcenter=false, bool vcenter=false,
270 const core::rect<s32>* clip=0) override;
271
272 void draw(const EnrichedString& text, const core::rect<s32>& position,
273 bool hcenter=false, bool vcenter=false,
274 const core::rect<s32>* clip=0);
275
277 virtual core::dimension2du getDimension(const wchar_t* text) const override;
278
280 virtual s32 getCharacterFromPos(const wchar_t* text, s32 pixel_x) const override;
281
283 virtual void setKerningWidth(s32 kerning) override;
284
286 virtual void setKerningHeight(s32 kerning) override;
287
289 virtual core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const override;
290
292 virtual void setInvisibleCharacters(const wchar_t *s) override;
293
297
300 //should be better typed. fix later.
301 CGUITTGlyphPage* createGlyphPage(const u8 pixel_mode);
302
304 u32 getLastGlyphPageIndex() const { return Glyph_Pages.size() - 1; }
305
307 void setFallback(gui::IGUIFont* font) { fallback = font; }
308
312 video::IImage* createTextureFromChar(const char32_t& ch);
313
316 video::ITexture* getPageTextureByIndex(const u32& page_index) const;
317
318 inline video::IVideoDriver *getDriver() const { return Driver; }
319
320 inline s32 getAscender() const { return font_metrics.ascender; }
321
322 protected:
327 u32 size;
329 core::dimension2du max_page_texture_size;
330
331 private:
332 // Manages the FreeType library.
333 static FT_Library c_library;
334 static std::map<io::path, SGUITTFace*> c_faces;
335 static bool c_libraryLoaded;
336
337 // Helper functions for the same-named public member functions above
338 // (Since std::u32string is nicer to work with than wchar_t *)
339 core::dimension2d<u32> getDimension(const std::u32string& text) const;
340 s32 getCharacterFromPos(const std::u32string& text, s32 pixel_x) const;
341
342 // Helper function for the above helper functions :P
343 std::u32string convertWCharToU32String(const wchar_t* const) const;
344
345 CGUITTFont(IGUIEnvironment *env);
346 bool load(const io::path& filename, const u32 size, const bool antialias, const bool transparency);
347 void reset_images();
348 void update_glyph_pages() const;
350 {
351 // Set up our loading flags.
352 load_flags = FT_LOAD_DEFAULT | FT_LOAD_RENDER;
353 if (!useHinting()) load_flags |= FT_LOAD_NO_HINTING;
354 if (!useAutoHinting()) load_flags |= FT_LOAD_NO_AUTOHINT;
355 if (useMonochrome()) load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO;
356 else load_flags |= FT_LOAD_TARGET_NORMAL;
357 }
358 u32 getWidthFromCharacter(char32_t c) const;
359 u32 getHeightFromCharacter(char32_t c) const;
360 u32 getGlyphIndexByChar(char32_t c) const;
361 core::vector2di getKerning(const char32_t thisLetter, const char32_t previousLetter) const;
362
363 video::IVideoDriver* Driver;
364 io::path filename;
365 FT_Face tt_face;
366 FT_Size_Metrics font_metrics;
367 FT_Int32 load_flags;
368
369 mutable core::array<CGUITTGlyphPage*> Glyph_Pages;
370 mutable core::array<SGUITTGlyph> Glyphs;
371
374 std::u32string Invisible;
377
378 gui::IGUIFont* fallback;
379 };
380
381} // end namespace gui
382} // end namespace irr
Definition enriched_string.h:29
Class representing a TrueType font.
Definition CGUITTFont.h:214
s32 getAscender() const
Definition CGUITTFont.h:320
video::ITexture * getPageTextureByIndex(const u32 &page_index) const
This function is for debugging mostly.
Definition CGUITTFont.cpp:910
s32 GlobalKerningHeight
Definition CGUITTFont.h:373
io::path filename
Definition CGUITTFont.h:364
u32 shadow_offset
Definition CGUITTFont.h:375
void reset_images()
Definition CGUITTFont.cpp:348
video::IVideoDriver * getDriver() const
Definition CGUITTFont.h:318
u32 getHeightFromCharacter(char32_t c) const
Definition CGUITTFont.cpp:715
void setMonochrome(const bool flag)
Tells the font to use monochrome rendering.
Definition CGUITTFont.cpp:444
bool load(const io::path &filename, const u32 size, const bool antialias, const bool transparency)
Definition CGUITTFont.cpp:249
void setBatchLoadSize(u32 batch_size)
Sets the amount of glyphs to batch load.
Definition CGUITTFont.h:229
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:457
bool isTransparent() const
Check the font's transparency.
Definition CGUITTFont.h:238
core::array< CGUITTGlyphPage * > Glyph_Pages
Definition CGUITTFont.h:369
void update_load_flags()
Definition CGUITTFont.h:349
bool useAutoHinting() const
Check if the font auto-hinting is enabled.
Definition CGUITTFont.h:242
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:875
core::dimension2du max_page_texture_size
Definition CGUITTFont.h:329
bool useHinting() const
Check if the font hinting is enabled.
Definition CGUITTFont.h:245
FT_Face tt_face
Definition CGUITTFont.h:365
core::array< SGUITTGlyph > Glyphs
Definition CGUITTFont.h:370
static FT_Library c_library
Definition CGUITTFont.h:333
u32 getGlyphIndexByChar(char32_t c) const
Definition CGUITTFont.cpp:735
void setFallback(gui::IGUIFont *font)
Set font that should be used for glyphs not present in ours.
Definition CGUITTFont.h:307
void update_glyph_pages() const
Definition CGUITTFont.cpp:363
FT_Int32 load_flags
Definition CGUITTFont.h:367
FT_Size_Metrics font_metrics
Definition CGUITTFont.h:366
u32 getFontSize() const
Get the font size.
Definition CGUITTFont.h:235
CGUITTFont(IGUIEnvironment *env)
Constructor.
Definition CGUITTFont.cpp:232
u32 batch_load_size
Definition CGUITTFont.h:328
s32 GlobalKerningWidth
Definition CGUITTFont.h:372
u32 shadow_alpha
Definition CGUITTFont.h:376
static CGUITTFont * createTTFont(IGUIEnvironment *env, const io::path &filename, const u32 size, const bool antialias=true, const bool transparency=true, const u32 shadow=0, const u32 shadow_alpha=255)
Creates a new TrueType font and returns a pointer to it.
Definition CGUITTFont.cpp:206
void setFontHinting(const bool enable, const bool enable_auto_hinting=true)
Enables or disables font hinting.
Definition CGUITTFont.cpp:450
virtual core::dimension2du getDimension(const wchar_t *text) const override
Returns the dimension of a text string.
Definition CGUITTFont.cpp:630
u32 getLastGlyphPageIndex() const
Get the last glyph page's index.
Definition CGUITTFont.h:304
gui::IGUIFont * fallback
Definition CGUITTFont.h:378
CGUITTGlyphPage * getLastGlyphPage() const
Get the last glyph page if there's still available slots.
Definition CGUITTFont.cpp:372
std::u32string Invisible
Definition CGUITTFont.h:374
bool useMonochrome() const
Check if the font is being loaded as a monochrome font.
Definition CGUITTFont.h:249
bool use_auto_hinting
Definition CGUITTFont.h:326
u32 getWidthFromCharacter(char32_t c) const
Definition CGUITTFont.cpp:696
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:778
bool use_transparency
Definition CGUITTFont.h:324
bool use_hinting
Definition CGUITTFont.h:325
virtual ~CGUITTFont()
Destructor.
Definition CGUITTFont.cpp:319
static bool c_libraryLoaded
Definition CGUITTFont.h:335
bool use_monochrome
Definition CGUITTFont.h:323
static std::map< io::path, SGUITTFace * > c_faces
Definition CGUITTFont.h:334
virtual void setKerningHeight(s32 kerning) override
Sets global kerning height for the font.
Definition CGUITTFont.cpp:815
virtual void setKerningWidth(s32 kerning) override
Sets global kerning width for the font.
Definition CGUITTFont.cpp:810
std::u32string convertWCharToU32String(const wchar_t *const) const
Definition CGUITTFont.cpp:918
void setMaxPageTextureSize(const core::dimension2du &texture_size)
Sets the maximum texture size for a page of glyphs.
Definition CGUITTFont.h:232
video::IVideoDriver * Driver
Definition CGUITTFont.h:363
CGUITTGlyphPage * createGlyphPage(const u8 pixel_mode)
Create a new glyph page texture.
Definition CGUITTFont.cpp:386
void setTransparency(const bool flag)
Tells the font to allow transparency when rendering.
Definition CGUITTFont.cpp:438
u32 size
Definition CGUITTFont.h:327
virtual void setInvisibleCharacters(const wchar_t *s) override
Define which characters should not be drawn by the font.
Definition CGUITTFont.cpp:870
virtual core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const override
Returns the distance between letters.
Definition CGUITTFont.cpp:820
Holds a sheet of glyphs.
Definition CGUITTFont.h:119
bool dirty
Definition CGUITTFont.h:200
core::array< core::recti > render_source_rects
Definition CGUITTFont.h:203
u32 used_slots
Definition CGUITTFont.h:199
void pushGlyphToBePaged(const SGUITTGlyph *glyph)
Add the glyph to a list of glyphs to be paged.
Definition CGUITTFont.h:165
io::path name
Definition CGUITTFont.h:209
u32 available_slots
Definition CGUITTFont.h:198
video::IVideoDriver * driver
Definition CGUITTFont.h:208
core::array< video::SColor > render_colors
Definition CGUITTFont.h:204
core::array< const SGUITTGlyph * > glyph_to_be_paged
Definition CGUITTFont.h:207
bool createPageTexture(const u8 &pixel_mode, const core::dimension2du &texture_size)
Create the actual page texture,.
Definition CGUITTFont.h:134
core::array< core::vector2di > render_positions
Definition CGUITTFont.h:202
~CGUITTGlyphPage()
Definition CGUITTFont.h:122
video::ITexture * texture
Definition CGUITTFont.h:197
CGUITTGlyphPage(video::IVideoDriver *Driver, const io::path &texture_name)
Definition CGUITTFont.h:121
void updateTexture()
Updates the texture atlas with new glyphs.
Definition CGUITTFont.h:171
Definition clientmap.h:30
Structure representing a single TrueType glyph.
Definition CGUITTFont.h:56
void unload()
Unloads the glyph.
Definition CGUITTFont.cpp:193
video::IImage * surface
This is just the temporary image holder.
Definition CGUITTFont.h:114
core::recti source_rect
The source rectangle for the glyph.
Definition CGUITTFont.h:104
SGUITTGlyph()
Constructor.
Definition CGUITTFont.h:58
void preload(u32 char_index, FT_Face face, CGUITTFont *parent, u32 font_size, const FT_Int32 loadFlags)
Preload the glyph.
Definition CGUITTFont.cpp:145
video::IImage * createGlyphImage(const FT_Bitmap &bits, video::IVideoDriver *driver) const
Creates the IImage object from the FT_Bitmap.
Definition CGUITTFont.cpp:72
u32 glyph_page
The page the glyph is on.
Definition CGUITTFont.h:101
SGUITTGlyph(SGUITTGlyph &&other) noexcept
This class would be trivially copyable except for the reference count on surface.
Definition CGUITTFont.h:69
core::vector2di offset
The offset of glyph when drawn.
Definition CGUITTFont.h:107
bool isLoaded() const
If true, the glyph has been loaded.
Definition CGUITTFont.h:83
DISABLE_CLASS_COPY(SGUITTGlyph)
core::vector2di advance
Glyph advance information.
Definition CGUITTFont.h:110
~SGUITTGlyph()
Destructor.
Definition CGUITTFont.h:80