gFont.h 6.8 KB

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