axmol/core/2d/FontAtlas.cpp

432 lines
14 KiB
C++
Raw Normal View History

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.
Copyright (c) 2021-2022 Bytedance Inc.
2021-11-11 17:20:21 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
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:
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/FontAtlas.h"
2022-07-16 10:43:05 +08:00
#if AX_TARGET_PLATFORM != AX_PLATFORM_WIN32 && AX_TARGET_PLATFORM != AX_PLATFORM_ANDROID
#elif AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
2022-10-01 16:24:52 +08:00
# include "platform/android/jni/Java_org_axmol_lib_AxmolEngine.h"
2019-11-23 20:27:39 +08:00
#endif
#include <algorithm>
#include "2d/FontFreeType.h"
#include "base/UTF8.h"
#include "base/Director.h"
#include "base/EventListenerCustom.h"
#include "base/EventDispatcher.h"
#include "base/EventType.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
const int FontAtlas::CacheTextureWidth = 512;
const int FontAtlas::CacheTextureHeight = 512;
2019-11-23 20:27:39 +08:00
const char* FontAtlas::CMD_PURGE_FONTATLAS = "__cc_PURGE_FONTATLAS";
const char* FontAtlas::CMD_RESET_FONTATLAS = "__cc_RESET_FONTATLAS";
2021-12-25 10:04:45 +08:00
FontAtlas::FontAtlas(Font* theFont) : _font(theFont)
2019-11-23 20:27:39 +08:00
{
_font->retain();
_fontFreeType = dynamic_cast<FontFreeType*>(_font);
if (_fontFreeType)
{
2021-12-25 10:04:45 +08:00
_lineHeight = (float)_font->getFontMaxHeight();
_fontAscender = _fontFreeType->getFontAscender();
2019-11-23 20:27:39 +08:00
_letterEdgeExtend = 2;
auto outlineSize = _fontFreeType->getOutlineSize();
if (outlineSize > 0)
{
_strideShift = 1;
_pixelFormat = AX_GLES_PROFILE != 200 ? backend::PixelFormat::RG8 : backend::PixelFormat::LA8;
_currentPageDataSize = CacheTextureWidth * CacheTextureHeight << _strideShift;
2022-07-16 10:43:05 +08:00
#if defined(AX_USE_METAL)
_currentPageDataSizeRGBA = CacheTextureWidth * CacheTextureHeight * 4;
#endif
2019-11-23 20:27:39 +08:00
_lineHeight += 2 * outlineSize;
}
else
{
_strideShift = 0;
_pixelFormat = AX_GLES_PROFILE != 200 ? backend::PixelFormat::R8 : backend::PixelFormat::A8;
_currentPageDataSize = CacheTextureWidth * CacheTextureHeight;
}
if (_fontFreeType->isDistanceFieldEnabled())
{
_letterPadding += 2 * FontFreeType::DistanceMapSpread;
}
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2019-11-23 20:27:39 +08:00
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
2021-12-25 10:04:45 +08:00
_rendererRecreatedListener = EventListenerCustom::create(
2022-07-16 10:43:05 +08:00
EVENT_RENDERER_RECREATED, AX_CALLBACK_1(FontAtlas::listenRendererRecreated, this));
2019-11-23 20:27:39 +08:00
eventDispatcher->addEventListenerWithFixedPriority(_rendererRecreatedListener, 1);
#endif
}
}
void FontAtlas::reinit()
{
if (!_currentPageData)
_currentPageData = new uint8_t[_currentPageDataSize];
_currentPage = -1;
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
#if defined(AX_USE_METAL)
if (_strideShift && !_currentPageDataRGBA)
_currentPageDataRGBA = new uint8_t[_currentPageDataSizeRGBA];
#endif
2019-11-23 20:27:39 +08:00
addNewPage();
2019-11-23 20:27:39 +08:00
}
2019-11-23 20:27:39 +08:00
FontAtlas::~FontAtlas()
{
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2019-11-23 20:27:39 +08:00
if (_fontFreeType && _rendererRecreatedListener)
{
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->removeEventListener(_rendererRecreatedListener);
_rendererRecreatedListener = nullptr;
}
#endif
_font->release();
releaseTextures();
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE_ARRAY(_currentPageData);
#if defined(AX_USE_METAL)
AX_SAFE_DELETE_ARRAY(_currentPageDataRGBA);
#endif
2019-11-23 20:27:39 +08:00
}
void FontAtlas::reset()
{
releaseTextures();
2021-12-25 10:04:45 +08:00
_currLineHeight = 0;
2019-11-23 20:27:39 +08:00
_currentPageOrigX = 0;
_currentPageOrigY = 0;
_letterDefinitions.clear();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
reinit();
}
void FontAtlas::releaseTextures()
{
for (auto&& item : _atlasTextures)
2019-11-23 20:27:39 +08:00
{
item.second->release();
}
_atlasTextures.clear();
}
void FontAtlas::purgeTexturesAtlas()
{
if (_fontFreeType)
{
reset();
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
2021-12-25 10:04:45 +08:00
eventDispatcher->dispatchCustomEvent(CMD_PURGE_FONTATLAS, this);
eventDispatcher->dispatchCustomEvent(CMD_RESET_FONTATLAS, this);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
void FontAtlas::listenRendererRecreated(EventCustom* /*event*/)
2019-11-23 20:27:39 +08:00
{
purgeTexturesAtlas();
}
2021-12-25 10:04:45 +08:00
void FontAtlas::addLetterDefinition(char32_t utf32Char, const FontLetterDefinition& letterDefinition)
2019-11-23 20:27:39 +08:00
{
_letterDefinitions[utf32Char] = letterDefinition;
}
void FontAtlas::scaleFontLetterDefinition(float scaleFactor)
{
2021-12-25 10:04:45 +08:00
for (auto&& fontDefinition : _letterDefinitions)
{
2019-11-23 20:27:39 +08:00
auto& letterDefinition = fontDefinition.second;
letterDefinition.width *= scaleFactor;
letterDefinition.height *= scaleFactor;
letterDefinition.offsetX *= scaleFactor;
letterDefinition.offsetY *= scaleFactor;
letterDefinition.xAdvance = (int)(letterDefinition.xAdvance * scaleFactor);
}
}
2021-12-25 10:04:45 +08:00
bool FontAtlas::getLetterDefinitionForChar(char32_t utf32Char, FontLetterDefinition& letterDefinition)
2019-11-23 20:27:39 +08:00
{
auto outIterator = _letterDefinitions.find(utf32Char);
if (outIterator != _letterDefinitions.end())
{
letterDefinition = (*outIterator).second;
return letterDefinition.validDefinition;
}
else
{
return false;
}
}
void FontAtlas::findNewCharacters(const std::u32string& u32Text, std::unordered_set<char32_t>& charset)
2019-11-23 20:27:39 +08:00
{
if (_letterDefinitions.empty())
{
std::copy(u32Text.begin(), u32Text.end(), std::inserter(charset, charset.end()));
2019-11-23 20:27:39 +08:00
}
else
{
for (auto&& charCode : u32Text)
if (_letterDefinitions.find(charCode) == _letterDefinitions.end())
charset.insert(charCode);
2019-11-23 20:27:39 +08:00
}
}
bool FontAtlas::prepareLetterDefinitions(const std::u32string& utf32Text)
{
if (_fontFreeType == nullptr)
{
return false;
}
if (!_currentPageData)
2021-12-25 10:04:45 +08:00
reinit();
std::unordered_set<char32_t> charCodeSet;
findNewCharacters(utf32Text, charCodeSet);
if (charCodeSet.empty())
2019-11-23 20:27:39 +08:00
{
return false;
}
int adjustForDistanceMap = _letterPadding / 2;
2021-12-25 10:04:45 +08:00
int adjustForExtend = _letterEdgeExtend / 2;
int bitmapWidth = 0;
int bitmapHeight = 0;
2019-11-23 20:27:39 +08:00
int glyphHeight;
Rect tempRect;
FontLetterDefinition tempDef;
2022-07-16 10:43:05 +08:00
auto scaleFactor = AX_CONTENT_SCALE_FACTOR();
auto pixelFormat = _pixelFormat;
2019-11-23 20:27:39 +08:00
int startY = (int)_currentPageOrigY;
for (auto&& charCode : charCodeSet)
2019-11-23 20:27:39 +08:00
{
auto bitmap = _fontFreeType->getGlyphBitmap(charCode, bitmapWidth, bitmapHeight, tempRect, tempDef.xAdvance);
2019-11-23 20:27:39 +08:00
if (bitmap && bitmapWidth > 0 && bitmapHeight > 0)
{
tempDef.validDefinition = true;
2021-12-25 10:04:45 +08:00
tempDef.width = tempRect.size.width + _letterPadding + _letterEdgeExtend;
tempDef.height = tempRect.size.height + _letterPadding + _letterEdgeExtend;
tempDef.offsetX = tempRect.origin.x - adjustForDistanceMap - adjustForExtend;
tempDef.offsetY = _fontAscender + tempRect.origin.y - adjustForDistanceMap - adjustForExtend;
2019-11-23 20:27:39 +08:00
if (_currentPageOrigX + tempDef.width > CacheTextureWidth)
{
_currentPageOrigY += _currLineHeight;
2021-12-25 10:04:45 +08:00
_currLineHeight = 0;
2019-11-23 20:27:39 +08:00
_currentPageOrigX = 0;
if (_currentPageOrigY + _lineHeight + _letterPadding + _letterEdgeExtend >= CacheTextureHeight)
{
updateTextureContent(pixelFormat, startY);
startY = 0;
addNewPage();
2019-11-23 20:27:39 +08:00
}
}
glyphHeight = static_cast<int>(bitmapHeight) + _letterPadding + _letterEdgeExtend;
if (glyphHeight > _currLineHeight)
{
_currLineHeight = glyphHeight;
}
2021-12-25 10:04:45 +08:00
_fontFreeType->renderCharAt(_currentPageData, (int)_currentPageOrigX + adjustForExtend,
(int)_currentPageOrigY + adjustForExtend, bitmap, bitmapWidth, bitmapHeight);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
tempDef.U = _currentPageOrigX;
tempDef.V = _currentPageOrigY;
2019-11-23 20:27:39 +08:00
tempDef.textureID = _currentPage;
_currentPageOrigX += tempDef.width + 1;
// take from pixels to points
2021-12-25 10:04:45 +08:00
tempDef.width = tempDef.width / scaleFactor;
tempDef.height = tempDef.height / scaleFactor;
tempDef.U = tempDef.U / scaleFactor;
tempDef.V = tempDef.V / scaleFactor;
tempDef.rotated = false;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else
{
2021-12-25 10:04:45 +08:00
if (bitmap)
2019-11-23 20:27:39 +08:00
delete[] bitmap;
tempDef.validDefinition = !!tempDef.xAdvance;
tempDef.width = 0;
tempDef.height = 0;
tempDef.U = 0;
tempDef.V = 0;
tempDef.offsetX = 0;
tempDef.offsetY = 0;
tempDef.textureID = 0;
tempDef.rotated = false;
2019-11-23 20:27:39 +08:00
_currentPageOrigX += 1;
}
_letterDefinitions[charCode] = tempDef;
2019-11-23 20:27:39 +08:00
}
updateTextureContent(pixelFormat, startY);
2019-11-23 20:27:39 +08:00
return true;
}
void FontAtlas::updateTextureContent(backend::PixelFormat format, int startY)
{
2022-07-16 10:43:05 +08:00
#if !defined(AX_USE_METAL)
auto data = _currentPageData + (CacheTextureWidth * (int)startY << _strideShift);
_atlasTextures[_currentPage]->updateWithSubData(data, 0, startY, CacheTextureWidth,
(int)_currentPageOrigY - startY + _currLineHeight);
#else
2021-12-25 10:04:45 +08:00
unsigned char* data = nullptr;
if (_strideShift)
2019-11-23 20:27:39 +08:00
{
int nLen = CacheTextureWidth * ((int)_currentPageOrigY - startY + _currLineHeight);
2021-12-25 10:04:45 +08:00
data = _currentPageData + CacheTextureWidth * (int)startY * 2;
2019-11-23 20:27:39 +08:00
memset(_currentPageDataRGBA, 0, 4 * nLen);
for (auto i = 0; i < nLen; i++)
{
2021-12-25 10:04:45 +08:00
_currentPageDataRGBA[i * 4] = data[i * 2];
_currentPageDataRGBA[i * 4 + 3] = data[i * 2 + 1];
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
_atlasTextures[_currentPage]->updateWithSubData(_currentPageDataRGBA, 0, startY, CacheTextureWidth,
(int)_currentPageOrigY - startY + _currLineHeight);
2019-11-23 20:27:39 +08:00
}
else
{
data = _currentPageData + CacheTextureWidth * (int)startY;
2021-12-25 10:04:45 +08:00
_atlasTextures[_currentPage]->updateWithSubData(data, 0, startY, CacheTextureWidth,
(int)_currentPageOrigY - startY + _currLineHeight);
2019-11-23 20:27:39 +08:00
}
#endif
}
void FontAtlas::addNewPage()
{
auto texture = new Texture2D();
memset(_currentPageData, 0, _currentPageDataSize);
2022-07-16 10:43:05 +08:00
#if !defined(AX_USE_METAL)
texture->initWithData(_currentPageData, _currentPageDataSize, _pixelFormat, CacheTextureWidth, CacheTextureHeight);
#else
if (_strideShift)
{
memset(_currentPageDataRGBA, 0, _currentPageDataSizeRGBA);
texture->initWithData(_currentPageDataRGBA, _currentPageDataSizeRGBA, backend::PixelFormat::RGBA8,
CacheTextureWidth, CacheTextureHeight);
}
else
{
texture->initWithData(_currentPageData, _currentPageDataSize, _pixelFormat, CacheTextureWidth,
CacheTextureHeight);
}
#endif
if (_antialiasEnabled)
texture->setAntiAliasTexParameters();
else
texture->setAliasTexParameters();
setTexture(++_currentPage, texture);
texture->release();
_currentPageOrigY = 0;
2019-11-23 20:27:39 +08:00
}
void FontAtlas::setTexture(unsigned int slot, Texture2D* texture)
2019-11-23 20:27:39 +08:00
{
texture->retain();
_atlasTextures[slot] = texture;
}
Texture2D* FontAtlas::getTexture(int slot)
{
return _atlasTextures[slot];
}
void FontAtlas::setLineHeight(float newHeight)
{
_lineHeight = newHeight;
}
std::string_view FontAtlas::getFontName() const
2019-11-23 20:27:39 +08:00
{
std::string_view fontName = _fontFreeType ? _fontFreeType->getFontName() : ""sv;
2021-12-25 10:04:45 +08:00
if (fontName.empty())
return fontName;
2019-11-23 20:27:39 +08:00
auto idx = fontName.rfind('/');
2021-12-25 10:04:45 +08:00
if (idx != std::string::npos)
{
return fontName.substr(idx + 1);
}
2019-11-23 20:27:39 +08:00
idx = fontName.rfind('\\');
2021-12-25 10:04:45 +08:00
if (idx != std::string::npos)
{
return fontName.substr(idx + 1);
}
2019-11-23 20:27:39 +08:00
return fontName;
}
void FontAtlas::setAliasTexParameters()
{
if (_antialiasEnabled)
{
_antialiasEnabled = false;
2021-12-25 10:04:45 +08:00
for (const auto& tex : _atlasTextures)
2019-11-23 20:27:39 +08:00
{
tex.second->setAliasTexParameters();
}
}
}
void FontAtlas::setAntiAliasTexParameters()
{
2021-12-25 10:04:45 +08:00
if (!_antialiasEnabled)
2019-11-23 20:27:39 +08:00
{
_antialiasEnabled = true;
2021-12-25 10:04:45 +08:00
for (const auto& tex : _atlasTextures)
2019-11-23 20:27:39 +08:00
{
tex.second->setAntiAliasTexParameters();
}
}
}
NS_AX_END