Luanti 5.10.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 <ft2build.h>
36#include <map>
37#include "IGUIEnvironment.h"
38#include "IGUIFont.h"
39#include "ISceneManager.h"
40#include "IVideoDriver.h"
41#include "IrrlichtDevice.h"
42#include "SMesh.h"
44#include "util/basic_macros.h"
45#include FT_FREETYPE_H
46
47namespace irr
48{
49namespace gui
50{
51 struct SGUITTFace;
52 class CGUITTFont;
53
56 {
59 isLoaded(false),
60 glyph_page(0),
62 offset(),
63 advance(),
64 surface(0),
65 parent(0)
66 {}
67
69
71 SGUITTGlyph(SGUITTGlyph &&other) noexcept :
72 isLoaded(other.isLoaded),
73 glyph_page(other.glyph_page),
74 source_rect(other.source_rect),
75 offset(other.offset),
76 advance(other.advance),
77 surface(other.surface),
78 parent(other.parent)
79 {
80 other.surface = 0;
81 }
82
85
91 void preload(u32 char_index, FT_Face face, video::IVideoDriver* driver, u32 font_size, const FT_Int32 loadFlags);
92
94 void unload();
95
97 video::IImage* createGlyphImage(const FT_Bitmap& bits, video::IVideoDriver* driver) const;
98
101
104
106 core::recti source_rect;
107
109 core::vector2di offset;
110
112 FT_Vector advance;
113
116 mutable video::IImage* surface;
117
120 };
121
124 {
125 public:
126 CGUITTGlyphPage(video::IVideoDriver* Driver, const io::path& texture_name) :texture(0), available_slots(0), used_slots(0), dirty(false), driver(Driver), name(texture_name) {}
128 {
129 if (texture)
130 {
131 if (driver)
132 driver->removeTexture(texture);
133 else texture->drop();
134 }
135 }
136
138 bool createPageTexture(const u8& pixel_mode, const core::dimension2du& texture_size)
139 {
140 if( texture )
141 return false;
142
143 bool flgmip = driver->getTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS);
144 driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
145 bool flgcpy = driver->getTextureCreationFlag(video::ETCF_ALLOW_MEMORY_COPY);
146 driver->setTextureCreationFlag(video::ETCF_ALLOW_MEMORY_COPY, true);
147
148 // Set the texture color format.
149 switch (pixel_mode)
150 {
151 case FT_PIXEL_MODE_MONO:
152 texture = driver->addTexture(texture_size, name, video::ECF_A1R5G5B5);
153 break;
154 case FT_PIXEL_MODE_GRAY:
155 default:
156 texture = driver->addTexture(texture_size, name, video::ECF_A8R8G8B8);
157 break;
158 }
159
160 // Restore our texture creation flags.
161 driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, flgmip);
162 driver->setTextureCreationFlag(video::ETCF_ALLOW_MEMORY_COPY, flgcpy);
163
164 return texture ? true : false;
165 }
166
170 {
171 glyph_to_be_paged.push_back(glyph);
172 }
173
176 {
177 if (!dirty) return;
178
179 void* ptr = texture->lock();
180 video::ECOLOR_FORMAT format = texture->getColorFormat();
181 core::dimension2du size = texture->getOriginalSize();
182 video::IImage* pageholder = driver->createImageFromData(format, size, ptr, true, false);
183
184 for (u32 i = 0; i < glyph_to_be_paged.size(); ++i)
185 {
186 const SGUITTGlyph* glyph = glyph_to_be_paged[i];
187 if (glyph && glyph->isLoaded)
188 {
189 if (glyph->surface)
190 {
191 glyph->surface->copyTo(pageholder, glyph->source_rect.UpperLeftCorner);
192 glyph->surface->drop();
193 glyph->surface = 0;
194 }
195 else
196 {
197 ; // TODO: add error message?
198 //currently, if we failed to create the image, just ignore this operation.
199 }
200 }
201 }
202
203 pageholder->drop();
204 texture->unlock();
205 glyph_to_be_paged.clear();
206 dirty = false;
207 }
208
209 video::ITexture* texture;
212 bool dirty;
213
214 core::array<core::vector2di> render_positions;
215 core::array<core::recti> render_source_rects;
216 core::array<video::SColor> render_colors;
217
218 private:
219 core::array<const SGUITTGlyph*> glyph_to_be_paged;
220 video::IVideoDriver* driver;
221 io::path name;
222 };
223
225 class CGUITTFont : public IGUIFont
226 {
227 public:
235 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);
236
238 virtual ~CGUITTFont();
239
241 virtual void setBatchLoadSize(u32 batch_size) { batch_load_size = batch_size; }
242
244 virtual void setMaxPageTextureSize(const core::dimension2du& texture_size) { max_page_texture_size = texture_size; }
245
247 virtual u32 getFontSize() const { return size; }
248
250 virtual bool isTransparent() const { return use_transparency; }
251
254 virtual bool useAutoHinting() const { return use_auto_hinting; }
255
257 virtual bool useHinting() const { return use_hinting; }
258
261 virtual bool useMonochrome() const { return use_monochrome; }
262
266 virtual void setTransparency(const bool flag);
267
271 virtual void setMonochrome(const bool flag);
272
277 virtual void setFontHinting(const bool enable, const bool enable_auto_hinting = true);
278
280 virtual void draw(const core::stringw& text, const core::rect<s32>& position,
281 video::SColor color, bool hcenter=false, bool vcenter=false,
282 const core::rect<s32>* clip=0);
283
284 void draw(const EnrichedString& text, const core::rect<s32>& position,
285 bool hcenter=false, bool vcenter=false,
286 const core::rect<s32>* clip=0);
287
289 virtual core::dimension2d<u32> getCharDimension(const wchar_t ch) const;
290
292 virtual core::dimension2d<u32> getDimension(const wchar_t* text) const;
293
295 virtual s32 getCharacterFromPos(const wchar_t* text, s32 pixel_x) const;
296
298 virtual void setKerningWidth(s32 kerning);
299
301 virtual void setKerningHeight(s32 kerning);
302
304 virtual s32 getKerningWidth(const wchar_t* thisLetter=0, const wchar_t* previousLetter=0) const;
305 virtual s32 getKerningWidth(const char32_t thisLetter=0, const char32_t previousLetter=0) const;
306
308 virtual s32 getKerningHeight() const;
309
311 virtual void setInvisibleCharacters(const wchar_t *s);
312
316
319 //should be better typed. fix later.
320 CGUITTGlyphPage* createGlyphPage(const u8& pixel_mode);
321
323 u32 getLastGlyphPageIndex() const { return Glyph_Pages.size() - 1; }
324
326 void setFallback(gui::IGUIFont* font) { fallback = font; }
327
331 virtual video::IImage* createTextureFromChar(const char32_t& ch);
332
335 virtual video::ITexture* getPageTextureByIndex(const u32& page_index) const;
336
338 virtual core::array<scene::ISceneNode*> addTextSceneNode
339 (const wchar_t* text, scene::ISceneManager* smgr, scene::ISceneNode* parent = 0,
340 const video::SColor& color = video::SColor(255, 0, 0, 0), bool center = false );
341
342 inline s32 getAscender() const { return font_metrics.ascender; }
343
344 protected:
349 u32 size;
351 core::dimension2du max_page_texture_size;
352
353 private:
354 // Manages the FreeType library.
355 static FT_Library c_library;
356 static std::map<io::path, SGUITTFace*> c_faces;
357 static bool c_libraryLoaded;
358 static scene::IMesh* shared_plane_ptr_;
359 static scene::SMesh shared_plane_;
360
361 // Helper functions for the same-named public member functions above
362 // (Since std::u32string is nicer to work with than wchar_t *)
363 core::dimension2d<u32> getDimension(const std::u32string& text) const;
364 s32 getCharacterFromPos(const std::u32string& text, s32 pixel_x) const;
365
366 // Helper function for the above helper functions :P
367 std::u32string convertWCharToU32String(const wchar_t* const) const;
368
369 CGUITTFont(IGUIEnvironment *env);
370 bool load(const io::path& filename, const u32 size, const bool antialias, const bool transparency);
371 void reset_images();
372 void update_glyph_pages() const;
374 {
375 // Set up our loading flags.
376 load_flags = FT_LOAD_DEFAULT | FT_LOAD_RENDER;
377 if (!useHinting()) load_flags |= FT_LOAD_NO_HINTING;
378 if (!useAutoHinting()) load_flags |= FT_LOAD_NO_AUTOHINT;
379 if (useMonochrome()) load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO;
380 else load_flags |= FT_LOAD_TARGET_NORMAL;
381 }
382 u32 getWidthFromCharacter(wchar_t c) const;
383 u32 getWidthFromCharacter(char32_t c) const;
384 u32 getHeightFromCharacter(wchar_t c) const;
385 u32 getHeightFromCharacter(char32_t c) const;
386 u32 getGlyphIndexByChar(wchar_t c) const;
387 u32 getGlyphIndexByChar(char32_t c) const;
388 core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const;
389 core::vector2di getKerning(const char32_t thisLetter, const char32_t previousLetter) const;
390 core::dimension2d<u32> getDimensionUntilEndOfLine(const wchar_t* p) const;
391
392 void createSharedPlane();
393
394 irr::IrrlichtDevice* Device;
395 gui::IGUIEnvironment* Environment;
396 video::IVideoDriver* Driver;
397 io::path filename;
398 FT_Face tt_face;
399 FT_Size_Metrics font_metrics;
400 FT_Int32 load_flags;
401
402 mutable core::array<CGUITTGlyphPage*> Glyph_Pages;
403 mutable core::array<SGUITTGlyph> Glyphs;
404
407 std::u32string Invisible;
410
411 gui::IGUIFont* fallback;
412 };
413
414} // end namespace gui
415} // end namespace irr
Definition enriched_string.h:29
Class representing a TrueType font.
Definition CGUITTFont.h:226
virtual core::array< scene::ISceneNode * > addTextSceneNode(const wchar_t *text, scene::ISceneManager *smgr, scene::ISceneNode *parent=0, const video::SColor &color=video::SColor(255, 0, 0, 0), bool center=false)
Add a list of scene nodes generated by putting font textures on the 3D planes.
Definition CGUITTFont.cpp:1073
s32 getAscender() const
Definition CGUITTFont.h:342
virtual video::ITexture * getPageTextureByIndex(const u32 &page_index) const
This function is for debugging mostly.
Definition CGUITTFont.cpp:1026
virtual void setKerningHeight(s32 kerning)
Sets global kerning height for the font.
Definition CGUITTFont.cpp:907
s32 GlobalKerningHeight
Definition CGUITTFont.h:406
io::path filename
Definition CGUITTFont.h:397
u32 shadow_offset
Definition CGUITTFont.h:408
u32 getWidthFromCharacter(wchar_t c) const
Definition CGUITTFont.cpp:765
void reset_images()
Definition CGUITTFont.cpp:412
static scene::SMesh shared_plane_
Definition CGUITTFont.h:359
virtual void setBatchLoadSize(u32 batch_size)
Sets the amount of glyphs to batch load.
Definition CGUITTFont.h:241
virtual void setMonochrome(const bool flag)
Tells the font to use monochrome rendering.
Definition CGUITTFont.cpp:507
virtual u32 getFontSize() const
Get the font size.
Definition CGUITTFont.h:247
u32 getHeightFromCharacter(wchar_t c) const
Definition CGUITTFont.cpp:793
bool load(const io::path &filename, const u32 size, const bool antialias, const bool transparency)
Definition CGUITTFont.cpp:273
virtual void setKerningWidth(s32 kerning)
Sets global kerning width for the font.
Definition CGUITTFont.cpp:902
core::array< CGUITTGlyphPage * > Glyph_Pages
Definition CGUITTFont.h:402
void update_load_flags()
Definition CGUITTFont.h:373
virtual 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:992
core::dimension2du max_page_texture_size
Definition CGUITTFont.h:351
virtual void setMaxPageTextureSize(const core::dimension2du &texture_size)
Sets the maximum texture size for a page of glyphs.
Definition CGUITTFont.h:244
virtual void setInvisibleCharacters(const wchar_t *s)
Define which characters should not be drawn by the font.
Definition CGUITTFont.cpp:987
irr::IrrlichtDevice * Device
Definition CGUITTFont.h:394
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)
Draws some text and clips it to the specified rectangle if wanted.
Definition CGUITTFont.cpp:520
FT_Face tt_face
Definition CGUITTFont.h:398
core::array< SGUITTGlyph > Glyphs
Definition CGUITTFont.h:403
virtual s32 getKerningHeight() const
Returns the distance between letters.
Definition CGUITTFont.cpp:928
static FT_Library c_library
Definition CGUITTFont.h:355
void setFallback(gui::IGUIFont *font)
Set font that should be used for glyphs not present in ours.
Definition CGUITTFont.h:326
void update_glyph_pages() const
Definition CGUITTFont.cpp:427
virtual bool isTransparent() const
Check the font's transparency.
Definition CGUITTFont.h:250
CGUITTGlyphPage * createGlyphPage(const u8 &pixel_mode)
Create a new glyph page texture.
Definition CGUITTFont.cpp:450
FT_Int32 load_flags
Definition CGUITTFont.h:400
FT_Size_Metrics font_metrics
Definition CGUITTFont.h:399
CGUITTFont(IGUIEnvironment *env)
Constructor.
Definition CGUITTFont.cpp:252
u32 batch_load_size
Definition CGUITTFont.h:350
s32 GlobalKerningWidth
Definition CGUITTFont.h:405
virtual core::dimension2d< u32 > getDimension(const wchar_t *text) const
Returns the dimension of a text string.
Definition CGUITTFont.cpp:699
u32 shadow_alpha
Definition CGUITTFont.h:409
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:226
virtual void setFontHinting(const bool enable, const bool enable_auto_hinting=true)
Enables or disables font hinting.
Definition CGUITTFont.cpp:513
u32 getLastGlyphPageIndex() const
Get the last glyph page's index.
Definition CGUITTFont.h:323
gui::IGUIFont * fallback
Definition CGUITTFont.h:411
CGUITTGlyphPage * getLastGlyphPage() const
Get the last glyph page if there's still available slots.
Definition CGUITTFont.cpp:436
static scene::IMesh * shared_plane_ptr_
Definition CGUITTFont.h:358
std::u32string Invisible
Definition CGUITTFont.h:407
virtual bool useHinting() const
Check if the font hinting is enabled.
Definition CGUITTFont.h:257
bool use_auto_hinting
Definition CGUITTFont.h:348
bool use_transparency
Definition CGUITTFont.h:346
void createSharedPlane()
Definition CGUITTFont.cpp:1034
bool use_hinting
Definition CGUITTFont.h:347
virtual core::dimension2d< u32 > getCharDimension(const wchar_t ch) const
Returns the dimension of a character produced by this font.
Definition CGUITTFont.cpp:694
virtual ~CGUITTFont()
Destructor.
Definition CGUITTFont.cpp:383
virtual bool useAutoHinting() const
Check if the font auto-hinting is enabled.
Definition CGUITTFont.h:254
static bool c_libraryLoaded
Definition CGUITTFont.h:357
bool use_monochrome
Definition CGUITTFont.h:345
u32 getGlyphIndexByChar(wchar_t c) const
Definition CGUITTFont.cpp:822
core::dimension2d< u32 > getDimensionUntilEndOfLine(const wchar_t *p) const
Definition CGUITTFont.cpp:1064
static std::map< io::path, SGUITTFace * > c_faces
Definition CGUITTFont.h:356
virtual s32 getKerningWidth(const wchar_t *thisLetter=0, const wchar_t *previousLetter=0) const
Gets kerning values (distance between letters) for the font. If no parameters are provided,...
Definition CGUITTFont.cpp:912
std::u32string convertWCharToU32String(const wchar_t *const) const
Definition CGUITTFont.cpp:1224
video::IVideoDriver * Driver
Definition CGUITTFont.h:396
gui::IGUIEnvironment * Environment
Definition CGUITTFont.h:395
virtual void setTransparency(const bool flag)
Tells the font to allow transparency when rendering.
Definition CGUITTFont.cpp:501
u32 size
Definition CGUITTFont.h:349
virtual bool useMonochrome() const
Check if the font is being loaded as a monochrome font.
Definition CGUITTFont.h:261
core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const
Definition CGUITTFont.cpp:934
virtual s32 getCharacterFromPos(const wchar_t *text, s32 pixel_x) const
Calculates the index of the character in the text which is on a specific position.
Definition CGUITTFont.cpp:869
Holds a sheet of glyphs.
Definition CGUITTFont.h:124
bool dirty
Definition CGUITTFont.h:212
core::array< core::recti > render_source_rects
Definition CGUITTFont.h:215
u32 used_slots
Definition CGUITTFont.h:211
void pushGlyphToBePaged(const SGUITTGlyph *glyph)
Add the glyph to a list of glyphs to be paged.
Definition CGUITTFont.h:169
io::path name
Definition CGUITTFont.h:221
u32 available_slots
Definition CGUITTFont.h:210
video::IVideoDriver * driver
Definition CGUITTFont.h:220
core::array< video::SColor > render_colors
Definition CGUITTFont.h:216
core::array< const SGUITTGlyph * > glyph_to_be_paged
Definition CGUITTFont.h:219
bool createPageTexture(const u8 &pixel_mode, const core::dimension2du &texture_size)
Create the actual page texture,.
Definition CGUITTFont.h:138
core::array< core::vector2di > render_positions
Definition CGUITTFont.h:214
~CGUITTGlyphPage()
Definition CGUITTFont.h:127
video::ITexture * texture
Definition CGUITTFont.h:209
CGUITTGlyphPage(video::IVideoDriver *Driver, const io::path &texture_name)
Definition CGUITTFont.h:126
void updateTexture()
Updates the texture atlas with new glyphs.
Definition CGUITTFont.h:175
Definition clientmap.h:30
Structure representing a single TrueType glyph.
Definition CGUITTFont.h:56
CGUITTFont * parent
The pointer pointing to the parent (CGUITTFont)
Definition CGUITTFont.h:119
void unload()
Unloads the glyph.
Definition CGUITTFont.cpp:214
video::IImage * surface
This is just the temporary image holder.
Definition CGUITTFont.h:116
core::recti source_rect
The source rectangle for the glyph.
Definition CGUITTFont.h:106
SGUITTGlyph()
Constructor.
Definition CGUITTFont.h:58
FT_Vector advance
Glyph advance information.
Definition CGUITTFont.h:112
video::IImage * createGlyphImage(const FT_Bitmap &bits, video::IVideoDriver *driver) const
Creates the IImage object from the FT_Bitmap.
Definition CGUITTFont.cpp:89
bool isLoaded
If true, the glyph has been loaded.
Definition CGUITTFont.h:100
u32 glyph_page
The page the glyph is on.
Definition CGUITTFont.h:103
SGUITTGlyph(SGUITTGlyph &&other) noexcept
This class would be trivially copyable except for the reference count on surface.
Definition CGUITTFont.h:71
core::vector2di offset
The offset of glyph when drawn.
Definition CGUITTFont.h:109
DISABLE_CLASS_COPY(SGUITTGlyph)
void preload(u32 char_index, FT_Face face, video::IVideoDriver *driver, u32 font_size, const FT_Int32 loadFlags)
Preload the glyph.
Definition CGUITTFont.cpp:163
~SGUITTGlyph()
Destructor.
Definition CGUITTFont.h:84
static std::string p(std::string path)
Definition test_filesys.cpp:53