123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- /****************************************************************************
- Copyright (c) 2013 Zynga Inc.
- Copyright (c) 2013-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
-
- http://www.cocos2d-x.org
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- #include "2d/CCFontAtlasCache.h"
- #include "base/CCDirector.h"
- #include "2d/CCFontFNT.h"
- #include "2d/CCFontFreeType.h"
- #include "2d/CCFontAtlas.h"
- #include "2d/CCFontCharMap.h"
- #include "2d/CCLabel.h"
- #include "platform/CCFileUtils.h"
- NS_CC_BEGIN
- std::unordered_map<std::string, FontAtlas *> FontAtlasCache::_atlasMap;
- #define ATLAS_MAP_KEY_BUFFER 255
- void FontAtlasCache::purgeCachedData()
- {
- auto atlasMapCopy = _atlasMap;
- for (auto&& atlas : atlasMapCopy)
- {
- auto refCount = atlas.second->getReferenceCount();
- atlas.second->release();
- if (refCount != 1)
- atlas.second->purgeTexturesAtlas();
- }
- _atlasMap.clear();
- }
- FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config)
- {
- auto realFontFilename = FileUtils::getInstance()->getNewFilename(config->fontFilePath); // resolves real file path, to prevent storing multiple atlases for the same file.
- bool useDistanceField = config->distanceFieldEnabled;
- if(config->outlineSize > 0)
- {
- useDistanceField = false;
- }
- char tmp[ATLAS_MAP_KEY_BUFFER];
- if (useDistanceField) {
- snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "df %.2f %d %s", config->fontSize, config->outlineSize,
- realFontFilename.c_str());
- } else {
- snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %d %s", config->fontSize, config->outlineSize,
- realFontFilename.c_str());
- }
- std::string atlasName = tmp;
- auto it = _atlasMap.find(atlasName);
- if ( it == _atlasMap.end() )
- {
- auto font = FontFreeType::create(realFontFilename, config->fontSize, config->glyphs,
- config->customGlyphs, useDistanceField, config->outlineSize);
- if (font)
- {
- auto tempAtlas = font->createFontAtlas();
- if (tempAtlas)
- {
- _atlasMap[atlasName] = tempAtlas;
- return _atlasMap[atlasName];
- }
- }
- }
- else
- return _atlasMap[atlasName];
- return nullptr;
- }
- FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset /* = Vec2::ZERO */)
- {
- auto realFontFilename = FileUtils::getInstance()->getNewFilename(fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file.
- char tmp[ATLAS_MAP_KEY_BUFFER];
- snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, realFontFilename.c_str());
- std::string atlasName = tmp;
-
- auto it = _atlasMap.find(atlasName);
- if ( it == _atlasMap.end() )
- {
- auto font = FontFNT::create(realFontFilename, imageOffset);
- if(font)
- {
- auto tempAtlas = font->createFontAtlas();
- if (tempAtlas)
- {
- _atlasMap[atlasName] = tempAtlas;
- return _atlasMap[atlasName];
- }
- }
- }
- else
- return _atlasMap[atlasName];
-
- return nullptr;
- }
- FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile)
- {
- std::string atlasName = plistFile;
-
- auto it = _atlasMap.find(atlasName);
- if ( it == _atlasMap.end() )
- {
- auto font = FontCharMap::create(plistFile);
- if(font)
- {
- auto tempAtlas = font->createFontAtlas();
- if (tempAtlas)
- {
- _atlasMap[atlasName] = tempAtlas;
- return _atlasMap[atlasName];
- }
- }
- }
- else
- return _atlasMap[atlasName];
- return nullptr;
- }
- FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
- {
- char tmp[30];
- sprintf(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap);
- std::string atlasName = tmp;
- auto it = _atlasMap.find(atlasName);
- if ( it == _atlasMap.end() )
- {
- auto font = FontCharMap::create(texture,itemWidth,itemHeight,startCharMap);
- if(font)
- {
- auto tempAtlas = font->createFontAtlas();
- if (tempAtlas)
- {
- _atlasMap[atlasName] = tempAtlas;
- return _atlasMap[atlasName];
- }
- }
- }
- else
- return _atlasMap[atlasName];
- return nullptr;
- }
- FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
- {
- char tmp[ATLAS_MAP_KEY_BUFFER];
- snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%d %d %d %s", itemWidth, itemHeight, startCharMap, charMapFile.c_str());
- std::string atlasName = tmp;
- auto it = _atlasMap.find(atlasName);
- if ( it == _atlasMap.end() )
- {
- auto font = FontCharMap::create(charMapFile,itemWidth,itemHeight,startCharMap);
- if(font)
- {
- auto tempAtlas = font->createFontAtlas();
- if (tempAtlas)
- {
- _atlasMap[atlasName] = tempAtlas;
- return _atlasMap[atlasName];
- }
- }
- }
- else
- return _atlasMap[atlasName];
- return nullptr;
- }
- bool FontAtlasCache::releaseFontAtlas(FontAtlas *atlas)
- {
- if (nullptr != atlas)
- {
- for( auto &item: _atlasMap )
- {
- if ( item.second == atlas )
- {
- if (atlas->getReferenceCount() == 1)
- {
- _atlasMap.erase(item.first);
- }
-
- atlas->release();
-
- return true;
- }
- }
- }
-
- return false;
- }
- void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset/* = Vec2::ZERO*/)
- {
- char tmp[ATLAS_MAP_KEY_BUFFER];
- snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, fontFileName.c_str());
- std::string atlasName = tmp;
-
- auto it = _atlasMap.find(atlasName);
- if (it != _atlasMap.end())
- {
- CC_SAFE_RELEASE_NULL(it->second);
- _atlasMap.erase(it);
- }
- FontFNT::reloadBMFontResource(fontFileName);
- auto font = FontFNT::create(fontFileName, imageOffset);
- if (font)
- {
- auto tempAtlas = font->createFontAtlas();
- if (tempAtlas)
- {
- _atlasMap[atlasName] = tempAtlas;
- }
- }
- }
- void FontAtlasCache::unloadFontAtlasTTF(const std::string& fontFileName)
- {
- auto item = _atlasMap.begin();
- while (item != _atlasMap.end())
- {
- if (item->first.find(fontFileName) != std::string::npos)
- {
- CC_SAFE_RELEASE_NULL(item->second);
- item = _atlasMap.erase(item);
- }
- else
- item++;
- }
- }
- NS_CC_END
|