Luanti 5.16.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 "IGUIFont.h"
41#include "irrArray.h"
42#include "path.h"
43
44namespace gui {
45 class IGUIEnvironment;
46}
47
48namespace video {
49 class IImage;
50 class ITexture;
51 class IVideoDriver;
52}
53
54class EnrichedString;
55
56namespace gui
57{
58 class CGUITTFont;
59 class CGUITTGlyphPage; // internal
60
63 {
65 glyph_page(0),
67 offset(),
68 advance()
69 {}
71
73 inline bool isLoaded() const {
74 return source_rect != core::recti();
75 }
76
78 void preload(u32 char_index, FT_Face face, CGUITTFont *parent, u32 font_size, const FT_Int32 loadFlags);
79
81 void unload();
82
84 video::IImage* createGlyphImage(const FT_Bitmap &bits, video::IVideoDriver *driver) const;
85
88
90 core::recti source_rect;
91
93 core::vector2di offset;
94
96 core::vector2di advance;
97 };
98
99 // Manages the FT_Face cache.
100 struct SGUITTFace : public IReferenceCounted
101 {
102 private:
103
104 static FT_Library freetype_library;
105 static size_t n_faces;
106
107 static FT_Library getFreeTypeLibrary();
108
109 // This holds the font file data for this face.
110 // Must not be deallocated until we are done with the face!
111 std::string face_buffer;
112
113 public:
114 SGUITTFace(std::string &&buffer);
115 ~SGUITTFace();
116
117 FT_Face face;
118
119 static SGUITTFace* createFace(std::string &&buffer);
120
121 static SGUITTFace* loadFace(const io::path &filename);
122 };
123
125 class CGUITTFont final : public IGUIFont
126 {
127 public:
134 static CGUITTFont* createTTFont(IGUIEnvironment *env,
135 SGUITTFace *face, u32 size, bool antialias = true,
136 bool preload = true, u32 shadow = 0, u32 shadow_alpha = 255);
137
139 virtual ~CGUITTFont();
140
142 void setBatchLoadSize(u32 batch_size) { batch_load_size = batch_size; }
143
145 u32 getFontSize() const { return size; }
146
149 bool useAutoHinting() const { return use_auto_hinting; }
150
152 bool useHinting() const { return use_hinting; }
153
156 bool useMonochrome() const { return use_monochrome; }
157
161 void setMonochrome(const bool flag);
162
167 void setFontHinting(const bool enable, const bool enable_auto_hinting = true);
168
170 virtual void draw(const core::stringw& utext, const core::rect<s32>& position,
171 video::SColor color, bool hcenter=false, bool vcenter=false,
172 const core::rect<s32>* clip=0) override;
173
174 void draw(const EnrichedString& text, const core::rect<s32>& position,
175 bool hcenter=false, bool vcenter=false,
176 const core::rect<s32>* clip=0);
177
179 virtual core::dimension2du getDimension(const wchar_t* text) const override;
180
182 virtual s32 getCharacterFromPos(const wchar_t* text, s32 pixel_x) const override;
183
185 virtual void setKerningWidth(s32 kerning) override;
186
188 virtual void setKerningHeight(s32 kerning) override;
189
191 virtual core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const override;
192
194 virtual void setInvisibleCharacters(const wchar_t *s) override;
195
199
202 //should be better typed. fix later.
203 CGUITTGlyphPage* createGlyphPage(const u8 pixel_mode);
204
206 u32 getLastGlyphPageIndex() const { return Glyph_Pages.size() - 1; }
207
209 void setFallback(gui::IGUIFont *font);
210
211 inline s32 getAscender() const { return font_metrics.ascender; }
212
213 protected:
217 u32 size;
219
220 private:
221 // Helper functions for the same-named public member functions above
222 // (Since std::u32string is nicer to work with than wchar_t *)
223 core::dimension2d<u32> getDimension(const std::u32string& text) const;
224 s32 getCharacterFromPos(const std::u32string& text, s32 pixel_x) const;
225
226 // Helper function for the above helper functions :P
227 std::u32string convertWCharToU32String(const wchar_t* const) const;
228
229 CGUITTFont(IGUIEnvironment *env);
230 bool load(SGUITTFace *face, const u32 size, const bool antialias,
231 const bool transparency, const bool preload);
232
233 void reset_images();
234 void update_glyph_pages() const;
236 {
237 // Set up our loading flags.
238 load_flags = FT_LOAD_DEFAULT | FT_LOAD_RENDER;
239 if (!useHinting())
240 load_flags |= FT_LOAD_NO_HINTING;
241 if (!useAutoHinting())
242 load_flags |= FT_LOAD_NO_AUTOHINT;
243 if (useMonochrome())
244 load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO;
245 else
246 load_flags |= FT_LOAD_TARGET_NORMAL;
247 }
248
250 inline u32 getLineHeight() const { return font_metrics.height / 64 + 1; }
251 u32 getWidthFromCharacter(char32_t c) const;
252 u32 getHeightFromCharacter(char32_t c) const;
255 u32 getGlyphIndexByChar(char32_t c) const;
256 core::vector2di getKerning(const char32_t thisLetter, const char32_t previousLetter) const;
257
258 video::IVideoDriver* Driver = nullptr;
259 FT_Face tt_face;
260 FT_Size_Metrics font_metrics;
261 FT_Int32 load_flags;
262
263 mutable core::array<CGUITTGlyphPage*> Glyph_Pages;
264 mutable core::array<SGUITTGlyph> Glyphs;
265
268 std::u32string InvisibleChars;
271
272 irr_ptr<gui::IGUIFont> fallback;
273 };
274
275} // end namespace gui
Definition enriched_string.h:28
Class representing a TrueType font.
Definition CGUITTFont.h:126
static CGUITTFont * createTTFont(IGUIEnvironment *env, SGUITTFace *face, u32 size, bool antialias=true, bool preload=true, u32 shadow=0, u32 shadow_alpha=255)
Definition CGUITTFont.cpp:370
video::IVideoDriver * Driver
Definition CGUITTFont.h:258
bool use_hinting
Definition CGUITTFont.h:215
u32 size
Definition CGUITTFont.h:217
virtual ~CGUITTFont()
Destructor.
Definition CGUITTFont.cpp:444
void setBatchLoadSize(u32 batch_size)
Sets the amount of glyphs to batch load.
Definition CGUITTFont.h:142
void setMonochrome(const bool flag)
Definition CGUITTFont.cpp:540
void setFontHinting(const bool enable, const bool enable_auto_hinting=true)
Definition CGUITTFont.cpp:546
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:553
virtual void setInvisibleCharacters(const wchar_t *s) override
Define which characters should not be drawn by the font.
Definition CGUITTFont.cpp:966
bool use_auto_hinting
Definition CGUITTFont.h:216
FT_Face tt_face
Definition CGUITTFont.h:259
CGUITTGlyphPage * getLastGlyphPage() const
Definition CGUITTFont.cpp:479
CGUITTGlyphPage * createGlyphPage(const u8 pixel_mode)
Definition CGUITTFont.cpp:489
bool useMonochrome() const
Definition CGUITTFont.h:156
virtual core::dimension2du getDimension(const wchar_t *text) const override
Returns the dimension of a text string.
Definition CGUITTFont.cpp:729
void setFallback(gui::IGUIFont *font)
Set font that should be used for glyphs not present in ours.
Definition CGUITTFont.cpp:534
CGUITTFont(IGUIEnvironment *env)
Constructor.
Definition CGUITTFont.cpp:390
u32 getFontSize() const
Get the font size.
Definition CGUITTFont.h:145
u32 shadow_offset
Definition CGUITTFont.h:269
virtual void setKerningHeight(s32 kerning) override
Sets global kerning height for the font.
Definition CGUITTFont.cpp:911
bool use_monochrome
Definition CGUITTFont.h:214
u32 getLastGlyphPageIndex() const
Get the last glyph page's index.
Definition CGUITTFont.h:206
void update_load_flags()
Definition CGUITTFont.h:235
std::u32string InvisibleChars
Definition CGUITTFont.h:268
virtual core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const override
Returns the distance between letters.
Definition CGUITTFont.cpp:916
u32 getWidthFromCharacter(char32_t c) const
Definition CGUITTFont.cpp:790
FT_Size_Metrics font_metrics
Definition CGUITTFont.h:260
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:829
FT_Int32 load_flags
Definition CGUITTFont.h:261
s32 GlobalKerningWidth
Definition CGUITTFont.h:266
u32 getHeightFromCharacter(char32_t c) const
Definition CGUITTFont.cpp:809
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:874
core::array< CGUITTGlyphPage * > Glyph_Pages
Definition CGUITTFont.h:263
void reset_images()
Definition CGUITTFont.cpp:455
u32 batch_load_size
Definition CGUITTFont.h:218
s32 GlobalKerningHeight
Definition CGUITTFont.h:267
core::array< SGUITTGlyph > Glyphs
Definition CGUITTFont.h:264
u32 shadow_alpha
Definition CGUITTFont.h:270
bool load(SGUITTFace *face, const u32 size, const bool antialias, const bool transparency, const bool preload)
Definition CGUITTFont.cpp:406
virtual void setKerningWidth(s32 kerning) override
Sets global kerning width for the font.
Definition CGUITTFont.cpp:906
u32 getLineHeight() const
Gets the overall font height, including a line gap of 1 px.
Definition CGUITTFont.h:250
void update_glyph_pages() const
Definition CGUITTFont.cpp:470
s32 getAscender() const
Definition CGUITTFont.h:211
std::u32string convertWCharToU32String(const wchar_t *const) const
Definition CGUITTFont.cpp:971
irr_ptr< gui::IGUIFont > fallback
Definition CGUITTFont.h:272
bool useAutoHinting() const
Definition CGUITTFont.h:149
bool useHinting() const
Check if the font hinting is enabled.
Definition CGUITTFont.h:152
Holds a sheet of glyphs.
Definition CGUITTFont.cpp:139
Definition fontengine.h:15
Definition clientmap.h:36
Definition CGUITTFont.h:101
static FT_Library getFreeTypeLibrary()
Definition CGUITTFont.cpp:55
std::string face_buffer
Definition CGUITTFont.h:111
static SGUITTFace * createFace(std::string &&buffer)
Definition CGUITTFont.cpp:84
static size_t n_faces
Definition CGUITTFont.h:105
static SGUITTFace * loadFace(const io::path &filename)
Definition CGUITTFont.cpp:96
FT_Face face
Definition CGUITTFont.h:117
static FT_Library freetype_library
Definition CGUITTFont.h:104
SGUITTFace(std::string &&buffer)
Definition CGUITTFont.cpp:66
~SGUITTFace()
Definition CGUITTFont.cpp:72
core::vector2di offset
The offset of glyph when drawn.
Definition CGUITTFont.h:93
video::IImage * createGlyphImage(const FT_Bitmap &bits, video::IVideoDriver *driver) const
Creates the IImage object from the FT_Bitmap.
Definition CGUITTFont.cpp:240
SGUITTGlyph()
Definition CGUITTFont.h:64
~SGUITTGlyph()
Definition CGUITTFont.h:70
void preload(u32 char_index, FT_Face face, CGUITTFont *parent, u32 font_size, const FT_Int32 loadFlags)
Preload the glyph.
Definition CGUITTFont.cpp:313
void unload()
Unloads the glyph.
Definition CGUITTFont.cpp:361
core::recti source_rect
The source rectangle for the glyph.
Definition CGUITTFont.h:90
bool isLoaded() const
If true, the glyph has been loaded.
Definition CGUITTFont.h:73
core::vector2di advance
Glyph advance information.
Definition CGUITTFont.h:96
u32 glyph_page
The page the glyph is on.
Definition CGUITTFont.h:87