gFont.h

Engine/source/gfx/gFont.h

More...

Classes:

class

Public Functions

GFX_DeclareTextureProfile(GFXFontTextureProfile )

Detailed Description

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
 47class GFont
 48{
 49public:
 50   enum Constants 
 51   {
 52      TabWidthInSpaces = 3,
 53      TextureSheetSize = 256,
 54   };
 55
 56public:
 57   GFont();
 58   virtual ~GFont();
 59   
 60   static Resource<GFont> create(const String &faceName, U32 size, const char *cacheDirectory = 0, U32 charset = TGE_ANSI_CHARSET);
 61
 62   GFXTexHandle getTextureHandle(S32 index) const { return mTextureSheets[index]; }
 63
 64   const PlatformFont::CharInfo& getCharInfo(const UTF16 in_charIndex);
 65   static const PlatformFont::CharInfo& getDefaultCharInfo();
 66
 67   U32  getCharHeight(const UTF16 in_charIndex);
 68   U32  getCharWidth(const UTF16 in_charIndex);
 69   U32  getCharXIncrement(const UTF16 in_charIndex);
 70   
 71   bool isValidChar(const UTF16 in_charIndex) const;
 72
 73   const U32 getHeight() const   { return mHeight; }
 74   const U32 getBaseline() const { return mBaseline; }
 75   const U32 getAscent() const   { return mAscent; }
 76   const U32 getDescent() const  { return mDescent; }
 77
 78   U32 getBreakPos(const UTF16 *string, U32 strlen, U32 width, bool breakOnWhitespace);
 79
 80   /// These are the preferred width functions.
 81   U32 getStrNWidth(const UTF16*, U32 n);
 82   U32 getStrNWidthPrecise(const UTF16*, U32 n);
 83   
 84   /// These UTF8 versions of the width functions will be deprecated, please avoid them.
 85   U32 getStrWidth(const UTF8*);   // Note: ignores c/r
 86   U32 getStrNWidth(const UTF8*, U32 n);
 87   U32 getStrWidthPrecise(const UTF8*);   // Note: ignores c/r
 88   U32 getStrNWidthPrecise(const UTF8*, U32 n);
 89   void wrapString(const UTF8 *string, U32 width, Vector<U32> &startLineOffset, Vector<U32> &lineLen);
 90
 91   /// Dump information about this font to the console.
 92   void dumpInfo() const;
 93
 94   /// Export to an image strip for image processing.
 95   void exportStrip(const char *fileName, U32 padding, U32 kerning);
 96
 97   /// Import an image strip generated with exportStrip, make sure parameters match!
 98   void importStrip(const char *fileName, U32 padding, U32 kerning);
 99
100   void  setPlatformFont(PlatformFont *inPlatformFont);
101
102   /// Query as to presence of platform font. If absent, we cannot generate more
103   /// chars!
104   const bool hasPlatformFont() const
105   {
106      return mPlatformFont != NULL;
107   }
108
109   /// Query to determine if we should use add or modulate (as A8 textures
110   /// are treated as having 0 for RGB).
111   bool isAlphaOnly() const
112   {
113      return mTextureSheets[0]->getBitmap()->getFormat() == GFXFormatA8;
114   }
115
116   /// Get the filename for a cached font.
117   static String getFontCacheFilename(const String &faceName, U32 faceSize);
118
119   /// Get the face name of the font.
120   String   getFontFaceName() const { return mFaceName; };
121   U32      getFontSize() const { return mSize; }
122   U32      getFontCharSet() const { return mCharSet; }
123
124   bool read(Stream& io_rStream);
125   bool write(Stream& io_rStream);
126
127   static GFont* load( const Torque::Path& path );
128
129protected:
130   bool loadCharInfo(const UTF16 ch);
131   void addBitmap(PlatformFont::CharInfo &charInfo);
132   void addSheet(void);
133   void assignSheet(S32 sheetNum, GBitmap *bmp);
134
135   void *mMutex;
136
137private:
138   static const U32 csm_fileVersion;
139
140   PlatformFont *mPlatformFont;
141   Vector<GFXTexHandle>mTextureSheets;
142
143   S32 mCurX;
144   S32 mCurY;
145   S32 mCurSheet;
146
147   bool mNeedSave;
148   Torque::Path mGFTFile;
149   String mFaceName;
150   U32 mSize;
151   U32 mCharSet;
152
153   U32 mHeight;
154   U32 mBaseline;
155   U32 mAscent;
156   U32 mDescent;
157
158   /// List of character info structures, must be accessed through the 
159   /// getCharInfo(U32) function to account for remapping.
160   Vector<PlatformFont::CharInfo>  mCharInfoList;
161
162   /// Index remapping
163   S32             mRemapTable[65536];
164};
165
166inline U32 GFont::getCharXIncrement(const UTF16 in_charIndex)
167{
168    const PlatformFont::CharInfo& rChar = getCharInfo(in_charIndex);
169    return rChar.xIncrement;
170}
171
172inline U32 GFont::getCharWidth(const UTF16 in_charIndex)
173{
174    const PlatformFont::CharInfo& rChar = getCharInfo(in_charIndex);
175    return rChar.width;
176}
177
178inline U32 GFont::getCharHeight(const UTF16 in_charIndex)
179{
180    const PlatformFont::CharInfo& rChar = getCharInfo(in_charIndex);
181    return rChar.height;
182}
183
184inline bool GFont::isValidChar(const UTF16 in_charIndex) const
185{
186   if(mRemapTable[in_charIndex] != -1)
187      return true;
188
189   if(mPlatformFont)
190      return mPlatformFont->isValidChar(in_charIndex);
191
192   return false;
193}
194
195#endif //_GFONT_H_
196