2013-11-07 09:05:13 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 Zynga Inc.
|
2016-08-05 09:42:15 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
|
2013-11-07 09:05:13 +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
|
|
|
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCFontAtlas.h"
|
2015-07-28 16:28:32 +08:00
|
|
|
#if CC_TARGET_PLATFORM != CC_PLATFORM_WIN32 && CC_TARGET_PLATFORM != CC_PLATFORM_WINRT && CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID
|
|
|
|
#include <iconv.h>
|
|
|
|
#elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
|
2015-07-28 16:28:32 +08:00
|
|
|
#endif
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCFontFreeType.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCEventType.h"
|
2013-07-20 01:33:26 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-05-14 16:41:26 +08:00
|
|
|
const int FontAtlas::CacheTextureWidth = 512;
|
|
|
|
const int FontAtlas::CacheTextureHeight = 512;
|
2015-01-21 21:58:18 +08:00
|
|
|
const char* FontAtlas::CMD_PURGE_FONTATLAS = "__cc_PURGE_FONTATLAS";
|
|
|
|
const char* FontAtlas::CMD_RESET_FONTATLAS = "__cc_RESET_FONTATLAS";
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-03-07 11:42:13 +08:00
|
|
|
FontAtlas::FontAtlas(Font &theFont)
|
|
|
|
: _font(&theFont)
|
2015-07-28 16:28:32 +08:00
|
|
|
, _fontFreeType(nullptr)
|
|
|
|
, _iconv(nullptr)
|
2014-03-07 11:42:13 +08:00
|
|
|
, _currentPageData(nullptr)
|
|
|
|
, _fontAscender(0)
|
2014-07-09 23:03:04 +08:00
|
|
|
, _rendererRecreatedListener(nullptr)
|
2014-03-27 15:35:51 +08:00
|
|
|
, _antialiasEnabled(true)
|
2015-07-03 18:08:32 +08:00
|
|
|
, _currLineHeight(0)
|
2013-07-25 01:22:46 +08:00
|
|
|
{
|
2013-10-31 17:52:22 +08:00
|
|
|
_font->retain();
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2015-07-28 16:28:32 +08:00
|
|
|
_fontFreeType = dynamic_cast<FontFreeType*>(_font);
|
|
|
|
if (_fontFreeType)
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_lineHeight = _font->getFontMaxHeight();
|
2015-07-28 16:28:32 +08:00
|
|
|
_fontAscender = _fontFreeType->getFontAscender();
|
2014-08-28 07:31:57 +08:00
|
|
|
auto texture = new (std::nothrow) Texture2D;
|
2013-10-29 20:25:03 +08:00
|
|
|
_currentPage = 0;
|
|
|
|
_currentPageOrigX = 0;
|
|
|
|
_currentPageOrigY = 0;
|
2015-07-30 16:24:47 +08:00
|
|
|
_letterEdgeExtend = 2;
|
2014-01-22 14:57:11 +08:00
|
|
|
_letterPadding = 0;
|
2014-01-21 10:36:32 +08:00
|
|
|
|
2015-07-28 16:28:32 +08:00
|
|
|
if (_fontFreeType->isDistanceFieldEnabled())
|
2013-12-13 12:42:15 +08:00
|
|
|
{
|
2014-01-21 10:36:32 +08:00
|
|
|
_letterPadding += 2 * FontFreeType::DistanceMapSpread;
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
_currentPageDataSize = CacheTextureWidth * CacheTextureHeight;
|
2015-07-28 16:28:32 +08:00
|
|
|
auto outlineSize = _fontFreeType->getOutlineSize();
|
2014-09-12 16:03:01 +08:00
|
|
|
if(outlineSize > 0)
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_lineHeight += 2 * outlineSize;
|
2014-03-05 15:54:40 +08:00
|
|
|
_currentPageDataSize *= 2;
|
2015-07-03 18:08:32 +08:00
|
|
|
}
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2015-12-16 14:02:55 +08:00
|
|
|
_currentPageData = new (std::nothrow) unsigned char[_currentPageDataSize];
|
2014-03-27 15:11:04 +08:00
|
|
|
memset(_currentPageData, 0, _currentPageDataSize);
|
|
|
|
|
2014-09-12 16:03:01 +08:00
|
|
|
auto pixelFormat = outlineSize > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8;
|
2014-03-27 15:11:04 +08:00
|
|
|
texture->initWithData(_currentPageData, _currentPageDataSize,
|
|
|
|
pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) );
|
|
|
|
|
2014-03-05 16:51:16 +08:00
|
|
|
addTexture(texture,0);
|
2014-03-05 15:54:40 +08:00
|
|
|
texture->release();
|
2014-07-09 23:03:04 +08:00
|
|
|
|
2014-03-14 14:59:26 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
|
2014-07-09 23:03:04 +08:00
|
|
|
|
|
|
|
_rendererRecreatedListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(FontAtlas::listenRendererRecreated, this));
|
|
|
|
eventDispatcher->addEventListenerWithFixedPriority(_rendererRecreatedListener, 1);
|
2014-03-14 14:59:26 +08:00
|
|
|
#endif
|
2014-01-21 10:36:32 +08:00
|
|
|
}
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FontAtlas::~FontAtlas()
|
|
|
|
{
|
2014-03-14 14:59:26 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2015-07-28 16:28:32 +08:00
|
|
|
if (_fontFreeType && _rendererRecreatedListener)
|
2014-03-14 14:59:26 +08:00
|
|
|
{
|
|
|
|
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
|
2014-07-09 23:03:04 +08:00
|
|
|
eventDispatcher->removeEventListener(_rendererRecreatedListener);
|
|
|
|
_rendererRecreatedListener = nullptr;
|
2014-03-14 14:59:26 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-31 17:52:22 +08:00
|
|
|
_font->release();
|
2015-11-06 16:36:24 +08:00
|
|
|
releaseTextures();
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2013-10-31 17:52:22 +08:00
|
|
|
delete []_currentPageData;
|
2015-07-28 16:28:32 +08:00
|
|
|
|
|
|
|
#if CC_TARGET_PLATFORM != CC_PLATFORM_WIN32 && CC_TARGET_PLATFORM != CC_PLATFORM_WINRT && CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID
|
|
|
|
if (_iconv)
|
|
|
|
{
|
|
|
|
iconv_close(_iconv);
|
|
|
|
_iconv = nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
|
2016-03-24 23:05:22 +08:00
|
|
|
void FontAtlas::reset()
|
|
|
|
{
|
|
|
|
releaseTextures();
|
|
|
|
|
|
|
|
_currLineHeight = 0;
|
|
|
|
_currentPage = 0;
|
|
|
|
_currentPageOrigX = 0;
|
|
|
|
_currentPageOrigY = 0;
|
|
|
|
_letterDefinitions.clear();
|
|
|
|
}
|
|
|
|
|
2015-11-06 16:36:24 +08:00
|
|
|
void FontAtlas::releaseTextures()
|
2013-07-25 01:22:46 +08:00
|
|
|
{
|
|
|
|
for( auto &item: _atlasTextures)
|
|
|
|
{
|
2013-08-06 07:41:23 +08:00
|
|
|
item.second->release();
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
2014-03-14 14:59:26 +08:00
|
|
|
_atlasTextures.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FontAtlas::purgeTexturesAtlas()
|
|
|
|
{
|
2016-03-24 23:05:22 +08:00
|
|
|
if (_fontFreeType)
|
2014-03-14 14:59:26 +08:00
|
|
|
{
|
2016-03-24 23:05:22 +08:00
|
|
|
reset();
|
2014-03-14 14:59:26 +08:00
|
|
|
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
|
2015-01-21 21:58:18 +08:00
|
|
|
eventDispatcher->dispatchCustomEvent(CMD_PURGE_FONTATLAS,this);
|
|
|
|
eventDispatcher->dispatchCustomEvent(CMD_RESET_FONTATLAS,this);
|
2014-03-14 14:59:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 23:03:04 +08:00
|
|
|
void FontAtlas::listenRendererRecreated(EventCustom *event)
|
2014-03-14 14:59:26 +08:00
|
|
|
{
|
2016-03-24 23:05:22 +08:00
|
|
|
purgeTexturesAtlas();
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
void FontAtlas::addLetterDefinition(char16_t utf16Char, const FontLetterDefinition &letterDefinition)
|
2013-07-20 01:33:26 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_letterDefinitions[utf16Char] = letterDefinition;
|
2013-07-20 01:33:26 +08:00
|
|
|
}
|
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
void FontAtlas::scaleFontLetterDefinition(float scaleFactor)
|
|
|
|
{
|
|
|
|
for (auto&& fontDefinition : _letterDefinitions) {
|
2015-11-26 12:03:18 +08:00
|
|
|
auto& letterDefinition = fontDefinition.second;
|
2015-11-19 16:06:13 +08:00
|
|
|
letterDefinition.width *= scaleFactor;
|
|
|
|
letterDefinition.height *= scaleFactor;
|
|
|
|
letterDefinition.offsetX *= scaleFactor;
|
|
|
|
letterDefinition.offsetY *= scaleFactor;
|
|
|
|
letterDefinition.xAdvance *= scaleFactor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
bool FontAtlas::getLetterDefinitionForChar(char16_t utf16Char, FontLetterDefinition &letterDefinition)
|
2013-07-20 01:33:26 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
auto outIterator = _letterDefinitions.find(utf16Char);
|
2014-03-27 15:35:51 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
if (outIterator != _letterDefinitions.end())
|
2013-07-30 04:43:23 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
letterDefinition = (*outIterator).second;
|
2015-08-05 23:46:41 +08:00
|
|
|
return letterDefinition.validDefinition;
|
2013-07-30 04:43:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-06 08:49:20 +08:00
|
|
|
return false;
|
2013-07-30 04:43:23 +08:00
|
|
|
}
|
2013-07-20 01:33:26 +08:00
|
|
|
}
|
|
|
|
|
2015-07-28 16:28:32 +08:00
|
|
|
void FontAtlas::conversionU16TOGB2312(const std::u16string& u16Text, std::unordered_map<unsigned short, unsigned short>& charCodeMap)
|
|
|
|
{
|
|
|
|
size_t strLen = u16Text.length();
|
|
|
|
auto gb2312StrSize = strLen * 2;
|
|
|
|
auto gb2312Text = new (std::nothrow) char[gb2312StrSize];
|
|
|
|
memset(gb2312Text, 0, gb2312StrSize);
|
|
|
|
|
|
|
|
switch (_fontFreeType->getEncoding())
|
|
|
|
{
|
|
|
|
case FT_ENCODING_GB2312:
|
|
|
|
{
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
|
|
|
|
WideCharToMultiByte(936, NULL, (LPCWCH)u16Text.c_str(), strLen, (LPSTR)gb2312Text, gb2312StrSize, NULL, NULL);
|
|
|
|
#elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
|
|
|
|
conversionEncodingJNI((char*)u16Text.c_str(), gb2312StrSize, "UTF-16LE", gb2312Text, "GB2312");
|
|
|
|
#else
|
|
|
|
if (_iconv == nullptr)
|
|
|
|
{
|
|
|
|
_iconv = iconv_open("gb2312", "utf-16le");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_iconv == (iconv_t)-1)
|
|
|
|
{
|
|
|
|
CCLOG("conversion from utf16 to gb2312 not available");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char* pin = (char*)u16Text.c_str();
|
|
|
|
char* pout = gb2312Text;
|
|
|
|
size_t inLen = strLen * 2;
|
|
|
|
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;
|
|
|
|
unsigned char* dst = (unsigned char*)&gb2312Code;
|
|
|
|
unsigned short u16Code;
|
|
|
|
for (size_t index = 0, gbIndex = 0; index < strLen; ++index)
|
|
|
|
{
|
|
|
|
u16Code = u16Text[index];
|
|
|
|
if (u16Code < 256)
|
|
|
|
{
|
|
|
|
charCodeMap[u16Code] = u16Code;
|
|
|
|
gbIndex += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dst[0] = gb2312Text[gbIndex + 1];
|
|
|
|
dst[1] = gb2312Text[gbIndex];
|
|
|
|
charCodeMap[u16Code] = gb2312Code;
|
|
|
|
|
|
|
|
gbIndex += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] gb2312Text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FontAtlas::findNewCharacters(const std::u16string& u16Text, std::unordered_map<unsigned short, unsigned short>& charCodeMap)
|
|
|
|
{
|
|
|
|
std::u16string newChars;
|
|
|
|
FT_Encoding charEncoding = _fontFreeType->getEncoding();
|
|
|
|
|
|
|
|
//find new characters
|
|
|
|
if (_letterDefinitions.empty())
|
|
|
|
{
|
2016-07-28 11:19:22 +08:00
|
|
|
// 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::u16string`
|
|
|
|
// will affect the memory validity, it means after `newChars` is destroyed, the memory of `u16Text` holds
|
|
|
|
// will be a dead region. `u16text` represents the variable in `Label::_utf16Text`, when somewhere
|
|
|
|
// allocates memory by `malloc, realloc, new, new[]`, the generated memory address may be the same
|
|
|
|
// as `Label::_utf16Text` holds. If doing a `memset` or other memory operations, the orignal `Label::_utf16Text`
|
|
|
|
// will be in an unknown state. Meanwhile, a bunch lots of logic which depends on `Label::_utf16Text`
|
|
|
|
// will be broken.
|
|
|
|
|
|
|
|
// newChars = u16Text;
|
|
|
|
|
|
|
|
// Using `append` method is a workaround for this issue. So please be carefuly while using the assignment operator
|
|
|
|
// of `std::u16string`.
|
|
|
|
newChars.append(u16Text);
|
2015-07-28 16:28:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto length = u16Text.length();
|
2015-08-17 22:46:19 +08:00
|
|
|
newChars.reserve(length);
|
2015-07-28 16:28:32 +08:00
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
auto outIterator = _letterDefinitions.find(u16Text[i]);
|
|
|
|
if (outIterator == _letterDefinitions.end())
|
|
|
|
{
|
|
|
|
newChars.push_back(u16Text[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newChars.empty())
|
|
|
|
{
|
|
|
|
switch (charEncoding)
|
|
|
|
{
|
|
|
|
case FT_ENCODING_UNICODE:
|
|
|
|
{
|
|
|
|
for (auto u16Code : newChars)
|
|
|
|
{
|
|
|
|
charCodeMap[u16Code] = u16Code;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FT_ENCODING_GB2312:
|
|
|
|
{
|
|
|
|
conversionU16TOGB2312(newChars, charCodeMap);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
CCLOG("FontAtlas::findNewCharacters: Unsupported encoding:%d", charEncoding);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FontAtlas::prepareLetterDefinitions(const std::u16string& utf16Text)
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
2015-07-28 16:28:32 +08:00
|
|
|
if (_fontFreeType == nullptr)
|
|
|
|
{
|
2013-10-29 20:25:03 +08:00
|
|
|
return false;
|
2015-07-28 16:28:32 +08:00
|
|
|
}
|
2014-07-09 23:03:04 +08:00
|
|
|
|
2015-07-28 16:28:32 +08:00
|
|
|
std::unordered_map<unsigned short, unsigned short> codeMapOfNewChar;
|
|
|
|
findNewCharacters(utf16Text, codeMapOfNewChar);
|
|
|
|
if (codeMapOfNewChar.empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2015-07-30 16:24:47 +08:00
|
|
|
int adjustForDistanceMap = _letterPadding / 2;
|
|
|
|
int adjustForExtend = _letterEdgeExtend / 2;
|
2014-03-20 11:58:36 +08:00
|
|
|
long bitmapWidth;
|
|
|
|
long bitmapHeight;
|
2016-02-02 11:16:36 +08:00
|
|
|
int glyphHeight;
|
2014-03-05 15:54:40 +08:00
|
|
|
Rect tempRect;
|
|
|
|
FontLetterDefinition tempDef;
|
|
|
|
|
|
|
|
auto scaleFactor = CC_CONTENT_SCALE_FACTOR();
|
2015-07-28 16:28:32 +08:00
|
|
|
auto pixelFormat = _fontFreeType->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8;
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-03-27 15:11:04 +08:00
|
|
|
float startY = _currentPageOrigY;
|
2014-03-24 14:16:27 +08:00
|
|
|
|
2015-07-28 16:28:32 +08:00
|
|
|
for (auto&& it : codeMapOfNewChar)
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
2015-07-28 16:28:32 +08:00
|
|
|
auto bitmap = _fontFreeType->getGlyphBitmap(it.second, bitmapWidth, bitmapHeight, tempRect, tempDef.xAdvance);
|
|
|
|
if (bitmap && bitmapWidth > 0 && bitmapHeight > 0)
|
|
|
|
{
|
|
|
|
tempDef.validDefinition = true;
|
2015-07-30 16:24:47 +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;
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2015-07-28 16:28:32 +08:00
|
|
|
if (_currentPageOrigX + tempDef.width > CacheTextureWidth)
|
|
|
|
{
|
|
|
|
_currentPageOrigY += _currLineHeight;
|
|
|
|
_currLineHeight = 0;
|
|
|
|
_currentPageOrigX = 0;
|
2016-03-02 08:06:45 +08:00
|
|
|
if (_currentPageOrigY + _lineHeight + _letterPadding + _letterEdgeExtend >= CacheTextureHeight)
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
2015-07-28 16:28:32 +08:00
|
|
|
unsigned char *data = nullptr;
|
|
|
|
if (pixelFormat == Texture2D::PixelFormat::AI88)
|
|
|
|
{
|
|
|
|
data = _currentPageData + CacheTextureWidth * (int)startY * 2;
|
|
|
|
}
|
|
|
|
else
|
2014-06-09 10:12:22 +08:00
|
|
|
{
|
2015-07-28 16:28:32 +08:00
|
|
|
data = _currentPageData + CacheTextureWidth * (int)startY;
|
|
|
|
}
|
|
|
|
_atlasTextures[_currentPage]->updateWithData(data, 0, startY,
|
|
|
|
CacheTextureWidth, CacheTextureHeight - startY);
|
|
|
|
|
|
|
|
startY = 0.0f;
|
|
|
|
|
|
|
|
_currentPageOrigY = 0;
|
|
|
|
memset(_currentPageData, 0, _currentPageDataSize);
|
|
|
|
_currentPage++;
|
|
|
|
auto tex = new (std::nothrow) Texture2D;
|
|
|
|
if (_antialiasEnabled)
|
|
|
|
{
|
|
|
|
tex->setAntiAliasTexParameters();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tex->setAliasTexParameters();
|
|
|
|
}
|
|
|
|
tex->initWithData(_currentPageData, _currentPageDataSize,
|
|
|
|
pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth, CacheTextureHeight));
|
|
|
|
addTexture(tex, _currentPage);
|
|
|
|
tex->release();
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
2013-10-29 20:25:03 +08:00
|
|
|
}
|
2016-03-02 08:06:45 +08:00
|
|
|
glyphHeight = static_cast<int>(bitmapHeight) + _letterPadding + _letterEdgeExtend;
|
|
|
|
if (glyphHeight > _currLineHeight)
|
|
|
|
{
|
|
|
|
_currLineHeight = glyphHeight;
|
|
|
|
}
|
2015-07-30 16:24:47 +08:00
|
|
|
_fontFreeType->renderCharAt(_currentPageData, _currentPageOrigX + adjustForExtend, _currentPageOrigY + adjustForExtend, bitmap, bitmapWidth, bitmapHeight);
|
2015-07-28 16:28:32 +08:00
|
|
|
|
|
|
|
tempDef.U = _currentPageOrigX;
|
|
|
|
tempDef.V = _currentPageOrigY;
|
|
|
|
tempDef.textureID = _currentPage;
|
|
|
|
_currentPageOrigX += tempDef.width + 1;
|
|
|
|
// take from pixels to points
|
|
|
|
tempDef.width = tempDef.width / scaleFactor;
|
|
|
|
tempDef.height = tempDef.height / scaleFactor;
|
|
|
|
tempDef.U = tempDef.U / scaleFactor;
|
|
|
|
tempDef.V = tempDef.V / scaleFactor;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
if (tempDef.xAdvance)
|
|
|
|
tempDef.validDefinition = true;
|
|
|
|
else
|
|
|
|
tempDef.validDefinition = false;
|
|
|
|
|
|
|
|
tempDef.width = 0;
|
|
|
|
tempDef.height = 0;
|
|
|
|
tempDef.U = 0;
|
|
|
|
tempDef.V = 0;
|
|
|
|
tempDef.offsetX = 0;
|
|
|
|
tempDef.offsetY = 0;
|
|
|
|
tempDef.textureID = 0;
|
|
|
|
_currentPageOrigX += 1;
|
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2015-07-28 16:28:32 +08:00
|
|
|
_letterDefinitions[it.first] = tempDef;
|
2013-10-29 20:25:03 +08:00
|
|
|
}
|
|
|
|
|
2015-07-28 16:28:32 +08:00
|
|
|
unsigned char *data = nullptr;
|
|
|
|
if (pixelFormat == Texture2D::PixelFormat::AI88)
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
2015-07-28 16:28:32 +08:00
|
|
|
data = _currentPageData + CacheTextureWidth * (int)startY * 2;
|
2013-10-29 20:25:03 +08:00
|
|
|
}
|
2015-07-28 16:28:32 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
data = _currentPageData + CacheTextureWidth * (int)startY;
|
|
|
|
}
|
2016-03-02 08:06:45 +08:00
|
|
|
_atlasTextures[_currentPage]->updateWithData(data, 0, startY, CacheTextureWidth, _currentPageOrigY - startY + _currLineHeight);
|
2015-07-28 16:28:32 +08:00
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-05 16:51:16 +08:00
|
|
|
void FontAtlas::addTexture(Texture2D *texture, int slot)
|
2013-07-20 01:33:26 +08:00
|
|
|
{
|
2014-03-05 16:51:16 +08:00
|
|
|
texture->retain();
|
|
|
|
_atlasTextures[slot] = texture;
|
2013-07-20 01:33:26 +08:00
|
|
|
}
|
|
|
|
|
2014-03-05 16:51:16 +08:00
|
|
|
Texture2D* FontAtlas::getTexture(int slot)
|
2013-07-20 01:33:26 +08:00
|
|
|
{
|
2014-03-05 16:51:16 +08:00
|
|
|
return _atlasTextures[slot];
|
2013-07-20 01:33:26 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
void FontAtlas::setLineHeight(float newHeight)
|
2013-07-25 01:22:46 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_lineHeight = newHeight;
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
|
2014-03-27 15:35:51 +08:00
|
|
|
void FontAtlas::setAliasTexParameters()
|
|
|
|
{
|
|
|
|
if (_antialiasEnabled)
|
|
|
|
{
|
|
|
|
_antialiasEnabled = false;
|
|
|
|
for (const auto & tex : _atlasTextures)
|
|
|
|
{
|
|
|
|
tex.second->setAliasTexParameters();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FontAtlas::setAntiAliasTexParameters()
|
|
|
|
{
|
|
|
|
if (! _antialiasEnabled)
|
|
|
|
{
|
|
|
|
_antialiasEnabled = true;
|
|
|
|
for (const auto & tex : _atlasTextures)
|
|
|
|
{
|
|
|
|
tex.second->setAntiAliasTexParameters();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 15:03:16 +08:00
|
|
|
NS_CC_END
|