gFont.h 5.9 KB

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