gFont.h

Engine/source/gfx/gFont.h

More...

Classes:

class

Public Defines

define

Public Functions

GFX_DeclareTextureProfile(GFXFontTextureProfile )

Detailed Description

Public Defines

Font_Table_MAX() 65536

Public Functions

GFX_DeclareTextureProfile(GFXFontTextureProfile )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _GFONT_H_
 25#define _GFONT_H_
 26
 27//Includes
 28#ifndef _RESOURCE_H_
 29#include "core/resource.h"
 30#endif
 31#ifndef _PLATFORMFONT_H_
 32#include "platform/platformFont.h"
 33#endif
 34#ifndef _GBITMAP_H_
 35#include "gfx/bitmap/gBitmap.h"
 36#endif
 37#ifndef _GFXDEVICE_H_
 38#include "gfx/gfxDevice.h"
 39#endif
 40#ifndef _GFXTEXTUREHANDLE_H_
 41#include "gfx/gfxTextureHandle.h"
 42#endif
 43
 44
 45GFX_DeclareTextureProfile(GFXFontTextureProfile);
 46#define Font_Table_MAX 65536
 47
 48class GFont
 49{
 50public:
 51   enum Constants 
 52   {
 53      TabWidthInSpaces = 3,
 54      TextureSheetSize = 256,
 55   };
 56
 57public:
 58   GFont();
 59   virtual ~GFont();
 60   
 61   static Resource<GFont> create(const String &faceName, U32 size, const char *cacheDirectory = 0, U32 charset = TGE_ANSI_CHARSET);
 62
 63   GFXTexHandle getTextureHandle(S32 index) const { return mTextureSheets[index]; }
 64
 65   const PlatformFont::CharInfo& getCharInfo(const UTF16 in_charIndex);
 66   static const PlatformFont::CharInfo& getDefaultCharInfo();
 67
 68   U32  getCharHeight(const UTF16 in_charIndex);
 69   U32  getCharWidth(const UTF16 in_charIndex);
 70   U32  getCharXIncrement(const UTF16 in_charIndex);
 71   
 72   bool isValidChar(const UTF16 in_charIndex) const;
 73
 74   const U32 getHeight() const   { return mHeight; }
 75   const U32 getBaseline() const { return mBaseline; }
 76   const U32 getAscent() const   { return mAscent; }
 77   const U32 getDescent() const  { return mDescent; }
 78
 79   U32 getBreakPos(const UTF16 *string, U32 strlen, U32 width, bool breakOnWhitespace);
 80
 81   /// These are the preferred width functions.
 82   U32 getStrNWidth(const UTF16*, U32 n);
 83   U32 getStrNWidthPrecise(const UTF16*, U32 n);
 84   
 85   /// These UTF8 versions of the width functions will be deprecated, please avoid them.
 86   U32 getStrWidth(const UTF8*);   // Note: ignores c/r
 87   U32 getStrNWidth(const UTF8*, U32 n);
 88   U32 getStrWidthPrecise(const UTF8*);   // Note: ignores c/r
 89   U32 getStrNWidthPrecise(const UTF8*, U32 n);
 90   void wrapString(const UTF8 *string, U32 width, Vector<U32> &startLineOffset, Vector<U32> &lineLen);
 91
 92   /// Dump information about this font to the console.
 93   void dumpInfo() const;
 94
 95   /// Export to an image strip for image processing.
 96   void exportStrip(const char *fileName, U32 padding, U32 kerning);
 97
 98   /// Import an image strip generated with exportStrip, make sure parameters match!
 99   void importStrip(const char *fileName, U32 padding, U32 kerning);
100
101   void  setPlatformFont(PlatformFont *inPlatformFont);
102
103   /// Query as to presence of platform font. If absent, we cannot generate more
104   /// chars!
105   const bool hasPlatformFont() const
106   {
107      return mPlatformFont != NULL;
108   }
109
110   /// Query to determine if we should use add or modulate (as A8 textures
111   /// are treated as having 0 for RGB).
112   bool isAlphaOnly() const
113   {
114      return mTextureSheets[0]->getBitmap()->getFormat() == GFXFormatA8;
115   }
116
117   /// Get the filename for a cached font.
118   static String getFontCacheFilename(const String &faceName, U32 faceSize);
119
120   /// Get the face name of the font.
121   String   getFontFaceName() const { return mFaceName; };
122   U32      getFontSize() const { return mSize; }
123   U32      getFontCharSet() const { return mCharSet; }
124
125   bool read(Stream& io_rStream);
126   bool write(Stream& io_rStream);
127
128   static GFont* load( const Torque::Path& path );
129
130protected:
131   bool loadCharInfo(const UTF16 ch);
132   void addBitmap(PlatformFont::CharInfo &charInfo);
133   void addSheet(void);
134   void assignSheet(S32 sheetNum, GBitmap *bmp);
135
136   void *mMutex;
137
138private:
139   static const U32 csm_fileVersion;
140
141   PlatformFont *mPlatformFont;
142   Vector<GFXTexHandle>mTextureSheets;
143
144   S32 mCurX;
145   S32 mCurY;
146   S32 mCurSheet;
147
148   bool mNeedSave;
149   Torque::Path mGFTFile;
150   String mFaceName;
151   U32 mSize;
152   U32 mCharSet;
153
154   U32 mHeight;
155   U32 mBaseline;
156   U32 mAscent;
157   U32 mDescent;
158
159   /// List of character info structures, must be accessed through the 
160   /// getCharInfo(U32) function to account for remapping.
161   Vector<PlatformFont::CharInfo>  mCharInfoList;
162
163   /// Index remapping
164   S32             mRemapTable[Font_Table_MAX];
165};
166
167inline U32 GFont::getCharXIncrement(const UTF16 in_charIndex)
168{
169    const PlatformFont::CharInfo& rChar = getCharInfo(in_charIndex);
170    return rChar.xIncrement;
171}
172
173inline U32 GFont::getCharWidth(const UTF16 in_charIndex)
174{
175    const PlatformFont::CharInfo& rChar = getCharInfo(in_charIndex);
176    return rChar.width;
177}
178
179inline U32 GFont::getCharHeight(const UTF16 in_charIndex)
180{
181    const PlatformFont::CharInfo& rChar = getCharInfo(in_charIndex);
182    return rChar.height;
183}
184
185inline bool GFont::isValidChar(const UTF16 in_charIndex) const
186{
187   if(mRemapTable[in_charIndex] != -1)
188      return true;
189
190   if(mPlatformFont)
191      return mPlatformFont->isValidChar(in_charIndex);
192
193   return false;
194}
195
196#endif //_GFONT_H_
197