]> git.lizzy.rs Git - minetest.git/blob - src/irrlicht_changes/CGUITTFont.h
Remove dead code behind Irrlicht version checks
[minetest.git] / src / irrlicht_changes / CGUITTFont.h
1 /*
2    CGUITTFont FreeType class for Irrlicht
3    Copyright (c) 2009-2010 John Norman
4    Copyright (c) 2016 NathanaĆ«lle 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 <map>
38 #include <irrUString.h>
39 #include "util/enriched_string.h"
40 #include "util/basic_macros.h"
41 #include FT_FREETYPE_H
42
43 namespace irr
44 {
45 namespace gui
46 {
47         struct SGUITTFace;
48         class CGUITTFont;
49
50         //! Structure representing a single TrueType glyph.
51         struct SGUITTGlyph
52         {
53                 //! Constructor.
54                 SGUITTGlyph() :
55                         isLoaded(false),
56                         glyph_page(0),
57                         source_rect(),
58                         offset(),
59                         advance(),
60                         surface(0),
61                         parent(0)
62                 {}
63
64                 DISABLE_CLASS_COPY(SGUITTGlyph);
65
66                 //! This class would be trivially copyable except for the reference count on `surface`.
67                 SGUITTGlyph(SGUITTGlyph &&other) :
68                         isLoaded(other.isLoaded),
69                         glyph_page(other.glyph_page),
70                         source_rect(other.source_rect),
71                         offset(other.offset),
72                         advance(other.advance),
73                         surface(other.surface),
74                         parent(other.parent)
75                 {
76                         other.surface = 0;
77                 }
78
79                 //! Destructor.
80                 ~SGUITTGlyph() { unload(); }
81
82                 //! Preload the glyph.
83                 //!     The preload process occurs when the program tries to cache the glyph from FT_Library.
84                 //! However, it simply defines the SGUITTGlyph's properties and will only create the page
85                 //! textures if necessary.  The actual creation of the textures should only occur right
86                 //! before the batch draw call.
87                 void preload(u32 char_index, FT_Face face, video::IVideoDriver* driver, u32 font_size, const FT_Int32 loadFlags);
88
89                 //! Unloads the glyph.
90                 void unload();
91
92                 //! Creates the IImage object from the FT_Bitmap.
93                 video::IImage* createGlyphImage(const FT_Bitmap& bits, video::IVideoDriver* driver) const;
94
95                 //! If true, the glyph has been loaded.
96                 bool isLoaded;
97
98                 //! The page the glyph is on.
99                 u32 glyph_page;
100
101                 //! The source rectangle for the glyph.
102                 core::recti source_rect;
103
104                 //! The offset of glyph when drawn.
105                 core::vector2di offset;
106
107                 //! Glyph advance information.
108                 FT_Vector advance;
109
110                 //! This is just the temporary image holder.  After this glyph is paged,
111                 //! it will be dropped.
112                 mutable video::IImage* surface;
113
114                 //! The pointer pointing to the parent (CGUITTFont)
115                 CGUITTFont* parent;
116         };
117
118         //! Holds a sheet of glyphs.
119         class CGUITTGlyphPage
120         {
121                 public:
122                         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                         ~CGUITTGlyphPage()
124                         {
125                                 if (texture)
126                                 {
127                                         if (driver)
128                                                 driver->removeTexture(texture);
129                                         else texture->drop();
130                                 }
131                         }
132
133                         //! Create the actual page texture,
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
163                         //! Add the glyph to a list of glyphs to be paged.
164                         //! This collection will be cleared after updateTexture is called.
165                         void pushGlyphToBePaged(const SGUITTGlyph* glyph)
166                         {
167                                 glyph_to_be_paged.push_back(glyph);
168                         }
169
170                         //! Updates the texture atlas with new glyphs.
171                         void updateTexture()
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->isLoaded)
184                                         {
185                                                 if (glyph->surface)
186                                                 {
187                                                         glyph->surface->copyTo(pageholder, glyph->source_rect.UpperLeftCorner);
188                                                         glyph->surface->drop();
189                                                         glyph->surface = 0;
190                                                 }
191                                                 else
192                                                 {
193                                                         ; // TODO: add error message?
194                                                         //currently, if we failed to create the image, just ignore this operation.
195                                                 }
196                                         }
197                                 }
198
199                                 pageholder->drop();
200                                 texture->unlock();
201                                 glyph_to_be_paged.clear();
202                                 dirty = false;
203                         }
204
205                         video::ITexture* texture;
206                         u32 available_slots;
207                         u32 used_slots;
208                         bool dirty;
209
210                         core::array<core::vector2di> render_positions;
211                         core::array<core::recti> render_source_rects;
212                         core::array<video::SColor> render_colors;
213
214                 private:
215                         core::array<const SGUITTGlyph*> glyph_to_be_paged;
216                         video::IVideoDriver* driver;
217                         io::path name;
218         };
219
220         //! Class representing a TrueType font.
221         class CGUITTFont : public IGUIFont
222         {
223                 public:
224                         //! Creates a new TrueType font and returns a pointer to it.  The pointer must be drop()'ed when finished.
225                         //! \param env The IGUIEnvironment the font loads out of.
226                         //! \param filename The filename of the font.
227                         //! \param size The size of the font glyphs in pixels.  Since this is the size of the individual glyphs, the true height of the font may change depending on the characters used.
228                         //! \param antialias set the use_monochrome (opposite to antialias) flag
229                         //! \param transparency set the use_transparency flag
230                         //! \return Returns a pointer to a CGUITTFont.  Will return 0 if the font failed to load.
231                         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);
232                         static CGUITTFont* createTTFont(IrrlichtDevice *device, const io::path& filename, const u32 size, const bool antialias = true, const bool transparency = true);
233                         static CGUITTFont* create(IGUIEnvironment *env, const io::path& filename, const u32 size, const bool antialias = true, const bool transparency = true);
234                         static CGUITTFont* create(IrrlichtDevice *device, const io::path& filename, const u32 size, const bool antialias = true, const bool transparency = true);
235
236                         //! Destructor
237                         virtual ~CGUITTFont();
238
239                         //! Sets the amount of glyphs to batch load.
240                         virtual void setBatchLoadSize(u32 batch_size) { batch_load_size = batch_size; }
241
242                         //! Sets the maximum texture size for a page of glyphs.
243                         virtual void setMaxPageTextureSize(const core::dimension2du& texture_size) { max_page_texture_size = texture_size; }
244
245                         //! Get the font size.
246                         virtual u32 getFontSize() const { return size; }
247
248                         //! Check the font's transparency.
249                         virtual bool isTransparent() const { return use_transparency; }
250
251                         //! Check if the font auto-hinting is enabled.
252                         //! Auto-hinting is FreeType's built-in font hinting engine.
253                         virtual bool useAutoHinting() const { return use_auto_hinting; }
254
255                         //! Check if the font hinting is enabled.
256                         virtual bool useHinting()        const { return use_hinting; }
257
258                         //! Check if the font is being loaded as a monochrome font.
259                         //! The font can either be a 256 color grayscale font, or a 2 color monochrome font.
260                         virtual bool useMonochrome()  const { return use_monochrome; }
261
262                         //! Tells the font to allow transparency when rendering.
263                         //! Default: true.
264                         //! \param flag If true, the font draws using transparency.
265                         virtual void setTransparency(const bool flag);
266
267                         //! Tells the font to use monochrome rendering.
268                         //! Default: false.
269                         //! \param flag If true, the font draws using a monochrome image.  If false, the font uses a grayscale image.
270                         virtual void setMonochrome(const bool flag);
271
272                         //! Enables or disables font hinting.
273                         //! Default: Hinting and auto-hinting true.
274                         //! \param enable If false, font hinting is turned off. If true, font hinting is turned on.
275                         //! \param enable_auto_hinting If true, FreeType uses its own auto-hinting algorithm.  If false, it tries to use the algorithm specified by the font.
276                         virtual void setFontHinting(const bool enable, const bool enable_auto_hinting = true);
277
278                         //! Draws some text and clips it to the specified rectangle if wanted.
279                         virtual void draw(const core::stringw& text, const core::rect<s32>& position,
280                                 video::SColor color, bool hcenter=false, bool vcenter=false,
281                                 const core::rect<s32>* clip=0);
282
283                         void draw(const EnrichedString& text, const core::rect<s32>& position,
284                                 bool hcenter=false, bool vcenter=false,
285                                 const core::rect<s32>* clip=0);
286
287                         //! Returns the dimension of a character produced by this font.
288                         virtual core::dimension2d<u32> getCharDimension(const wchar_t ch) const;
289
290                         //! Returns the dimension of a text string.
291                         virtual core::dimension2d<u32> getDimension(const wchar_t* text) const;
292                         virtual core::dimension2d<u32> getDimension(const core::ustring& text) const;
293
294                         //! Calculates the index of the character in the text which is on a specific position.
295                         virtual s32 getCharacterFromPos(const wchar_t* text, s32 pixel_x) const;
296                         virtual s32 getCharacterFromPos(const core::ustring& text, s32 pixel_x) const;
297
298                         //! Sets global kerning width for the font.
299                         virtual void setKerningWidth(s32 kerning);
300
301                         //! Sets global kerning height for the font.
302                         virtual void setKerningHeight(s32 kerning);
303
304                         //! Gets kerning values (distance between letters) for the font. If no parameters are provided,
305                         virtual s32 getKerningWidth(const wchar_t* thisLetter=0, const wchar_t* previousLetter=0) const;
306                         virtual s32 getKerningWidth(const uchar32_t thisLetter=0, const uchar32_t previousLetter=0) const;
307
308                         //! Returns the distance between letters
309                         virtual s32 getKerningHeight() const;
310
311                         //! Define which characters should not be drawn by the font.
312                         virtual void setInvisibleCharacters(const wchar_t *s);
313                         virtual void setInvisibleCharacters(const core::ustring& s);
314
315                         //! Get the last glyph page if there's still available slots.
316                         //! If not, it will return zero.
317                         CGUITTGlyphPage* getLastGlyphPage() const;
318
319                         //! Create a new glyph page texture.
320                         //! \param pixel_mode the pixel mode defined by FT_Pixel_Mode
321                         //should be better typed. fix later.
322                         CGUITTGlyphPage* createGlyphPage(const u8& pixel_mode);
323
324                         //! Get the last glyph page's index.
325                         u32 getLastGlyphPageIndex() const { return Glyph_Pages.size() - 1; }
326
327                         //! Set font that should be used for glyphs not present in ours
328                         void setFallback(gui::IGUIFont* font) { fallback = font; }
329
330                         //! Create corresponding character's software image copy from the font,
331                         //! so you can use this data just like any ordinary video::IImage.
332                         //! \param ch The character you need
333                         virtual video::IImage* createTextureFromChar(const uchar32_t& ch);
334
335                         //! This function is for debugging mostly. If the page doesn't exist it returns zero.
336                         //! \param page_index Simply return the texture handle of a given page index.
337                         virtual video::ITexture* getPageTextureByIndex(const u32& page_index) const;
338
339                         //! Add a list of scene nodes generated by putting font textures on the 3D planes.
340                         virtual core::array<scene::ISceneNode*> addTextSceneNode
341                                 (const wchar_t* text, scene::ISceneManager* smgr, scene::ISceneNode* parent = 0,
342                                  const video::SColor& color = video::SColor(255, 0, 0, 0), bool center = false );
343
344                         inline s32 getAscender() const { return font_metrics.ascender; }
345
346                 protected:
347                         bool use_monochrome;
348                         bool use_transparency;
349                         bool use_hinting;
350                         bool use_auto_hinting;
351                         u32 size;
352                         u32 batch_load_size;
353                         core::dimension2du max_page_texture_size;
354
355                 private:
356                         // Manages the FreeType library.
357                         static FT_Library c_library;
358                         static std::map<io::path, SGUITTFace*> c_faces;
359                         static bool c_libraryLoaded;
360                         static scene::IMesh* shared_plane_ptr_;
361                         static scene::SMesh  shared_plane_;
362
363                         CGUITTFont(IGUIEnvironment *env);
364                         bool load(const io::path& filename, const u32 size, const bool antialias, const bool transparency);
365                         void reset_images();
366                         void update_glyph_pages() const;
367                         void update_load_flags()
368                         {
369                                 // Set up our loading flags.
370                                 load_flags = FT_LOAD_DEFAULT | FT_LOAD_RENDER;
371                                 if (!useHinting()) load_flags |= FT_LOAD_NO_HINTING;
372                                 if (!useAutoHinting()) load_flags |= FT_LOAD_NO_AUTOHINT;
373                                 if (useMonochrome()) load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO;
374                                 else load_flags |= FT_LOAD_TARGET_NORMAL;
375                         }
376                         u32 getWidthFromCharacter(wchar_t c) const;
377                         u32 getWidthFromCharacter(uchar32_t c) const;
378                         u32 getHeightFromCharacter(wchar_t c) const;
379                         u32 getHeightFromCharacter(uchar32_t c) const;
380                         u32 getGlyphIndexByChar(wchar_t c) const;
381                         u32 getGlyphIndexByChar(uchar32_t c) const;
382                         core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const;
383                         core::vector2di getKerning(const uchar32_t thisLetter, const uchar32_t previousLetter) const;
384                         core::dimension2d<u32> getDimensionUntilEndOfLine(const wchar_t* p) const;
385
386                         void createSharedPlane();
387
388                         irr::IrrlichtDevice* Device;
389                         gui::IGUIEnvironment* Environment;
390                         video::IVideoDriver* Driver;
391                         io::path filename;
392                         FT_Face tt_face;
393                         FT_Size_Metrics font_metrics;
394                         FT_Int32 load_flags;
395
396                         mutable core::array<CGUITTGlyphPage*> Glyph_Pages;
397                         mutable core::array<SGUITTGlyph> Glyphs;
398
399                         s32 GlobalKerningWidth;
400                         s32 GlobalKerningHeight;
401                         core::ustring Invisible;
402                         u32 shadow_offset;
403                         u32 shadow_alpha;
404
405                         gui::IGUIFont* fallback;
406         };
407
408 } // end namespace gui
409 } // end namespace irr