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-2006 Torus Knot Software Ltd 00008 Also see acknowledgements in Readme.html 00009 00010 This library is free software; you can redistribute it and/or modify it 00011 under the terms of the GNU Lesser General Public License (LGPL) as 00012 published by the Free Software Foundation; either version 2.1 of the 00013 License, or (at your option) any later version. 00014 00015 This library is distributed in the hope that it will be useful, but 00016 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00017 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00018 License for more details. 00019 00020 You should have received a copy of the GNU Lesser General Public License 00021 along with this library; if not, write to the Free Software Foundation, 00022 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or go to 00023 http://www.gnu.org/copyleft/lesser.txt 00024 -------------------------------------------------------------------------*/ 00025 00026 #ifndef _Font_H__ 00027 #define _Font_H__ 00028 00029 #include "OgrePrerequisites.h" 00030 #include "OgreResource.h" 00031 #include "OgreTexture.h" 00032 #include "OgreMaterial.h" 00033 #include "OgreCommon.h" 00034 00035 namespace Ogre 00036 { 00038 enum FontType 00039 { 00041 FT_TRUETYPE = 1, 00043 FT_IMAGE = 2 00044 }; 00045 00046 00060 class _OgreExport Font : public Resource, public ManualResourceLoader 00061 { 00062 protected: 00064 class _OgreExport CmdType : public ParamCommand 00065 { 00066 public: 00067 String doGet(const void* target) const; 00068 void doSet(void* target, const String& val); 00069 }; 00071 class _OgreExport CmdSource : public ParamCommand 00072 { 00073 public: 00074 String doGet(const void* target) const; 00075 void doSet(void* target, const String& val); 00076 }; 00078 class _OgreExport CmdSize : public ParamCommand 00079 { 00080 public: 00081 String doGet(const void* target) const; 00082 void doSet(void* target, const String& val); 00083 }; 00085 class _OgreExport CmdResolution : public ParamCommand 00086 { 00087 public: 00088 String doGet(const void* target) const; 00089 void doSet(void* target, const String& val); 00090 }; 00092 class _OgreExport CmdCodePoints : public ParamCommand 00093 { 00094 public: 00095 String doGet(const void* target) const; 00096 void doSet(void* target, const String& val); 00097 }; 00098 00099 // Command object for setting / getting parameters 00100 static CmdType msTypeCmd; 00101 static CmdSource msSourceCmd; 00102 static CmdSize msSizeCmd; 00103 static CmdResolution msResolutionCmd; 00104 static CmdCodePoints msCodePointsCmd; 00105 00107 FontType mType; 00108 00110 String mSource; 00111 00113 Real mTtfSize; 00115 uint mTtfResolution; 00116 00117 00118 public: 00119 typedef Ogre::uint32 CodePoint; 00120 typedef Ogre::FloatRect UVRect; 00122 struct GlyphInfo 00123 { 00124 CodePoint codePoint; 00125 UVRect uvRect; 00126 Real aspectRatio; 00127 00128 GlyphInfo(CodePoint id, const UVRect& rect, Real aspect) 00129 : codePoint(id), uvRect(rect), aspectRatio(aspect) 00130 { 00131 00132 } 00133 }; 00135 typedef std::pair<CodePoint, CodePoint> CodePointRange; 00136 typedef std::vector<CodePointRange> CodePointRangeList; 00137 protected: 00139 typedef std::map<CodePoint, GlyphInfo> CodePointMap; 00140 CodePointMap mCodePointMap; 00141 00143 MaterialPtr mpMaterial; 00144 00146 TexturePtr mTexture; 00147 00149 bool mAntialiasColour; 00150 00152 CodePointRangeList mCodePointRangeList; 00153 00155 void createTextureFromFont(void); 00156 00158 virtual void loadImpl(); 00160 virtual void unloadImpl(); 00162 size_t calculateSize(void) const { return 0; } // permanent resource is in the texture 00163 public: 00164 00168 Font(ResourceManager* creator, const String& name, ResourceHandle handle, 00169 const String& group, bool isManual = false, ManualResourceLoader* loader = 0); 00170 virtual ~Font(); 00171 00173 void setType(FontType ftype); 00174 00176 FontType getType(void) const; 00177 00193 void setSource(const String& source); 00194 00197 const String& getSource(void) const; 00198 00204 void setTrueTypeSize(Real ttfSize); 00209 void setTrueTypeResolution(uint ttfResolution); 00210 00217 Real getTrueTypeSize(void) const; 00222 uint getTrueTypeResolution(void) const; 00223 00224 00231 inline const UVRect& getGlyphTexCoords(CodePoint id) const 00232 { 00233 CodePointMap::const_iterator i = mCodePointMap.find(id); 00234 if (i != mCodePointMap.end()) 00235 { 00236 return i->second.uvRect; 00237 } 00238 else 00239 { 00240 static UVRect nullRect(0.0, 0.0, 0.0, 0.0); 00241 return nullRect; 00242 } 00243 } 00244 00252 inline void setGlyphTexCoords(CodePoint id, Real u1, Real v1, Real u2, Real v2, Real textureAspect) 00253 { 00254 CodePointMap::iterator i = mCodePointMap.find(id); 00255 if (i != mCodePointMap.end()) 00256 { 00257 i->second.uvRect.left = u1; 00258 i->second.uvRect.top = v1; 00259 i->second.uvRect.right = u2; 00260 i->second.uvRect.bottom = v2; 00261 i->second.aspectRatio = textureAspect * (u2 - u1) / (v2 - v1); 00262 } 00263 else 00264 { 00265 mCodePointMap.insert( 00266 CodePointMap::value_type(id, 00267 GlyphInfo(id, UVRect(u1, v1, u2, v2), 00268 textureAspect * (u2 - u1) / (v2 - v1)))); 00269 } 00270 00271 } 00273 inline Real getGlyphAspectRatio(CodePoint id) const 00274 { 00275 CodePointMap::const_iterator i = mCodePointMap.find(id); 00276 if (i != mCodePointMap.end()) 00277 { 00278 return i->second.aspectRatio; 00279 } 00280 else 00281 { 00282 return 1.0; 00283 } 00284 } 00290 inline void setGlyphAspectRatio(CodePoint id, Real ratio) 00291 { 00292 CodePointMap::iterator i = mCodePointMap.find(id); 00293 if (i != mCodePointMap.end()) 00294 { 00295 i->second.aspectRatio = ratio; 00296 } 00297 } 00298 00302 const GlyphInfo& getGlyphInfo(CodePoint id) const; 00303 00312 void addCodePointRange(const CodePointRange& range) 00313 { 00314 mCodePointRangeList.push_back(range); 00315 } 00316 00319 void clearCodePointRanges() 00320 { 00321 mCodePointRangeList.clear(); 00322 } 00326 const CodePointRangeList& getCodePointRangeList() const 00327 { 00328 return mCodePointRangeList; 00329 } 00334 inline const MaterialPtr& getMaterial() const 00335 { 00336 return mpMaterial; 00337 } 00342 inline const MaterialPtr& getMaterial() 00343 { 00344 return mpMaterial; 00345 } 00357 inline void setAntialiasColour(bool enabled) 00358 { 00359 mAntialiasColour = enabled; 00360 } 00361 00365 inline bool getAntialiasColour(void) const 00366 { 00367 return mAntialiasColour; 00368 } 00369 00373 void loadResource(Resource* resource); 00374 }; 00381 class _OgreExport FontPtr : public SharedPtr<Font> 00382 { 00383 public: 00384 FontPtr() : SharedPtr<Font>() {} 00385 explicit FontPtr(Font* rep) : SharedPtr<Font>(rep) {} 00386 FontPtr(const FontPtr& r) : SharedPtr<Font>(r) {} 00387 FontPtr(const ResourcePtr& r) : SharedPtr<Font>() 00388 { 00389 // lock & copy other mutex pointer 00390 OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME) 00391 { 00392 OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME) 00393 OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME) 00394 pRep = static_cast<Font*>(r.getPointer()); 00395 pUseCount = r.useCountPointer(); 00396 if (pUseCount) 00397 { 00398 ++(*pUseCount); 00399 } 00400 } 00401 } 00402 00404 FontPtr& operator=(const ResourcePtr& r) 00405 { 00406 if (pRep == static_cast<Font*>(r.getPointer())) 00407 return *this; 00408 release(); 00409 // lock & copy other mutex pointer 00410 OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME) 00411 { 00412 OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME) 00413 OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME) 00414 pRep = static_cast<Font*>(r.getPointer()); 00415 pUseCount = r.useCountPointer(); 00416 if (pUseCount) 00417 { 00418 ++(*pUseCount); 00419 } 00420 } 00421 else 00422 { 00423 // RHS must be a null pointer 00424 assert(r.isNull() && "RHS must be null if it has no mutex!"); 00425 setNull(); 00426 } 00427 return *this; 00428 } 00429 }; 00430 } 00431 00432 #endif
Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Mon Aug 20 13:50:46 2007