axmol/core/2d/CCFontAtlasCache.cpp

312 lines
9.5 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2013 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-11-11 17:20:21 +08:00
Copyright (c) 2021 Bytedance Inc.
https://adxe.org
2021-12-25 10:04:45 +08:00
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:
2021-12-25 10:04:45 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
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-07-14 17:53:55 +08:00
#include "2d/CCFontAtlasCache.h"
2015-07-30 11:33:18 +08:00
#include "base/CCDirector.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCFontFNT.h"
#include "2d/CCFontFreeType.h"
2014-08-27 13:39:50 +08:00
#include "2d/CCFontAtlas.h"
#include "2d/CCFontCharMap.h"
2015-07-30 11:33:18 +08:00
#include "2d/CCLabel.h"
#include "platform/CCFileUtils.h"
NS_CC_BEGIN
2021-12-26 23:26:34 +08:00
hlookup::string_map<FontAtlas*> FontAtlasCache::_atlasMap;
#define ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE 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();
}
2016-03-24 23:05:22 +08:00
_atlasMap.clear();
}
2015-07-30 11:33:18 +08:00
FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config)
{
2021-12-25 10:04:45 +08:00
auto realFontFilename = FileUtils::getInstance()->getNewFilename(
config->fontFilePath); // resolves real file path, to prevent storing multiple atlases for the same file.
2015-07-30 11:33:18 +08:00
bool useDistanceField = config->distanceFieldEnabled;
2021-12-25 10:04:45 +08:00
if (config->outlineSize > 0)
{
useDistanceField = false;
}
std::string key;
char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
2021-12-25 10:04:45 +08:00
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, useDistanceField ? "df %.2f %d " : "%.2f %d ",
config->fontSize, config->outlineSize);
std::string atlasName(keyPrefix);
atlasName += realFontFilename;
auto it = _atlasMap.find(atlasName);
2021-12-25 10:04:45 +08:00
if (it == _atlasMap.end())
{
2021-12-25 10:04:45 +08:00
auto font = FontFreeType::create(realFontFilename, config->fontSize, config->glyphs, config->customGlyphs,
useDistanceField, (float)config->outlineSize);
if (font)
{
2021-11-11 17:20:21 +08:00
auto tempAtlas = font->newFontAtlas();
if (tempAtlas)
{
2021-12-27 13:52:08 +08:00
return _atlasMap.emplace(atlasName, tempAtlas).first->second;
}
}
}
else
return it->second;
return nullptr;
}
2021-12-26 23:26:34 +08:00
FontAtlas* FontAtlasCache::getFontAtlasFNT(std::string_view fontFileName)
{
return getFontAtlasFNT(fontFileName, Rect::ZERO, false);
}
2021-12-26 23:26:34 +08:00
FontAtlas* FontAtlasCache::getFontAtlasFNT(std::string_view fontFileName, std::string_view subTextureKey)
{
2021-12-25 10:04:45 +08:00
const auto realFontFilename = FileUtils::getInstance()->getNewFilename(
fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file.
2021-12-26 23:26:34 +08:00
std::string atlasName{subTextureKey};
atlasName.append(" ", 1).append(realFontFilename);
const auto it = _atlasMap.find(atlasName);
if (it == _atlasMap.end())
{
const auto font = FontFNT::create(realFontFilename, subTextureKey);
if (font)
{
2021-11-11 17:20:21 +08:00
const auto tempAtlas = font->newFontAtlas();
if (tempAtlas)
{
_atlasMap[atlasName] = tempAtlas;
return _atlasMap[atlasName];
}
}
}
else
return it->second;
return nullptr;
}
2021-12-26 23:26:34 +08:00
FontAtlas* FontAtlasCache::getFontAtlasFNT(std::string_view fontFileName, const Rect& imageRect, bool imageRotated)
{
2021-12-25 10:04:45 +08:00
const auto realFontFilename = FileUtils::getInstance()->getNewFilename(
fontFileName); // resolves real file path, to prevent storing multiple atlases for the same file.
char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageRect.origin.x, imageRect.origin.y);
std::string atlasName(keyPrefix);
atlasName += realFontFilename;
2021-12-25 10:04:45 +08:00
const auto it = _atlasMap.find(atlasName);
2021-12-25 10:04:45 +08:00
if (it == _atlasMap.end())
{
const auto font = FontFNT::create(realFontFilename, imageRect, imageRotated);
2021-12-25 10:04:45 +08:00
if (font)
{
2021-11-11 17:20:21 +08:00
const auto tempAtlas = font->newFontAtlas();
if (tempAtlas)
{
_atlasMap[atlasName] = tempAtlas;
return _atlasMap[atlasName];
}
}
}
else
return it->second;
2021-12-25 10:04:45 +08:00
return nullptr;
}
2021-12-26 23:26:34 +08:00
FontAtlas* FontAtlasCache::getFontAtlasFNT(std::string_view fontFileName, const Vec2& imageOffset)
{
return getFontAtlasFNT(fontFileName, Rect(imageOffset.x, imageOffset.y, 0, 0), false);
}
2021-12-26 23:26:34 +08:00
FontAtlas* FontAtlasCache::getFontAtlasCharMap(std::string_view plistFile)
{
2021-12-26 23:26:34 +08:00
std::string_view atlasName = plistFile;
2021-12-25 10:04:45 +08:00
auto it = _atlasMap.find(atlasName);
2021-12-25 10:04:45 +08:00
if (it == _atlasMap.end())
{
auto font = FontCharMap::create(plistFile);
2021-12-25 10:04:45 +08:00
if (font)
{
2021-11-11 17:20:21 +08:00
auto tempAtlas = font->newFontAtlas();
if (tempAtlas)
{
2021-12-26 23:26:34 +08:00
hlookup::set_item(_atlasMap, atlasName, tempAtlas); // _atlasMap[atlasName] = tempAtlas;
return tempAtlas;
}
}
}
else
return it->second;
return nullptr;
}
2015-07-30 11:33:18 +08:00
FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
{
char key[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
2021-12-25 10:04:45 +08:00
sprintf(key, "name:%p_%d_%d_%d", texture->getBackendTexture(), itemWidth, itemHeight, startCharMap);
std::string atlasName = key;
auto it = _atlasMap.find(atlasName);
2021-12-25 10:04:45 +08:00
if (it == _atlasMap.end())
{
2021-12-25 10:04:45 +08:00
auto font = FontCharMap::create(texture, itemWidth, itemHeight, startCharMap);
2021-12-25 10:04:45 +08:00
if (font)
{
2021-11-11 17:20:21 +08:00
auto tempAtlas = font->newFontAtlas();
if (tempAtlas)
{
_atlasMap[atlasName] = tempAtlas;
return _atlasMap[atlasName];
}
}
}
else
return it->second;
return nullptr;
}
2021-12-26 23:26:34 +08:00
FontAtlas* FontAtlasCache::getFontAtlasCharMap(std::string_view charMapFile,
2021-12-25 10:04:45 +08:00
int itemWidth,
int itemHeight,
int startCharMap)
{
char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%d %d %d ", itemWidth, itemHeight, startCharMap);
std::string atlasName(keyPrefix);
atlasName += charMapFile;
auto it = _atlasMap.find(atlasName);
2021-12-25 10:04:45 +08:00
if (it == _atlasMap.end())
{
2021-12-25 10:04:45 +08:00
auto font = FontCharMap::create(charMapFile, itemWidth, itemHeight, startCharMap);
2021-12-25 10:04:45 +08:00
if (font)
{
2021-11-11 17:20:21 +08:00
auto tempAtlas = font->newFontAtlas();
if (tempAtlas)
{
_atlasMap[atlasName] = tempAtlas;
return _atlasMap[atlasName];
}
}
}
else
return it->second;
return nullptr;
}
2021-12-25 10:04:45 +08:00
bool FontAtlasCache::releaseFontAtlas(FontAtlas* atlas)
{
if (nullptr != atlas)
{
if (atlas->getReferenceCount() == 1)
{
2021-12-25 10:04:45 +08:00
for (auto& item : _atlasMap)
{
2021-10-28 21:09:03 +08:00
if (item.second == atlas)
{
_atlasMap.erase(item.first);
break;
}
}
2021-12-25 10:04:45 +08:00
}
atlas->release();
return true;
}
2021-12-25 10:04:45 +08:00
return false;
}
2021-12-26 23:26:34 +08:00
void FontAtlasCache::reloadFontAtlasFNT(std::string_view fontFileName, const Rect& imageRect, bool imageRotated)
{
char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageRect.origin.x, imageRect.origin.y);
std::string atlasName(keyPrefix);
atlasName += fontFileName;
2021-12-25 10:04:45 +08:00
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, imageRect, imageRotated);
if (font)
{
2021-11-11 17:20:21 +08:00
auto tempAtlas = font->newFontAtlas();
if (tempAtlas)
{
_atlasMap[atlasName] = tempAtlas;
}
}
}
2021-12-26 23:26:34 +08:00
void FontAtlasCache::reloadFontAtlasFNT(std::string_view fontFileName, const Vec2& imageOffset)
{
reloadFontAtlasFNT(fontFileName, Rect(imageOffset.x, imageOffset.y, 0, 0), false);
}
2021-12-26 23:26:34 +08:00
void FontAtlasCache::unloadFontAtlasTTF(std::string_view 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