CCFontAtlas.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. #include "CCFontAtlas.h"
  21. #include "renderer/gfx/Texture2D.h"
  22. #include "renderer/gfx/DeviceGraphics.h"
  23. #include "base/ccConfig.h"
  24. #include <cassert>
  25. #if CC_ENABLE_TTF_LABEL_RENDERER
  26. static const int PIXEL_PADDING = 2;
  27. namespace cocos2d {
  28. FontAtlasFrame::FontAtlasFrame()
  29. {
  30. _currentRowX = PIXEL_PADDING;
  31. _currentRowY = PIXEL_PADDING;
  32. }
  33. FontAtlasFrame::FontAtlasFrame(FontAtlasFrame&& o)
  34. {
  35. #if CC_ENABLE_CACHE_TTF_FONT_TEXTURE
  36. // move buffer instead of copy
  37. std::swap(_buffer, o._buffer);
  38. _dirtyFlag = o._dirtyFlag;
  39. _dirtyRegion = o._dirtyRegion;
  40. #endif
  41. _WIDTH = o._WIDTH;
  42. _HEIGHT = o._HEIGHT;
  43. _currentRowX = o._currentRowX;
  44. _currentRowY = o._currentRowY;
  45. _currRowHeight = o._currRowHeight;
  46. _pixelMode = o._pixelMode;
  47. _texture = o._texture;
  48. o._texture = nullptr;
  49. }
  50. FontAtlasFrame::~FontAtlasFrame()
  51. {
  52. CC_SAFE_RELEASE(_texture);
  53. }
  54. void FontAtlasFrame::reinit(PixelMode pixelMode, int width, int height)
  55. {
  56. _pixelMode = pixelMode;
  57. _WIDTH = width;
  58. _HEIGHT = height;
  59. _currentRowY = PIXEL_PADDING;
  60. _currentRowY = PIXEL_PADDING;
  61. _currRowHeight = 0;
  62. #if CC_ENABLE_CACHE_TTF_FONT_TEXTURE
  63. _buffer.resize(PixelModeSize(pixelMode) * width * height);
  64. std::fill(_buffer.begin(), _buffer.end(), 0);
  65. _dirtyFlag = 0;
  66. #endif
  67. getTexture(); // init texture
  68. }
  69. FontAtlasFrame::FrameResult FontAtlasFrame::append(int width, int height, std::vector<uint8_t> &data, Rect &out)
  70. {
  71. #if CC_ENABLE_CACHE_TTF_FONT_TEXTURE
  72. assert(_buffer.size() > 0);
  73. assert(width <= _WIDTH && height <= _HEIGHT);
  74. #endif
  75. if (!hasSpace(width, height)) {
  76. return FrameResult::E_FULL;
  77. }
  78. #if CC_ENABLE_CACHE_TTF_FONT_TEXTURE
  79. //update texture-data in CPU memory
  80. const int pixelSize = PixelModeSize(_pixelMode);
  81. uint8_t* dst = _buffer.data();
  82. uint8_t* src = data.data();
  83. uint8_t* dstOrigin = pixelSize * (_currentRowY * _WIDTH + _currentRowX) + dst;
  84. const int BytesEachRow = pixelSize * width;
  85. for (int i = 0; i < height; i++)
  86. {
  87. memcpy(dstOrigin + i * _WIDTH * pixelSize, src + i * BytesEachRow, BytesEachRow);
  88. }
  89. if (_dirtyFlag == 0)
  90. {
  91. _dirtyFlag |= DIRTY_RECT;
  92. _dirtyRegion = Rect(_currentRowX, _currentRowY, width, height);
  93. }
  94. else
  95. {
  96. _dirtyRegion.merge(Rect(_currentRowX, _currentRowY, width, height));
  97. }
  98. #else
  99. //update GPU texture immediately
  100. renderer::Texture::SubImageOption opt;
  101. opt.imageData = data.data();
  102. opt.x = _currentRowX;
  103. opt.y = _currentRowY;
  104. opt.width = width;
  105. opt.height = height;
  106. opt.imageDataLength = data.size();
  107. _texture->updateSubImage(opt);
  108. #endif
  109. out.origin.set(_currentRowX, _currentRowY);
  110. out.size.width = width;
  111. out.size.height = height;
  112. // move cursor
  113. moveToNextCursor(width, height);
  114. return FrameResult::SUCCESS;
  115. }
  116. bool FontAtlasFrame::hasSpace(int width, int height)
  117. {
  118. if (hasRowXSpace(width) && hasYSpace(height)) {
  119. return true;
  120. }
  121. if (hasNextRowXSpace(width) && hasNextRowYSpace(height))
  122. {
  123. moveToNextRow();
  124. return true;
  125. }
  126. return false;
  127. }
  128. int FontAtlasFrame::remainRowXSpace() const
  129. {
  130. return _WIDTH - _currentRowX;
  131. }
  132. int FontAtlasFrame::remainYSpace() const
  133. {
  134. return _HEIGHT - _currentRowY;
  135. }
  136. bool FontAtlasFrame::hasRowXSpace(int x) const
  137. {
  138. return x + PIXEL_PADDING <= remainRowXSpace();
  139. }
  140. bool FontAtlasFrame::hasYSpace(int y) const
  141. {
  142. return y + PIXEL_PADDING <= remainYSpace();
  143. }
  144. bool FontAtlasFrame::hasNextRowXSpace(int x) const
  145. {
  146. return x + PIXEL_PADDING <= _WIDTH;
  147. }
  148. bool FontAtlasFrame::hasNextRowYSpace(int y) const
  149. {
  150. return y + PIXEL_PADDING <= remainYSpace() - _currRowHeight;
  151. }
  152. void FontAtlasFrame::moveToNextRow()
  153. {
  154. _currentRowY += _currRowHeight + PIXEL_PADDING;
  155. _currentRowX = PIXEL_PADDING;
  156. _currRowHeight = 0;
  157. }
  158. void FontAtlasFrame::moveToNextCursor(int width, int height)
  159. {
  160. _currRowHeight = std::max(_currRowHeight, height);
  161. _currentRowX += width + PIXEL_PADDING;
  162. }
  163. renderer::Texture2D * FontAtlasFrame::getTexture()
  164. {
  165. if (!_texture)
  166. {
  167. auto* device = renderer::DeviceGraphics::getInstance();
  168. _texture = new cocos2d::renderer::Texture2D();
  169. cocos2d::renderer::Texture::Options option;
  170. option.width = _WIDTH;
  171. option.height = _HEIGHT;
  172. // alpha only
  173. option.glFormat = GL_ALPHA;
  174. option.glInternalFormat = GL_ALPHA;
  175. option.glType = GL_UNSIGNED_BYTE;
  176. option.bpp = 8 * PixelModeSize(_pixelMode);
  177. renderer::Texture::Image img;
  178. #if CC_ENABLE_CACHE_TTF_FONT_TEXTURE
  179. img.data = _buffer.data();
  180. img.length = _buffer.size();
  181. #else
  182. std::vector<uint8_t> buffer(_WIDTH * _HEIGHT * PixelModeSize(_pixelMode), 0);
  183. img.length = buffer.size();
  184. img.data = buffer.data();
  185. #endif
  186. option.images.push_back(img);
  187. _texture->init(device, option);
  188. }
  189. #if CC_ENABLE_CACHE_TTF_FONT_TEXTURE
  190. if (_dirtyFlag & DIRTY_ALL)
  191. {
  192. renderer::Texture::SubImageOption opt;
  193. opt.imageData = _buffer.data();
  194. opt.x = 0;
  195. opt.y = 0;
  196. opt.width = _WIDTH;
  197. opt.height = _HEIGHT;
  198. opt.imageDataLength = (uint32_t)_buffer.size();
  199. _texture->updateSubImage(opt);
  200. }
  201. else if (_dirtyFlag & DIRTY_RECT)
  202. {
  203. int yMin = _dirtyRegion.getMinY();
  204. int yHeight = _dirtyRegion.size.height;
  205. renderer::Texture::SubImageOption opt;
  206. opt.imageData = _buffer.data() + PixelModeSize(_pixelMode) * _WIDTH * yMin;
  207. opt.x = 0;
  208. opt.y = yMin;
  209. opt.width = _WIDTH;
  210. opt.height = yHeight;
  211. opt.imageDataLength = PixelModeSize(_pixelMode) * _WIDTH * yHeight;
  212. _texture->updateSubImage(opt);
  213. }
  214. _dirtyFlag = 0;
  215. #endif
  216. return _texture;
  217. }
  218. FontAtlas::FontAtlas(PixelMode pixelMode, int width, int height, bool hasoutline)
  219. :_pixelMode(pixelMode), _width(width), _height(height), _useSDF(hasoutline)
  220. {
  221. }
  222. FontAtlas::~FontAtlas()
  223. {
  224. }
  225. bool FontAtlas::init()
  226. {
  227. _textureFrame.reinit(_pixelMode, _width, _height);
  228. _letterMap.clear();
  229. return true;
  230. }
  231. bool FontAtlas::prepareLetter(unsigned long ch, std::shared_ptr<GlyphBitmap> bitmap)
  232. {
  233. if (_letterMap.find(ch) != _letterMap.end())
  234. {
  235. return true;
  236. }
  237. Rect rect;
  238. FontAtlasFrame::FrameResult ret = _textureFrame.append(bitmap->getWidth(), bitmap->getHeight(), bitmap->getData(), rect);
  239. switch (ret) {
  240. case FontAtlasFrame::FrameResult::E_ERROR:
  241. //TODO: ERROR LOG
  242. assert(false);
  243. return false;
  244. case FontAtlasFrame::FrameResult::E_FULL:
  245. // Allocate a new frame & add bitmap the frame
  246. _buffers.push_back(std::move(_textureFrame));
  247. _textureBufferIndex += 1;
  248. _textureFrame.reinit(_pixelMode, _width, _height);
  249. return prepareLetter(ch, bitmap);
  250. case FontAtlasFrame::FrameResult::SUCCESS:
  251. addLetterDef(ch, bitmap, rect);
  252. return true;
  253. default:
  254. //TODO: LOG
  255. assert(false);
  256. }
  257. return false;
  258. }
  259. void FontAtlas::addLetterDef(unsigned long ch, std::shared_ptr<GlyphBitmap> bitmap, const Rect& rect)
  260. {
  261. assert(bitmap->getPixelMode() == _pixelMode);
  262. auto& def = _letterMap[ch];
  263. def.validate = true;
  264. def.textureID = _textureBufferIndex;
  265. def.xAdvance = bitmap->getXAdvance();
  266. def.rect = bitmap->getRect();
  267. def.texX = (rect.origin.x - 0.5f) / _textureFrame.getWidth();
  268. def.texY = (rect.origin.y -0.5f)/ _textureFrame.getHeight();
  269. def.texWidth = (rect.size.width + 1.0f) / _textureFrame.getWidth();
  270. def.texHeight = (rect.size.height + 1.0f) / _textureFrame.getHeight();
  271. def.outline = bitmap->getOutline();
  272. }
  273. bool FontAtlas::prepareLetters(const std::u32string &text, cocos2d::FontFreeType *font)
  274. {
  275. bool ok = true;
  276. for (int i = 0; i < text.length(); i++)
  277. {
  278. auto it = _letterMap.find(text[i]);
  279. if(it == _letterMap.end())
  280. {
  281. auto glyph = font->getGlyphBitmap(text[i], _useSDF);
  282. ok &= prepareLetter(text[i], glyph);
  283. }
  284. }
  285. return ok;
  286. }
  287. FontLetterDefinition* FontAtlas::getOrLoad(unsigned long ch, cocos2d::FontFreeType* font)
  288. {
  289. auto it = _letterMap.find(ch);
  290. if (it != _letterMap.end()) return &it->second;
  291. if (font) {
  292. auto bitmap = font->getGlyphBitmap(ch, _useSDF);
  293. if (bitmap) {
  294. if (prepareLetter(ch, bitmap)) {
  295. return getOrLoad(ch, nullptr);
  296. }
  297. }
  298. }
  299. return nullptr;
  300. }
  301. FontAtlasFrame& FontAtlas::frameAt(int idx)
  302. {
  303. return idx == _textureBufferIndex ? _textureFrame : _buffers.at(idx);
  304. }
  305. }
  306. #endif