CCFontAtlas.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /****************************************************************************
  2. Copyright (c) 2020 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos.com
  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 deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include "CCFontFreetype.h"
  22. #include <unordered_map>
  23. #include <algorithm>
  24. #include "base/ccConfig.h"
  25. #if CC_ENABLE_TTF_LABEL_RENDERER
  26. namespace cocos2d {
  27. namespace renderer {
  28. class Texture2D;
  29. }
  30. class FontAtlas;
  31. struct FontLetterDefinition
  32. {
  33. float texX = 0, texY = 0;
  34. float texWidth = 0, texHeight = 0;
  35. Rect rect;
  36. int textureID = -1;
  37. float xAdvance = 0;
  38. int outline = 0;
  39. bool validate = false;
  40. };
  41. class FontAtlasFrame
  42. {
  43. public:
  44. enum class FrameResult {
  45. SUCCESS,
  46. E_FULL,
  47. E_ERROR
  48. };
  49. FontAtlasFrame();
  50. FontAtlasFrame(FontAtlasFrame&&); //move
  51. virtual ~FontAtlasFrame();
  52. void reinit(PixelMode mode, int width, int height);
  53. FrameResult append(int width, int height, std::vector<uint8_t> &, Rect &out);
  54. float getWidth() const { return _WIDTH; }
  55. float getHeight() const { return _HEIGHT; }
  56. renderer::Texture2D* getTexture();
  57. private:
  58. enum DirtyType {
  59. DIRTY_RECT = 1,
  60. DIRTY_ALL= 2,
  61. };
  62. bool hasSpace(int width, int height);
  63. int remainRowXSpace() const;
  64. int remainYSpace() const;
  65. bool hasRowXSpace(int x) const;
  66. bool hasYSpace(int y) const;
  67. bool hasNextRowXSpace(int x) const;
  68. bool hasNextRowYSpace(int y) const;
  69. void moveToNextRow();
  70. void moveToNextCursor(int width, int height);
  71. #if CC_ENABLE_CACHE_TTF_FONT_TEXTURE
  72. mutable std::vector<uint8_t> _buffer;
  73. int _dirtyFlag = 0;
  74. Rect _dirtyRegion;
  75. #endif
  76. int _WIDTH = 0;
  77. int _HEIGHT = 0;
  78. int _currentRowY = 0;
  79. int _currentRowX = 0;
  80. int _currRowHeight = 0;
  81. PixelMode _pixelMode = PixelMode::A8;
  82. renderer::Texture2D *_texture = nullptr;
  83. friend class FontAtlas;
  84. };
  85. class FontAtlas {
  86. public:
  87. FontAtlas(PixelMode mode, int width, int height, bool hasoutline);
  88. virtual ~FontAtlas();
  89. bool init();
  90. bool prepareLetter(unsigned long ch, std::shared_ptr<GlyphBitmap> bitmap);
  91. bool prepareLetters(const std::u32string &text, cocos2d::FontFreeType *font);
  92. FontLetterDefinition* getOrLoad(unsigned long ch, FontFreeType* font);
  93. FontAtlasFrame& frameAt(int idx);
  94. private:
  95. void addLetterDef(unsigned long ch, std::shared_ptr<GlyphBitmap> bitmap, const Rect& rect);
  96. std::unordered_map<uint64_t, FontLetterDefinition> _letterMap;
  97. FontAtlasFrame _textureFrame;
  98. std::vector<FontAtlasFrame> _buffers;
  99. int _textureBufferIndex = 0;
  100. int _width = 0;
  101. int _height = 0;
  102. PixelMode _pixelMode = PixelMode::A8;
  103. bool _useSDF = false;
  104. };
  105. }
  106. #endif