CCFontAtlas.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /****************************************************************************
  2. Copyright (c) 2013 Zynga Inc.
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef _CCFontAtlas_h_
  22. #define _CCFontAtlas_h_
  23. /// @cond DO_NOT_SHOW
  24. #include <string>
  25. #include <unordered_map>
  26. #include "platform/CCPlatformMacros.h"
  27. #include "base/CCRef.h"
  28. #include "platform/CCStdC.h" // ssize_t on windows
  29. #include "math/CCGeometry.h"
  30. NS_CC_BEGIN
  31. class Font;
  32. class Texture2D;
  33. class EventCustom;
  34. class EventListenerCustom;
  35. class FontFreeType;
  36. struct FontLetterDefinition
  37. {
  38. float U;
  39. float V;
  40. float width;
  41. float height;
  42. float offsetX;
  43. float offsetY;
  44. int textureID;
  45. bool validDefinition;
  46. int xAdvance;
  47. };
  48. class CC_DLL FontAtlas : public Ref
  49. {
  50. public:
  51. static const int CacheTextureWidth;
  52. static const int CacheTextureHeight;
  53. static const char* CMD_PURGE_FONTATLAS;
  54. static const char* CMD_RESET_FONTATLAS;
  55. /**
  56. * @js ctor
  57. */
  58. FontAtlas(Font &theFont);
  59. /**
  60. * @js NA
  61. * @lua NA
  62. */
  63. virtual ~FontAtlas();
  64. void addLetterDefinition(char32_t utf32Char, const FontLetterDefinition &letterDefinition);
  65. bool getLetterDefinitionForChar(char32_t utf32Char, FontLetterDefinition &letterDefinition);
  66. bool prepareLetterDefinitions(const std::u32string& utf16String);
  67. const std::unordered_map<ssize_t, Texture2D*>& getTextures() const { return _atlasTextures; }
  68. void addTexture(Texture2D *texture, int slot);
  69. void addRectInTexture(const Rect& rect, int slot);
  70. float getLineHeight() const { return _lineHeight; }
  71. void setLineHeight(float newHeight);
  72. Texture2D* getTexture(int slot);
  73. Rect* getRectInTexture(int slot);
  74. const Font* getFont() const { return _font; }
  75. /** listen the event that renderer was recreated on Android/WP8
  76. It only has effect on Android and WP8.
  77. */
  78. void listenRendererRecreated(EventCustom *event);
  79. /** Removes textures atlas.
  80. It will purge the textures atlas and if multiple texture exist in the FontAtlas.
  81. */
  82. void purgeTexturesAtlas();
  83. /** sets font texture parameters:
  84. - GL_TEXTURE_MIN_FILTER = GL_LINEAR
  85. - GL_TEXTURE_MAG_FILTER = GL_LINEAR
  86. */
  87. void setAntiAliasTexParameters();
  88. /** sets font texture parameters:
  89. - GL_TEXTURE_MIN_FILTER = GL_NEAREST
  90. - GL_TEXTURE_MAG_FILTER = GL_NEAREST
  91. */
  92. void setAliasTexParameters();
  93. protected:
  94. void reset();
  95. void reinit();
  96. void releaseTextures();
  97. void findNewCharacters(const std::u32string& u32Text, std::unordered_map<unsigned int, unsigned int>& charCodeMap);
  98. void conversionU32TOGB2312(const std::u32string& u32Text, std::unordered_map<unsigned int, unsigned int>& charCodeMap);
  99. /**
  100. * Scale each font letter by scaleFactor.
  101. *
  102. * @param scaleFactor A float scale factor for scaling font letter info.
  103. */
  104. void scaleFontLetterDefinition(float scaleFactor);
  105. std::unordered_map<ssize_t, Texture2D*> _atlasTextures;
  106. std::unordered_map<ssize_t, Rect> _atlasRectInTextures;
  107. std::unordered_map<char32_t, FontLetterDefinition> _letterDefinitions;
  108. float _lineHeight;
  109. Font* _font;
  110. FontFreeType* _fontFreeType;
  111. void* _iconv;
  112. // Dynamic GlyphCollection related stuff
  113. int _currentPage;
  114. unsigned char *_currentPageData;
  115. int _currentPageDataSize;
  116. float _currentPageOrigX;
  117. float _currentPageOrigY;
  118. int _letterPadding;
  119. int _letterEdgeExtend;
  120. int _fontAscender;
  121. EventListenerCustom* _rendererRecreatedListener;
  122. bool _antialiasEnabled;
  123. int _currLineHeight;
  124. friend class Label;
  125. };
  126. NS_CC_END
  127. /// @endcond
  128. #endif /* defined(__cocos2d_libs__CCFontAtlas__) */