axmol/cocos/2d/CCFontAtlas.cpp

566 lines
18 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.
2021-11-11 17:20:21 +08:00
Copyright (c) 2021 Bytedance Inc.
https://adxe.org
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/CCFontAtlas.h"
#if CC_TARGET_PLATFORM != CC_PLATFORM_WIN32 && CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID
2021-12-25 10:04:45 +08:00
# include <iconv.h>
2019-11-23 20:27:39 +08:00
#elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
2021-12-25 10:04:45 +08:00
# include "platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
2019-11-23 20:27:39 +08:00
#endif
#include "2d/CCFontFreeType.h"
#include "base/ccUTF8.h"
#include "base/CCDirector.h"
#include "base/CCEventListenerCustom.h"
#include "base/CCEventDispatcher.h"
#include "base/CCEventType.h"
NS_CC_BEGIN
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;
if (_fontFreeType->isDistanceFieldEnabled())
{
2021-12-25 10:04:45 +08:00
_letterPadding += 2 * FontFreeType::DistanceMapSpread;
2019-11-23 20:27:39 +08:00
}
auto outlineSize = _fontFreeType->getOutlineSize();
if (outlineSize > 0)
{
_lineHeight += 2 * outlineSize;
}
#if CC_ENABLE_CACHE_TEXTURE_DATA
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
2021-12-25 10:04:45 +08:00
_rendererRecreatedListener = EventListenerCustom::create(
EVENT_RENDERER_RECREATED, CC_CALLBACK_1(FontAtlas::listenRendererRecreated, this));
2019-11-23 20:27:39 +08:00
eventDispatcher->addEventListenerWithFixedPriority(_rendererRecreatedListener, 1);
#endif
}
}
void FontAtlas::reinit()
{
if (_currentPageData)
{
2021-12-25 10:04:45 +08:00
delete[] _currentPageData;
2019-11-23 20:27:39 +08:00
_currentPageData = nullptr;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE_ARRAY(_currentPageDataRGBA);
2021-12-25 10:04:45 +08:00
2021-12-08 00:11:53 +08:00
auto texture = new Texture2D;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_currentPageDataSize = CacheTextureWidth * CacheTextureHeight;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
auto outlineSize = _fontFreeType->getOutlineSize();
2021-12-25 10:04:45 +08:00
if (outlineSize > 0)
2019-11-23 20:27:39 +08:00
{
_currentPageDataSize *= 2;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_currentPageDataSizeRGBA = _currentPageDataSize * 2;
2021-12-25 10:04:45 +08:00
_currentPageDataRGBA = new unsigned char[_currentPageDataSizeRGBA];
2019-11-23 20:27:39 +08:00
memset(_currentPageDataRGBA, 0, _currentPageDataSizeRGBA);
}
2021-12-25 10:04:45 +08:00
2021-12-08 00:11:53 +08:00
_currentPageData = new unsigned char[_currentPageDataSize];
2019-11-23 20:27:39 +08:00
memset(_currentPageData, 0, _currentPageDataSize);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
initTextureWithZeros(texture);
2021-12-25 10:04:45 +08:00
addTexture(texture, 0);
2019-11-23 20:27:39 +08:00
texture->release();
}
FontAtlas::~FontAtlas()
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
if (_fontFreeType && _rendererRecreatedListener)
{
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->removeEventListener(_rendererRecreatedListener);
_rendererRecreatedListener = nullptr;
}
#endif
_font->release();
releaseTextures();
2021-12-25 10:04:45 +08:00
delete[] _currentPageData;
2019-11-23 20:27:39 +08:00
#if CC_TARGET_PLATFORM != CC_PLATFORM_WIN32 && CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID
if (_iconv)
{
iconv_close(_iconv);
_iconv = nullptr;
}
#endif
}
2021-12-25 10:04:45 +08:00
void FontAtlas::initTextureWithZeros(Texture2D* texture)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
char* zeros = nullptr;
2019-11-23 20:27:39 +08:00
backend::PixelFormat pixelFormat;
float outlineSize = _fontFreeType->getOutlineSize();
2021-12-25 10:04:45 +08:00
size_t zeroBytes = 0;
2019-11-23 20:27:39 +08:00
if (outlineSize > 0)
2021-12-25 10:04:45 +08:00
{
// metal do no support AI88 format
pixelFormat = backend::PixelFormat::RGBA8;
2021-12-25 10:04:45 +08:00
zeroBytes = CacheTextureWidth * CacheTextureWidth * 4;
2019-11-23 20:27:39 +08:00
}
else
{
pixelFormat = backend::PixelFormat::A8;
2021-12-25 10:04:45 +08:00
zeroBytes = CacheTextureWidth * CacheTextureWidth;
2019-11-23 20:27:39 +08:00
}
zeros = new char[zeroBytes]();
2021-12-25 10:04:45 +08:00
// std::fill(zeros, zeros + cnt, 0);
texture->initWithData(zeros, zeroBytes, pixelFormat, CacheTextureWidth, CacheTextureHeight,
Vec2(CacheTextureWidth, CacheTextureHeight));
2019-11-23 20:27:39 +08:00
delete[] zeros;
}
void FontAtlas::reset()
{
releaseTextures();
2021-12-25 10:04:45 +08:00
_currLineHeight = 0;
_currentPage = 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()
{
2021-12-25 10:04:45 +08:00
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;
}
}
2021-12-25 10:04:45 +08:00
void FontAtlas::conversionU32TOGB2312(const std::u32string& u32Text,
std::unordered_map<unsigned int, unsigned int>& charCodeMap)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
size_t strLen = u32Text.length();
2019-11-23 20:27:39 +08:00
auto gb2312StrSize = strLen * 2;
2021-12-25 10:04:45 +08:00
auto gb2312Text = new char[gb2312StrSize];
2019-11-23 20:27:39 +08:00
memset(gb2312Text, 0, gb2312StrSize);
switch (_fontFreeType->getEncoding())
{
case FT_ENCODING_GB2312:
{
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
std::u16string u16Text;
cocos2d::StringUtils::UTF32ToUTF16(u32Text, u16Text);
WideCharToMultiByte(936, NULL, (LPCWCH)u16Text.c_str(), strLen, (LPSTR)gb2312Text, gb2312StrSize, NULL, NULL);
#elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
conversionEncodingJNI((char*)u32Text.c_str(), gb2312StrSize, "UTF-32LE", gb2312Text, "GB2312");
#else
if (_iconv == nullptr)
{
_iconv = iconv_open("GBK//TRANSLIT", "UTF-32LE");
}
if (_iconv == (iconv_t)-1)
{
CCLOG("conversion from utf32 to gb2312 not available");
}
else
{
2021-12-25 10:04:45 +08:00
char* pin = (char*)u32Text.c_str();
char* pout = gb2312Text;
size_t inLen = strLen * 2;
2019-11-23 20:27:39 +08:00
size_t outLen = gb2312StrSize;
iconv(_iconv, (char**)&pin, &inLen, &pout, &outLen);
}
#endif
}
break;
default:
CCLOG("Unsupported encoding:%d", _fontFreeType->getEncoding());
break;
}
unsigned short gb2312Code = 0;
2021-12-25 10:04:45 +08:00
unsigned char* dst = (unsigned char*)&gb2312Code;
2019-11-23 20:27:39 +08:00
char32_t u32Code;
for (size_t index = 0, gbIndex = 0; index < strLen; ++index)
{
u32Code = u32Text[index];
if (u32Code < 256)
{
charCodeMap[u32Code] = u32Code;
gbIndex += 1;
}
else
{
2021-12-25 10:04:45 +08:00
dst[0] = gb2312Text[gbIndex + 1];
dst[1] = gb2312Text[gbIndex];
2019-11-23 20:27:39 +08:00
charCodeMap[u32Code] = gb2312Code;
gbIndex += 2;
}
}
delete[] gb2312Text;
}
2021-12-25 10:04:45 +08:00
void FontAtlas::findNewCharacters(const std::u32string& u32Text,
std::unordered_map<unsigned int, unsigned int>& charCodeMap)
2019-11-23 20:27:39 +08:00
{
std::u32string newChars;
FT_Encoding charEncoding = _fontFreeType->getEncoding();
2021-12-25 10:04:45 +08:00
// find new characters
2019-11-23 20:27:39 +08:00
if (_letterDefinitions.empty())
{
// fixed #16169: new android project crash in android 5.0.2 device (Nexus 7) when use 3.12.
// While using clang compiler with gnustl_static on android, the copy assignment operator of `std::u32string`
// will affect the memory validity, it means after `newChars` is destroyed, the memory of `u32Text` holds
// will be a dead region. `u32text` represents the variable in `Label::_utf32Text`, when somewhere
// allocates memory by `malloc, realloc, new, new[]`, the generated memory address may be the same
// as `Label::_utf32Text` holds. If doing a `memset` or other memory operations, the orignal `Label::_utf32Text`
// will be in an unknown state. Meanwhile, a bunch lots of logic which depends on `Label::_utf32Text`
// will be broken.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// newChars = u32Text;
2021-12-25 10:04:45 +08:00
// Using `append` method is a workaround for this issue. So please be carefuly while using the assignment
// operator of `std::u32string`.
2019-11-23 20:27:39 +08:00
newChars.append(u32Text);
}
else
{
auto length = u32Text.length();
newChars.reserve(length);
for (size_t i = 0; i < length; ++i)
{
auto outIterator = _letterDefinitions.find(u32Text[i]);
if (outIterator == _letterDefinitions.end())
{
newChars.push_back(u32Text[i]);
}
}
}
if (!newChars.empty())
{
switch (charEncoding)
{
case FT_ENCODING_UNICODE:
{
for (auto u32Code : newChars)
{
charCodeMap[u32Code] = u32Code;
}
break;
}
case FT_ENCODING_GB2312:
{
conversionU32TOGB2312(newChars, charCodeMap);
break;
}
default:
CCLOG("FontAtlas::findNewCharacters: Unsupported encoding:%d", charEncoding);
break;
}
}
}
bool FontAtlas::prepareLetterDefinitions(const std::u32string& utf32Text)
{
if (_fontFreeType == nullptr)
{
return false;
}
if (!_currentPageData)
2021-12-25 10:04:45 +08:00
reinit();
2019-11-23 20:27:39 +08:00
std::unordered_map<unsigned int, unsigned int> codeMapOfNewChar;
findNewCharacters(utf32Text, codeMapOfNewChar);
if (codeMapOfNewChar.empty())
{
return false;
}
int adjustForDistanceMap = _letterPadding / 2;
2021-12-25 10:04:45 +08:00
int adjustForExtend = _letterEdgeExtend / 2;
int32_t bitmapWidth;
int32_t bitmapHeight;
2019-11-23 20:27:39 +08:00
int glyphHeight;
Rect tempRect;
FontLetterDefinition tempDef;
auto scaleFactor = CC_CONTENT_SCALE_FACTOR();
2021-12-25 10:04:45 +08:00
auto pixelFormat = _fontFreeType->getOutlineSize() > 0 ? backend::PixelFormat::LA8 : backend::PixelFormat::A8;
2019-11-23 20:27:39 +08:00
int startY = (int)_currentPageOrigY;
for (auto&& it : codeMapOfNewChar)
{
auto bitmap = _fontFreeType->getGlyphBitmap(it.second, bitmapWidth, bitmapHeight, tempRect, tempDef.xAdvance);
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;
_currentPageOrigY = 0;
memset(_currentPageData, 0, _currentPageDataSize);
_currentPage++;
2021-12-08 00:11:53 +08:00
auto tex = new Texture2D;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
initTextureWithZeros(tex);
if (_antialiasEnabled)
{
tex->setAntiAliasTexParameters();
}
else
{
tex->setAliasTexParameters();
}
addTexture(tex, _currentPage);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
tex->release();
}
}
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
{
if (bitmap)
2019-11-23 20:27:39 +08:00
delete[] bitmap;
if (tempDef.xAdvance)
tempDef.validDefinition = true;
else
tempDef.validDefinition = false;
2021-12-25 10:04:45 +08:00
tempDef.width = 0;
tempDef.height = 0;
tempDef.U = 0;
tempDef.V = 0;
tempDef.offsetX = 0;
tempDef.offsetY = 0;
2019-11-23 20:27:39 +08:00
tempDef.textureID = 0;
2021-12-25 10:04:45 +08:00
tempDef.rotated = false;
2019-11-23 20:27:39 +08:00
_currentPageOrigX += 1;
}
_letterDefinitions[it.first] = tempDef;
}
updateTextureContent(pixelFormat, startY);
return true;
}
void FontAtlas::updateTextureContent(backend::PixelFormat format, int startY)
{
2021-12-25 10:04:45 +08:00
unsigned char* data = nullptr;
auto outlineSize = _fontFreeType->getOutlineSize();
if (outlineSize > 0 && format == backend::PixelFormat::LA8)
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
}
}
2021-12-25 10:04:45 +08:00
void FontAtlas::addTexture(Texture2D* texture, int slot)
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;
}
2021-12-26 23:26:34 +08:00
std::string_view FontAtlas::getFontName() const
2019-11-23 20:27:39 +08:00
{
2021-12-26 23:26:34 +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_CC_END