gFont.h 5.9 KB

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