Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/ogre_src_v1-9-0/OgreMain/include/Overlay/OgreFont.h @ 148

Last change on this file since 148 was 148, checked in by patricwi, 6 years ago

Added new dependencies for ogre1.9 and cegui0.8

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