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.
|
|
|
|
****************************************************************************/
|
2013-07-26 08:58:13 +08:00
|
|
|
|
|
|
|
#include "CCFontFNT.h"
|
2013-08-08 07:36:04 +08:00
|
|
|
#include "CCFontAtlas.h"
|
2013-07-26 08:58:13 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
FontFNT * FontFNT::create(const std::string& fntFilePath)
|
2013-08-06 06:56:18 +08:00
|
|
|
{
|
2013-08-07 04:43:29 +08:00
|
|
|
CCBMFontConfiguration *newConf = FNTConfigLoadFile(fntFilePath);
|
|
|
|
if (!newConf)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// add the texture
|
2013-11-07 19:11:09 +08:00
|
|
|
Texture2D *tempTexture = Director::getInstance()->getTextureCache()->addImage(newConf->getAtlasName());
|
2013-09-13 11:46:46 +08:00
|
|
|
if (!tempTexture)
|
2013-08-07 04:43:29 +08:00
|
|
|
{
|
|
|
|
delete newConf;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FontFNT *tempFont = new FontFNT(newConf);
|
|
|
|
|
|
|
|
if (!tempFont)
|
|
|
|
{
|
|
|
|
delete newConf;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-10-31 20:17:30 +08:00
|
|
|
tempFont->autorelease();
|
2013-08-07 04:43:29 +08:00
|
|
|
return tempFont;
|
2013-08-06 06:56:18 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 08:58:13 +08:00
|
|
|
FontFNT::~FontFNT()
|
|
|
|
{
|
2013-10-31 17:52:22 +08:00
|
|
|
|
2013-07-26 08:58:13 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
Size * FontFNT::getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
2013-09-13 11:46:46 +08:00
|
|
|
if (!text)
|
2013-07-26 08:58:13 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
outNumLetters = cc_wcslen(text);
|
2013-07-26 08:58:13 +08:00
|
|
|
|
|
|
|
if (!outNumLetters)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
Size *sizes = new Size[outNumLetters];
|
|
|
|
if (!sizes)
|
2013-07-26 08:58:13 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
for (int c = 0; c < outNumLetters; ++c)
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
|
|
|
int advance = 0;
|
|
|
|
int kerning = 0;
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
advance = getAdvanceForChar(text[c]);
|
2013-07-26 08:58:13 +08:00
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
if (c < (outNumLetters-1))
|
|
|
|
kerning = getHorizontalKerningForChars(text[c], text[c+1]);
|
2013-07-26 08:58:13 +08:00
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
sizes[c].width = (advance + kerning);
|
2013-07-26 08:58:13 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
return sizes;
|
2013-07-26 08:58:13 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
int FontFNT::getAdvanceForChar(unsigned short theChar) const
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
2013-09-13 11:46:46 +08:00
|
|
|
tFontDefHashElement *element = nullptr;
|
2013-07-26 08:58:13 +08:00
|
|
|
|
|
|
|
// unichar is a short, and an int is needed on HASH_FIND_INT
|
|
|
|
unsigned int key = theChar;
|
|
|
|
HASH_FIND_INT(_configuration->_fontDefDictionary, &key, element);
|
|
|
|
if (! element)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return element->fontDef.xAdvance;
|
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
int FontFNT::getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar) const
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2013-09-13 11:46:46 +08:00
|
|
|
unsigned int key = (firstChar << 16) | (secondChar & 0xffff);
|
2013-07-26 08:58:13 +08:00
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
if (_configuration->_kerningDictionary)
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
2013-09-13 11:46:46 +08:00
|
|
|
tKerningHashElement *element = nullptr;
|
2013-07-26 08:58:13 +08:00
|
|
|
HASH_FIND_INT(_configuration->_kerningDictionary, &key, element);
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
if (element)
|
2013-07-26 08:58:13 +08:00
|
|
|
ret = element->amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
Rect FontFNT::getRectForCharInternal(unsigned short theChar) const
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
|
|
|
Rect retRect;
|
|
|
|
ccBMFontDef fontDef;
|
2013-09-13 11:46:46 +08:00
|
|
|
tFontDefHashElement *element = nullptr;
|
2013-07-26 08:58:13 +08:00
|
|
|
unsigned int key = theChar;
|
|
|
|
|
|
|
|
HASH_FIND_INT(_configuration->_fontDefDictionary, &key, element);
|
|
|
|
|
|
|
|
if (element)
|
|
|
|
{
|
|
|
|
retRect = element->fontDef.rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retRect;
|
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
Rect FontFNT::getRectForChar(unsigned short theChar) const
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
|
|
|
return getRectForCharInternal(theChar);
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:36:34 +08:00
|
|
|
FontAtlas * FontFNT::createFontAtlas()
|
|
|
|
{
|
2013-08-06 08:49:20 +08:00
|
|
|
FontAtlas *tempAtlas = new FontAtlas(*this);
|
2013-08-02 05:36:34 +08:00
|
|
|
if (!tempAtlas)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// check that everything is fine with the BMFontCofniguration
|
|
|
|
if (!_configuration->_fontDefDictionary)
|
|
|
|
return nullptr;
|
|
|
|
|
2013-12-06 16:32:06 +08:00
|
|
|
size_t numGlyphs = _configuration->_characterSet->size();
|
2013-08-02 05:36:34 +08:00
|
|
|
if (!numGlyphs)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (_configuration->_commonHeight == 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// commone height
|
|
|
|
tempAtlas->setCommonLineHeight(_configuration->_commonHeight);
|
|
|
|
|
|
|
|
|
|
|
|
ccBMFontDef fontDef;
|
2013-09-13 11:46:46 +08:00
|
|
|
tFontDefHashElement *currentElement, *tmp;
|
2013-08-02 05:36:34 +08:00
|
|
|
|
|
|
|
// Purge uniform hash
|
2013-09-13 11:46:46 +08:00
|
|
|
HASH_ITER(hh, _configuration->_fontDefDictionary, currentElement, tmp)
|
2013-08-02 05:36:34 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
FontLetterDefinition tempDefinition;
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
fontDef = currentElement->fontDef;
|
2013-08-02 05:36:34 +08:00
|
|
|
Rect tempRect;
|
|
|
|
|
|
|
|
tempRect = fontDef.rect;
|
|
|
|
tempRect = CC_RECT_PIXELS_TO_POINTS(tempRect);
|
|
|
|
|
|
|
|
tempDefinition.letteCharUTF16 = fontDef.charID;
|
|
|
|
|
|
|
|
tempDefinition.offsetX = fontDef.xOffset;
|
|
|
|
tempDefinition.offsetY = fontDef.yOffset;
|
|
|
|
|
|
|
|
tempDefinition.U = tempRect.origin.x;
|
|
|
|
tempDefinition.V = tempRect.origin.y;
|
|
|
|
|
|
|
|
tempDefinition.width = tempRect.size.width;
|
|
|
|
tempDefinition.height = tempRect.size.height;
|
|
|
|
|
|
|
|
//carloX: only one texture supported FOR NOW
|
|
|
|
tempDefinition.textureID = 0;
|
|
|
|
|
|
|
|
tempDefinition.anchorX = 0.5f;
|
|
|
|
tempDefinition.anchorY = 0.5f;
|
2013-10-29 20:25:03 +08:00
|
|
|
tempDefinition.validDefinition = true;
|
2013-08-02 05:36:34 +08:00
|
|
|
// add the new definition
|
|
|
|
tempAtlas->addLetterDefinition(tempDefinition);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the texture (only one texture for now)
|
|
|
|
|
2013-11-07 19:11:09 +08:00
|
|
|
Texture2D *tempTexture = Director::getInstance()->getTextureCache()->addImage(_configuration->getAtlasName());
|
2013-08-02 05:36:34 +08:00
|
|
|
if (!tempTexture)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// add the texture
|
2013-08-06 08:49:20 +08:00
|
|
|
tempAtlas->addTexture(*tempTexture, 0);
|
2013-08-02 05:36:34 +08:00
|
|
|
|
|
|
|
// done
|
|
|
|
return tempAtlas;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-26 08:58:13 +08:00
|
|
|
NS_CC_END
|