2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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.
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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
|
|
|
|
2019-11-23 20:27:39 +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
|
|
|
|
2019-11-23 20:27:39 +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.
|
|
|
|
****************************************************************************/
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "2d/FontAtlasCache.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Director.h"
|
|
|
|
#include "2d/FontFNT.h"
|
|
|
|
#include "2d/FontFreeType.h"
|
|
|
|
#include "2d/FontAtlas.h"
|
|
|
|
#include "2d/FontCharMap.h"
|
|
|
|
#include "2d/Label.h"
|
|
|
|
#include "platform/FileUtils.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
hlookup::string_map<FontAtlas*> FontAtlasCache::_atlasMap;
|
2019-11-23 20:27:39 +08:00
|
|
|
#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();
|
|
|
|
}
|
|
|
|
_atlasMap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config)
|
|
|
|
{
|
2022-06-12 22:26:54 +08:00
|
|
|
auto realFontFilename = config->fontFilePath; // resolves real file path, to prevent storing multiple atlases for the same file.
|
2019-11-23 20:27:39 +08:00
|
|
|
bool useDistanceField = config->distanceFieldEnabled;
|
2023-08-09 13:52:28 +08:00
|
|
|
if (config->outlineSize > 0)
|
|
|
|
{
|
|
|
|
useDistanceField = false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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 ",
|
2023-08-09 13:52:28 +08:00
|
|
|
config->fontSize, config->outlineSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string atlasName(keyPrefix);
|
|
|
|
atlasName += realFontFilename;
|
|
|
|
|
|
|
|
auto it = _atlasMap.find(atlasName);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it == _atlasMap.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto font = FontFreeType::create(realFontFilename, config->fontSize, config->glyphs, config->customGlyphs,
|
2023-08-09 13:52:28 +08:00
|
|
|
useDistanceField, (float)config->outlineSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (font)
|
|
|
|
{
|
2021-11-11 17:20:21 +08:00
|
|
|
auto tempAtlas = font->newFontAtlas();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (tempAtlas)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
return _atlasMap.emplace(atlasName, tempAtlas).first->second;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
FontAtlas* FontAtlasCache::getFontAtlasFNT(std::string_view fontFileName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-01-06 09:35:52 +08:00
|
|
|
return getFontAtlasFNT(fontFileName, Rect::ZERO, false);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
FontAtlas* FontAtlasCache::getFontAtlasFNT(std::string_view fontFileName, std::string_view subTextureKey)
|
2020-01-06 09:35:52 +08:00
|
|
|
{
|
2022-06-12 22:26:54 +08:00
|
|
|
const auto realFontFilename = fontFileName; // resolves real file path, to prevent storing multiple atlases for the same file.
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string atlasName{subTextureKey};
|
|
|
|
atlasName.append(" ", 1).append(realFontFilename);
|
2020-01-06 09:35:52 +08:00
|
|
|
|
|
|
|
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();
|
2020-01-06 09:35:52 +08:00
|
|
|
if (tempAtlas)
|
|
|
|
{
|
|
|
|
_atlasMap[atlasName] = tempAtlas;
|
|
|
|
return _atlasMap[atlasName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
FontAtlas* FontAtlasCache::getFontAtlasFNT(std::string_view fontFileName, const Rect& imageRect, bool imageRotated)
|
2020-01-06 09:35:52 +08:00
|
|
|
{
|
2022-06-12 22:26:54 +08:00
|
|
|
const auto realFontFilename = fontFileName; // resolves real file path, to prevent storing multiple atlases for the same file.
|
2019-11-23 20:27:39 +08:00
|
|
|
char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
|
2020-01-06 09:35:52 +08:00
|
|
|
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageRect.origin.x, imageRect.origin.y);
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string atlasName(keyPrefix);
|
|
|
|
atlasName += realFontFilename;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-01-06 09:35:52 +08:00
|
|
|
const auto it = _atlasMap.find(atlasName);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it == _atlasMap.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-01-06 09:35:52 +08:00
|
|
|
const auto font = FontFNT::create(realFontFilename, imageRect, imageRotated);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (font)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-11 17:20:21 +08:00
|
|
|
const auto tempAtlas = font->newFontAtlas();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (tempAtlas)
|
|
|
|
{
|
|
|
|
_atlasMap[atlasName] = tempAtlas;
|
|
|
|
return _atlasMap[atlasName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return it->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
FontAtlas* FontAtlasCache::getFontAtlasFNT(std::string_view fontFileName, const Vec2& imageOffset)
|
2020-01-06 09:35:52 +08:00
|
|
|
{
|
|
|
|
return getFontAtlasFNT(fontFileName, Rect(imageOffset.x, imageOffset.y, 0, 0), false);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
FontAtlas* FontAtlasCache::getFontAtlasCharMap(std::string_view plistFile)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view atlasName = plistFile;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto it = _atlasMap.find(atlasName);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it == _atlasMap.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto font = FontCharMap::create(plistFile);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (font)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-11 17:20:21 +08:00
|
|
|
auto tempAtlas = font->newFontAtlas();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (tempAtlas)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
hlookup::set_item(_atlasMap, atlasName, tempAtlas); // _atlasMap[atlasName] = tempAtlas;
|
|
|
|
return tempAtlas;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
|
|
|
|
{
|
|
|
|
char key[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
|
2022-11-14 15:02:16 +08:00
|
|
|
snprintf(key, sizeof(key), "name:%p_%d_%d_%d", texture->getBackendTexture(), itemWidth, itemHeight, startCharMap);
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string atlasName = key;
|
|
|
|
|
|
|
|
auto it = _atlasMap.find(atlasName);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it == _atlasMap.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto font = FontCharMap::create(texture, itemWidth, itemHeight, startCharMap);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (font)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-11 17:20:21 +08:00
|
|
|
auto tempAtlas = font->newFontAtlas();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (tempAtlas)
|
|
|
|
{
|
|
|
|
_atlasMap[atlasName] = tempAtlas;
|
|
|
|
return _atlasMap[atlasName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
FontAtlas* FontAtlasCache::getFontAtlasCharMap(std::string_view charMapFile,
|
2021-12-25 10:04:45 +08:00
|
|
|
int itemWidth,
|
|
|
|
int itemHeight,
|
|
|
|
int startCharMap)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
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())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto font = FontCharMap::create(charMapFile, itemWidth, itemHeight, startCharMap);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (font)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-11-11 17:20:21 +08:00
|
|
|
auto tempAtlas = font->newFontAtlas();
|
2019-11-23 20:27:39 +08:00
|
|
|
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)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (nullptr != atlas)
|
|
|
|
{
|
2021-10-28 21:07:46 +08:00
|
|
|
if (atlas->getReferenceCount() == 1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& item : _atlasMap)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-28 21:09:03 +08:00
|
|
|
if (item.second == atlas)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-28 21:07:46 +08:00
|
|
|
_atlasMap.erase(item.first);
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2021-10-28 21:07:46 +08:00
|
|
|
atlas->release();
|
|
|
|
return true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FontAtlasCache::reloadFontAtlasFNT(std::string_view fontFileName, const Rect& imageRect, bool imageRotated)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
char keyPrefix[ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE];
|
2020-01-06 09:35:52 +08:00
|
|
|
snprintf(keyPrefix, ATLAS_MAP_KEY_PREFIX_BUFFER_SIZE, "%.2f %.2f ", imageRect.origin.x, imageRect.origin.y);
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string atlasName(keyPrefix);
|
|
|
|
atlasName += fontFileName;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto it = _atlasMap.find(atlasName);
|
|
|
|
if (it != _atlasMap.end())
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(it->second);
|
2019-11-23 20:27:39 +08:00
|
|
|
_atlasMap.erase(it);
|
|
|
|
}
|
|
|
|
FontFNT::reloadBMFontResource(fontFileName);
|
2020-01-06 09:35:52 +08:00
|
|
|
auto font = FontFNT::create(fontFileName, imageRect, imageRotated);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (font)
|
|
|
|
{
|
2021-11-11 17:20:21 +08:00
|
|
|
auto tempAtlas = font->newFontAtlas();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (tempAtlas)
|
|
|
|
{
|
|
|
|
_atlasMap[atlasName] = tempAtlas;
|
|
|
|
}
|
|
|
|
}
|
2020-01-06 09:35:52 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FontAtlasCache::reloadFontAtlasFNT(std::string_view fontFileName, const Vec2& imageOffset)
|
2020-01-06 09:35:52 +08:00
|
|
|
{
|
|
|
|
reloadFontAtlasFNT(fontFileName, Rect(imageOffset.x, imageOffset.y, 0, 0), false);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void FontAtlasCache::unloadFontAtlasTTF(std::string_view fontFileName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto item = _atlasMap.begin();
|
|
|
|
while (item != _atlasMap.end())
|
|
|
|
{
|
|
|
|
if (item->first.find(fontFileName) != std::string::npos)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(item->second);
|
2019-11-23 20:27:39 +08:00
|
|
|
item = _atlasMap.erase(item);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
item++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|