axmol/cocos2dx/label_nodes/CCFontDefinition.cpp

234 lines
7.7 KiB
C++
Raw Normal View History

/****************************************************************************
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"
#include "CCFontDefinition.h"
2013-07-11 02:59:05 +08:00
NS_CC_BEGIN
2013-09-13 11:46:46 +08:00
const int FontDefinitionTTF::DEFAUL_ATALS_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
{
}
FontDefinitionTTF* FontDefinitionTTF::create(Font *font, int textureSize)
{
2013-08-06 09:03:52 +08:00
if (textureSize == 0)
2013-09-13 11:46:46 +08:00
textureSize = DEFAUL_ATALS_TEXTURE_SIZE;
2013-08-06 09:03:52 +08:00
FontDefinitionTTF *ret = new FontDefinitionTTF;
2013-07-11 02:59:05 +08:00
2013-09-13 11:46:46 +08:00
if (!ret)
return 0;
2013-09-13 11:46:46 +08:00
const char *glyph = font->getCurrentGlyphCollection();
if (!glyph)
return nullptr;
2013-09-13 11:46:46 +08:00
if (ret->initDefinition(font, glyph, 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
{
// get all the pages
2013-09-13 11:46:46 +08:00
TextFontPagesDef *pages = pageDefs;
if (!pages)
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)
{
// 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)
{
float posXUV = 0.0;
2013-09-13 11:46:46 +08:00
float posYUV = pages->getPageAt(cPages)->getLineAt(cLines)->getY();
int charsCounter = 0;
2013-09-13 11:46:46 +08:00
for (int c = 0; c < pages->getPageAt(cPages)->getLineAt(cLines)->getNumGlyph(); ++c)
{
// the current glyph
2013-09-13 11:46:46 +08:00
GlyphDef currentGlyph = pages->getPageAt(cPages)->getLineAt(cLines)->getGlyphAt(c);
// 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();
// add this letter definition
2013-08-02 02:02:32 +08:00
FontLetterDefinition tempDef;
// carloX little hack (this should be done outside the loop)
if (posXUV == 0.0)
posXUV = currentGlyph.getPadding();
tempDef.validDefinition = currentGlyph.isValid();
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;
}
// add this definition
addLetterDefinition(tempDef);
// move bounding box to the next letter
posXUV += letterWidth + currentGlyph.getPadding();
// next char in the string
++charsCounter;
}
}
}
// store the common line height
_commonLineHeight = maxLineHeight;
//
return true;
}
2013-08-07 05:37:19 +08:00
bool FontDefinitionTTF::initDefinition(cocos2d::Font *font, const char *letters, int textureSize)
{
2013-07-11 02:59:05 +08:00
// preare texture/image stuff
_textImages = new TextImage();
if (!_textImages)
return false;
if (!_textImages->initWithString(letters, textureSize, textureSize, font, true))
2013-07-11 02:59:05 +08:00
{
delete _textImages;
_textImages = 0;
return false;
}
// 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)
{
if (_fontLettersDefinitionUTF16.find(defToAdd.letteCharUTF16) == _fontLettersDefinitionUTF16.end())
{
_fontLettersDefinitionUTF16[defToAdd.letteCharUTF16] = defToAdd;
}
}
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);
}
// 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-08-02 02:02:32 +08:00
FontLetterDefinition tempDefinition = item.second;
tempDefinition.offsetX = 0;
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