⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gnewfont.h

📁 五行MMORPG引擎系统V1.0
💻 H
字号:
//-----------------------------------------------------------------------------
// Torque Game Engine 
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

#ifndef _GFONT_H_
#define _GFONT_H_

//Includes
#ifndef _PLATFORM_H_
#include "platform/platform.h"
#endif
#ifndef _PLATFORMFONT_H_
#include "platform/platformFont.h"
#endif
#ifndef _GBITMAP_H_
#include "dgl/gBitmap.h"
#endif
#ifndef _TVECTOR_H_
#include "core/tVector.h"
#endif
#ifndef _MRECT_H_
#include "math/mRect.h"
#endif
#ifndef _RESMANAGER_H_
#include "core/resManager.h"
#endif

#include "dgl/gTexManager.h"




extern ResourceInstance* constructNewFont(Stream& stream);

class TextureHandle;

class GFont : public ResourceInstance
{
   friend ResourceInstance* constructNewFont(Stream& stream);

   static const U32 csm_fileVersion;
   static S32 smSheetIdCount;
   
   // Enumerations and structs available to everyone...
public:

   enum Constants 
   {
      TabWidthInSpaces = 3,
      TextureSheetSize = 256,
   };

#ifdef TGE_CHINESE
	/// Chinese
   /// GB2312区位划分
   enum GB2312Area
   {
	   GB_MASK		= 0xFF80,	///< 高字节掩位,如果非0则非Ascii

		MAP_ASCII	= 0x20,		///> ASCII映射开始
		MAP_ASCII2	= 0x80,
		MAP_ASCIINUM = MAP_ASCII2-MAP_ASCII,

	   GB_UNIT		= 0xa1,		///< 位号从0xa1到0xfe
	   GB_UNITEND	= 0xff,
	   GB_UNITNUM	= GB_UNITEND - GB_UNIT,		///< 位号数量

	   GB_AREA		= 0xa1,		///< 区号从0xa1到0xfe
	   GB_AREAEND	= 0xf8,
	   GB_AREANUM	= GB_AREAEND - GB_AREA,	///< 总区数量(从汉字区最后区为止)

	   CHAR_AMOUNT	= MAP_ASCIINUM +GB_UNITNUM*GB_AREANUM,	///< 映射总数

	   GB_UNITCHK	= 0xa100,	///< 汉字码在索引保存时,位号放在高位
	   GB_AREACHK	= 0xa1,		///< 汉字码在索引保存时,区号放在低位
  };
#endif


   // Enumerations and structures available to derived classes
private:
   PlatformFont *mPlatformFont;
   Vector<TextureHandle>mTextureSheets;

   S32 mCurX;
   S32 mCurY;
   S32 mCurSheet;

   StringTableEntry mGFTFile;
   StringTableEntry mFaceName;
   U32 mSize;
   U32 mCharSet;

   U32 mHeight;
   U32 mBaseline;
   U32 mAscent;
   U32 mDescent;

   Vector<PlatformFont::CharInfo>  mCharInfoList;       // - List of character info structures, must
                                          //    be accessed through the getCharInfo(U32)
                                          //    function to account for remapping...
   S32             mRemapTable[65536];    // - Index remapping
public:
   GFont();
   virtual ~GFont();

protected:
    bool loadCharInfo(const UTF16 ch);
    void addBitmap(PlatformFont::CharInfo &charInfo);
    void addSheet(void);
    void assignSheet(S32 sheetNum, GBitmap *bmp);

    void *mMutex;


#ifdef TGE_CHINESE
#endif

public:
	/// Chinese
#ifdef TGE_CHINESE
   static Resource<GFont> create(const char *faceName, U32 size, const char *cacheDirectory, U32 charset = TGE_GB2312_CHARSET);
#else
	static Resource<GFont> create(const char *faceName, U32 size, const char *cacheDirectory, U32 charset = TGE_ANSI_CHARSET);
#endif

   TextureHandle getTextureHandle(S32 index)
   {
       return mTextureSheets[index];
   }

   const PlatformFont::CharInfo& getCharInfo(const UTF16 in_charIndex);

   U32  getCharHeight(const UTF16 in_charIndex);
   U32  getCharWidth(const UTF16 in_charIndex);
   U32  getCharXIncrement(const UTF16 in_charIndex);
   
   bool isValidChar(const UTF16 in_charIndex);

   const U32 getHeight() const   { return mHeight; }
   const U32 getBaseline() const { return mBaseline; }
   const U32 getAscent() const   { return mAscent; }
   const U32 getDescent() const  { return mDescent; }



#ifdef TGE_CHINESE
   U32 getBreakPos(const UTF16 *string, U32 strlen, U32 width, bool breakOnWhitespace);
   void wrapString(const UTF16 *string, U32 width, Vector<U32> &startLineOffset, Vector<U32> &lineLen);
#endif

   U32 getBreakPos(const UTF8 *string, U32 strlen, U32 width, bool breakOnWhitespace);
   void wrapString(const UTF8 *string, U32 width, Vector<U32> &startLineOffset, Vector<U32> &lineLen);


   /// These are the preferred width functions.
   U32 getStrNWidth(const UTF16*, U32 n);
   U32 getStrNWidth(const UTF8*, U32 n);
   
   /// These UTF8 versions of the width functions will be deprecated, please avoid them.

   U32 getStrNWidthPrecise(const UTF8*, U32 n);
	U32 getStrNWidthPrecise(const UTF16*, U32 n);
   
   U32 getStrWidth(const UTF8*);   // Note: ignores c/r
   U32 getStrWidthPrecise(const UTF8*);   // Note: ignores c/r


   /// Dump information about this font to the console.
   void dumpInfo();

   /// Export to an image strip for image processing.
   void exportStrip(const char *fileName, U32 padding, U32 kerning);

   /// Import an image strip generated with exportStrip, make sure parameters match!
   void importStrip(const char *fileName, U32 padding, U32 kerning);

   /// Query as to presence of platform font. If absent, we cannot generate more
   /// chars!
   const bool hasPlatformFont() const
   {
      return mPlatformFont;
   }

   /// Query to determine if we should use add or modulate (as A8 textures
   /// are treated as having 0 for RGB).
   bool isAlphaOnly()
   {
      return mTextureSheets[0].getBitmap()->getFormat() == GBitmap::Alpha;
   }

   /// Get the filename for a cached font.
   static void getFontCacheFilename(const char *faceName, U32 faceSize, U32 buffLen, char *outBuff);

   bool read(Stream& io_rStream);
   bool write(Stream& io_rStream);

   /// Override existing platform font if any with a new one from an external
   /// source. This is primarily used in font processing tools to enable
   /// trickery (ie, putting characters from multiple fonts in a single
   /// GFT) and should be used with caution!
   void forcePlatformFont(PlatformFont  *pf)
   {
      mPlatformFont = pf;
   }
};





#define IsGB(uCharIndex)		(((uCharIndex) & GFont::GB_MASK) != 0)
#define NotGB(uCharIndex)		(((uCharIndex) & GFont::GB_MASK) == 0)
#define IsAscii(uCharIndex)		(((uCharIndex) & GFont::GB_MASK) == 0 && uCharIndex >= 0x20)
#define GBCHAR(x)				( IsGB(*(x)) ? (*((U16*)(x))) : (*(x)) )
#define GBSTEP(x)				( IsGB(*(x)) ? (*((U16*)(x)++)): (*(x)) )
#define GBSTEPN(x,n)			( IsGB(((x)[(n)])) ? (*((U16*)((x)+(n)++))) : ((x)[(n)]) )
#define GBCHARN(x,n)			( IsGB(((x)[(n)])) ? (*((U16*)((x)+(n)))) : ((x)[(n)]) )
#define U16STEP(x)				(*((U16*)(x)++))
#define U16CHAR(x)				(*((U16*)(x)))
#define U16CHARN(x,n)			(*((U16*)((x)+(n))))
#define U16STEPN(x,n)			(*((U16*)((x)+(n)++)))
#define GBGetStep(x)			( IsGB(*(x))? 2 : 1)
#define GBGetStepN(x,n)			( IsGB((x)[n])? 2 : 1)
#define GBGetFitR(x)			((x) + GBGetStep(x) )
#define GBGetFitRN(x,n)			((x) + (n) + GBGetStepN(x,n) )
#define GBGetFitL(x)			((x) - GBGetStep((x)-1) )
#define GBGetFitLN(x,n)			((x) + (n)- GBGetStepN((x)-1,n) )




inline U32 GFont::getCharXIncrement(const UTF16 in_charIndex)
{
    const PlatformFont::CharInfo& rChar = getCharInfo(in_charIndex);
    return rChar.xIncrement;
}

inline U32 GFont::getCharWidth(const UTF16 in_charIndex)
{
    const PlatformFont::CharInfo& rChar = getCharInfo(in_charIndex);
    return rChar.width;
}

inline U32 GFont::getCharHeight(const UTF16 in_charIndex)
{
    const PlatformFont::CharInfo& rChar = getCharInfo(in_charIndex);
    return rChar.height;
}

inline bool GFont::isValidChar(const UTF16 in_charIndex)
{
   if(mRemapTable[in_charIndex] != -1)
      return true;

   if(mPlatformFont)
      return mPlatformFont->isValidChar(in_charIndex);

   return false;
}


#endif //_GFONT_H_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -