2013-07-25 01:22:46 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 Zynga Inc.
|
|
|
|
|
|
|
|
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 "CCFontAtlasCache.h"
|
|
|
|
#include "CCFontAtlasFactory.h"
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
std::map<std::string, FontAtlas *> FontAtlasCache::_atlasMap;
|
|
|
|
|
|
|
|
|
2013-07-31 07:41:26 +08:00
|
|
|
FontAtlas * FontAtlasCache::getFontAtlasTTF(const char *fontFileName, int size, GlyphCollection glyphs, const char *customGlyphs)
|
2013-07-25 01:22:46 +08:00
|
|
|
{
|
2013-08-01 03:11:56 +08:00
|
|
|
std::string atlasName = generateFontName(fontFileName, size, glyphs);
|
2013-07-25 01:22:46 +08:00
|
|
|
FontAtlas *tempAtlas = _atlasMap[atlasName];
|
|
|
|
|
|
|
|
if ( !tempAtlas )
|
|
|
|
{
|
2013-07-31 07:41:26 +08:00
|
|
|
tempAtlas = FontAtlasFactory::createAtlasFromTTF(fontFileName, size, glyphs, customGlyphs);
|
2013-07-25 01:22:46 +08:00
|
|
|
|
|
|
|
if (tempAtlas)
|
|
|
|
{
|
|
|
|
_atlasMap[atlasName] = tempAtlas;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-25 08:21:51 +08:00
|
|
|
return nullptr;
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-31 07:41:26 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tempAtlas->retain();
|
|
|
|
}
|
2013-07-25 01:22:46 +08:00
|
|
|
|
|
|
|
return tempAtlas;
|
|
|
|
}
|
|
|
|
|
|
|
|
FontAtlas * FontAtlasCache::getFontAtlasFNT(const char *fontFileName)
|
|
|
|
{
|
2013-08-01 03:11:56 +08:00
|
|
|
std::string atlasName = generateFontName(fontFileName, 0, GlyphCollection::CUSTOM);
|
2013-07-25 01:22:46 +08:00
|
|
|
FontAtlas *tempAtlas = _atlasMap[atlasName];
|
|
|
|
|
|
|
|
if ( !tempAtlas )
|
|
|
|
{
|
|
|
|
tempAtlas = FontAtlasFactory::createAtlasFromFNT(fontFileName);
|
|
|
|
|
|
|
|
if (tempAtlas)
|
|
|
|
{
|
|
|
|
_atlasMap[atlasName] = tempAtlas;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-25 08:21:51 +08:00
|
|
|
return nullptr;
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-31 07:41:26 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tempAtlas->retain();
|
|
|
|
}
|
2013-07-25 01:22:46 +08:00
|
|
|
|
|
|
|
return tempAtlas;
|
|
|
|
}
|
|
|
|
|
2013-08-01 03:11:56 +08:00
|
|
|
std::string FontAtlasCache::generateFontName(const char *fontFileName, int size, GlyphCollection theGlyphs)
|
2013-07-25 01:22:46 +08:00
|
|
|
{
|
|
|
|
std::string tempName(fontFileName);
|
2013-08-01 03:11:56 +08:00
|
|
|
|
|
|
|
switch (theGlyphs)
|
|
|
|
{
|
|
|
|
|
|
|
|
case GlyphCollection::DYNAMIC:
|
2013-08-02 07:29:39 +08:00
|
|
|
tempName.append("_DYNAMIC_");
|
2013-08-01 03:11:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GlyphCollection::NEHE:
|
2013-08-02 07:29:39 +08:00
|
|
|
tempName.append("_NEHE_");
|
2013-08-01 03:11:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GlyphCollection::ASCII:
|
2013-08-02 07:29:39 +08:00
|
|
|
tempName.append("_ASCII_");
|
2013-08-01 03:11:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GlyphCollection::CUSTOM:
|
2013-08-02 07:29:39 +08:00
|
|
|
tempName.append("_CUSTOM_");
|
2013-08-01 03:11:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-02 13:46:26 +08:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << size;
|
|
|
|
// std::to_string is not supported on android, using std::stringstream instead.
|
|
|
|
return tempName.append(ss.str());
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FontAtlasCache::releaseFontAtlas(FontAtlas *atlas)
|
|
|
|
{
|
|
|
|
if (atlas)
|
|
|
|
{
|
|
|
|
for( auto &item: _atlasMap)
|
|
|
|
{
|
|
|
|
if ( item.second == atlas )
|
|
|
|
{
|
|
|
|
bool removeFromList = false;
|
|
|
|
if(item.second->isSingleReference())
|
|
|
|
removeFromList = true;
|
|
|
|
|
|
|
|
item.second->release();
|
|
|
|
|
|
|
|
if (removeFromList)
|
|
|
|
_atlasMap.erase(item.first);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|