OGRE: OgreFont.h Source File - OGRE Documentation

# 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-2009 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         };
    00085         class _OgreExport CmdSize : public ParamCommand
    00086         {
    00087         public:
    00088             String doGet(const void* target) const;
    00089             void doSet(void* target, const String& val);
    00090         };
    00092         class _OgreExport CmdResolution : public ParamCommand
    00093         {
    00094         public:
    00095             String doGet(const void* target) const;
    00096             void doSet(void* target, const String& val);
    00097         };
    00099         class _OgreExport CmdCodePoints : public ParamCommand
    00100         {
    00101         public:
    00102             String doGet(const void* target) const;
    00103             void doSet(void* target, const String& val);
    00104         };
    00105
    00106         // Command object for setting / getting parameters
    00107         static CmdType msTypeCmd;
    00108         static CmdSource msSourceCmd;
    00109         static CmdSize msSizeCmd;
    00110         static CmdResolution msResolutionCmd;
    00111         static CmdCodePoints msCodePointsCmd;
    00112
    00114         FontType mType;
    00115
    00117         String mSource;
    00118
    00120         Real mTtfSize;
    00122         uint mTtfResolution;
    00124         int mTtfMaxBearingY;
    00125
    00126
    00127     public:
    00128         typedef Ogre::uint32 CodePoint;
    00129         typedef Ogre::FloatRect UVRect;
    00131         struct GlyphInfo
    00132         {
    00133             CodePoint codePoint;
    00134             UVRect uvRect;
    00135             Real aspectRatio;
    00136
    00137             GlyphInfo(CodePoint id, const UVRect& rect, Real aspect)
    00138                 : codePoint(id), uvRect(rect), aspectRatio(aspect)
    00139             {
    00140
    00141             }
    00142         };
    00144         typedef std::pair<CodePoint, CodePoint> CodePointRange;
    00145         typedef vector<CodePointRange>::type CodePointRangeList;
    00146     protected:
    00148         typedef map<CodePoint, GlyphInfo>::type CodePointMap;
    00149         CodePointMap mCodePointMap;
    00150
    00152         MaterialPtr mpMaterial;
    00153
    00155         TexturePtr mTexture;
    00156
    00158         bool mAntialiasColour;
    00159
    00161         CodePointRangeList mCodePointRangeList;
    00162
    00164         void createTextureFromFont(void);
    00165
    00167         virtual void loadImpl();
    00169         virtual void unloadImpl();
    00171         size_t calculateSize(void) const { return 0; } // permanent resource is in the texture
    00172     public:
    00173
    00177         Font(ResourceManager* creator, const String& name, ResourceHandle handle,
    00178             const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
    00179         virtual ~Font();
    00180
    00182         void setType(FontType ftype);
    00183
    00185         FontType getType(void) const;
    00186
    00202         void setSource(const String& source);
    00203
    00206         const String& getSource(void) const;
    00207
    00213         void setTrueTypeSize(Real ttfSize);
    00218         void setTrueTypeResolution(uint ttfResolution);
    00219
    00226         Real getTrueTypeSize(void) const;
    00231         uint getTrueTypeResolution(void) const;
    00241         int getTrueTypeMaxBearingY() const;
    00242
    00243
    00250         inline const UVRect& getGlyphTexCoords(CodePoint id) const
    00251         {
    00252             CodePointMap::const_iterator i = mCodePointMap.find(id);
    00253             if (i != mCodePointMap.end())
    00254             {
    00255                 return i->second.uvRect;
    00256             }
    00257             else
    00258             {
    00259                 static UVRect nullRect(0.0, 0.0, 0.0, 0.0);
    00260                 return nullRect;
    00261             }
    00262         }
    00263
    00271         inline void setGlyphTexCoords(CodePoint id, Real u1, Real v1, Real u2, Real v2, Real textureAspect)
    00272         {
    00273             CodePointMap::iterator i = mCodePointMap.find(id);
    00274             if (i != mCodePointMap.end())
    00275             {
    00276                 i->second.uvRect.left = u1;
    00277                 i->second.uvRect.top = v1;
    00278                 i->second.uvRect.right = u2;
    00279                 i->second.uvRect.bottom = v2;
    00280                 i->second.aspectRatio = textureAspect * (u2 - u1)  / (v2 - v1);
    00281             }
    00282             else
    00283             {
    00284                 mCodePointMap.insert(
    00285                     CodePointMap::value_type(id,
    00286                         GlyphInfo(id, UVRect(u1, v1, u2, v2),
    00287                             textureAspect * (u2 - u1)  / (v2 - v1))));
    00288             }
    00289
    00290         }
    00292         inline Real getGlyphAspectRatio(CodePoint id) const
    00293         {
    00294             CodePointMap::const_iterator i = mCodePointMap.find(id);
    00295             if (i != mCodePointMap.end())
    00296             {
    00297                 return i->second.aspectRatio;
    00298             }
    00299             else
    00300             {
    00301                 return 1.0;
    00302             }
    00303         }
    00309         inline void setGlyphAspectRatio(CodePoint id, Real ratio)
    00310         {
    00311             CodePointMap::iterator i = mCodePointMap.find(id);
    00312             if (i != mCodePointMap.end())
    00313             {
    00314                 i->second.aspectRatio = ratio;
    00315             }
    00316         }
    00317
    00321         const GlyphInfo& getGlyphInfo(CodePoint id) const;
    00322
    00331         void addCodePointRange(const CodePointRange& range)
    00332         {
    00333             mCodePointRangeList.push_back(range);
    00334         }
    00335
    00338         void clearCodePointRanges()
    00339         {
    00340             mCodePointRangeList.clear();
    00341         }
    00345         const CodePointRangeList& getCodePointRangeList() const
    00346         {
    00347             return mCodePointRangeList;
    00348         }
    00353         inline const MaterialPtr& getMaterial() const
    00354         {
    00355             return mpMaterial;
    00356         }
    00361         inline const MaterialPtr& getMaterial()
    00362         {
    00363             return mpMaterial;
    00364         }
    00376         inline void setAntialiasColour(bool enabled)
    00377         {
    00378             mAntialiasColour = enabled;
    00379         }
    00380
    00384         inline bool getAntialiasColour(void) const
    00385         {
    00386             return mAntialiasColour;
    00387         }
    00388
    00392         void loadResource(Resource* resource);
    00393     };
    00400     class _OgreExport FontPtr : public SharedPtr<Font>
    00401     {
    00402     public:
    00403         FontPtr() : SharedPtr<Font>() {}
    00404         explicit FontPtr(Font* rep) : SharedPtr<Font>(rep) {}
    00405         FontPtr(const FontPtr& r) : SharedPtr<Font>(r) {}
    00406         FontPtr(const ResourcePtr& r) : SharedPtr<Font>()
    00407         {
    00408             // lock & copy other mutex pointer
    00409             OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
    00410             {
    00411                 OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
    00412                 OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
    00413                 pRep = static_cast<Font*>(r.getPointer());
    00414                 pUseCount = r.useCountPointer();
    00415                 if (pUseCount)
    00416                 {
    00417                     ++(*pUseCount);
    00418                 }
    00419             }
    00420         }
    00421
    00423         FontPtr& operator=(const ResourcePtr& r)
    00424         {
    00425             if (pRep == static_cast<Font*>(r.getPointer()))
    00426                 return *this;
    00427             release();
    00428             // lock & copy other mutex pointer
    00429             OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
    00430             {
    00431                 OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
    00432                 OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
    00433                 pRep = static_cast<Font*>(r.getPointer());
    00434                 pUseCount = r.useCountPointer();
    00435                 if (pUseCount)
    00436                 {
    00437                     ++(*pUseCount);
    00438                 }
    00439             }
    00440             else
    00441             {
    00442                 // RHS must be a null pointer
    00443                 assert(r.isNull() && "RHS must be null if it has no mutex!");
    00444                 setNull();
    00445             }
    00446             return *this;
    00447         }
    00448     };
    00451 }
    00452
    00453 #endif

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Wed Nov 3 2010 19:24:51