1 /* 2 CGUITTFont FreeType class for Irrlicht 3 Copyright (c) 2009-2010 John Norman 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any 7 damages arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any 10 purpose, including commercial applications, and to alter it and 11 redistribute it freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you 14 must not claim that you wrote the original software. If you use 15 this software in a product, an acknowledgment in the product 16 documentation would be appreciated but is not required. 17 18 2. Altered source versions must be plainly marked as such, and 19 must not be misrepresented as being the original software. 20 21 3. This notice may not be removed or altered from any source 22 distribution. 23 24 The original version of this class can be located at: 25 http://irrlicht.suckerfreegames.com/ 26 27 John Norman 28 john@suckerfreegames.com 29 */ 30 31 #ifndef __C_GUI_TTFONT_H_INCLUDED__ 32 #define __C_GUI_TTFONT_H_INCLUDED__ 33 34 #include <irrlicht.h> 35 #include <ft2build.h> 36 #include <vector> 37 #include FT_FREETYPE_H 38 39 namespace irr 40 { 41 namespace gui 42 { 43 struct SGUITTFace; 44 class CGUITTFont; 45 46 //! Class to assist in deleting glyphs. 47 class CGUITTAssistDelete 48 { 49 public: 50 template <class T, typename TAlloc> Delete(core::array<T,TAlloc> & a)51 static void Delete(core::array<T, TAlloc>& a) 52 { 53 TAlloc allocator; 54 allocator.deallocate(a.pointer()); 55 } 56 }; 57 58 //! Structure representing a single TrueType glyph. 59 struct SGUITTGlyph 60 { 61 //! Constructor. SGUITTGlyphSGUITTGlyph62 SGUITTGlyph() : isLoaded(false), glyph_page(0), surface(0), parent(0) {} 63 64 //! Destructor. ~SGUITTGlyphSGUITTGlyph65 ~SGUITTGlyph() { unload(); } 66 67 //! Preload the glyph. 68 //! The preload process occurs when the program tries to cache the glyph from FT_Library. 69 //! However, it simply defines the SGUITTGlyph's properties and will only create the page 70 //! textures if necessary. The actual creation of the textures should only occur right 71 //! before the batch draw call. 72 void preload(u32 char_index, FT_Face face, video::IVideoDriver* driver, u32 font_size, const FT_Int32 loadFlags); 73 74 //! Unloads the glyph. 75 void unload(); 76 77 //! Creates the IImage object from the FT_Bitmap. 78 video::IImage* createGlyphImage(const FT_Bitmap& bits, video::IVideoDriver* driver) const; 79 80 //! If true, the glyph has been loaded. 81 bool isLoaded; 82 83 //! The page the glyph is on. 84 u32 glyph_page; 85 86 //! The source rectangle for the glyph. 87 core::recti source_rect; 88 89 //! The offset of glyph when drawn. 90 core::vector2di offset; 91 92 //! Glyph advance information. 93 FT_Vector advance; 94 95 //! This is just the temporary image holder. After this glyph is paged, 96 //! it will be dropped. 97 mutable video::IImage* surface; 98 99 //! The pointer pointing to the parent (CGUITTFont) 100 CGUITTFont* parent; 101 }; 102 103 //! Holds a sheet of glyphs. 104 class CGUITTGlyphPage 105 { 106 public: CGUITTGlyphPage(video::IVideoDriver * Driver,const io::path & texture_name)107 CGUITTGlyphPage(video::IVideoDriver* Driver, const io::path& texture_name) :texture(0), available_slots(0), used_slots(0), dirty(false), driver(Driver), name(texture_name) {} ~CGUITTGlyphPage()108 ~CGUITTGlyphPage() 109 { 110 if (texture) 111 { 112 if (driver) 113 driver->removeTexture(texture); 114 else texture->drop(); 115 } 116 } 117 118 //! Create the actual page texture, createPageTexture(const u8 & pixel_mode,const core::dimension2du & texture_size)119 bool createPageTexture(const u8& pixel_mode, const core::dimension2du& texture_size) 120 { 121 if( texture ) 122 return false; 123 124 bool flgmip = driver->getTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS); 125 driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false); 126 127 // Set the texture color format. 128 switch (pixel_mode) 129 { 130 case FT_PIXEL_MODE_MONO: 131 texture = driver->addTexture(texture_size, name, video::ECF_A1R5G5B5); 132 break; 133 case FT_PIXEL_MODE_GRAY: 134 default: 135 texture = driver->addTexture(texture_size, name, video::ECF_A8R8G8B8); 136 break; 137 } 138 139 // Restore our texture creation flags. 140 driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, flgmip); 141 return texture ? true : false; 142 } 143 144 //! Add the glyph to a list of glyphs to be paged. 145 //! This collection will be cleared after updateTexture is called. pushGlyphToBePaged(const SGUITTGlyph * glyph)146 void pushGlyphToBePaged(const SGUITTGlyph* glyph) 147 { 148 glyph_to_be_paged.push_back(glyph); 149 } 150 151 //! Updates the texture atlas with new glyphs. updateTexture()152 void updateTexture() 153 { 154 if (!dirty) return; 155 156 void* ptr = texture->lock(); 157 video::ECOLOR_FORMAT format = texture->getColorFormat(); 158 core::dimension2du size = texture->getOriginalSize(); 159 video::IImage* pageholder = driver->createImageFromData(format, size, ptr, true, false); 160 161 for (u32 i = 0; i < glyph_to_be_paged.size(); ++i) 162 { 163 const SGUITTGlyph* glyph = glyph_to_be_paged[i]; 164 if (glyph && glyph->isLoaded) 165 { 166 if (glyph->surface) 167 { 168 glyph->surface->copyTo(pageholder, glyph->source_rect.UpperLeftCorner); 169 glyph->surface->drop(); 170 glyph->surface = 0; 171 } 172 else 173 { 174 ; // TODO: add error message? 175 //currently, if we failed to create the image, just ignore this operation. 176 } 177 } 178 } 179 180 pageholder->drop(); 181 texture->unlock(); 182 glyph_to_be_paged.clear(); 183 dirty = false; 184 } 185 186 video::ITexture* texture; 187 u32 available_slots; 188 u32 used_slots; 189 bool dirty; 190 191 core::array<core::vector2di> render_positions; 192 core::array<core::recti> render_source_rects; 193 194 private: 195 core::array<const SGUITTGlyph*> glyph_to_be_paged; 196 video::IVideoDriver* driver; 197 io::path name; 198 }; 199 200 //! Class representing a TrueType font. 201 class CGUITTFont : public IGUIFont 202 { 203 public: 204 //! Creates a new TrueType font and returns a pointer to it. The pointer must be drop()'ed when finished. 205 //! \param env The IGUIEnvironment the font loads out of. 206 //! \param filename The filename of the font. 207 //! \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. 208 //! \param antialias set the use_monochrome (opposite to antialias) flag 209 //! \param transparency set the use_transparency flag 210 //! \return Returns a pointer to a CGUITTFont. Will return 0 if the font failed to load. 211 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); 212 static CGUITTFont* createTTFont(IrrlichtDevice *device, const io::path& filename, const u32 size, const bool antialias = true, const bool transparency = true); 213 static CGUITTFont* create(IGUIEnvironment *env, const io::path& filename, const u32 size, const bool antialias = true, const bool transparency = true); 214 static CGUITTFont* create(IrrlichtDevice *device, const io::path& filename, const u32 size, const bool antialias = true, const bool transparency = true); 215 216 //! Destructor 217 virtual ~CGUITTFont(); 218 219 //! Sets the amount of glyphs to batch load. setBatchLoadSize(u32 batch_size)220 virtual void setBatchLoadSize(u32 batch_size) { batch_load_size = batch_size; } 221 222 //! Sets the maximum texture size for a page of glyphs. setMaxPageTextureSize(const core::dimension2du & texture_size)223 virtual void setMaxPageTextureSize(const core::dimension2du& texture_size) { max_page_texture_size = texture_size; } 224 225 //! Get the font size. getFontSize()226 virtual u32 getFontSize() const { return size; } 227 228 //! Check the font's transparency. isTransparent()229 virtual bool isTransparent() const { return use_transparency; } 230 231 //! Check if the font auto-hinting is enabled. 232 //! Auto-hinting is FreeType's built-in font hinting engine. useAutoHinting()233 virtual bool useAutoHinting() const { return use_auto_hinting; } 234 235 //! Check if the font hinting is enabled. useHinting()236 virtual bool useHinting() const { return use_hinting; } 237 238 //! Check if the font is being loaded as a monochrome font. 239 //! The font can either be a 256 color grayscale font, or a 2 color monochrome font. useMonochrome()240 virtual bool useMonochrome() const { return use_monochrome; } 241 242 //! Tells the font to allow transparency when rendering. 243 //! Default: true. 244 //! \param flag If true, the font draws using transparency. 245 virtual void setTransparency(const bool flag); 246 247 //! Tells the font to use monochrome rendering. 248 //! Default: false. 249 //! \param flag If true, the font draws using a monochrome image. If false, the font uses a grayscale image. 250 virtual void setMonochrome(const bool flag); 251 252 //! Enables or disables font hinting. 253 //! Default: Hinting and auto-hinting true. 254 //! \param enable If false, font hinting is turned off. If true, font hinting is turned on. 255 //! \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. 256 virtual void setFontHinting(const bool enable, const bool enable_auto_hinting = true); 257 258 //! Draws some text and clips it to the specified rectangle if wanted. 259 virtual void draw(const core::stringw& text, const core::rect<s32>& position, 260 video::SColor color, bool hcenter=false, bool vcenter=false, 261 const core::rect<s32>* clip=0); 262 virtual void draw(const core::stringw& text, const core::rect<s32>& position, 263 const std::vector<video::SColor>& color, bool hcenter=false, bool vcenter=false, 264 const core::rect<s32>* clip=0); 265 266 //! Returns the dimension of a character produced by this font. 267 virtual core::dimension2d<u32> getCharDimension(const wchar_t ch) const; 268 269 //! Returns the dimension of a text string. 270 virtual core::dimension2d<u32> getDimension(const wchar_t* text) const; 271 virtual core::dimension2d<u32> getDimension(const core::ustring& text) const; 272 273 //! Calculates the index of the character in the text which is on a specific position. 274 virtual s32 getCharacterFromPos(const wchar_t* text, s32 pixel_x) const; 275 virtual s32 getCharacterFromPos(const core::ustring& text, s32 pixel_x) const; 276 277 //! Sets global kerning width for the font. 278 virtual void setKerningWidth(s32 kerning); 279 280 //! Sets global kerning height for the font. 281 virtual void setKerningHeight(s32 kerning); 282 283 //! Gets kerning values (distance between letters) for the font. If no parameters are provided, 284 virtual s32 getKerningWidth(const wchar_t* thisLetter=0, const wchar_t* previousLetter=0) const; 285 virtual s32 getKerningWidth(const uchar32_t thisLetter=0, const uchar32_t previousLetter=0) const; 286 287 //! Returns the distance between letters 288 virtual s32 getKerningHeight() const; 289 290 //! Define which characters should not be drawn by the font. 291 virtual void setInvisibleCharacters(const wchar_t *s); 292 virtual void setInvisibleCharacters(const core::ustring& s); 293 294 //! Get the last glyph page if there's still available slots. 295 //! If not, it will return zero. 296 CGUITTGlyphPage* getLastGlyphPage() const; 297 298 //! Create a new glyph page texture. 299 //! \param pixel_mode the pixel mode defined by FT_Pixel_Mode 300 //should be better typed. fix later. 301 CGUITTGlyphPage* createGlyphPage(const u8& pixel_mode); 302 303 //! Get the last glyph page's index. getLastGlyphPageIndex()304 u32 getLastGlyphPageIndex() const { return Glyph_Pages.size() - 1; } 305 306 //! Create corresponding character's software image copy from the font, 307 //! so you can use this data just like any ordinary video::IImage. 308 //! \param ch The character you need 309 virtual video::IImage* createTextureFromChar(const uchar32_t& ch); 310 311 //! This function is for debugging mostly. If the page doesn't exist it returns zero. 312 //! \param page_index Simply return the texture handle of a given page index. 313 virtual video::ITexture* getPageTextureByIndex(const u32& page_index) const; 314 315 //! Add a list of scene nodes generated by putting font textures on the 3D planes. 316 virtual core::array<scene::ISceneNode*> addTextSceneNode 317 (const wchar_t* text, scene::ISceneManager* smgr, scene::ISceneNode* parent = 0, 318 const video::SColor& color = video::SColor(255, 0, 0, 0), bool center = false ); 319 320 protected: 321 bool use_monochrome; 322 bool use_transparency; 323 bool use_hinting; 324 bool use_auto_hinting; 325 u32 size; 326 u32 batch_load_size; 327 core::dimension2du max_page_texture_size; 328 329 private: 330 // Manages the FreeType library. 331 static FT_Library c_library; 332 static core::map<io::path, SGUITTFace*> c_faces; 333 static bool c_libraryLoaded; 334 static scene::IMesh* shared_plane_ptr_; 335 static scene::SMesh shared_plane_; 336 337 CGUITTFont(IGUIEnvironment *env); 338 bool load(const io::path& filename, const u32 size, const bool antialias, const bool transparency); 339 void reset_images(); 340 void update_glyph_pages() const; update_load_flags()341 void update_load_flags() 342 { 343 // Set up our loading flags. 344 load_flags = FT_LOAD_DEFAULT | FT_LOAD_RENDER; 345 if (!useHinting()) load_flags |= FT_LOAD_NO_HINTING; 346 if (!useAutoHinting()) load_flags |= FT_LOAD_NO_AUTOHINT; 347 if (useMonochrome()) load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO | FT_RENDER_MODE_MONO; 348 else load_flags |= FT_LOAD_TARGET_NORMAL; 349 } 350 u32 getWidthFromCharacter(wchar_t c) const; 351 u32 getWidthFromCharacter(uchar32_t c) const; 352 u32 getHeightFromCharacter(wchar_t c) const; 353 u32 getHeightFromCharacter(uchar32_t c) const; 354 u32 getGlyphIndexByChar(wchar_t c) const; 355 u32 getGlyphIndexByChar(uchar32_t c) const; 356 core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const; 357 core::vector2di getKerning(const uchar32_t thisLetter, const uchar32_t previousLetter) const; 358 core::dimension2d<u32> getDimensionUntilEndOfLine(const wchar_t* p) const; 359 360 void createSharedPlane(); 361 362 irr::IrrlichtDevice* Device; 363 gui::IGUIEnvironment* Environment; 364 video::IVideoDriver* Driver; 365 io::path filename; 366 FT_Face tt_face; 367 FT_Size_Metrics font_metrics; 368 FT_Int32 load_flags; 369 370 mutable core::array<CGUITTGlyphPage*> Glyph_Pages; 371 mutable core::array<SGUITTGlyph> Glyphs; 372 373 s32 GlobalKerningWidth; 374 s32 GlobalKerningHeight; 375 core::ustring Invisible; 376 u32 shadow_offset; 377 u32 shadow_alpha; 378 }; 379 380 } // end namespace gui 381 } // end namespace irr 382 383 #endif // __C_GUI_TTFONT_H_INCLUDED__ 384