gFont.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _PLATFORM_H_
  26. #include "platform/platform.h"
  27. #endif
  28. #ifndef _PLATFORMFONT_H_
  29. #include "platform/platformFont.h"
  30. #endif
  31. #ifndef _GBITMAP_H_
  32. #include "graphics/gBitmap.h"
  33. #endif
  34. #ifndef _VECTOR_H_
  35. #include "collection/vector.h"
  36. #endif
  37. #ifndef _MRECT_H_
  38. #include "math/mRect.h"
  39. #endif
  40. #ifndef _RESMANAGER_H_
  41. #include "io/resource/resourceManager.h"
  42. #endif
  43. #include "graphics/TextureManager.h"
  44. //-Mat use this to make space characters default to a certain x increment
  45. #define PUAP_SPACE_CHAR_X_INCREMENT 5
  46. extern ResourceInstance* constructNewFont(Stream& stream);
  47. extern ResourceInstance* constructBMFont(Stream& stream);
  48. class TextureHandle;
  49. class GFont : public ResourceInstance
  50. {
  51. friend ResourceInstance* constructNewFont(Stream& stream);
  52. friend ResourceInstance* constructBMFont(Stream& stream);
  53. static const U32 csm_fileVersion;
  54. static S32 smSheetIdCount;
  55. // Enumerations and structs available to everyone...
  56. public:
  57. enum Constants
  58. {
  59. TabWidthInSpaces = 3,
  60. TextureSheetSize = 256,
  61. };
  62. // Enumerations and structures available to derived classes
  63. private:
  64. PlatformFont *mPlatformFont;
  65. Vector<TextureHandle>mTextureSheets;
  66. S32 mCurX;
  67. S32 mCurY;
  68. S32 mCurSheet;
  69. bool mNeedSave;
  70. StringTableEntry mGFTFile;
  71. StringTableEntry mFaceName;
  72. U32 mSize;
  73. U32 mCharSet;
  74. U32 mHeight;
  75. U32 mBaseline;
  76. U32 mAscent;
  77. U32 mDescent;
  78. Vector<PlatformFont::CharInfo> mCharInfoList; // - List of character info structures, must
  79. // be accessed through the getCharInfo(U32)
  80. // function to account for remapping...
  81. S32 mRemapTable[65536]; // - Index remapping
  82. public:
  83. GFont();
  84. virtual ~GFont();
  85. protected:
  86. bool loadCharInfo(const UTF16 ch);
  87. void addBitmap(PlatformFont::CharInfo &charInfo);
  88. void addSheet(void);
  89. void assignSheet(S32 sheetNum, GBitmap *bmp);
  90. void *mMutex;
  91. public:
  92. static Resource<GFont> create(const char *faceName, U32 size, const char *cacheDirectory, U32 charset = TGE_ANSI_CHARSET);
  93. TextureHandle getTextureHandle(S32 index)
  94. {
  95. return mTextureSheets[index];
  96. }
  97. const PlatformFont::CharInfo& getCharInfo(const UTF16 in_charIndex);
  98. static const PlatformFont::CharInfo& getDefaultCharInfo();
  99. U32 getCharHeight(const UTF16 in_charIndex);
  100. U32 getCharWidth(const UTF16 in_charIndex);
  101. U32 getCharXIncrement(const UTF16 in_charIndex);
  102. bool isValidChar(const UTF16 in_charIndex) const;
  103. const U32 getHeight() const { return mHeight; }
  104. const U32 getBaseline() const { return mBaseline; }
  105. const U32 getAscent() const { return mAscent; }
  106. const U32 getDescent() const { return mDescent; }
  107. U32 getBreakPos(const UTF16 *string, U32 strlen, U32 width, bool breakOnWhitespace);
  108. /// These are the preferred width functions.
  109. U32 getStrNWidth(const UTF16*, U32 n);
  110. U32 getStrNWidthPrecise(const UTF16*, U32 n);
  111. /// These UTF8 versions of the width functions will be deprecated, please avoid them.
  112. U32 getStrWidth(const UTF8*); // Note: ignores c/r
  113. U32 getStrNWidth(const UTF8*, U32 n);
  114. U32 getStrWidthPrecise(const UTF8*); // Note: ignores c/r
  115. U32 getStrNWidthPrecise(const UTF8*, U32 n);
  116. void wrapString(const UTF8 *string, U32 width, Vector<U32> &startLineOffset, Vector<U32> &lineLen);
  117. /// Dump information about this font to the console.
  118. void dumpInfo();
  119. /// Export to an image strip for image processing.
  120. void exportStrip(const char *fileName, U32 padding, U32 kerning);
  121. /// Import an image strip generated with exportStrip, make sure parameters match!
  122. void importStrip(const char *fileName, U32 padding, U32 kerning);
  123. /// Query as to presence of platform font. If absent, we cannot generate more
  124. /// chars!
  125. const bool hasPlatformFont() const
  126. {
  127. return mPlatformFont != NULL;
  128. }
  129. /// Query to determine if we should use add or modulate (as A8 textures
  130. /// are treated as having 0 for RGB).
  131. bool isAlphaOnly()
  132. {
  133. return mTextureSheets[0].getBitmap()->getFormat() == GBitmap::Alpha;
  134. }
  135. /// Get the filename for a cached font.
  136. static void getFontCacheFilename(const char *faceName, U32 faceSize, U32 buffLen, char *outBuff);
  137. /// Get the face name of the font.
  138. StringTableEntry getFontFaceName() const { return mFaceName; };
  139. bool readBMFont(Stream& io_rStream);
  140. bool read(Stream& io_rStream);
  141. bool write(Stream& io_rStream);
  142. /// Override existing platform font if any with a new one from an external
  143. /// source. This is primarily used in font processing tools to enable
  144. /// trickery (ie, putting characters from multiple fonts in a single
  145. /// GFT) and should be used with caution!
  146. void forcePlatformFont(PlatformFont *pf)
  147. {
  148. mPlatformFont = pf;
  149. }
  150. };
  151. inline U32 GFont::getCharXIncrement(const UTF16 in_charIndex)
  152. {
  153. const PlatformFont::CharInfo& rChar = getCharInfo(in_charIndex);
  154. return rChar.xIncrement;
  155. }
  156. inline U32 GFont::getCharWidth(const UTF16 in_charIndex)
  157. {
  158. const PlatformFont::CharInfo& rChar = getCharInfo(in_charIndex);
  159. return rChar.width;
  160. }
  161. inline U32 GFont::getCharHeight(const UTF16 in_charIndex)
  162. {
  163. const PlatformFont::CharInfo& rChar = getCharInfo(in_charIndex);
  164. return rChar.height;
  165. }
  166. inline bool GFont::isValidChar(const UTF16 in_charIndex) const
  167. {
  168. if(mRemapTable[in_charIndex] != -1)
  169. return true;
  170. if(mPlatformFont)
  171. return mPlatformFont->isValidChar(in_charIndex);
  172. return false;
  173. }
  174. #endif //_GFONT_H_