source: GTP/trunk/App/Demos/Geom/OgreStuff/include/OgreFont.h @ 1812

Revision 1812, 13.3 KB checked in by gumbau, 18 years ago (diff)
Line 
1/*-------------------------------------------------------------------------
2This source file is a part of OGRE
3(Object-oriented Graphics Rendering Engine)
4
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This library is free software; you can redistribute it and/or modify it
11under the terms of the GNU Lesser General Public License (LGPL) as
12published by the Free Software Foundation; either version 2.1 of the
13License, or (at your option) any later version.
14
15This library is distributed in the hope that it will be useful, but
16WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18License for more details.
19
20You should have received a copy of the GNU Lesser General Public License
21along with this library; if not, write to the Free Software Foundation,
22Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA or go to
23http://www.gnu.org/copyleft/lesser.txt
24-------------------------------------------------------------------------*/
25
26#ifndef _Font_H__
27#define _Font_H__
28
29#include "OgrePrerequisites.h"
30#include "OgreResource.h"
31#include "OgreTexture.h"
32#include "OgreMaterial.h"
33
34namespace Ogre
35{
36    // Define the number of glyphs allowed
37    // We ignore 0-31 since these are control characters
38#if OGRE_WCHAR_T_STRINGS
39    // Allow wide chars
40    #define OGRE_NUM_GLYPHS (1024 - 32)
41#else
42    // Allow 8-bit ASCII
43    // (we don't want to offend people with charcodes 127-256 in their name eh cearny? ;)
44    // Only chars 33+ are any use though
45    #define OGRE_NUM_GLYPHS (256 - 32)
46#endif
47
48    // How to look up chars
49    #define OGRE_GLYPH_INDEX(c) c - 33
50
51    /** Enumerates the types of Font usable in the engine. */
52    enum FontType
53    {
54        /// Generated from a truetype (.ttf) font
55        FT_TRUETYPE = 1,
56        /// Loaded from an image created by an artist
57        FT_IMAGE = 2
58    };
59
60
61    /** Class representing a font in the system.
62    @remarks
63    This class is simply a way of getting a font texture into the OGRE system and
64    to easily retrieve the texture coordinates required to accurately render them.
65    Fonts can either be loaded from precreated textures, or the texture can be generated
66    using a truetype font. You can either create the texture manually in code, or you
67    can use a .fontdef script to define it (probably more practical since you can reuse
68    the definition more easily)
69        @note
70        This class extends both Resource and ManualResourceLoader since it is
71        both a resource in it's own right, but it also provides the manual load
72        implementation for the Texture it creates.
73    */
74        class _OgreExport Font : public Resource, public ManualResourceLoader
75    {
76    protected:
77                /// Command object for Font - see ParamCommand
78                class _OgreExport CmdType : public ParamCommand
79                {
80                public:
81                        String doGet(const void* target) const;
82                        void doSet(void* target, const String& val);
83                };
84                /// Command object for Font - see ParamCommand
85                class _OgreExport CmdSource : public ParamCommand
86                {
87                public:
88                        String doGet(const void* target) const;
89                        void doSet(void* target, const String& val);
90                };
91                /// Command object for Font - see ParamCommand
92                class _OgreExport CmdSize : public ParamCommand
93                {
94                public:
95                        String doGet(const void* target) const;
96                        void doSet(void* target, const String& val);
97                };
98                /// Command object for Font - see ParamCommand
99                class _OgreExport CmdResolution : public ParamCommand
100                {
101                public:
102                        String doGet(const void* target) const;
103                        void doSet(void* target, const String& val);
104                };
105
106                // Command object for setting / getting parameters
107                static CmdType msTypeCmd;
108                static CmdSource msSourceCmd;
109                static CmdSize msSizeCmd;
110                static CmdResolution msResolutionCmd;
111
112                /// The type of font
113        FontType mType;
114
115        /// Source of the font (either an image name or a truetype font)
116        String mSource;
117
118        /// Size of the truetype font, in points
119        Real mTtfSize;
120        /// Resolution (dpi) of truetype font
121        uint mTtfResolution;
122
123
124        /// Start u coords
125        Real mTexCoords_u1[OGRE_NUM_GLYPHS];
126        /// End u coords
127        Real mTexCoords_u2[OGRE_NUM_GLYPHS];
128        /// Start v coords
129        Real mTexCoords_v1[OGRE_NUM_GLYPHS];
130        /// End v coords
131        Real mTexCoords_v2[OGRE_NUM_GLYPHS];
132
133        /// Aspect ratio between x and y (width / height)
134        Real mAspectRatio[OGRE_NUM_GLYPHS];
135
136        /// The material which is generated for this font
137        MaterialPtr mpMaterial;
138
139                /// Texture pointer
140                TexturePtr mTexture;
141
142        /// for TRUE_TYPE font only
143        bool mAntialiasColour;
144
145        /// Internal method for loading from ttf
146        void createTextureFromFont(void);
147
148                /// @copydoc Resource::loadImpl
149                virtual void loadImpl();
150                /// @copydoc Resource::unloadImpl
151                virtual void unloadImpl();
152                /// @copydoc Resource::calculateSize
153                size_t calculateSize(void) const { return 0; } // permanent resource is in the texture
154    public:
155
156        /** Constructor.
157                @see Resource
158        */
159                Font(ResourceManager* creator, const String& name, ResourceHandle handle,
160                        const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
161        virtual ~Font();
162
163        /** Sets the type of font. Must be set before loading. */
164        void setType(FontType ftype);
165
166        /** Gets the type of font. */
167        FontType getType(void) const;
168
169        /** Sets the source of the font.
170        @remarks
171            If you have created a font of type FT_IMAGE, this method tells the
172            Font which image to use as the source for the characters. So the parameter
173            should be the name of an appropriate image file. Note that when using an image
174            as a font source, you will also need to tell the font where each character is
175            located using setGlyphTexCoords (for each character).
176        @par
177            If you have created a font of type FT_TRUETYPE, this method tells the
178            Font which .ttf file to use to generate the text. You will also need to call
179            setTrueTypeSize and setTrueTypeResolution.
180        @param source An image file or a truetype font, depending on the type of this font
181        */
182        void setSource(const String& source);
183
184        /** Gets the source this font (either an image or a truetype font).
185        */
186        const String& getSource(void) const;
187
188        /** Sets the size of a truetype font (only required for FT_TRUETYPE).
189        @param ttfSize The size of the font in points. Note that the
190            size of the font does not affect how big it is on the screen, just how large it is in
191            the texture and thus how detailed it is.
192        */
193        void setTrueTypeSize(Real ttfSize);
194        /** Gets the resolution (dpi) of the font used to generate the texture
195        (only required for FT_TRUETYPE).
196        @param ttfResolution The resolution in dpi
197        */
198        void setTrueTypeResolution(uint ttfResolution);
199
200        /** Gets the point size of the font used to generate the texture.
201        @remarks
202            Only applicable for FT_TRUETYPE Font objects.
203            Note that the size of the font does not affect how big it is on the screen,
204            just how large it is in the texture and thus how detailed it is.           
205        */
206        Real getTrueTypeSize(void) const;
207        /** Gets the resolution (dpi) of the font used to generate the texture.
208        @remarks
209            Only applicable for FT_TRUETYPE Font objects.
210        */
211        uint getTrueTypeResolution(void) const;
212
213        /** Returns the size in pixels of a box that could contain the whole string.
214        */
215        std::pair< uint, uint > StrBBox( const String & text, Real char_height, RenderWindow & window  );
216
217
218        /** Returns the teture coordinates of the associated glyph.
219            @remarks Parameter is a short to allow both ASCII and wide chars.
220            @param id The character code
221            @param u1, u2, v1, v2 location to place the results
222        */
223        inline void getGlyphTexCoords(OgreChar id, Real& u1, Real& v1, Real& u2, Real& v2 ) const
224        {
225            unsigned OgreChar idx = OGRE_GLYPH_INDEX(id);
226            u1 = mTexCoords_u1[ idx ];
227            v1 = mTexCoords_v1[ idx ];
228            u2 = mTexCoords_u2[ idx ];
229            v2 = mTexCoords_v2[ idx ];
230        }
231
232        /** Sets the texture coordinates of a glyph.
233        @remarks
234            You only need to call this if you're setting up a font loaded from a texture manually.
235        @note
236            Also sets the aspect ratio (width / height) of this character.
237        */
238        inline void setGlyphTexCoords( OgreChar id, Real u1, Real v1, Real u2, Real v2 )
239        {
240            unsigned OgreChar idx = OGRE_GLYPH_INDEX(id);
241            mTexCoords_u1[ idx ] = u1;
242            mTexCoords_v1[ idx ] = v1;
243            mTexCoords_u2[ idx ] = u2;
244            mTexCoords_v2[ idx ] = v2;
245            mAspectRatio[ idx ] = ( u2 - u1 ) / ( v2 - v1 );
246        }
247        /** Gets the aspect ratio (width / height) of this character. */
248        inline Real getGlyphAspectRatio( OgreChar id ) const
249        {
250            unsigned OgreChar idx = OGRE_GLYPH_INDEX(id);
251            return mAspectRatio[ idx ];
252        }
253        /** Sets the aspect ratio (width / height) of this character.
254        @remarks
255            You only need to call this if you're setting up a font loaded from a texture manually,
256            and your aspect ratio is really freaky.
257        */
258        inline void setGlyphAspectRatio( OgreChar id, Real ratio )
259        {
260            unsigned OgreChar idx = OGRE_GLYPH_INDEX(id);
261            mAspectRatio[ idx ] = ratio;
262        }
263        /** Gets the material generated for this font, as a weak reference.
264        @remarks
265            This will only be valid after the Font has been loaded.
266        */
267        inline const MaterialPtr& getMaterial() const
268        {
269            return mpMaterial;
270        }
271        /** Gets the material generated for this font, as a weak reference.
272        @remarks
273            This will only be valid after the Font has been loaded.
274        */
275        inline const MaterialPtr& getMaterial()
276        {
277            return mpMaterial;
278        }
279        /** Sets whether or not the colour of this font is antialiased as it is generated
280            from a true type font.
281        @remarks
282                This is valid only for a FT_TRUETYPE font. If you are planning on using
283            alpha blending to draw your font, then it is a good idea to set this to
284            false (which is the default), otherwise the darkening of the font will combine
285            with the fading out of the alpha around the edges and make your font look thinner
286            than it should. However, if you intend to blend your font using a colour blending
287            mode (add or modulate for example) then it's a good idea to set this to true, in
288            order to soften your font edges.
289        */
290        inline void setAntialiasColour(bool enabled)
291        {
292                mAntialiasColour = enabled;
293        }
294
295                /** Gets whether or not the colour of this font is antialiased as it is generated
296                from a true type font.
297                */
298        inline bool getAntialiasColour(void) const
299        {
300            return mAntialiasColour;
301        }
302
303                /** Implementation of ManualResourceLoader::loadResource, called
304                        when the Texture that this font creates needs to (re)load.
305                */
306                void loadResource(Resource* resource);
307    };
308        /** Specialisation of SharedPtr to allow SharedPtr to be assigned to FontPtr
309        @note Has to be a subclass since we need operator=.
310        We could templatise this instead of repeating per Resource subclass,
311        except to do so requires a form VC6 does not support i.e.
312        ResourceSubclassPtr<T> : public SharedPtr<T>
313        */
314        class _OgreExport FontPtr : public SharedPtr<Font>
315        {
316        public:
317                FontPtr() : SharedPtr<Font>() {}
318                explicit FontPtr(Font* rep) : SharedPtr<Font>(rep) {}
319                FontPtr(const FontPtr& r) : SharedPtr<Font>(r) {}
320                FontPtr(const ResourcePtr& r) : SharedPtr<Font>()
321                {
322                        // lock & copy other mutex pointer
323            OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
324            {
325                            OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
326                            OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
327                            pRep = static_cast<Font*>(r.getPointer());
328                            pUseCount = r.useCountPointer();
329                            if (pUseCount)
330                            {
331                                    ++(*pUseCount);
332                            }
333            }
334                }
335
336                /// Operator used to convert a ResourcePtr to a FontPtr
337                FontPtr& operator=(const ResourcePtr& r)
338                {
339                        if (pRep == static_cast<Font*>(r.getPointer()))
340                                return *this;
341                        release();
342                        // lock & copy other mutex pointer
343            OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
344            {
345                OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
346                            OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
347                            pRep = static_cast<Font*>(r.getPointer());
348                            pUseCount = r.useCountPointer();
349                            if (pUseCount)
350                            {
351                                    ++(*pUseCount);
352                            }
353            }
354                        return *this;
355                }
356        };
357}
358
359#endif
Note: See TracBrowser for help on using the repository browser.