CCTTFLabelAtlasCache.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "CCTTFLabelAtlasCache.h"
  21. #include "platform/CCFileUtils.h"
  22. #include <cmath>
  23. #include "base/ccConfig.h"
  24. #if CC_ENABLE_TTF_LABEL_RENDERER
  25. #define FTT_MAP_FONT_SIZE 0
  26. #define FTT_USE_FONT_MAP_SIZE_POWEROFTWO 0
  27. #define FTT_TEXTURE_SIZE 1024
  28. namespace cocos2d {
  29. namespace {
  30. TTFLabelAtlasCache *_instance = nullptr;
  31. /**
  32. * Labels of different size can share a same FontAtlas by mapping font size.
  33. */
  34. #if FTT_MAP_FONT_SIZE
  35. inline int mapFontSize(float x) {
  36. #if FTT_USE_FONT_MAP_SIZE_POWEROFTWO
  37. // round to power of 2, which waste lots of memory?
  38. return 1 << (int)(::ceil(::log(x) / ::log(2.0)));
  39. #else
  40. if (x <= 8.0f) return 8;
  41. else if (x <= 16.0f) return 16;
  42. else if (x <= 32.0f) return 32;
  43. else if (x <= 48.0f) return 48;
  44. return (int)x;
  45. #endif
  46. }
  47. #else
  48. #define mapFontSize(f) (int)(f)
  49. #endif
  50. }
  51. TTFLabelAtals::TTFLabelAtals(const std::string &fontPath, float fontSize, LabelLayoutInfo *info)
  52. :_fontName(fontPath), _fontSize(fontSize), _info(info)
  53. {
  54. init();
  55. }
  56. bool TTFLabelAtals::init()
  57. {
  58. assert(FileUtils::getInstance()->isFileExist(_fontName));
  59. _ttfFont = std::make_shared<FontFreeType>(_fontName, _fontSize, _info);
  60. if(!_ttfFont->loadFont()){
  61. return false;
  62. }
  63. _fontAtlas = std::make_shared<FontAtlas>(PixelMode::A8, FTT_TEXTURE_SIZE, FTT_TEXTURE_SIZE, _info->outlineSize > 0 || _info->bold);
  64. _fontAtlas->init();
  65. return true;
  66. }
  67. TTFLabelAtlasCache * TTFLabelAtlasCache::getInstance()
  68. {
  69. if (!_instance)
  70. {
  71. _instance = new TTFLabelAtlasCache();
  72. }
  73. return _instance;
  74. }
  75. void TTFLabelAtlasCache::destroyInstance()
  76. {
  77. delete _instance;
  78. _instance = nullptr;
  79. }
  80. void TTFLabelAtlasCache::reset()
  81. {
  82. _cache.clear();
  83. }
  84. std::shared_ptr<TTFLabelAtals> TTFLabelAtlasCache::load(const std::string &font, float fontSizeF, LabelLayoutInfo *info)
  85. {
  86. int fontSize = mapFontSize(fontSizeF);
  87. std::string keybuffer = cacheKeyFor(font, fontSize, info);
  88. #if CC_TTF_LABELATLAS_ENABLE_GC
  89. std::weak_ptr<TTFLabelAtals> &atlasWeak= _cache[keybuffer];
  90. std::shared_ptr<TTFLabelAtals> atlas = atlasWeak.lock();
  91. if (!atlas)
  92. {
  93. atlas = std::make_shared<TTFLabelAtals>(font, fontSize, info);
  94. if(!atlas->init())
  95. {
  96. return nullptr;
  97. }
  98. atlasWeak = atlas;
  99. }
  100. #else
  101. std::shared_ptr<TTFLabelAtals> &atlas = _cache[keybuffer];
  102. if (!atlas)
  103. {
  104. atlas = std::make_shared<TTFLabelAtals>(font, fontSize, info);
  105. if(!atlas->init())
  106. {
  107. return nullptr;
  108. }
  109. }
  110. #endif
  111. return atlas;
  112. }
  113. void TTFLabelAtlasCache::unload(TTFLabelAtals *atlas)
  114. {
  115. int fontSize = mapFontSize(atlas->_fontSize);
  116. std::string key = cacheKeyFor(atlas->_fontName, fontSize, atlas->_info);
  117. _cache.erase(key);
  118. }
  119. std::string TTFLabelAtlasCache::cacheKeyFor(const std::string &font, int fontSize, LabelLayoutInfo * info)
  120. {
  121. char keybuffer[512] = { 0 };
  122. std::string fullPath = FileUtils::getInstance()->fullPathForFilename(font);
  123. //NOTICE: fontpath may be too long
  124. snprintf(keybuffer, 511, "s:%d/sdf:%d/p:%s", fontSize, info->outlineSize > 0 || info->bold, fullPath.c_str());
  125. return keybuffer;
  126. }
  127. }
  128. #endif