CCFontAtlasCache.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #include "2d/CCFontAtlasCache.h"
  23. #include "base/CCDirector.h"
  24. #include "2d/CCFontFNT.h"
  25. #include "2d/CCFontFreeType.h"
  26. #include "2d/CCFontAtlas.h"
  27. #include "2d/CCFontCharMap.h"
  28. #include "2d/CCLabel.h"
  29. #include "platform/CCFileUtils.h"
  30. NS_CC_BEGIN
  31. std::unordered_map<std::string, FontAtlas *> FontAtlasCache::_atlasMap;
  32. #define ATLAS_MAP_KEY_BUFFER 255
  33. void FontAtlasCache::purgeCachedData()
  34. {
  35. auto atlasMapCopy = _atlasMap;
  36. for (auto&& atlas : atlasMapCopy)
  37. {
  38. auto refCount = atlas.second->getReferenceCount();
  39. atlas.second->release();
  40. if (refCount != 1)
  41. atlas.second->purgeTexturesAtlas();
  42. }
  43. _atlasMap.clear();
  44. }
  45. FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config)
  46. {
  47. auto realFontFilename = FileUtils::getInstance()->getNewFilename(config->fontFilePath); // resolves real file path, to prevent storing multiple atlases for the same file.
  48. bool useDistanceField = config->distanceFieldEnabled;
  49. if(config->outlineSize > 0)
  50. {
  51. useDistanceField = false;
  52. }
  53. char tmp[ATLAS_MAP_KEY_BUFFER];
  54. if (useDistanceField) {
  55. snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "df %.2f %d %s", config->fontSize, config->outlineSize,
  56. realFontFilename.c_str());
  57. } else {
  58. snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %d %s", config->fontSize, config->outlineSize,
  59. realFontFilename.c_str());
  60. }
  61. std::string atlasName = tmp;
  62. auto it = _atlasMap.find(atlasName);
  63. if ( it == _atlasMap.end() )
  64. {
  65. auto font = FontFreeType::create(realFontFilename, config->fontSize, config->glyphs,
  66. config->customGlyphs, useDistanceField, config->outlineSize);
  67. if (font)
  68. {
  69. auto tempAtlas = font->createFontAtlas();
  70. if (tempAtlas)
  71. {
  72. _atlasMap[atlasName] = tempAtlas;
  73. return _atlasMap[atlasName];
  74. }
  75. }
  76. }
  77. else
  78. return _atlasMap[atlasName];
  79. return nullptr;
  80. }
  81. FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset /* = Vec2::ZERO */)
  82. {
  83. auto realFontFilename = FileUtils::getInstance()->getNewFilename(fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file.
  84. char tmp[ATLAS_MAP_KEY_BUFFER];
  85. snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, realFontFilename.c_str());
  86. std::string atlasName = tmp;
  87. auto it = _atlasMap.find(atlasName);
  88. if ( it == _atlasMap.end() )
  89. {
  90. auto font = FontFNT::create(realFontFilename, imageOffset);
  91. if(font)
  92. {
  93. auto tempAtlas = font->createFontAtlas();
  94. if (tempAtlas)
  95. {
  96. _atlasMap[atlasName] = tempAtlas;
  97. return _atlasMap[atlasName];
  98. }
  99. }
  100. }
  101. else
  102. return _atlasMap[atlasName];
  103. return nullptr;
  104. }
  105. FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile)
  106. {
  107. std::string atlasName = plistFile;
  108. auto it = _atlasMap.find(atlasName);
  109. if ( it == _atlasMap.end() )
  110. {
  111. auto font = FontCharMap::create(plistFile);
  112. if(font)
  113. {
  114. auto tempAtlas = font->createFontAtlas();
  115. if (tempAtlas)
  116. {
  117. _atlasMap[atlasName] = tempAtlas;
  118. return _atlasMap[atlasName];
  119. }
  120. }
  121. }
  122. else
  123. return _atlasMap[atlasName];
  124. return nullptr;
  125. }
  126. FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
  127. {
  128. char tmp[30];
  129. sprintf(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap);
  130. std::string atlasName = tmp;
  131. auto it = _atlasMap.find(atlasName);
  132. if ( it == _atlasMap.end() )
  133. {
  134. auto font = FontCharMap::create(texture,itemWidth,itemHeight,startCharMap);
  135. if(font)
  136. {
  137. auto tempAtlas = font->createFontAtlas();
  138. if (tempAtlas)
  139. {
  140. _atlasMap[atlasName] = tempAtlas;
  141. return _atlasMap[atlasName];
  142. }
  143. }
  144. }
  145. else
  146. return _atlasMap[atlasName];
  147. return nullptr;
  148. }
  149. FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
  150. {
  151. char tmp[ATLAS_MAP_KEY_BUFFER];
  152. snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%d %d %d %s", itemWidth, itemHeight, startCharMap, charMapFile.c_str());
  153. std::string atlasName = tmp;
  154. auto it = _atlasMap.find(atlasName);
  155. if ( it == _atlasMap.end() )
  156. {
  157. auto font = FontCharMap::create(charMapFile,itemWidth,itemHeight,startCharMap);
  158. if(font)
  159. {
  160. auto tempAtlas = font->createFontAtlas();
  161. if (tempAtlas)
  162. {
  163. _atlasMap[atlasName] = tempAtlas;
  164. return _atlasMap[atlasName];
  165. }
  166. }
  167. }
  168. else
  169. return _atlasMap[atlasName];
  170. return nullptr;
  171. }
  172. bool FontAtlasCache::releaseFontAtlas(FontAtlas *atlas)
  173. {
  174. if (nullptr != atlas)
  175. {
  176. for( auto &item: _atlasMap )
  177. {
  178. if ( item.second == atlas )
  179. {
  180. if (atlas->getReferenceCount() == 1)
  181. {
  182. _atlasMap.erase(item.first);
  183. }
  184. atlas->release();
  185. return true;
  186. }
  187. }
  188. }
  189. return false;
  190. }
  191. void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset/* = Vec2::ZERO*/)
  192. {
  193. char tmp[ATLAS_MAP_KEY_BUFFER];
  194. snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, fontFileName.c_str());
  195. std::string atlasName = tmp;
  196. auto it = _atlasMap.find(atlasName);
  197. if (it != _atlasMap.end())
  198. {
  199. CC_SAFE_RELEASE_NULL(it->second);
  200. _atlasMap.erase(it);
  201. }
  202. FontFNT::reloadBMFontResource(fontFileName);
  203. auto font = FontFNT::create(fontFileName, imageOffset);
  204. if (font)
  205. {
  206. auto tempAtlas = font->createFontAtlas();
  207. if (tempAtlas)
  208. {
  209. _atlasMap[atlasName] = tempAtlas;
  210. }
  211. }
  212. }
  213. void FontAtlasCache::unloadFontAtlasTTF(const std::string& fontFileName)
  214. {
  215. auto item = _atlasMap.begin();
  216. while (item != _atlasMap.end())
  217. {
  218. if (item->first.find(fontFileName) != std::string::npos)
  219. {
  220. CC_SAFE_RELEASE_NULL(item->second);
  221. item = _atlasMap.erase(item);
  222. }
  223. else
  224. item++;
  225. }
  226. }
  227. NS_CC_END