2013-07-25 01:22:46 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-07-25 01:22:46 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-17 13:35:58 +08:00
|
|
|
#include <sstream>
|
|
|
|
|
2013-07-25 01:22:46 +08:00
|
|
|
#include "CCFontAtlasCache.h"
|
|
|
|
|
2014-01-20 10:32:12 +08:00
|
|
|
#include "CCFontFNT.h"
|
|
|
|
#include "CCFontFreeType.h"
|
|
|
|
#include "CCFontCharMap.h"
|
2014-03-11 18:06:14 +08:00
|
|
|
#include "CCDirector.h"
|
2013-07-25 01:22:46 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
std::unordered_map<std::string, FontAtlas *> FontAtlasCache::_atlasMap;
|
2013-07-25 01:22:46 +08:00
|
|
|
|
2014-03-14 14:59:26 +08:00
|
|
|
void FontAtlasCache::purgeCachedData()
|
|
|
|
{
|
|
|
|
for (auto & atlas:_atlasMap)
|
|
|
|
{
|
|
|
|
atlas.second->purgeTexturesAtlas();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
FontAtlas * FontAtlasCache::getFontAtlasTTF(const TTFConfig & config)
|
|
|
|
{
|
|
|
|
bool useDistanceField = config.distanceFieldEnabled;
|
|
|
|
if(config.outlineSize > 0)
|
|
|
|
{
|
|
|
|
useDistanceField = false;
|
|
|
|
}
|
|
|
|
int fontSize = config.fontSize;
|
2014-03-25 09:58:16 +08:00
|
|
|
auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
|
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
if (useDistanceField)
|
|
|
|
{
|
2014-03-25 09:58:16 +08:00
|
|
|
fontSize = Label::DistanceFieldFontSize / contentScaleFactor;
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
|
|
|
|
2014-03-28 16:52:48 +08:00
|
|
|
auto atlasName = generateFontName(config.fontFilePath, fontSize, GlyphCollection::DYNAMIC, useDistanceField);
|
2014-03-05 15:54:40 +08:00
|
|
|
atlasName.append("_outline_");
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << config.outlineSize;
|
|
|
|
atlasName.append(ss.str());
|
|
|
|
|
2014-03-28 16:52:48 +08:00
|
|
|
auto it = _atlasMap.find(atlasName);
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-03-28 16:52:48 +08:00
|
|
|
if ( it == _atlasMap.end() )
|
2013-07-25 01:22:46 +08:00
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
auto font = FontFreeType::create(config.fontFilePath, fontSize * contentScaleFactor, config.glyphs,
|
2014-03-25 09:58:16 +08:00
|
|
|
config.customGlyphs,useDistanceField,config.outlineSize * contentScaleFactor);
|
2014-01-20 10:32:12 +08:00
|
|
|
if (font)
|
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
auto tempAtlas = font->createFontAtlas();
|
2014-01-20 10:32:12 +08:00
|
|
|
if (tempAtlas)
|
2014-03-28 16:52:48 +08:00
|
|
|
{
|
2014-01-20 10:32:12 +08:00
|
|
|
_atlasMap[atlasName] = tempAtlas;
|
2014-03-28 16:52:48 +08:00
|
|
|
return _atlasMap[atlasName];
|
|
|
|
}
|
2014-01-20 10:32:12 +08:00
|
|
|
}
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
2013-07-31 07:41:26 +08:00
|
|
|
else
|
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
_atlasMap[atlasName]->retain();
|
|
|
|
return _atlasMap[atlasName];
|
2013-07-31 07:41:26 +08:00
|
|
|
}
|
2014-01-20 10:32:12 +08:00
|
|
|
|
2014-03-28 16:52:48 +08:00
|
|
|
return nullptr;
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 22:33:52 +08:00
|
|
|
FontAtlas * FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Point& imageOffset /* = Point::ZERO */)
|
2013-07-25 01:22:46 +08:00
|
|
|
{
|
2013-12-13 12:42:15 +08:00
|
|
|
std::string atlasName = generateFontName(fontFileName, 0, GlyphCollection::CUSTOM,false);
|
2014-03-28 16:52:48 +08:00
|
|
|
auto it = _atlasMap.find(atlasName);
|
|
|
|
|
|
|
|
if ( it == _atlasMap.end() )
|
2013-07-25 01:22:46 +08:00
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
auto font = FontFNT::create(fontFileName,imageOffset);
|
2014-01-20 10:32:12 +08:00
|
|
|
|
|
|
|
if(font)
|
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
auto tempAtlas = font->createFontAtlas();
|
2014-01-20 10:32:12 +08:00
|
|
|
if (tempAtlas)
|
2014-03-28 16:52:48 +08:00
|
|
|
{
|
2014-01-20 10:32:12 +08:00
|
|
|
_atlasMap[atlasName] = tempAtlas;
|
2014-03-28 16:52:48 +08:00
|
|
|
return _atlasMap[atlasName];
|
|
|
|
}
|
2014-01-20 10:32:12 +08:00
|
|
|
}
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
2013-07-31 07:41:26 +08:00
|
|
|
else
|
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
_atlasMap[atlasName]->retain();
|
|
|
|
return _atlasMap[atlasName];
|
2013-07-31 07:41:26 +08:00
|
|
|
}
|
2013-07-25 01:22:46 +08:00
|
|
|
|
2014-03-28 16:52:48 +08:00
|
|
|
return nullptr;
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
|
2014-01-16 16:37:29 +08:00
|
|
|
FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile)
|
|
|
|
{
|
|
|
|
std::string atlasName = generateFontName(plistFile, 0, GlyphCollection::CUSTOM,false);
|
2014-03-28 16:52:48 +08:00
|
|
|
auto it = _atlasMap.find(atlasName);
|
2014-01-16 16:37:29 +08:00
|
|
|
|
2014-03-28 16:52:48 +08:00
|
|
|
if ( it == _atlasMap.end() )
|
2014-01-16 16:37:29 +08:00
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
auto font = FontCharMap::create(plistFile);
|
2014-01-20 10:32:12 +08:00
|
|
|
|
|
|
|
if(font)
|
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
auto tempAtlas = font->createFontAtlas();
|
2014-01-20 10:32:12 +08:00
|
|
|
if (tempAtlas)
|
2014-03-28 16:52:48 +08:00
|
|
|
{
|
2014-01-20 10:32:12 +08:00
|
|
|
_atlasMap[atlasName] = tempAtlas;
|
2014-03-28 16:52:48 +08:00
|
|
|
return _atlasMap[atlasName];
|
|
|
|
}
|
2014-01-20 10:32:12 +08:00
|
|
|
}
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
_atlasMap[atlasName]->retain();
|
|
|
|
return _atlasMap[atlasName];
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
|
2014-03-28 16:52:48 +08:00
|
|
|
return nullptr;
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FontAtlas * FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
|
|
|
|
{
|
|
|
|
char tmp[30];
|
2014-01-16 17:50:09 +08:00
|
|
|
sprintf(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap);
|
2014-01-16 16:37:29 +08:00
|
|
|
std::string atlasName = generateFontName(tmp, 0, GlyphCollection::CUSTOM,false);
|
|
|
|
|
2014-03-28 16:52:48 +08:00
|
|
|
auto it = _atlasMap.find(atlasName);
|
|
|
|
if ( it == _atlasMap.end() )
|
2014-01-16 16:37:29 +08:00
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
auto font = FontCharMap::create(texture,itemWidth,itemHeight,startCharMap);
|
2014-01-20 10:32:12 +08:00
|
|
|
|
|
|
|
if(font)
|
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
auto tempAtlas = font->createFontAtlas();
|
2014-01-20 10:32:12 +08:00
|
|
|
if (tempAtlas)
|
2014-03-28 16:52:48 +08:00
|
|
|
{
|
2014-01-20 10:32:12 +08:00
|
|
|
_atlasMap[atlasName] = tempAtlas;
|
2014-03-28 16:52:48 +08:00
|
|
|
return _atlasMap[atlasName];
|
|
|
|
}
|
2014-01-20 10:32:12 +08:00
|
|
|
}
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
_atlasMap[atlasName]->retain();
|
|
|
|
return _atlasMap[atlasName];
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
|
2014-03-28 16:52:48 +08:00
|
|
|
return nullptr;
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
|
|
|
|
{
|
|
|
|
std::string atlasName = generateFontName(charMapFile, 0, GlyphCollection::CUSTOM,false);
|
|
|
|
|
2014-03-28 16:52:48 +08:00
|
|
|
auto it = _atlasMap.find(atlasName);
|
|
|
|
if ( it == _atlasMap.end() )
|
2014-01-16 16:37:29 +08:00
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
auto font = FontCharMap::create(charMapFile,itemWidth,itemHeight,startCharMap);
|
2014-01-20 10:32:12 +08:00
|
|
|
|
|
|
|
if(font)
|
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
auto tempAtlas = font->createFontAtlas();
|
2014-01-20 10:32:12 +08:00
|
|
|
if (tempAtlas)
|
2014-03-28 16:52:48 +08:00
|
|
|
{
|
2014-01-20 10:32:12 +08:00
|
|
|
_atlasMap[atlasName] = tempAtlas;
|
2014-03-28 16:52:48 +08:00
|
|
|
return _atlasMap[atlasName];
|
|
|
|
}
|
2014-01-20 10:32:12 +08:00
|
|
|
}
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-28 16:52:48 +08:00
|
|
|
_atlasMap[atlasName]->retain();
|
|
|
|
return _atlasMap[atlasName];
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
|
2014-03-28 16:52:48 +08:00
|
|
|
return nullptr;
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
std::string FontAtlasCache::generateFontName(const std::string& fontFileName, int size, GlyphCollection theGlyphs, bool useDistanceField)
|
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-06 06:11:07 +08:00
|
|
|
tempName.append("_DYNAMIC_");
|
2013-08-01 03:11:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GlyphCollection::NEHE:
|
2013-08-06 06:11:07 +08:00
|
|
|
tempName.append("_NEHE_");
|
2013-08-01 03:11:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GlyphCollection::ASCII:
|
2013-08-06 06:11:07 +08:00
|
|
|
tempName.append("_ASCII_");
|
2013-08-01 03:11:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GlyphCollection::CUSTOM:
|
2013-08-06 06:11:07 +08:00
|
|
|
tempName.append("_CUSTOM_");
|
2013-08-01 03:11:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-12-13 12:42:15 +08:00
|
|
|
if(useDistanceField)
|
|
|
|
tempName.append("df");
|
2013-08-14 02:28:54 +08:00
|
|
|
// std::to_string is not supported on android, using std::stringstream instead.
|
2013-08-02 13:46:26 +08:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << size;
|
|
|
|
return tempName.append(ss.str());
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FontAtlasCache::releaseFontAtlas(FontAtlas *atlas)
|
|
|
|
{
|
2014-01-04 18:22:50 +08:00
|
|
|
if (nullptr != atlas)
|
2013-07-25 01:22:46 +08:00
|
|
|
{
|
2013-08-06 06:11:07 +08:00
|
|
|
for( auto &item: _atlasMap )
|
2013-07-25 01:22:46 +08:00
|
|
|
{
|
|
|
|
if ( item.second == atlas )
|
|
|
|
{
|
2014-01-22 13:47:29 +08:00
|
|
|
if (atlas->getReferenceCount() == 1)
|
2014-01-04 18:22:50 +08:00
|
|
|
{
|
|
|
|
_atlasMap.erase(item.first);
|
|
|
|
}
|
2013-07-25 01:22:46 +08:00
|
|
|
|
2014-01-04 18:22:50 +08:00
|
|
|
atlas->release();
|
2013-07-25 01:22:46 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-04 18:22:50 +08:00
|
|
|
NS_CC_END
|