axmol/cocos/2d/CCFontAtlas.cpp

489 lines
16 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2013 Zynga Inc.
2016-08-05 09:42:15 +08:00
Copyright (c) 2013-2016 Chukong Technologies Inc.
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.
****************************************************************************/
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/CCFontAtlas.h"
#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
#include "platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#endif
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/CCFontFreeType.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
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;
const char* FontAtlas::CMD_PURGE_FONTATLAS = "__cc_PURGE_FONTATLAS";
const char* FontAtlas::CMD_RESET_FONTATLAS = "__cc_RESET_FONTATLAS";
FontAtlas::FontAtlas(Font &theFont)
: _font(&theFont)
, _fontFreeType(nullptr)
, _iconv(nullptr)
, _currentPageData(nullptr)
, _fontAscender(0)
, _rendererRecreatedListener(nullptr)
, _antialiasEnabled(true)
, _currLineHeight(0)
{
2013-10-31 17:52:22 +08:00
_font->retain();
_fontFreeType = dynamic_cast<FontFreeType*>(_font);
if (_fontFreeType)
{
_lineHeight = _font->getFontMaxHeight();
_fontAscender = _fontFreeType->getFontAscender();
auto texture = new (std::nothrow) Texture2D;
_currentPage = 0;
_currentPageOrigX = 0;
_currentPageOrigY = 0;
_letterEdgeExtend = 2;
_letterPadding = 0;
if (_fontFreeType->isDistanceFieldEnabled())
{
_letterPadding += 2 * FontFreeType::DistanceMapSpread;
}
_currentPageDataSize = CacheTextureWidth * CacheTextureHeight;
auto outlineSize = _fontFreeType->getOutlineSize();
if(outlineSize > 0)
{
_lineHeight += 2 * outlineSize;
_currentPageDataSize *= 2;
}
_currentPageData = new (std::nothrow) unsigned char[_currentPageDataSize];
memset(_currentPageData, 0, _currentPageDataSize);
auto pixelFormat = outlineSize > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8;
texture->initWithData(_currentPageData, _currentPageDataSize,
pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) );
addTexture(texture,0);
texture->release();
#if CC_ENABLE_CACHE_TEXTURE_DATA
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
_rendererRecreatedListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(FontAtlas::listenRendererRecreated, this));
eventDispatcher->addEventListenerWithFixedPriority(_rendererRecreatedListener, 1);
#endif
}
}
FontAtlas::~FontAtlas()
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
if (_fontFreeType && _rendererRecreatedListener)
{
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->removeEventListener(_rendererRecreatedListener);
_rendererRecreatedListener = nullptr;
}
#endif
2013-10-31 17:52:22 +08:00
_font->release();
2015-11-06 16:36:24 +08:00
releaseTextures();
2013-10-31 17:52:22 +08:00
delete []_currentPageData;
#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
}
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()
{
for( auto &item: _atlasTextures)
{
2013-08-06 07:41:23 +08:00
item.second->release();
}
_atlasTextures.clear();
}
void FontAtlas::purgeTexturesAtlas()
{
2016-03-24 23:05:22 +08:00
if (_fontFreeType)
{
2016-03-24 23:05:22 +08:00
reset();
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->dispatchCustomEvent(CMD_PURGE_FONTATLAS,this);
eventDispatcher->dispatchCustomEvent(CMD_RESET_FONTATLAS,this);
}
}
void FontAtlas::listenRendererRecreated(EventCustom *event)
{
2016-03-24 23:05:22 +08:00
purgeTexturesAtlas();
}
void FontAtlas::addLetterDefinition(char16_t utf16Char, const FontLetterDefinition &letterDefinition)
2013-07-20 01:33:26 +08:00
{
_letterDefinitions[utf16Char] = letterDefinition;
2013-07-20 01:33:26 +08:00
}
Add Label Overflow and wrap option. Squashed commit of the following: commit 7176582def3686f78701f92ead4d0976bccd115d Author: zilongshanren <guanghui8827@126.com> Date: Thu Nov 19 10:30:54 2015 +0800 add char map font test commit 7ea7c251ff549e4a74d5c5c3faa49893fe5defc4 Author: zilongshanren <guanghui8827@126.com> Date: Wed Nov 18 15:00:17 2015 +0800 system font overflow is not supported commit a02fe0c7f94692b95a0bfd6f194a94c4519cee3a Author: zilongshanren <guanghui8827@126.com> Date: Wed Nov 18 11:04:15 2015 +0800 add system font test commit eb098da8b16f485c86929a9b2ba00a64068264e9 Author: zilongshanren <guanghui8827@126.com> Date: Tue Nov 17 10:29:35 2015 +0800 Finish resize height mode commit ca78ba35abdbacc23bf6f8315454ec0a356a6aca Author: zilongshanren <guanghui8827@126.com> Date: Mon Nov 16 11:01:27 2015 +0800 fix wrap issue commit baf1a5e0dc3a08da0a13500aea5afc82017b054a Author: zilongshanren <guanghui8827@126.com> Date: Sat Nov 14 08:28:57 2015 +0800 horizontal shrink still has a issue commit 7e8abc0990cdf299d23c8072c5c1d4aa561bf798 Author: zilongshanren <guanghui8827@126.com> Date: Fri Nov 13 11:42:34 2015 +0800 finish shrink without wrap commit c9c45bd1f304ba00a6f9a274fa5631659a075bc1 Author: zilongshanren <guanghui8827@126.com> Date: Fri Nov 13 10:09:46 2015 +0800 update shrink commit ad82010ea846679e4d1a09f5127be29ae2e6adae Author: zilongshanren <guanghui8827@126.com> Date: Thu Nov 12 17:55:15 2015 +0800 refactor shrink logic commit effc1ee1cd9f06eaa4f46dd5c2a1401f6a285af6 Author: zilongshanren <guanghui8827@126.com> Date: Wed Nov 11 18:25:34 2015 +0800 add resize overflow type commit 194d552adc2a4ba2c439eff969b082556315e0ae Author: zilongshanren <guanghui8827@126.com> Date: Wed Nov 11 11:49:51 2015 +0800 no wrap, shrink is working. commit e2222ef14d5392817bd727895b3c2caec5ce4faf Author: zilongshanren <guanghui8827@126.com> Date: Tue Nov 10 16:16:27 2015 +0800 add overflow type to label. 1. optimize the testcase commit b65e2de4bfb3cb86a0d0cfa118be891a3f9fe858 Author: zilongshanren <guanghui8827@126.com> Date: Mon Nov 9 17:22:34 2015 +0800 finish bmfont wrap by char commit f339ff0309dc9e2ca2871f03f4551d329afce5e1 Author: zilongshanren <guanghui8827@126.com> Date: Mon Nov 9 17:04:51 2015 +0800 finish bmfont wrap by word commit 4c8edc65204a1294ac81315e72f1299acbdf43c0 Author: zilongshanren <guanghui8827@126.com> Date: Mon Nov 9 11:29:21 2015 +0800 add BMFont size support commit 3282481bda9dcb0f88b41f2568f7e5c856af6d53 Author: zilongshanren <guanghui8827@126.com> Date: Mon Nov 9 09:58:01 2015 +0800 add font size support for BMFONT commit 0c8e2a0374983625507a183cb2eb53a0b1709e25 Author: zilongshanren <guanghui8827@126.com> Date: Fri Nov 6 16:36:27 2015 +0800 add toggle wrap option. 1. make hirozontal clamp working commit 19dcd3e80a34c54b20934d08937eb270e689a7db Author: zilongshanren <guanghui8827@126.com> Date: Fri Nov 6 15:12:59 2015 +0800 tweak label layout commit e36a40babf4817ea7e9830614ac7560f2e69c583 Author: zilongshanren <guanghui8827@126.com> Date: Thu Nov 5 11:11:02 2015 +0800 finish the test skeleton commit a144a3a5d388d15c4f37cbfc12563eccabffb8e1 Author: zilongshanren <guanghui8827@126.com> Date: Wed Nov 4 16:38:54 2015 +0800 add setFontSize api to label. 1. add new chinese-english bmfont 2. beautify base test commit b22551c9eb3738fbb1c38ae4a3bfd07a202dcaf0 Author: zilongshanren <guanghui8827@126.com> Date: Wed Nov 4 11:27:18 2015 +0800 add label layout test skeleton
2015-11-19 16:06:13 +08:00
void FontAtlas::scaleFontLetterDefinition(float scaleFactor)
{
for (auto&& fontDefinition : _letterDefinitions) {
auto& letterDefinition = fontDefinition.second;
Add Label Overflow and wrap option. Squashed commit of the following: commit 7176582def3686f78701f92ead4d0976bccd115d Author: zilongshanren <guanghui8827@126.com> Date: Thu Nov 19 10:30:54 2015 +0800 add char map font test commit 7ea7c251ff549e4a74d5c5c3faa49893fe5defc4 Author: zilongshanren <guanghui8827@126.com> Date: Wed Nov 18 15:00:17 2015 +0800 system font overflow is not supported commit a02fe0c7f94692b95a0bfd6f194a94c4519cee3a Author: zilongshanren <guanghui8827@126.com> Date: Wed Nov 18 11:04:15 2015 +0800 add system font test commit eb098da8b16f485c86929a9b2ba00a64068264e9 Author: zilongshanren <guanghui8827@126.com> Date: Tue Nov 17 10:29:35 2015 +0800 Finish resize height mode commit ca78ba35abdbacc23bf6f8315454ec0a356a6aca Author: zilongshanren <guanghui8827@126.com> Date: Mon Nov 16 11:01:27 2015 +0800 fix wrap issue commit baf1a5e0dc3a08da0a13500aea5afc82017b054a Author: zilongshanren <guanghui8827@126.com> Date: Sat Nov 14 08:28:57 2015 +0800 horizontal shrink still has a issue commit 7e8abc0990cdf299d23c8072c5c1d4aa561bf798 Author: zilongshanren <guanghui8827@126.com> Date: Fri Nov 13 11:42:34 2015 +0800 finish shrink without wrap commit c9c45bd1f304ba00a6f9a274fa5631659a075bc1 Author: zilongshanren <guanghui8827@126.com> Date: Fri Nov 13 10:09:46 2015 +0800 update shrink commit ad82010ea846679e4d1a09f5127be29ae2e6adae Author: zilongshanren <guanghui8827@126.com> Date: Thu Nov 12 17:55:15 2015 +0800 refactor shrink logic commit effc1ee1cd9f06eaa4f46dd5c2a1401f6a285af6 Author: zilongshanren <guanghui8827@126.com> Date: Wed Nov 11 18:25:34 2015 +0800 add resize overflow type commit 194d552adc2a4ba2c439eff969b082556315e0ae Author: zilongshanren <guanghui8827@126.com> Date: Wed Nov 11 11:49:51 2015 +0800 no wrap, shrink is working. commit e2222ef14d5392817bd727895b3c2caec5ce4faf Author: zilongshanren <guanghui8827@126.com> Date: Tue Nov 10 16:16:27 2015 +0800 add overflow type to label. 1. optimize the testcase commit b65e2de4bfb3cb86a0d0cfa118be891a3f9fe858 Author: zilongshanren <guanghui8827@126.com> Date: Mon Nov 9 17:22:34 2015 +0800 finish bmfont wrap by char commit f339ff0309dc9e2ca2871f03f4551d329afce5e1 Author: zilongshanren <guanghui8827@126.com> Date: Mon Nov 9 17:04:51 2015 +0800 finish bmfont wrap by word commit 4c8edc65204a1294ac81315e72f1299acbdf43c0 Author: zilongshanren <guanghui8827@126.com> Date: Mon Nov 9 11:29:21 2015 +0800 add BMFont size support commit 3282481bda9dcb0f88b41f2568f7e5c856af6d53 Author: zilongshanren <guanghui8827@126.com> Date: Mon Nov 9 09:58:01 2015 +0800 add font size support for BMFONT commit 0c8e2a0374983625507a183cb2eb53a0b1709e25 Author: zilongshanren <guanghui8827@126.com> Date: Fri Nov 6 16:36:27 2015 +0800 add toggle wrap option. 1. make hirozontal clamp working commit 19dcd3e80a34c54b20934d08937eb270e689a7db Author: zilongshanren <guanghui8827@126.com> Date: Fri Nov 6 15:12:59 2015 +0800 tweak label layout commit e36a40babf4817ea7e9830614ac7560f2e69c583 Author: zilongshanren <guanghui8827@126.com> Date: Thu Nov 5 11:11:02 2015 +0800 finish the test skeleton commit a144a3a5d388d15c4f37cbfc12563eccabffb8e1 Author: zilongshanren <guanghui8827@126.com> Date: Wed Nov 4 16:38:54 2015 +0800 add setFontSize api to label. 1. add new chinese-english bmfont 2. beautify base test commit b22551c9eb3738fbb1c38ae4a3bfd07a202dcaf0 Author: zilongshanren <guanghui8827@126.com> Date: Wed Nov 4 11:27:18 2015 +0800 add label layout test skeleton
2015-11-19 16:06:13 +08:00
letterDefinition.width *= scaleFactor;
letterDefinition.height *= scaleFactor;
letterDefinition.offsetX *= scaleFactor;
letterDefinition.offsetY *= scaleFactor;
letterDefinition.xAdvance *= scaleFactor;
}
}
bool FontAtlas::getLetterDefinitionForChar(char16_t utf16Char, FontLetterDefinition &letterDefinition)
2013-07-20 01:33:26 +08:00
{
auto outIterator = _letterDefinitions.find(utf16Char);
if (outIterator != _letterDefinitions.end())
{
letterDefinition = (*outIterator).second;
return letterDefinition.validDefinition;
}
else
{
2013-08-06 08:49:20 +08:00
return false;
}
2013-07-20 01:33:26 +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())
{
// 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 original `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 carefully while using the assignment operator
// of `std::u16string`.
newChars.append(u16Text);
}
else
{
auto length = u16Text.length();
newChars.reserve(length);
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)
{
if (_fontFreeType == nullptr)
{
return false;
}
std::unordered_map<unsigned short, unsigned short> codeMapOfNewChar;
findNewCharacters(utf16Text, codeMapOfNewChar);
if (codeMapOfNewChar.empty())
{
return false;
}
int adjustForDistanceMap = _letterPadding / 2;
int adjustForExtend = _letterEdgeExtend / 2;
2014-03-20 11:58:36 +08:00
long bitmapWidth;
long bitmapHeight;
int glyphHeight;
Rect tempRect;
FontLetterDefinition tempDef;
auto scaleFactor = CC_CONTENT_SCALE_FACTOR();
auto pixelFormat = _fontFreeType->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8;
float startY = _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;
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;
if (_currentPageOrigX + tempDef.width > CacheTextureWidth)
{
_currentPageOrigY += _currLineHeight;
_currLineHeight = 0;
_currentPageOrigX = 0;
if (_currentPageOrigY + _lineHeight + _letterPadding + _letterEdgeExtend >= CacheTextureHeight)
{
unsigned char *data = nullptr;
if (pixelFormat == Texture2D::PixelFormat::AI88)
{
data = _currentPageData + CacheTextureWidth * (int)startY * 2;
}
else
{
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();
}
}
glyphHeight = static_cast<int>(bitmapHeight) + _letterPadding + _letterEdgeExtend;
if (glyphHeight > _currLineHeight)
{
_currLineHeight = glyphHeight;
}
_fontFreeType->renderCharAt(_currentPageData, _currentPageOrigX + adjustForExtend, _currentPageOrigY + adjustForExtend, bitmap, bitmapWidth, bitmapHeight);
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;
}
_letterDefinitions[it.first] = tempDef;
}
unsigned char *data = nullptr;
if (pixelFormat == Texture2D::PixelFormat::AI88)
{
data = _currentPageData + CacheTextureWidth * (int)startY * 2;
}
else
{
data = _currentPageData + CacheTextureWidth * (int)startY;
}
_atlasTextures[_currentPage]->updateWithData(data, 0, startY, CacheTextureWidth, _currentPageOrigY - startY + _currLineHeight);
return true;
}
void FontAtlas::addTexture(Texture2D *texture, int slot)
2013-07-20 01:33:26 +08:00
{
texture->retain();
_atlasTextures[slot] = texture;
2013-07-20 01:33:26 +08:00
}
Texture2D* FontAtlas::getTexture(int slot)
2013-07-20 01:33:26 +08:00
{
return _atlasTextures[slot];
2013-07-20 01:33:26 +08:00
}
void FontAtlas::setLineHeight(float newHeight)
{
_lineHeight = newHeight;
}
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();
}
}
}
NS_CC_END