Minetest  5.4.0
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Ć«l Courant
5 
6  This software is provided 'as-is', without any express or implied
7  warranty. In no event will the authors be held liable for any
8  damages arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any
11  purpose, including commercial applications, and to alter it and
12  redistribute it freely, subject to the following restrictions:
13 
14  1. The origin of this software must not be misrepresented; you
15  must not claim that you wrote the original software. If you use
16  this software in a product, an acknowledgment in the product
17  documentation would be appreciated but is not required.
18 
19  2. Altered source versions must be plainly marked as such, and
20  must not be misrepresented as being the original software.
21 
22  3. This notice may not be removed or altered from any source
23  distribution.
24 
25  The original version of this class can be located at:
26  http://irrlicht.suckerfreegames.com/
27 
28  John Norman
29  john@suckerfreegames.com
30 */
31 
32 #pragma once
33 
34 #include <irrlicht.h>
35 #include <ft2build.h>
36 #include <vector>
37 #include "irrUString.h"
38 #include "util/enriched_string.h"
39 #include FT_FREETYPE_H
40 
41 namespace irr
42 {
43 namespace gui
44 {
45  struct SGUITTFace;
46  class CGUITTFont;
47 
50  {
51  public:
52  template <class T, typename TAlloc>
53  static void Delete(core::array<T, TAlloc>& a)
54  {
55  TAlloc allocator;
56  allocator.deallocate(a.pointer());
57  }
58  };
59 
61  struct SGUITTGlyph
62  {
64  SGUITTGlyph() : isLoaded(false), glyph_page(0), surface(0), parent(0) {}
65 
68 
74  void preload(u32 char_index, FT_Face face, video::IVideoDriver* driver, u32 font_size, const FT_Int32 loadFlags);
75 
77  void unload();
78 
80  video::IImage* createGlyphImage(const FT_Bitmap& bits, video::IVideoDriver* driver) const;
81 
83  bool isLoaded;
84 
87 
89  core::recti source_rect;
90 
92  core::vector2di offset;
93 
95  FT_Vector advance;
96 
99  mutable video::IImage* surface;
100 
103  };
104 
107  {
108  public:
109  CGUITTGlyphPage(video::IVideoDriver* Driver, const io::path& texture_name) :texture(0), available_slots(0), used_slots(0), dirty(false), driver(Driver), name(texture_name) {}
111  {
112  if (texture)
113  {
114  if (driver)
115  driver->removeTexture(texture);
116  else texture->drop();
117  }
118  }
119 
121  bool createPageTexture(const u8& pixel_mode, const core::dimension2du& texture_size)
122  {
123  if( texture )
124  return false;
125 
126  bool flgmip = driver->getTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS);
127  driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
128 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
129  bool flgcpy = driver->getTextureCreationFlag(video::ETCF_ALLOW_MEMORY_COPY);
130  driver->setTextureCreationFlag(video::ETCF_ALLOW_MEMORY_COPY, true);
131 #endif
132 
133  // Set the texture color format.
134  switch (pixel_mode)
135  {
136  case FT_PIXEL_MODE_MONO:
137  texture = driver->addTexture(texture_size, name, video::ECF_A1R5G5B5);
138  break;
139  case FT_PIXEL_MODE_GRAY:
140  default:
141  texture = driver->addTexture(texture_size, name, video::ECF_A8R8G8B8);
142  break;
143  }
144 
145  // Restore our texture creation flags.
146  driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, flgmip);
147 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
148  driver->setTextureCreationFlag(video::ETCF_ALLOW_MEMORY_COPY, flgcpy);
149 #endif
150  return texture ? true : false;
151  }
152 
155  void pushGlyphToBePaged(const SGUITTGlyph* glyph)
156  {
157  glyph_to_be_paged.push_back(glyph);
158  }
159 
162  {
163  if (!dirty) return;
164 
165  void* ptr = texture->lock();
166  video::ECOLOR_FORMAT format = texture->getColorFormat();
167  core::dimension2du size = texture->getOriginalSize();
168  video::IImage* pageholder = driver->createImageFromData(format, size, ptr, true, false);
169 
170  for (u32 i = 0; i < glyph_to_be_paged.size(); ++i)
171  {
172  const SGUITTGlyph* glyph = glyph_to_be_paged[i];
173  if (glyph && glyph->isLoaded)
174  {
175  if (glyph->surface)
176  {
177  glyph->surface->copyTo(pageholder, glyph->source_rect.UpperLeftCorner);
178  glyph->surface->drop();
179  glyph->surface = 0;
180  }
181  else
182  {
183  ; // TODO: add error message?
184  //currently, if we failed to create the image, just ignore this operation.
185  }
186  }
187  }
188 
189  pageholder->drop();
190  texture->unlock();
191  glyph_to_be_paged.clear();
192  dirty = false;
193  }
194 
195  video::ITexture* texture;
198  bool dirty;
199 
200  core::array<core::vector2di> render_positions;
201  core::array<core::recti> render_source_rects;
202 
203  private:
204  core::array<const SGUITTGlyph*> glyph_to_be_paged;
205  video::IVideoDriver* driver;
206  io::path name;
207  };
208 
210  class CGUITTFont : public IGUIFont
211  {
212  public:
220  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);
221  static CGUITTFont* createTTFont(IrrlichtDevice *device, const io::path& filename, const u32 size, const bool antialias = true, const bool transparency = true);
222  static CGUITTFont* create(IGUIEnvironment *env, const io::path& filename, const u32 size, const bool antialias = true, const bool transparency = true);
223  static CGUITTFont* create(IrrlichtDevice *device, const io::path& filename, const u32 size, const bool antialias = true, const bool transparency = true);
224 
226  virtual ~CGUITTFont();
227 
229  virtual void setBatchLoadSize(u32 batch_size) { batch_load_size = batch_size; }
230 
232  virtual void setMaxPageTextureSize(const core::dimension2du& texture_size) { max_page_texture_size = texture_size; }
233 
235  virtual u32 getFontSize() const { return size; }
236 
238  virtual bool isTransparent() const { return use_transparency; }
239 
242  virtual bool useAutoHinting() const { return use_auto_hinting; }
243 
245  virtual bool useHinting() const { return use_hinting; }
246 
249  virtual bool useMonochrome() const { return use_monochrome; }
250 
254  virtual void setTransparency(const bool flag);
255 
259  virtual void setMonochrome(const bool flag);
260 
265  virtual 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);
271 
272  virtual void draw(const EnrichedString& text, const core::rect<s32>& position,
273  video::SColor color, bool hcenter=false, bool vcenter=false,
274  const core::rect<s32>* clip=0);
275 
277  virtual core::dimension2d<u32> getCharDimension(const wchar_t ch) const;
278 
280  virtual core::dimension2d<u32> getDimension(const wchar_t* text) const;
281  virtual core::dimension2d<u32> getDimension(const core::ustring& text) const;
282 
284  virtual s32 getCharacterFromPos(const wchar_t* text, s32 pixel_x) const;
285  virtual s32 getCharacterFromPos(const core::ustring& text, s32 pixel_x) const;
286 
288  virtual void setKerningWidth(s32 kerning);
289 
291  virtual void setKerningHeight(s32 kerning);
292 
294  virtual s32 getKerningWidth(const wchar_t* thisLetter=0, const wchar_t* previousLetter=0) const;
295  virtual s32 getKerningWidth(const uchar32_t thisLetter=0, const uchar32_t previousLetter=0) const;
296 
298  virtual s32 getKerningHeight() const;
299 
301  virtual void setInvisibleCharacters(const wchar_t *s);
302  virtual void setInvisibleCharacters(const core::ustring& s);
303 
307 
310  //should be better typed. fix later.
311  CGUITTGlyphPage* createGlyphPage(const u8& pixel_mode);
312 
314  u32 getLastGlyphPageIndex() const { return Glyph_Pages.size() - 1; }
315 
319  virtual video::IImage* createTextureFromChar(const uchar32_t& ch);
320 
323  virtual video::ITexture* getPageTextureByIndex(const u32& page_index) const;
324 
326  virtual core::array<scene::ISceneNode*> addTextSceneNode
327  (const wchar_t* text, scene::ISceneManager* smgr, scene::ISceneNode* parent = 0,
328  const video::SColor& color = video::SColor(255, 0, 0, 0), bool center = false );
329 
330  inline s32 getAscender() const { return font_metrics.ascender; }
331 
332  protected:
337  u32 size;
339  core::dimension2du max_page_texture_size;
340 
341  private:
342  // Manages the FreeType library.
343  static FT_Library c_library;
344  static core::map<io::path, SGUITTFace*> c_faces;
345  static bool c_libraryLoaded;
346  static scene::IMesh* shared_plane_ptr_;
347  static scene::SMesh shared_plane_;
348 
349  CGUITTFont(IGUIEnvironment *env);
350  bool load(const io::path& filename, const u32 size, const bool antialias, const bool transparency);
351  void reset_images();
352  void update_glyph_pages() const;
354  {
355  // Set up our loading flags.
356  load_flags = FT_LOAD_DEFAULT | FT_LOAD_RENDER;
357  if (!useHinting()) load_flags |= FT_LOAD_NO_HINTING;
358  if (!useAutoHinting()) load_flags |= FT_LOAD_NO_AUTOHINT;
359  if (useMonochrome()) load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO;
360  else load_flags |= FT_LOAD_TARGET_NORMAL;
361  }
362  u32 getWidthFromCharacter(wchar_t c) const;
363  u32 getWidthFromCharacter(uchar32_t c) const;
364  u32 getHeightFromCharacter(wchar_t c) const;
365  u32 getHeightFromCharacter(uchar32_t c) const;
366  u32 getGlyphIndexByChar(wchar_t c) const;
367  u32 getGlyphIndexByChar(uchar32_t c) const;
368  core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const;
369  core::vector2di getKerning(const uchar32_t thisLetter, const uchar32_t previousLetter) const;
370  core::dimension2d<u32> getDimensionUntilEndOfLine(const wchar_t* p) const;
371 
372  void createSharedPlane();
373 
374  irr::IrrlichtDevice* Device;
375  gui::IGUIEnvironment* Environment;
376  video::IVideoDriver* Driver;
377  io::path filename;
378  FT_Face tt_face;
379  FT_Size_Metrics font_metrics;
380  FT_Int32 load_flags;
381 
382  mutable core::array<CGUITTGlyphPage*> Glyph_Pages;
383  mutable core::array<SGUITTGlyph> Glyphs;
384 
390  };
391 
392 } // end namespace gui
393 } // end namespace irr
Definition: enriched_string.h:26
UTF-16 string class.
Definition: irrUString.h:249
Class to assist in deleting glyphs.
Definition: CGUITTFont.h:50
static void Delete(core::array< T, TAlloc > &a)
Definition: CGUITTFont.h:53
Class representing a TrueType font.
Definition: CGUITTFont.h:211
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:1039
static core::map< io::path, SGUITTFace * > c_faces
Definition: CGUITTFont.h:344
s32 getAscender() const
Definition: CGUITTFont.h:330
virtual video::ITexture * getPageTextureByIndex(const u32 &page_index) const
This function is for debugging mostly.
Definition: CGUITTFont.cpp:993
virtual void setKerningHeight(s32 kerning)
Sets global kerning height for the font.
Definition: CGUITTFont.cpp:884
s32 GlobalKerningHeight
Definition: CGUITTFont.h:386
io::path filename
Definition: CGUITTFont.h:377
u32 shadow_offset
Definition: CGUITTFont.h:388
u32 getWidthFromCharacter(wchar_t c) const
Definition: CGUITTFont.cpp:754
void reset_images()
Definition: CGUITTFont.cpp:441
static scene::SMesh shared_plane_
Definition: CGUITTFont.h:347
virtual void setBatchLoadSize(u32 batch_size)
Sets the amount of glyphs to batch load.
Definition: CGUITTFont.h:229
virtual void setMonochrome(const bool flag)
Tells the font to use monochrome rendering.
Definition: CGUITTFont.cpp:536
virtual u32 getFontSize() const
Get the font size.
Definition: CGUITTFont.h:235
u32 getHeightFromCharacter(wchar_t c) const
Definition: CGUITTFont.cpp:776
bool load(const io::path &filename, const u32 size, const bool antialias, const bool transparency)
Definition: CGUITTFont.cpp:301
virtual void setKerningWidth(s32 kerning)
Sets global kerning width for the font.
Definition: CGUITTFont.cpp:879
core::array< CGUITTGlyphPage * > Glyph_Pages
Definition: CGUITTFont.h:382
void update_load_flags()
Definition: CGUITTFont.h:353
core::dimension2du max_page_texture_size
Definition: CGUITTFont.h:339
virtual void setMaxPageTextureSize(const core::dimension2du &texture_size)
Sets the maximum texture size for a page of glyphs.
Definition: CGUITTFont.h:232
virtual void setInvisibleCharacters(const wchar_t *s)
Define which characters should not be drawn by the font.
Definition: CGUITTFont.cpp:951
irr::IrrlichtDevice * Device
Definition: CGUITTFont.h:374
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:549
FT_Face tt_face
Definition: CGUITTFont.h:378
core::array< SGUITTGlyph > Glyphs
Definition: CGUITTFont.h:383
virtual s32 getKerningHeight() const
Returns the distance between letters.
Definition: CGUITTFont.cpp:905
static FT_Library c_library
Definition: CGUITTFont.h:343
void update_glyph_pages() const
Definition: CGUITTFont.cpp:456
virtual bool isTransparent() const
Check the font's transparency.
Definition: CGUITTFont.h:238
CGUITTGlyphPage * createGlyphPage(const u8 &pixel_mode)
Create a new glyph page texture.
Definition: CGUITTFont.cpp:479
FT_Int32 load_flags
Definition: CGUITTFont.h:380
FT_Size_Metrics font_metrics
Definition: CGUITTFont.h:379
CGUITTFont(IGUIEnvironment *env)
Constructor.
Definition: CGUITTFont.cpp:278
u32 batch_load_size
Definition: CGUITTFont.h:338
s32 GlobalKerningWidth
Definition: CGUITTFont.h:385
virtual core::dimension2d< u32 > getDimension(const wchar_t *text) const
Returns the dimension of a text string.
Definition: CGUITTFont.cpp:688
u32 shadow_alpha
Definition: CGUITTFont.h:389
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:221
virtual void setFontHinting(const bool enable, const bool enable_auto_hinting=true)
Enables or disables font hinting.
Definition: CGUITTFont.cpp:542
u32 getLastGlyphPageIndex() const
Get the last glyph page's index.
Definition: CGUITTFont.h:314
virtual video::IImage * createTextureFromChar(const uchar32_t &ch)
Create corresponding character's software image copy from the font, so you can use this data just lik...
Definition: CGUITTFont.cpp:962
CGUITTGlyphPage * getLastGlyphPage() const
Get the last glyph page if there's still available slots.
Definition: CGUITTFont.cpp:465
static scene::IMesh * shared_plane_ptr_
Definition: CGUITTFont.h:346
virtual bool useHinting() const
Check if the font hinting is enabled.
Definition: CGUITTFont.h:245
bool use_auto_hinting
Definition: CGUITTFont.h:336
bool use_transparency
Definition: CGUITTFont.h:334
void createSharedPlane()
Definition: CGUITTFont.cpp:1001
bool use_hinting
Definition: CGUITTFont.h:335
virtual core::dimension2d< u32 > getCharDimension(const wchar_t ch) const
Returns the dimension of a character produced by this font.
Definition: CGUITTFont.cpp:683
virtual ~CGUITTFont()
Destructor.
Definition: CGUITTFont.cpp:411
virtual bool useAutoHinting() const
Check if the font auto-hinting is enabled.
Definition: CGUITTFont.h:242
static bool c_libraryLoaded
Definition: CGUITTFont.h:345
bool use_monochrome
Definition: CGUITTFont.h:333
u32 getGlyphIndexByChar(wchar_t c) const
Definition: CGUITTFont.cpp:799
core::dimension2d< u32 > getDimensionUntilEndOfLine(const wchar_t *p) const
Definition: CGUITTFont.cpp:1030
static CGUITTFont * create(IGUIEnvironment *env, const io::path &filename, const u32 size, const bool antialias=true, const bool transparency=true)
Definition: CGUITTFont.cpp:265
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:889
video::IVideoDriver * Driver
Definition: CGUITTFont.h:376
gui::IGUIEnvironment * Environment
Definition: CGUITTFont.h:375
virtual void setTransparency(const bool flag)
Tells the font to allow transparency when rendering.
Definition: CGUITTFont.cpp:530
u32 size
Definition: CGUITTFont.h:337
virtual bool useMonochrome() const
Check if the font is being loaded as a monochrome font.
Definition: CGUITTFont.h:249
core::ustring Invisible
Definition: CGUITTFont.h:387
core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const
Definition: CGUITTFont.cpp:911
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:846
Holds a sheet of glyphs.
Definition: CGUITTFont.h:107
bool dirty
Definition: CGUITTFont.h:198
core::array< core::recti > render_source_rects
Definition: CGUITTFont.h:201
u32 used_slots
Definition: CGUITTFont.h:197
void pushGlyphToBePaged(const SGUITTGlyph *glyph)
Add the glyph to a list of glyphs to be paged.
Definition: CGUITTFont.h:155
io::path name
Definition: CGUITTFont.h:206
u32 available_slots
Definition: CGUITTFont.h:196
video::IVideoDriver * driver
Definition: CGUITTFont.h:205
core::array< const SGUITTGlyph * > glyph_to_be_paged
Definition: CGUITTFont.h:204
bool createPageTexture(const u8 &pixel_mode, const core::dimension2du &texture_size)
Create the actual page texture,.
Definition: CGUITTFont.h:121
core::array< core::vector2di > render_positions
Definition: CGUITTFont.h:200
~CGUITTGlyphPage()
Definition: CGUITTFont.h:110
video::ITexture * texture
Definition: CGUITTFont.h:195
CGUITTGlyphPage(video::IVideoDriver *Driver, const io::path &texture_name)
Definition: CGUITTFont.h:109
void updateTexture()
Updates the texture atlas with new glyphs.
Definition: CGUITTFont.h:161
Definition: clouds.h:32
u32 uchar32_t
Definition: irrUString.h:93
Structure representing a single TrueType glyph.
Definition: CGUITTFont.h:62
CGUITTFont * parent
The pointer pointing to the parent (CGUITTFont)
Definition: CGUITTFont.h:102
void unload()
Unloads the glyph.
Definition: CGUITTFont.cpp:209
video::IImage * surface
This is just the temporary image holder.
Definition: CGUITTFont.h:99
core::recti source_rect
The source rectangle for the glyph.
Definition: CGUITTFont.h:89
SGUITTGlyph()
Constructor.
Definition: CGUITTFont.h:64
FT_Vector advance
Glyph advance information.
Definition: CGUITTFont.h:95
video::IImage * createGlyphImage(const FT_Bitmap &bits, video::IVideoDriver *driver) const
Creates the IImage object from the FT_Bitmap.
Definition: CGUITTFont.cpp:82
bool isLoaded
If true, the glyph has been loaded.
Definition: CGUITTFont.h:83
u32 glyph_page
The page the glyph is on.
Definition: CGUITTFont.h:86
core::vector2di offset
The offset of glyph when drawn.
Definition: CGUITTFont.h:92
void preload(u32 char_index, FT_Face face, video::IVideoDriver *driver, u32 font_size, const FT_Int32 loadFlags)
Preload the glyph.
Definition: CGUITTFont.cpp:158
~SGUITTGlyph()
Destructor.
Definition: CGUITTFont.h:67
std::string p(std::string path)
Definition: test_filepath.cpp:59