2013-11-07 09:05:13 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
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
|
|
|
|
2013-07-20 01:33:26 +08:00
|
|
|
#include "CCFontAtlas.h"
|
2013-10-29 20:25:03 +08:00
|
|
|
#include "CCFontFreeType.h"
|
2014-01-17 13:35:58 +08:00
|
|
|
#include "ccUTF8.h"
|
|
|
|
#include "CCDirector.h"
|
2013-07-20 01:33:26 +08:00
|
|
|
|
2013-12-13 12:42:15 +08:00
|
|
|
#define PAGE_WIDTH 1024
|
|
|
|
#define PAGE_HEIGHT 1024
|
|
|
|
|
2013-07-20 01:33:26 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
FontAtlas::FontAtlas(Font &theFont) :
|
2013-10-31 17:52:22 +08:00
|
|
|
_font(&theFont),
|
2013-10-29 20:25:03 +08:00
|
|
|
_currentPageData(nullptr)
|
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
|
|
|
|
2013-10-31 17:52:22 +08:00
|
|
|
FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(_font);
|
2014-01-20 10:32:12 +08:00
|
|
|
if (fontTTf)
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
2013-10-31 17:52:22 +08:00
|
|
|
_currentPageLineHeight = _font->getFontMaxHeight();
|
2013-10-29 20:25:03 +08:00
|
|
|
_commonLineHeight = _currentPageLineHeight * 0.8f;
|
|
|
|
Texture2D * tex = new Texture2D;
|
|
|
|
_currentPage = 0;
|
|
|
|
_currentPageOrigX = 0;
|
|
|
|
_currentPageOrigY = 0;
|
|
|
|
_letterPadding = 5;
|
2014-01-21 10:36:32 +08:00
|
|
|
|
|
|
|
_makeDistanceMap = fontTTf->isDistanceFieldEnabled();
|
2013-12-13 12:42:15 +08:00
|
|
|
if(_makeDistanceMap)
|
|
|
|
{
|
2014-01-21 10:36:32 +08:00
|
|
|
_commonLineHeight += 2 * FontFreeType::DistanceMapSpread;
|
|
|
|
_letterPadding += 2 * FontFreeType::DistanceMapSpread;
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
2014-01-20 10:32:12 +08:00
|
|
|
_currentPageDataSize = (PAGE_WIDTH * PAGE_HEIGHT * 1);
|
2013-10-29 20:25:03 +08:00
|
|
|
|
|
|
|
_currentPageData = new unsigned char[_currentPageDataSize];
|
|
|
|
memset(_currentPageData, 0, _currentPageDataSize);
|
|
|
|
addTexture(*tex,0);
|
2013-10-31 17:52:22 +08:00
|
|
|
tex->release();
|
2013-10-29 20:25:03 +08:00
|
|
|
}
|
2014-01-21 10:36:32 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_makeDistanceMap = false;
|
|
|
|
}
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FontAtlas::~FontAtlas()
|
|
|
|
{
|
2013-10-31 17:52:22 +08:00
|
|
|
_font->release();
|
2013-07-25 01:22:46 +08:00
|
|
|
relaseTextures();
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2013-10-31 17:52:22 +08:00
|
|
|
delete []_currentPageData;
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FontAtlas::relaseTextures()
|
|
|
|
{
|
|
|
|
for( auto &item: _atlasTextures)
|
|
|
|
{
|
2013-08-06 07:41:23 +08:00
|
|
|
item.second->release();
|
2013-07-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:49:20 +08:00
|
|
|
void FontAtlas::addLetterDefinition(const FontLetterDefinition &letterDefinition)
|
2013-07-20 01:33:26 +08:00
|
|
|
{
|
|
|
|
_fontLetterDefinitions[letterDefinition.letteCharUTF16] = letterDefinition;
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:49:20 +08:00
|
|
|
bool FontAtlas::getLetterDefinitionForChar(unsigned short letteCharUTF16, FontLetterDefinition &outDefinition)
|
2013-07-20 01:33:26 +08:00
|
|
|
{
|
2013-07-30 04:43:23 +08:00
|
|
|
auto outIterator = _fontLetterDefinitions.find(letteCharUTF16);
|
2013-08-06 08:49:20 +08:00
|
|
|
|
2013-07-30 04:43:23 +08:00
|
|
|
if (outIterator != _fontLetterDefinitions.end())
|
|
|
|
{
|
2013-08-06 08:49:20 +08:00
|
|
|
outDefinition = (*outIterator).second;
|
|
|
|
return true;
|
2013-07-30 04:43:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-16 11:47:06 +08:00
|
|
|
outDefinition.validDefinition = false;
|
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
|
|
|
}
|
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
|
|
|
|
{
|
|
|
|
if(_currentPageData == nullptr)
|
|
|
|
return false;
|
|
|
|
|
2013-10-31 17:52:22 +08:00
|
|
|
FontFreeType* fontTTf = (FontFreeType*)_font;
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2013-12-13 12:42:15 +08:00
|
|
|
std::unordered_map<unsigned short, FontLetterDefinition> fontDefs;
|
2013-10-29 20:25:03 +08:00
|
|
|
int length = cc_wcslen(utf16String);
|
|
|
|
|
|
|
|
//find out new letter
|
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
auto outIterator = _fontLetterDefinitions.find(utf16String[i]);
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
if (outIterator == _fontLetterDefinitions.end())
|
2013-12-13 12:42:15 +08:00
|
|
|
{
|
|
|
|
auto outIterator2 = fontDefs.find(utf16String[i]);
|
|
|
|
if(outIterator2 != fontDefs.end())
|
|
|
|
continue;
|
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
Rect tempRect;
|
|
|
|
|
2014-01-17 14:04:52 +08:00
|
|
|
FontLetterDefinition tempDef;
|
2013-10-29 20:25:03 +08:00
|
|
|
tempDef.anchorX = 0.0f;
|
|
|
|
tempDef.anchorY = 1.0f;
|
|
|
|
|
2014-01-21 17:55:49 +08:00
|
|
|
if (!fontTTf->getBBOXFotChar(utf16String[i], tempRect,tempDef.xAdvance))
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
|
|
|
tempDef.validDefinition = false;
|
|
|
|
tempDef.letteCharUTF16 = utf16String[i];
|
|
|
|
tempDef.commonLineHeight = 0;
|
|
|
|
tempDef.width = 0;
|
|
|
|
tempDef.height = 0;
|
|
|
|
tempDef.U = 0;
|
2014-01-17 14:04:52 +08:00
|
|
|
tempDef.V = 0;
|
|
|
|
tempDef.offsetX = 0;
|
2013-10-29 20:25:03 +08:00
|
|
|
tempDef.offsetY = 0;
|
|
|
|
tempDef.textureID = 0;
|
2014-01-21 17:55:49 +08:00
|
|
|
tempDef.xAdvance = 0;
|
2013-10-29 20:25:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tempDef.validDefinition = true;
|
|
|
|
tempDef.letteCharUTF16 = utf16String[i];
|
|
|
|
tempDef.width = tempRect.size.width + _letterPadding;
|
2014-01-17 14:04:52 +08:00
|
|
|
tempDef.height = _currentPageLineHeight - 1;
|
|
|
|
tempDef.offsetX = tempRect.origin.x;
|
2013-10-29 20:25:03 +08:00
|
|
|
tempDef.offsetY = tempRect.origin.y;
|
|
|
|
tempDef.commonLineHeight = _currentPageLineHeight;
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
|
|
|
fontDefs[utf16String[i]] = tempDef;
|
2013-10-29 20:25:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 12:42:15 +08:00
|
|
|
Size _pageContentSize = Size(PAGE_WIDTH,PAGE_HEIGHT);
|
2013-10-29 20:25:03 +08:00
|
|
|
float scaleFactor = CC_CONTENT_SCALE_FACTOR();
|
|
|
|
float glyphWidth;
|
2014-01-20 10:32:12 +08:00
|
|
|
Texture2D::PixelFormat pixelFormat = Texture2D::PixelFormat::A8;
|
|
|
|
|
2013-12-13 12:42:15 +08:00
|
|
|
|
|
|
|
for(auto it = fontDefs.begin(); it != fontDefs.end(); it++)
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
2013-12-13 12:42:15 +08:00
|
|
|
if(it->second.validDefinition)
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
|
|
|
_currentPageOrigX += _letterPadding;
|
2013-12-13 12:42:15 +08:00
|
|
|
glyphWidth = it->second.width - _letterPadding;
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2013-12-13 12:42:15 +08:00
|
|
|
if (_currentPageOrigX + glyphWidth > PAGE_WIDTH)
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
|
|
|
_currentPageOrigY += _currentPageLineHeight;
|
2013-12-13 12:42:15 +08:00
|
|
|
_currentPageOrigX = 0;
|
|
|
|
if(_currentPageOrigY >= PAGE_HEIGHT)
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
2013-12-13 12:42:15 +08:00
|
|
|
_atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, PAGE_WIDTH, PAGE_HEIGHT, _pageContentSize );
|
2013-10-29 20:25:03 +08:00
|
|
|
_currentPageOrigY = 0;
|
|
|
|
|
|
|
|
delete []_currentPageData;
|
|
|
|
_currentPageData = new unsigned char[_currentPageDataSize];
|
|
|
|
if(_currentPageData == nullptr)
|
|
|
|
return false;
|
|
|
|
memset(_currentPageData, 0, _currentPageDataSize);
|
|
|
|
_currentPage++;
|
2013-10-31 17:52:22 +08:00
|
|
|
Texture2D* tex = new Texture2D;
|
|
|
|
addTexture(*tex,_currentPage);
|
|
|
|
tex->release();
|
2013-10-29 20:25:03 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-21 10:36:32 +08:00
|
|
|
fontTTf->renderCharAt(it->second.letteCharUTF16,_currentPageOrigX,_currentPageOrigY,_currentPageData,PAGE_WIDTH);
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2013-12-13 12:42:15 +08:00
|
|
|
it->second.U = _currentPageOrigX - 1;
|
|
|
|
it->second.V = _currentPageOrigY;
|
|
|
|
it->second.textureID = _currentPage;
|
2013-10-29 20:25:03 +08:00
|
|
|
// take from pixels to points
|
2013-12-13 12:42:15 +08:00
|
|
|
it->second.width = it->second.width / scaleFactor;
|
|
|
|
it->second.height = it->second.height / scaleFactor;
|
|
|
|
it->second.U = it->second.U / scaleFactor;
|
|
|
|
it->second.V = it->second.V / scaleFactor;
|
2013-10-29 20:25:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
glyphWidth = 0;
|
|
|
|
|
2013-12-13 12:42:15 +08:00
|
|
|
_fontLetterDefinitions[it->second.letteCharUTF16] = it->second;
|
2013-10-29 20:25:03 +08:00
|
|
|
_currentPageOrigX += glyphWidth;
|
|
|
|
}
|
2013-12-13 12:42:15 +08:00
|
|
|
if(fontDefs.size() > 0)
|
|
|
|
_atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, PAGE_WIDTH, PAGE_HEIGHT, _pageContentSize );
|
2013-10-29 20:25:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:49:20 +08:00
|
|
|
void FontAtlas::addTexture(Texture2D &texture, int slot)
|
2013-07-20 01:33:26 +08:00
|
|
|
{
|
2013-08-06 08:49:20 +08:00
|
|
|
texture.retain();
|
|
|
|
_atlasTextures[slot] = &texture;
|
2013-07-20 01:33:26 +08:00
|
|
|
}
|
|
|
|
|
2013-08-06 08:49:20 +08:00
|
|
|
Texture2D & FontAtlas::getTexture(int slot)
|
2013-07-20 01:33:26 +08:00
|
|
|
{
|
2013-08-06 08:49:20 +08:00
|
|
|
return *(_atlasTextures[slot]);
|
2013-07-20 01:33:26 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
float FontAtlas::getCommonLineHeight() const
|
2013-07-25 01:22:46 +08:00
|
|
|
{
|
|
|
|
return _commonLineHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FontAtlas::setCommonLineHeight(float newHeight)
|
|
|
|
{
|
2013-12-13 12:42:15 +08:00
|
|
|
if(_makeDistanceMap)
|
2014-01-21 10:36:32 +08:00
|
|
|
newHeight += 2 * FontFreeType::DistanceMapSpread;
|
2013-07-25 01:22:46 +08:00
|
|
|
_commonLineHeight = newHeight;
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:52:22 +08:00
|
|
|
const Font * FontAtlas::getFont() const
|
2013-07-25 01:22:46 +08:00
|
|
|
{
|
|
|
|
return _font;
|
|
|
|
}
|
|
|
|
|
2013-07-20 01:33:26 +08:00
|
|
|
NS_CC_END
|