OgreFont.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002 This source file is a part of OGRE
00003 (Object-oriented Graphics Rendering Engine)
00004 
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2012 Torus Knot Software Ltd
00008 Permission is hereby granted, free of charge, to any person obtaining a copy
00009 of this software and associated documentation files (the "Software"), to deal
00010 in the Software without restriction, including without limitation the rights
00011 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012 copies of the Software, and to permit persons to whom the Software is
00013 furnished to do so, subject to the following conditions:
00014 
00015 The above copyright notice and this permission notice shall be included in
00016 all copies or substantial portions of the Software.
00017 
00018 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024 THE SOFTWARE
00025 -------------------------------------------------------------------------*/
00026 
00027 #ifndef _Font_H__
00028 #define _Font_H__
00029 
00030 #include "OgrePrerequisites.h"
00031 #include "OgreResource.h"
00032 #include "OgreTexture.h"
00033 #include "OgreMaterial.h"
00034 #include "OgreCommon.h"
00035 
00036 namespace Ogre
00037 {
00045     enum FontType
00046     {
00048         FT_TRUETYPE = 1,
00050         FT_IMAGE = 2
00051     };
00052 
00053 
00067     class _OgreExport Font : public Resource, public ManualResourceLoader
00068     {
00069     protected:
00071         class _OgreExport CmdType : public ParamCommand
00072         {
00073         public:
00074             String doGet(const void* target) const;
00075             void doSet(void* target, const String& val);
00076         };
00078         class _OgreExport CmdSource : public ParamCommand
00079         {
00080         public:
00081             String doGet(const void* target) const;
00082             void doSet(void* target, const String& val);
00083         };
00084         class _OgreExport CmdCharSpacer : public ParamCommand
00085         {
00086         public:
00087             String doGet(const void* target) const;
00088             void doSet(void* target, const String& val);
00089         };
00091         class _OgreExport CmdSize : public ParamCommand
00092         {
00093         public:
00094             String doGet(const void* target) const;
00095             void doSet(void* target, const String& val);
00096         };
00098         class _OgreExport CmdResolution : public ParamCommand
00099         {
00100         public:
00101             String doGet(const void* target) const;
00102             void doSet(void* target, const String& val);
00103         };
00105         class _OgreExport CmdCodePoints : public ParamCommand
00106         {
00107         public:
00108             String doGet(const void* target) const;
00109             void doSet(void* target, const String& val);
00110         };
00111 
00112         // Command object for setting / getting parameters
00113         static CmdType msTypeCmd;
00114         static CmdSource msSourceCmd;
00115         static CmdCharSpacer msCharacterSpacerCmd;
00116         static CmdSize msSizeCmd;
00117         static CmdResolution msResolutionCmd;
00118         static CmdCodePoints msCodePointsCmd;
00119 
00121         FontType mType;
00122 
00124         String mSource;
00125 
00128         uint mCharacterSpacer;
00129 
00131         Real mTtfSize;
00133         uint mTtfResolution;
00135         int mTtfMaxBearingY;
00136 
00137 
00138     public:
00139         typedef Ogre::uint32 CodePoint;
00140         typedef Ogre::FloatRect UVRect;
00142         struct GlyphInfo 
00143         {
00144             CodePoint codePoint;
00145             UVRect uvRect;
00146             Real aspectRatio;
00147 
00148             GlyphInfo(CodePoint id, const UVRect& rect, Real aspect)
00149                 : codePoint(id), uvRect(rect), aspectRatio(aspect)
00150             {
00151 
00152             }
00153         };
00155         typedef std::pair<CodePoint, CodePoint> CodePointRange;
00156         typedef vector<CodePointRange>::type CodePointRangeList;
00157     protected:
00159         typedef map<CodePoint, GlyphInfo>::type CodePointMap;
00160         CodePointMap mCodePointMap;
00161 
00163         MaterialPtr mMaterial;
00164 
00166         TexturePtr mTexture;
00167 
00169         bool mAntialiasColour;
00170 
00172         CodePointRangeList mCodePointRangeList;
00173 
00175         void createTextureFromFont(void);
00176 
00178         virtual void loadImpl();
00180         virtual void unloadImpl();
00182         size_t calculateSize(void) const { return 0; } // permanent resource is in the texture 
00183     public:
00184 
00188         Font(ResourceManager* creator, const String& name, ResourceHandle handle,
00189             const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
00190         virtual ~Font();
00191 
00193         void setType(FontType ftype);
00194 
00196         FontType getType(void) const;
00197 
00213         void setSource(const String& source);
00214 
00217         const String& getSource(void) const;
00218 
00224         void setCharacterSpacer(uint charSpacer);
00225  
00231         uint getCharacterSpacer(void) const;
00232 
00238         void setTrueTypeSize(Real ttfSize);
00243         void setTrueTypeResolution(uint ttfResolution);
00244 
00251         Real getTrueTypeSize(void) const;
00256         uint getTrueTypeResolution(void) const;
00266         int getTrueTypeMaxBearingY() const;
00267 
00268 
00275         inline const UVRect& getGlyphTexCoords(CodePoint id) const
00276         {
00277             CodePointMap::const_iterator i = mCodePointMap.find(id);
00278             if (i != mCodePointMap.end())
00279             {
00280                 return i->second.uvRect;
00281             }
00282             else
00283             {
00284                 static UVRect nullRect(0.0, 0.0, 0.0, 0.0);
00285                 return nullRect;
00286             }
00287         }
00288 
00296         inline void setGlyphTexCoords(CodePoint id, Real u1, Real v1, Real u2, Real v2, Real textureAspect)
00297         {
00298             CodePointMap::iterator i = mCodePointMap.find(id);
00299             if (i != mCodePointMap.end())
00300             {
00301                 i->second.uvRect.left = u1;
00302                 i->second.uvRect.top = v1;
00303                 i->second.uvRect.right = u2;
00304                 i->second.uvRect.bottom = v2;
00305                 i->second.aspectRatio = textureAspect * (u2 - u1)  / (v2 - v1);
00306             }
00307             else
00308             {
00309                 mCodePointMap.insert(
00310                     CodePointMap::value_type(id, 
00311                         GlyphInfo(id, UVRect(u1, v1, u2, v2), 
00312                             textureAspect * (u2 - u1)  / (v2 - v1))));
00313             }
00314 
00315         }
00317         inline Real getGlyphAspectRatio(CodePoint id) const
00318         {
00319             CodePointMap::const_iterator i = mCodePointMap.find(id);
00320             if (i != mCodePointMap.end())
00321             {
00322                 return i->second.aspectRatio;
00323             }
00324             else
00325             {
00326                 return 1.0;
00327             }
00328         }
00334         inline void setGlyphAspectRatio(CodePoint id, Real ratio)
00335         {
00336             CodePointMap::iterator i = mCodePointMap.find(id);
00337             if (i != mCodePointMap.end())
00338             {
00339                 i->second.aspectRatio = ratio;
00340             }
00341         }
00342 
00346         const GlyphInfo& getGlyphInfo(CodePoint id) const;
00347 
00356         void addCodePointRange(const CodePointRange& range)
00357         {
00358             mCodePointRangeList.push_back(range);
00359         }
00360 
00363         void clearCodePointRanges()
00364         {
00365             mCodePointRangeList.clear();
00366         }
00370         const CodePointRangeList& getCodePointRangeList() const
00371         {
00372             return mCodePointRangeList;
00373         }
00378         inline const MaterialPtr& getMaterial() const
00379         {
00380             return mMaterial;
00381         }
00386         inline const MaterialPtr& getMaterial()
00387         {
00388             return mMaterial;
00389         }
00401         inline void setAntialiasColour(bool enabled)
00402         {
00403             mAntialiasColour = enabled;
00404         }
00405 
00409         inline bool getAntialiasColour(void) const
00410         {
00411             return mAntialiasColour;
00412         }
00413 
00417         void loadResource(Resource* resource);
00418     };
00425     class _OgreExport FontPtr : public SharedPtr<Font> 
00426     {
00427     public:
00428         FontPtr() : SharedPtr<Font>() {}
00429         explicit FontPtr(Font* rep) : SharedPtr<Font>(rep) {}
00430         FontPtr(const FontPtr& r) : SharedPtr<Font>(r) {} 
00431         FontPtr(const ResourcePtr& r) : SharedPtr<Font>()
00432         {
00433             // lock & copy other mutex pointer
00434             OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
00435             {
00436                 OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
00437                 OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
00438                 pRep = static_cast<Font*>(r.getPointer());
00439                 pUseCount = r.useCountPointer();
00440                 if (pUseCount)
00441                 {
00442                     ++(*pUseCount);
00443                 }
00444             }
00445         }
00446 
00448         FontPtr& operator=(const ResourcePtr& r)
00449         {
00450             if (pRep == static_cast<Font*>(r.getPointer()))
00451                 return *this;
00452             release();
00453             // lock & copy other mutex pointer
00454             OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
00455             {
00456                 OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
00457                 OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
00458                 pRep = static_cast<Font*>(r.getPointer());
00459                 pUseCount = r.useCountPointer();
00460                 if (pUseCount)
00461                 {
00462                     ++(*pUseCount);
00463                 }
00464             }
00465             else
00466             {
00467                 // RHS must be a null pointer
00468                 assert(r.isNull() && "RHS must be null if it has no mutex!");
00469                 setNull();
00470             }
00471             return *this;
00472         }
00473     };
00476 }
00477 
00478 #endif

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Fri May 25 23:36:24 2012