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-07-11 02:59:05 +08:00
|
|
|
#include "CCFontDefinition.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
|
2013-07-20 01:33:26 +08:00
|
|
|
FontDefinitionTTF::FontDefinitionTTF():_textImages(0), _commonLineHeight(0)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-20 03:24:25 +08:00
|
|
|
FontDefinitionTTF* FontDefinitionTTF::create(const char *fontName, int fontSize, const char *letters, int textureSize )
|
|
|
|
{
|
|
|
|
FontDefinitionTTF *ret = new FontDefinitionTTF;
|
2013-07-11 02:59:05 +08:00
|
|
|
|
2013-07-20 03:24:25 +08:00
|
|
|
if(!ret)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ( ret->initDefinition( fontName, fontSize, letters, textureSize ) )
|
|
|
|
{
|
|
|
|
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
|
|
|
|
TextFontPagesDef *pPages = pageDefs;
|
|
|
|
if (!pPages)
|
|
|
|
return (false);
|
|
|
|
|
|
|
|
float maxLineHeight = -1;
|
|
|
|
|
|
|
|
// loops all the pages
|
|
|
|
for (int cPages = 0; cPages<pPages->getNumPages(); ++cPages)
|
|
|
|
{
|
|
|
|
// loops all the lines in this page
|
|
|
|
for (int cLines = 0; cLines<pPages->getPageAt(cPages)->getNumLines(); ++cLines)
|
|
|
|
{
|
|
|
|
float posXUV = 0.0;
|
|
|
|
float posYUV = pPages->getPageAt(cPages)->getLineAt(cLines)->getY();
|
|
|
|
|
|
|
|
int charsCounter = 0;
|
|
|
|
|
|
|
|
for (int c = 0; c < pPages->getPageAt(cPages)->getLineAt(cLines)->getNumGlyph(); ++c)
|
|
|
|
{
|
|
|
|
|
|
|
|
// the current glyph
|
|
|
|
GlyphDef currentGlyph = pPages->getPageAt(cPages)->getLineAt(cLines)->getGlyphAt(c);
|
|
|
|
|
|
|
|
// letter width
|
|
|
|
float letterWidth = currentGlyph.getRect().size.width;
|
|
|
|
|
|
|
|
// letter height
|
|
|
|
float letterHeight = pPages->getPageAt(cPages)->getLineAt(cLines)->getHeight();
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
|
|
|
// 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
|
|
|
|
{
|
|
|
|
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-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-07-20 03:24:25 +08:00
|
|
|
bool FontDefinitionTTF::initDefinition(const char *fontName, int fontSize, 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-07-20 03:24:25 +08:00
|
|
|
if (!_textImages->initWithString(letters, textureSize, textureSize, fontName, fontSize, 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-08-02 02:02:32 +08:00
|
|
|
void FontDefinitionTTF::addLetterDefinition(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-02 02:02:32 +08:00
|
|
|
FontLetterDefinition & FontDefinitionTTF::getLetterDefinition(unsigned short int theLetter)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2013-07-19 04:13:41 +08:00
|
|
|
return _fontLettersDefinitionUTF16[theLetter];
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 01:33:26 +08:00
|
|
|
Texture2D * FontDefinitionTTF::getTexture(int index)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2013-07-19 04:13:41 +08:00
|
|
|
TextFontPagesDef *pPages = _textImages->getPages();
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
if (!pPages)
|
2013-08-05 10:29:20 +08:00
|
|
|
return nullptr;
|
2013-07-20 01:33:26 +08:00
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
return pPages->getPageAt(index)->getPageTexture();
|
|
|
|
}
|
|
|
|
|
2013-07-20 01:33:26 +08:00
|
|
|
int FontDefinitionTTF::getNumTextures()
|
|
|
|
{
|
|
|
|
TextFontPagesDef *pPages = _textImages->getPages();
|
|
|
|
if (pPages)
|
|
|
|
{
|
|
|
|
return pPages->getNumPages();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FontAtlas * FontDefinitionTTF::createFontAtlas()
|
|
|
|
{
|
2013-08-02 08:08:57 +08:00
|
|
|
FontAtlas *retAtlas = new FontAtlas( *_textImages->getFont() );
|
2013-07-20 01:33:26 +08:00
|
|
|
|
|
|
|
if (!retAtlas)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// add all the textures
|
|
|
|
int numTextures = getNumTextures();
|
|
|
|
if (!numTextures)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (int c = 0; c<numTextures; ++c)
|
2013-08-02 08:49:51 +08:00
|
|
|
retAtlas->addTexture(*getTexture(c), c);
|
2013-07-25 01:22:46 +08:00
|
|
|
|
|
|
|
// set the common line height
|
2013-07-26 08:58:13 +08:00
|
|
|
retAtlas->setCommonLineHeight(getCommonLineHeight() * 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
|