1 /*------------------------------------------------------------------------- 2 This source file is a part of OGRE 3 (Object-oriented Graphics Rendering Engine) 4 5 For the latest info, see http://www.ogre3d.org/ 6 7 Copyright (c) 2000-2014 Torus Knot Software Ltd 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE 25 -------------------------------------------------------------------------*/ 26 27 #ifndef _Font_H__ 28 #define _Font_H__ 29 30 #include "OgreOverlayPrerequisites.h" 31 #include "OgreResource.h" 32 #include "OgreCommon.h" 33 #include "OgreSharedPtr.h" 34 35 namespace Ogre 36 { 37 /** \addtogroup Optional 38 * @{ 39 */ 40 /** \addtogroup Overlays 41 * @{ 42 */ 43 /** Enumerates the types of Font usable in the engine. */ 44 enum FontType 45 { 46 /// Generated from a truetype (.ttf) font 47 FT_TRUETYPE = 1, 48 /// Loaded from an image created by an artist 49 FT_IMAGE = 2 50 }; 51 52 53 /** Class representing a font in the system. 54 @remarks 55 This class is simply a way of getting a font texture into the OGRE system and 56 to easily retrieve the texture coordinates required to accurately render them. 57 Fonts can either be loaded from precreated textures, or the texture can be generated 58 using a truetype font. You can either create the texture manually in code, or you 59 can use a .fontdef script to define it (probably more practical since you can reuse 60 the definition more easily) 61 @note 62 This class extends both Resource and ManualResourceLoader since it is 63 both a resource in it's own right, but it also provides the manual load 64 implementation for the Texture it creates. 65 */ 66 class _OgreOverlayExport Font : public Resource, public ManualResourceLoader 67 { 68 protected: 69 /// Command object for Font - see ParamCommand 70 class _OgreOverlayExport CmdType : public ParamCommand 71 { 72 public: 73 String doGet(const void* target) const; 74 void doSet(void* target, const String& val); 75 }; 76 /// Command object for Font - see ParamCommand 77 class _OgreOverlayExport CmdSource : public ParamCommand 78 { 79 public: 80 String doGet(const void* target) const; 81 void doSet(void* target, const String& val); 82 }; 83 class _OgreOverlayExport CmdCharSpacer : public ParamCommand 84 { 85 public: 86 String doGet(const void* target) const; 87 void doSet(void* target, const String& val); 88 }; 89 /// Command object for Font - see ParamCommand 90 class _OgreOverlayExport CmdSize : public ParamCommand 91 { 92 public: 93 String doGet(const void* target) const; 94 void doSet(void* target, const String& val); 95 }; 96 /// Command object for Font - see ParamCommand 97 class _OgreOverlayExport CmdResolution : public ParamCommand 98 { 99 public: 100 String doGet(const void* target) const; 101 void doSet(void* target, const String& val); 102 }; 103 /// Command object for Font - see ParamCommand 104 class _OgreOverlayExport CmdCodePoints : public ParamCommand 105 { 106 public: 107 String doGet(const void* target) const; 108 void doSet(void* target, const String& val); 109 }; 110 111 // Command object for setting / getting parameters 112 static CmdType msTypeCmd; 113 static CmdSource msSourceCmd; 114 static CmdCharSpacer msCharacterSpacerCmd; 115 static CmdSize msSizeCmd; 116 static CmdResolution msResolutionCmd; 117 static CmdCodePoints msCodePointsCmd; 118 119 /// The type of font 120 FontType mType; 121 122 /// Source of the font (either an image name or a truetype font) 123 String mSource; 124 125 /** Add a gap between letters vertically and horizonally 126 prevents nasty artifacts caused by fonts atypically wide or tall characters. */ 127 uint mCharacterSpacer; 128 129 /// Size of the truetype font, in points 130 Real mTtfSize; 131 /// Resolution (dpi) of truetype font 132 uint mTtfResolution; 133 /// Max distance to baseline of this (truetype) font 134 int mTtfMaxBearingY; 135 136 137 public: 138 typedef Ogre::uint32 CodePoint; 139 typedef Ogre::FloatRect UVRect; 140 /// Information about the position and size of a glyph in a texture 141 struct GlyphInfo 142 { 143 CodePoint codePoint; 144 UVRect uvRect; 145 Real aspectRatio; 146 GlyphInfoGlyphInfo147 GlyphInfo(CodePoint id, const UVRect& rect, Real aspect) 148 : codePoint(id), uvRect(rect), aspectRatio(aspect) 149 { 150 151 } 152 }; 153 /// A range of code points, inclusive on both ends 154 typedef std::pair<CodePoint, CodePoint> CodePointRange; 155 typedef std::vector<CodePointRange> CodePointRangeList; 156 protected: 157 /// Map from unicode code point to texture coordinates 158 typedef std::map<CodePoint, GlyphInfo> CodePointMap; 159 CodePointMap mCodePointMap; 160 161 /// The material which is generated for this font 162 MaterialPtr mMaterial; 163 164 /// Texture pointer 165 TexturePtr mTexture; 166 167 /// For TRUE_TYPE font only 168 bool mAntialiasColour; 169 170 /// Range of code points to generate glyphs for (truetype only) 171 CodePointRangeList mCodePointRangeList; 172 173 /// Internal method for loading from ttf 174 void createTextureFromFont(void); 175 176 /// @copydoc Resource::loadImpl 177 virtual void loadImpl(); 178 /// @copydoc Resource::unloadImpl 179 virtual void unloadImpl(); 180 /// @copydoc Resource::calculateSize calculateSize(void)181 size_t calculateSize(void) const { return 0; } // permanent resource is in the texture 182 public: 183 184 /** Constructor. 185 @see Resource 186 */ 187 Font(ResourceManager* creator, const String& name, ResourceHandle handle, 188 const String& group, bool isManual = false, ManualResourceLoader* loader = 0); 189 virtual ~Font(); 190 191 /** Sets the type of font. Must be set before loading. */ 192 void setType(FontType ftype); 193 194 /** Gets the type of font. */ 195 FontType getType(void) const; 196 197 /** Sets the source of the font. 198 @remarks 199 If you have created a font of type FT_IMAGE, this method tells the 200 Font which image to use as the source for the characters. So the parameter 201 should be the name of an appropriate image file. Note that when using an image 202 as a font source, you will also need to tell the font where each character is 203 located using setGlyphTexCoords (for each character). 204 @par 205 If you have created a font of type FT_TRUETYPE, this method tells the 206 Font which .ttf file to use to generate the text. You will also need to call 207 setTrueTypeSize and setTrueTypeResolution, and call addCodePointRange 208 as many times as required to define the range of glyphs you want to be 209 available. 210 @param source An image file or a truetype font, depending on the type of this font 211 */ 212 void setSource(const String& source); 213 214 /** Gets the source this font (either an image or a truetype font). 215 */ 216 const String& getSource(void) const; 217 218 /** Sets the spacing to allocate for font characters to overlap each other. 219 @param charSpacer The size of the character spacer, in points. Increasing it 220 allows for more stretched-out fonts; decreasing it reduces memory and processing 221 time. The default is "5". 222 */ 223 void setCharacterSpacer(uint charSpacer); 224 225 /** Gets the spacing to allocate for font characters to overlap each other. 226 @remarks Returns the size of the character spacer, in points. A higher value 227 allows for more stretched-out fonts. A low value reduces memory and processing 228 time. The default is "5". 229 */ 230 uint getCharacterSpacer(void) const; 231 232 /** Sets the size of a truetype font (only required for FT_TRUETYPE). 233 @param ttfSize The size of the font in points. Note that the 234 size of the font does not affect how big it is on the screen, just how large it is in 235 the texture and thus how detailed it is. 236 */ 237 void setTrueTypeSize(Real ttfSize); 238 /** Gets the resolution (dpi) of the font used to generate the texture 239 (only required for FT_TRUETYPE). 240 @param ttfResolution The resolution in dpi 241 */ 242 void setTrueTypeResolution(uint ttfResolution); 243 244 /** Gets the point size of the font used to generate the texture. 245 @remarks 246 Only applicable for FT_TRUETYPE Font objects. 247 Note that the size of the font does not affect how big it is on the screen, 248 just how large it is in the texture and thus how detailed it is. 249 */ 250 Real getTrueTypeSize(void) const; 251 /** Gets the resolution (dpi) of the font used to generate the texture. 252 @remarks 253 Only applicable for FT_TRUETYPE Font objects. 254 */ 255 uint getTrueTypeResolution(void) const; 256 /** Gets the maximum baseline distance of all glyphs used in the texture. 257 @remarks 258 Only applicable for FT_TRUETYPE Font objects. 259 The baseline is the vertical origin of horizontal based glyphs. The bearingY 260 attribute is the distance from the baseline (origin) to the top of the glyph's 261 bounding box. 262 @note 263 This value is only available after the font has been loaded. 264 */ 265 int getTrueTypeMaxBearingY() const; 266 267 268 /** Returns the texture coordinates of the associated glyph. 269 @remarks Parameter is a short to allow both ASCII and wide chars. 270 @param id The code point (unicode) 271 @return A rectangle with the UV coordinates, or null UVs if the 272 code point was not present 273 */ getGlyphTexCoords(CodePoint id)274 inline const UVRect& getGlyphTexCoords(CodePoint id) const 275 { 276 CodePointMap::const_iterator i = mCodePointMap.find(id); 277 if (i != mCodePointMap.end()) 278 { 279 return i->second.uvRect; 280 } 281 else 282 { 283 static UVRect nullRect(0.0, 0.0, 0.0, 0.0); 284 return nullRect; 285 } 286 } 287 288 /** Sets the texture coordinates of a glyph. 289 @remarks 290 You only need to call this if you're setting up a font loaded from a texture manually. 291 @note 292 Also sets the aspect ratio (width / height) of this character. textureAspect 293 is the width/height of the texture (may be non-square) 294 */ setGlyphTexCoords(CodePoint id,Real u1,Real v1,Real u2,Real v2,Real textureAspect)295 inline void setGlyphTexCoords(CodePoint id, Real u1, Real v1, Real u2, Real v2, Real textureAspect) 296 { 297 CodePointMap::iterator i = mCodePointMap.find(id); 298 if (i != mCodePointMap.end()) 299 { 300 i->second.uvRect.left = u1; 301 i->second.uvRect.top = v1; 302 i->second.uvRect.right = u2; 303 i->second.uvRect.bottom = v2; 304 i->second.aspectRatio = textureAspect * (u2 - u1) / (v2 - v1); 305 } 306 else 307 { 308 mCodePointMap.insert( 309 CodePointMap::value_type(id, 310 GlyphInfo(id, UVRect(u1, v1, u2, v2), 311 textureAspect * (u2 - u1) / (v2 - v1)))); 312 } 313 314 } 315 /** Gets the aspect ratio (width / height) of this character. */ getGlyphAspectRatio(CodePoint id)316 inline Real getGlyphAspectRatio(CodePoint id) const 317 { 318 CodePointMap::const_iterator i = mCodePointMap.find(id); 319 if (i != mCodePointMap.end()) 320 { 321 return i->second.aspectRatio; 322 } 323 else 324 { 325 return 1.0; 326 } 327 } 328 /** Sets the aspect ratio (width / height) of this character. 329 @remarks 330 You only need to call this if you're setting up a font loaded from a 331 texture manually. 332 */ setGlyphAspectRatio(CodePoint id,Real ratio)333 inline void setGlyphAspectRatio(CodePoint id, Real ratio) 334 { 335 CodePointMap::iterator i = mCodePointMap.find(id); 336 if (i != mCodePointMap.end()) 337 { 338 i->second.aspectRatio = ratio; 339 } 340 } 341 342 /** Gets the information available for a glyph corresponding to a 343 given code point, or throws an exception if it doesn't exist; 344 */ 345 const GlyphInfo& getGlyphInfo(CodePoint id) const; 346 347 /** Adds a range of code points to the list of code point ranges to generate 348 glyphs for, if this is a truetype based font. 349 @remarks 350 In order to save texture space, only the glyphs which are actually 351 needed by the application are generated into the texture. Before this 352 object is loaded you must call this method as many times as necessary 353 to define the code point range that you need. 354 */ addCodePointRange(const CodePointRange & range)355 void addCodePointRange(const CodePointRange& range) 356 { 357 mCodePointRangeList.push_back(range); 358 } 359 360 /** Clear the list of code point ranges. 361 */ clearCodePointRanges()362 void clearCodePointRanges() 363 { 364 mCodePointRangeList.clear(); 365 } 366 /** Get a const reference to the list of code point ranges to be used to 367 generate glyphs from a truetype font. 368 */ getCodePointRangeList()369 const CodePointRangeList& getCodePointRangeList() const 370 { 371 return mCodePointRangeList; 372 } 373 /** Gets the material generated for this font, as a weak reference. 374 @remarks 375 This will only be valid after the Font has been loaded. 376 */ getMaterial()377 inline const MaterialPtr& getMaterial() const 378 { 379 return mMaterial; 380 } 381 /** Gets the material generated for this font, as a weak reference. 382 @remarks 383 This will only be valid after the Font has been loaded. 384 */ getMaterial()385 inline const MaterialPtr& getMaterial() 386 { 387 return mMaterial; 388 } 389 /** Sets whether or not the colour of this font is antialiased as it is generated 390 from a true type font. 391 @remarks 392 This is valid only for a FT_TRUETYPE font. If you are planning on using 393 alpha blending to draw your font, then it is a good idea to set this to 394 false (which is the default), otherwise the darkening of the font will combine 395 with the fading out of the alpha around the edges and make your font look thinner 396 than it should. However, if you intend to blend your font using a colour blending 397 mode (add or modulate for example) then it's a good idea to set this to true, in 398 order to soften your font edges. 399 */ setAntialiasColour(bool enabled)400 inline void setAntialiasColour(bool enabled) 401 { 402 mAntialiasColour = enabled; 403 } 404 405 /** Gets whether or not the colour of this font is antialiased as it is generated 406 from a true type font. 407 */ getAntialiasColour(void)408 inline bool getAntialiasColour(void) const 409 { 410 return mAntialiasColour; 411 } 412 413 /** Implementation of ManualResourceLoader::loadResource, called 414 when the Texture that this font creates needs to (re)load. 415 */ 416 void loadResource(Resource* resource); 417 }; 418 419 typedef SharedPtr<Font> FontPtr; 420 /** @} */ 421 /** @} */ 422 } 423 424 #endif 425