use unix text format

This commit is contained in:
dumganhar 2011-11-08 18:11:43 +08:00
parent a81b078286
commit 33db1c7951
1 changed files with 171 additions and 166 deletions

View File

@ -1,48 +1,48 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011 Zynga Inc. Copyright (c) 2011 Zynga Inc.
http://www.cocos2d-x.org http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 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 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
Use any of these editors to generate BMFonts: Use any of these editors to generate BMFonts:
http://glyphdesigner.71squared.com/ (Commercial, Mac OS X) http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
http://www.n4te.com/hiero/hiero.jnlp (Free, Java) http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java) http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
http://www.angelcode.com/products/bmfont/ (Free, Windows only) http://www.angelcode.com/products/bmfont/ (Free, Windows only)
****************************************************************************/ ****************************************************************************/
#include "CCLabelBMFont.h" #include "CCLabelBMFont.h"
#include "platform/platform.h" #include "platform/platform.h"
#include "CCMutableDictionary.h" #include "CCMutableDictionary.h"
#include "CCConfiguration.h" #include "CCConfiguration.h"
#include "CCDrawingPrimitives.h" #include "CCDrawingPrimitives.h"
#include "CCSprite.h" #include "CCSprite.h"
#include "CCPointExtension.h" #include "CCPointExtension.h"
#include "CCFileUtils.h" #include "CCFileUtils.h"
#include "support/data_support/uthash.h" #include "support/data_support/uthash.h"
namespace cocos2d{ namespace cocos2d{
// //
//FNTConfig Cache - free functions //FNTConfig Cache - free functions
@ -69,11 +69,11 @@ namespace cocos2d{
void FNTConfigRemoveCache( void ) void FNTConfigRemoveCache( void )
{ {
if (configurations) if (configurations)
{ {
configurations->removeAllObjects(); configurations->removeAllObjects();
CC_SAFE_RELEASE_NULL(configurations); CC_SAFE_RELEASE_NULL(configurations);
} }
} }
// //
//Hash Element //Hash Element
@ -139,30 +139,31 @@ namespace cocos2d{
CCAssert(pBuffer, "CCBMFontConfiguration::parseConfigFile | Open file error."); CCAssert(pBuffer, "CCBMFontConfiguration::parseConfigFile | Open file error.");
if (!pBuffer) if (!pBuffer)
{ {
return; return;
} }
// parse spacing / padding // parse spacing / padding
std::string line; std::string line;
std::string strLeft(pBuffer, nBufSize); std::string strLeft(pBuffer, nBufSize);
while (strLeft.length() > 0) { while (strLeft.length() > 0)
int pos = strLeft.find('\n'); {
int pos = strLeft.find('\n');
if (pos != (int)std::string::npos)
{ if (pos != (int)std::string::npos)
// the data is more than a line.get one line {
line = strLeft.substr(0, pos); // the data is more than a line.get one line
strLeft = strLeft.substr(pos + 1); line = strLeft.substr(0, pos);
} strLeft = strLeft.substr(pos + 1);
else }
{ else
// get the left data {
line = strLeft; // get the left data
strLeft.erase(); line = strLeft;
} strLeft.erase();
}
if(line.substr(0,strlen("info face")) == "info face") if(line.substr(0,strlen("info face")) == "info face")
{ {
// XXX: info parsing is incomplete // XXX: info parsing is incomplete
@ -199,16 +200,16 @@ namespace cocos2d{
else if(line.substr(0,strlen("kerning first")) == "kerning first") else if(line.substr(0,strlen("kerning first")) == "kerning first")
{ {
this->parseKerningEntry(line); this->parseKerningEntry(line);
} }
} }
} }
void CCBMFontConfiguration::parseImageFileName(std::string line, const char *fntFile) void CCBMFontConfiguration::parseImageFileName(std::string line, const char *fntFile)
{ {
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// line to parse: // line to parse:
// page id=0 file="bitmapFontTest.png" // page id=0 file="bitmapFontTest.png"
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// page ID. Sanity check // page ID. Sanity check
int index = line.find('=')+1; int index = line.find('=')+1;
int index2 = line.find(' ', index); int index2 = line.find(' ', index);
@ -223,11 +224,11 @@ namespace cocos2d{
} }
void CCBMFontConfiguration::parseInfoArguments(std::string line) void CCBMFontConfiguration::parseInfoArguments(std::string line)
{ {
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// possible lines to parse: // possible lines to parse:
// info face="Script" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=1,4,3,2 spacing=0,0 outline=0 // info face="Script" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=1,4,3,2 spacing=0,0 outline=0
// info face="Cracked" size=36 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1 // info face="Cracked" size=36 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// padding // padding
int index = line.find("padding="); int index = line.find("padding=");
@ -238,10 +239,10 @@ namespace cocos2d{
} }
void CCBMFontConfiguration::parseCommonArguments(std::string line) void CCBMFontConfiguration::parseCommonArguments(std::string line)
{ {
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// line to parse: // line to parse:
// common lineHeight=104 base=26 scaleW=1024 scaleH=512 pages=1 packed=0 // common lineHeight=104 base=26 scaleW=1024 scaleH=512 pages=1 packed=0
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// Height // Height
int index = line.find("lineHeight="); int index = line.find("lineHeight=");
@ -268,10 +269,10 @@ namespace cocos2d{
} }
void CCBMFontConfiguration::parseCharacterDefinition(std::string line, ccBMFontDef *characterDefinition) void CCBMFontConfiguration::parseCharacterDefinition(std::string line, ccBMFontDef *characterDefinition)
{ {
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// line to parse: // line to parse:
// char id=32 x=0 y=0 width=0 height=0 xoffset=0 yoffset=44 xadvance=14 page=0 chnl=0 // char id=32 x=0 y=0 width=0 height=0 xoffset=0 yoffset=44 xadvance=14 page=0 chnl=0
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// Character ID // Character ID
int index = line.find("id="); int index = line.find("id=");
@ -337,11 +338,11 @@ namespace cocos2d{
// kerningDictionary = ccHashSetNew(capacity, targetSetEql); // kerningDictionary = ccHashSetNew(capacity, targetSetEql);
} }
void CCBMFontConfiguration::parseKerningEntry(std::string line) void CCBMFontConfiguration::parseKerningEntry(std::string line)
{ {
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// line to parse: // line to parse:
// kerning first=121 second=44 amount=-7 // kerning first=121 second=44 amount=-7
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// first // first
int first; int first;
@ -400,17 +401,17 @@ namespace cocos2d{
m_pConfiguration->retain(); m_pConfiguration->retain();
CCAssert( m_pConfiguration, "Error creating config for LabelBMFont"); CCAssert( m_pConfiguration, "Error creating config for LabelBMFont");
if (CCSpriteBatchNode::initWithFile(m_pConfiguration->m_sAtlasName.c_str(), strlen(theString))) if (CCSpriteBatchNode::initWithFile(m_pConfiguration->m_sAtlasName.c_str(), strlen(theString)))
{ {
m_cOpacity = 255; m_cOpacity = 255;
m_tColor = ccWHITE; m_tColor = ccWHITE;
m_tContentSize = CCSizeZero; m_tContentSize = CCSizeZero;
m_bIsOpacityModifyRGB = m_pobTextureAtlas->getTexture()->getHasPremultipliedAlpha(); m_bIsOpacityModifyRGB = m_pobTextureAtlas->getTexture()->getHasPremultipliedAlpha();
setAnchorPoint(ccp(0.5f, 0.5f)); setAnchorPoint(ccp(0.5f, 0.5f));
this->setString(theString); this->setString(theString);
return true; return true;
} }
return false; return false;
} }
CCLabelBMFont::~CCLabelBMFont() CCLabelBMFont::~CCLabelBMFont()
{ {
@ -441,41 +442,41 @@ namespace cocos2d{
CCSize tmpSize = CCSizeZero; CCSize tmpSize = CCSizeZero;
int longestLine = 0; int longestLine = 0;
unsigned int totalHeight = 0; unsigned int totalHeight = 0;
unsigned int quantityOfLines = 1; unsigned int quantityOfLines = 1;
unsigned int stringLen = m_sString.length(); unsigned int stringLen = m_sString.length();
if (0 == stringLen) if (0 == stringLen)
{ {
return; return;
} }
for (unsigned int i = 0; i < stringLen - 1; ++i) for (unsigned int i = 0; i < stringLen - 1; ++i)
{ {
unsigned short c = m_sString[i]; unsigned short c = m_sString[i];
if (c == '\n') if (c == '\n')
{ {
quantityOfLines++; quantityOfLines++;
} }
} }
totalHeight = m_pConfiguration->m_uCommonHeight * quantityOfLines; totalHeight = m_pConfiguration->m_uCommonHeight * quantityOfLines;
nextFontPositionY = -(m_pConfiguration->m_uCommonHeight - m_pConfiguration->m_uCommonHeight * quantityOfLines); nextFontPositionY = -(m_pConfiguration->m_uCommonHeight - m_pConfiguration->m_uCommonHeight * quantityOfLines);
for (unsigned int i= 0; i < stringLen; i++) for (unsigned int i= 0; i < stringLen; i++)
{ {
unsigned short c = m_sString[i]; unsigned short c = m_sString[i];
CCAssert( c < kCCBMFontMaxChars, "LabelBMFont: character outside bounds"); CCAssert( c < kCCBMFontMaxChars, "LabelBMFont: character outside bounds");
if (c == '\n') if (c == '\n')
{ {
nextFontPositionX = 0; nextFontPositionX = 0;
nextFontPositionY -= m_pConfiguration->m_uCommonHeight; nextFontPositionY -= m_pConfiguration->m_uCommonHeight;
continue; continue;
} }
kerningAmount = this->kerningAmountForFirst(prev, c); kerningAmount = this->kerningAmountForFirst(prev, c);
@ -525,13 +526,13 @@ namespace cocos2d{
fontChar->setOpacity(m_cOpacity); fontChar->setOpacity(m_cOpacity);
} }
if (longestLine < nextFontPositionX) if (longestLine < nextFontPositionX)
{ {
longestLine = nextFontPositionX; longestLine = nextFontPositionX;
} }
} }
tmpSize.width = (float) longestLine; tmpSize.width = (float) longestLine;
tmpSize.height = (float) totalHeight; tmpSize.height = (float) totalHeight;
this->setContentSizeInPixels(tmpSize); this->setContentSizeInPixels(tmpSize);
@ -543,17 +544,18 @@ namespace cocos2d{
m_sString.clear(); m_sString.clear();
m_sString = newString; m_sString = newString;
if (m_pChildren && m_pChildren->count() != 0) if (m_pChildren && m_pChildren->count() != 0)
{ {
CCObject* child; CCObject* child;
CCARRAY_FOREACH(m_pChildren, child) CCARRAY_FOREACH(m_pChildren, child)
{ {
CCNode* pNode = (CCNode*) child; CCNode* pNode = (CCNode*) child;
if (pNode) if (pNode)
{ {
pNode->setIsVisible(false); pNode->setIsVisible(false);
} }
} } }
}
this->createFontChars(); this->createFontChars();
} }
@ -571,17 +573,18 @@ namespace cocos2d{
void CCLabelBMFont::setColor(const ccColor3B& var) void CCLabelBMFont::setColor(const ccColor3B& var)
{ {
m_tColor = var; m_tColor = var;
if (m_pChildren && m_pChildren->count() != 0) if (m_pChildren && m_pChildren->count() != 0)
{ {
CCObject* child; CCObject* child;
CCARRAY_FOREACH(m_pChildren, child) CCARRAY_FOREACH(m_pChildren, child)
{ {
CCSprite* pNode = (CCSprite*) child; CCSprite* pNode = (CCSprite*) child;
if (pNode) if (pNode)
{ {
pNode->setColor(m_tColor); pNode->setColor(m_tColor);
} }
} } }
}
} }
const ccColor3B& CCLabelBMFont::getColor() const ccColor3B& CCLabelBMFont::getColor()
{ {
@ -591,21 +594,22 @@ namespace cocos2d{
{ {
m_cOpacity = var; m_cOpacity = var;
if (m_pChildren && m_pChildren->count() != 0) if (m_pChildren && m_pChildren->count() != 0)
{ {
CCObject* child; CCObject* child;
CCARRAY_FOREACH(m_pChildren, child) CCARRAY_FOREACH(m_pChildren, child)
{ {
CCNode* pNode = (CCNode*) child; CCNode* pNode = (CCNode*) child;
if (pNode) if (pNode)
{ {
CCRGBAProtocol *pRGBAProtocol = pNode->convertToRGBAProtocol(); CCRGBAProtocol *pRGBAProtocol = pNode->convertToRGBAProtocol();
if (pRGBAProtocol) if (pRGBAProtocol)
{ {
pRGBAProtocol->setOpacity(m_cOpacity); pRGBAProtocol->setOpacity(m_cOpacity);
} }
} }
} } }
}
} }
GLubyte CCLabelBMFont::getOpacity() GLubyte CCLabelBMFont::getOpacity()
{ {
@ -614,21 +618,22 @@ namespace cocos2d{
void CCLabelBMFont::setIsOpacityModifyRGB(bool var) void CCLabelBMFont::setIsOpacityModifyRGB(bool var)
{ {
m_bIsOpacityModifyRGB = var; m_bIsOpacityModifyRGB = var;
if (m_pChildren && m_pChildren->count() != 0) if (m_pChildren && m_pChildren->count() != 0)
{ {
CCObject* child; CCObject* child;
CCARRAY_FOREACH(m_pChildren, child) CCARRAY_FOREACH(m_pChildren, child)
{ {
CCNode* pNode = (CCNode*) child; CCNode* pNode = (CCNode*) child;
if (pNode) if (pNode)
{ {
CCRGBAProtocol *pRGBAProtocol = pNode->convertToRGBAProtocol(); CCRGBAProtocol *pRGBAProtocol = pNode->convertToRGBAProtocol();
if (pRGBAProtocol) if (pRGBAProtocol)
{ {
pRGBAProtocol->setIsOpacityModifyRGB(m_bIsOpacityModifyRGB); pRGBAProtocol->setIsOpacityModifyRGB(m_bIsOpacityModifyRGB);
} }
} }
} } }
}
} }
bool CCLabelBMFont::getIsOpacityModifyRGB() bool CCLabelBMFont::getIsOpacityModifyRGB()
{ {
@ -658,5 +663,5 @@ namespace cocos2d{
ccDrawPoly(vertices, 4, true); ccDrawPoly(vertices, 4, true);
} }
#endif // CC_LABELBMFONT_DEBUG_DRAW #endif // CC_LABELBMFONT_DEBUG_DRAW
} }