2013-07-19 04:13:41 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 Zynga 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.
|
|
|
|
****************************************************************************/
|
2013-07-11 02:59:05 +08:00
|
|
|
|
2013-07-12 05:41:03 +08:00
|
|
|
#include "cocos2d.h"
|
2013-08-08 07:36:04 +08:00
|
|
|
#include "CCFontDefinition.h"
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
const int FontDefinitionTTF::DEFAUL_ATLAS_TEXTURE_SIZE = 1024;
|
2013-08-06 09:03:52 +08:00
|
|
|
|
2013-07-20 01:33:26 +08:00
|
|
|
FontDefinitionTTF::FontDefinitionTTF():_textImages(0), _commonLineHeight(0)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-06 06:11:07 +08:00
|
|
|
FontDefinitionTTF* FontDefinitionTTF::create(Font *font, int textureSize)
|
2013-07-20 03:24:25 +08:00
|
|
|
{
|
2013-08-06 09:03:52 +08:00
|
|
|
if (textureSize == 0)
|
2013-11-07 09:05:13 +08:00
|
|
|
textureSize = DEFAUL_ATLAS_TEXTURE_SIZE;
|
2013-08-06 09:03:52 +08:00
|
|
|
|
2013-07-20 03:24:25 +08:00
|
|
|
FontDefinitionTTF *ret = new FontDefinitionTTF;
|
2013-07-11 02:59:05 +08:00
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
if (!ret)
|
2013-07-20 03:24:25 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
const char *glyph = font->getCurrentGlyphCollection();
|
|
|
|
if (!glyph)
|
2013-08-06 06:11:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
if (ret->initDefinition(font, glyph, textureSize))
|
2013-07-20 03:24:25 +08:00
|
|
|
{
|
2013-10-31 17:52:22 +08:00
|
|
|
ret->autorelease();
|
2013-07-20 03:24:25 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete ret;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-20 01:33:26 +08:00
|
|
|
FontDefinitionTTF::~FontDefinitionTTF()
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
|
|
|
if (_textImages)
|
|
|
|
{
|
|
|
|
delete _textImages;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-20 01:33:26 +08:00
|
|
|
bool FontDefinitionTTF::prepareLetterDefinitions(TextFontPagesDef *pageDefs)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2013-07-18 08:31:28 +08:00
|
|
|
// get all the pages
|
2013-09-13 11:46:46 +08:00
|
|
|
TextFontPagesDef *pages = pageDefs;
|
|
|
|
if (!pages)
|
2013-07-18 08:31:28 +08:00
|
|
|
return (false);
|
|
|
|
|
|
|
|
float maxLineHeight = -1;
|
|
|
|
|
|
|
|
// loops all the pages
|
2013-09-13 11:46:46 +08:00
|
|
|
for (int cPages = 0; cPages < pages->getNumPages(); ++cPages)
|
2013-07-18 08:31:28 +08:00
|
|
|
{
|
|
|
|
// loops all the lines in this page
|
2013-09-13 11:46:46 +08:00
|
|
|
for (int cLines = 0; cLines<pages->getPageAt(cPages)->getNumLines(); ++cLines)
|
2013-07-18 08:31:28 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
float posXUV = 0.0;
|
|
|
|
float posYUV = pages->getPageAt(cPages)->getLineAt(cLines)->getY();
|
2013-07-18 08:31:28 +08:00
|
|
|
|
|
|
|
int charsCounter = 0;
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
for (int c = 0; c < pages->getPageAt(cPages)->getLineAt(cLines)->getNumGlyph(); ++c)
|
2013-07-18 08:31:28 +08:00
|
|
|
{
|
|
|
|
// the current glyph
|
2013-09-13 11:46:46 +08:00
|
|
|
GlyphDef currentGlyph = pages->getPageAt(cPages)->getLineAt(cLines)->getGlyphAt(c);
|
2013-07-18 08:31:28 +08:00
|
|
|
|
|
|
|
// letter width
|
|
|
|
float letterWidth = currentGlyph.getRect().size.width;
|
|
|
|
|
|
|
|
// letter height
|
2013-09-13 11:46:46 +08:00
|
|
|
float letterHeight = pages->getPageAt(cPages)->getLineAt(cLines)->getHeight();
|
2013-07-18 08:31:28 +08:00
|
|
|
|
|
|
|
// add this letter definition
|
2013-08-02 02:02:32 +08:00
|
|
|
FontLetterDefinition tempDef;
|
2013-07-18 08:31:28 +08:00
|
|
|
|
|
|
|
|
2013-07-19 04:13:41 +08:00
|
|
|
// carloX little hack (this should be done outside the loop)
|
2013-07-18 08:31:28 +08:00
|
|
|
if (posXUV == 0.0)
|
|
|
|
posXUV = currentGlyph.getPadding();
|
|
|
|
|
2013-07-30 04:43:23 +08:00
|
|
|
tempDef.validDefinition = currentGlyph.isValid();
|
2013-07-18 08:31:28 +08:00
|
|
|
|
2013-07-30 04:43:23 +08:00
|
|
|
if (tempDef.validDefinition)
|
|
|
|
{
|
2013-08-06 06:11:07 +08:00
|
|
|
tempDef.letteCharUTF16 = currentGlyph.getUTF8Letter();
|
|
|
|
tempDef.width = letterWidth + currentGlyph.getPadding();
|
|
|
|
tempDef.height = (letterHeight - 1);
|
|
|
|
tempDef.U = (posXUV - 1);
|
|
|
|
tempDef.V = posYUV;
|
|
|
|
tempDef.offsetX = currentGlyph.getRect().origin.x;
|
|
|
|
tempDef.offsetY = currentGlyph.getRect().origin.y;
|
|
|
|
tempDef.textureID = cPages;
|
|
|
|
tempDef.commonLineHeight = currentGlyph.getCommonHeight();
|
2013-07-30 04:43:23 +08:00
|
|
|
|
|
|
|
// take from pixels to points
|
|
|
|
tempDef.width = tempDef.width / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
tempDef.height = tempDef.height / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
tempDef.U = tempDef.U / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
tempDef.V = tempDef.V / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
|
|
|
|
if (tempDef.commonLineHeight>maxLineHeight)
|
|
|
|
maxLineHeight = tempDef.commonLineHeight;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-06 06:11:07 +08:00
|
|
|
tempDef.letteCharUTF16 = currentGlyph.getUTF8Letter();
|
|
|
|
tempDef.commonLineHeight = 0;
|
|
|
|
tempDef.width = 0;
|
|
|
|
tempDef.height = 0;
|
|
|
|
tempDef.U = 0;
|
|
|
|
tempDef.V = 0;
|
|
|
|
tempDef.offsetX = 0;
|
|
|
|
tempDef.offsetY = 0;
|
|
|
|
tempDef.textureID = 0;
|
2013-07-30 04:43:23 +08:00
|
|
|
}
|
2013-07-18 08:31:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
// add this definition
|
2013-07-19 04:13:41 +08:00
|
|
|
addLetterDefinition(tempDef);
|
2013-07-18 08:31:28 +08:00
|
|
|
|
|
|
|
// move bounding box to the next letter
|
|
|
|
posXUV += letterWidth + currentGlyph.getPadding();
|
|
|
|
|
|
|
|
// next char in the string
|
|
|
|
++charsCounter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// store the common line height
|
2013-07-19 04:13:41 +08:00
|
|
|
_commonLineHeight = maxLineHeight;
|
2013-07-18 08:31:28 +08:00
|
|
|
|
2013-07-20 03:24:25 +08:00
|
|
|
//
|
2013-07-18 08:31:28 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-07 05:37:19 +08:00
|
|
|
bool FontDefinitionTTF::initDefinition(cocos2d::Font *font, const char *letters, int textureSize)
|
2013-07-18 08:31:28 +08:00
|
|
|
{
|
2013-07-11 02:59:05 +08:00
|
|
|
// preare texture/image stuff
|
|
|
|
_textImages = new TextImage();
|
|
|
|
if (!_textImages)
|
|
|
|
return false;
|
|
|
|
|
2013-08-06 06:11:07 +08:00
|
|
|
if (!_textImages->initWithString(letters, textureSize, textureSize, font, true))
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
|
|
|
delete _textImages;
|
|
|
|
_textImages = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-18 08:31:28 +08:00
|
|
|
// prepare the new letter definition
|
|
|
|
return prepareLetterDefinitions(_textImages->getPages());
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
void FontDefinitionTTF::addLetterDefinition(const FontLetterDefinition &defToAdd)
|
2013-07-18 08:31:28 +08:00
|
|
|
{
|
2013-07-19 04:13:41 +08:00
|
|
|
if (_fontLettersDefinitionUTF16.find(defToAdd.letteCharUTF16) == _fontLettersDefinitionUTF16.end())
|
2013-07-18 08:31:28 +08:00
|
|
|
{
|
2013-07-19 04:13:41 +08:00
|
|
|
_fontLettersDefinitionUTF16[defToAdd.letteCharUTF16] = defToAdd;
|
2013-07-18 08:31:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:49:20 +08:00
|
|
|
FontAtlas * FontDefinitionTTF::createFontAtlas()
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2013-08-06 08:49:20 +08:00
|
|
|
int numTextures = 0;
|
2013-09-13 11:46:46 +08:00
|
|
|
TextFontPagesDef *pages = _textImages->getPages();
|
2013-07-11 02:59:05 +08:00
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
if (pages)
|
|
|
|
numTextures = pages->getNumPages();
|
2013-08-06 08:49:20 +08:00
|
|
|
else
|
|
|
|
return nullptr;
|
2013-07-20 01:33:26 +08:00
|
|
|
|
2013-08-06 08:49:20 +08:00
|
|
|
if (!numTextures)
|
|
|
|
return nullptr;
|
2013-07-20 01:33:26 +08:00
|
|
|
|
2013-08-06 08:49:20 +08:00
|
|
|
FontAtlas *retAtlas = new FontAtlas(*_textImages->getFont());
|
2013-07-20 01:33:26 +08:00
|
|
|
|
2013-08-06 08:49:20 +08:00
|
|
|
if (!retAtlas)
|
2013-07-20 01:33:26 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-08-06 08:49:20 +08:00
|
|
|
for (int c = 0; c < numTextures; ++c)
|
|
|
|
{
|
|
|
|
TextFontPagesDef *pPages = _textImages->getPages();
|
|
|
|
retAtlas->addTexture(*(pPages->getPageAt(c)->getPageTexture()), c);
|
|
|
|
}
|
2013-07-25 01:22:46 +08:00
|
|
|
|
|
|
|
// set the common line height
|
2013-08-06 08:49:20 +08:00
|
|
|
retAtlas->setCommonLineHeight(_commonLineHeight * 0.8);
|
2013-07-20 01:33:26 +08:00
|
|
|
|
2013-08-02 02:02:32 +08:00
|
|
|
for( auto &item: _fontLettersDefinitionUTF16 )
|
2013-07-20 01:33:26 +08:00
|
|
|
{
|
2013-08-02 02:02:32 +08:00
|
|
|
if ( item.second.validDefinition )
|
2013-07-30 04:43:23 +08:00
|
|
|
{
|
2013-08-02 02:02:32 +08:00
|
|
|
FontLetterDefinition tempDefinition = item.second;
|
|
|
|
tempDefinition.offsetX = 0;
|
2013-07-30 04:43:23 +08:00
|
|
|
tempDefinition.anchorX = 0.0f;
|
|
|
|
tempDefinition.anchorY = 1.0f;
|
|
|
|
retAtlas->addLetterDefinition(tempDefinition);
|
|
|
|
}
|
2013-07-20 01:33:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// done here
|
|
|
|
return retAtlas;
|
|
|
|
}
|
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
NS_CC_END
|