CCFontFreeType.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /****************************************************************************
  2. Copyright (c) 2013 Zynga Inc.
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  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 FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef _FontFreetype_h_
  23. #define _FontFreetype_h_
  24. /// @cond DO_NOT_SHOW
  25. #include "2d/CCFont.h"
  26. #include <string>
  27. #include <ft2build.h>
  28. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  29. #define generic GenericFromFreeTypeLibrary
  30. #define internal InternalFromFreeTypeLibrary
  31. #endif
  32. #include FT_FREETYPE_H
  33. #include FT_STROKER_H
  34. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  35. #undef generic
  36. #undef internal
  37. #endif
  38. NS_CC_BEGIN
  39. class CC_DLL FontFreeType : public Font
  40. {
  41. public:
  42. static const int DistanceMapSpread;
  43. static FontFreeType* create(const std::string &fontName, float fontSize, GlyphCollection glyphs,
  44. const char *customGlyphs,bool distanceFieldEnabled = false, float outline = 0);
  45. static void shutdownFreeType();
  46. bool isDistanceFieldEnabled() const { return _distanceFieldEnabled;}
  47. float getOutlineSize() const { return _outlineSize; }
  48. void renderCharAt(unsigned char *dest,int posX, int posY, unsigned char* bitmap,long bitmapWidth,long bitmapHeight);
  49. FT_Encoding getEncoding() const { return _encoding; }
  50. int* getHorizontalKerningForTextUTF32(const std::u32string& text, int &outNumLetters) const override;
  51. unsigned char* getGlyphBitmap(uint64_t theChar, long &outWidth, long &outHeight, Rect &outRect,int &xAdvance);
  52. int getFontAscender() const;
  53. const char* getFontFamily() const;
  54. virtual FontAtlas* createFontAtlas() override;
  55. virtual int getFontMaxHeight() const override { return _lineHeight; }
  56. static void releaseFont(const std::string &fontName);
  57. private:
  58. static const char* _glyphASCII;
  59. static const char* _glyphNEHE;
  60. static FT_Library _FTlibrary;
  61. static bool _FTInitialized;
  62. FontFreeType(bool distanceFieldEnabled = false, float outline = 0);
  63. virtual ~FontFreeType();
  64. bool createFontObject(const std::string &fontName, float fontSize);
  65. bool initFreeType();
  66. FT_Library getFTLibrary();
  67. int getHorizontalKerningForChars(uint64_t firstChar, uint64_t secondChar) const;
  68. unsigned char* getGlyphBitmapWithOutline(uint64_t code, FT_BBox &bbox);
  69. void setGlyphCollection(GlyphCollection glyphs, const char* customGlyphs = nullptr);
  70. const char* getGlyphCollection() const;
  71. FT_Face _fontRef;
  72. FT_Stroker _stroker;
  73. FT_Encoding _encoding;
  74. std::string _fontName;
  75. bool _distanceFieldEnabled;
  76. float _outlineSize;
  77. int _lineHeight;
  78. FontAtlas* _fontAtlas;
  79. GlyphCollection _usedGlyphs;
  80. std::string _customGlyphs;
  81. };
  82. /// @endcond
  83. NS_CC_END
  84. #endif