Merge commit 'refs/pull/5086/head' of git://github.com/cocos2d/cocos2d-x into new-label-atlas

This commit is contained in:
James Chen 2014-01-17 11:10:04 +08:00
commit 31199183b2
18 changed files with 542 additions and 12 deletions

View File

@ -47,6 +47,7 @@ CCEventListenerTouch.cpp \
CCEventMouse.cpp \
CCEventTouch.cpp \
CCFont.cpp \
CCFontCharMap.cpp \
CCFontAtlas.cpp \
CCFontAtlasCache.cpp \
CCFontAtlasFactory.cpp \

View File

@ -28,6 +28,7 @@
#include "CCFontFNT.h"
#include "CCFontFreeType.h"
#include "CCFontCharMap.h"
#include "edtaa3func.h"
NS_CC_BEGIN
@ -116,6 +117,21 @@ Font* Font::createWithFNT(const std::string& fntFilePath)
return FontFNT::create(fntFilePath);
}
Font* Font::createWithCharMap(const std::string& plistFile)
{
return FontCharMap::create(plistFile);
}
Font* Font::createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
{
return FontCharMap::create(texture,itemWidth,itemHeight,startCharMap);
}
Font* Font::createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
{
return FontCharMap::create(charMapFile,itemWidth,itemHeight,startCharMap);
}
unsigned char * Font::makeDistanceMap( unsigned char *img, unsigned int width, unsigned int height)
{
unsigned int pixelAmount = (width + 2 * DistanceMapSpread) * (height + 2 * DistanceMapSpread);

View File

@ -46,6 +46,10 @@ public:
static Font* createWithTTF(const std::string& fntName, int fontSize, GlyphCollection glyphs, const char *customGlyphs);
static Font* createWithFNT(const std::string& fntFilePath);
static Font * createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
static Font * createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
static Font * createWithCharMap(const std::string& plistFile);
static unsigned char * makeDistanceMap(unsigned char *img, unsigned int width, unsigned int height);
void setDistanceFieldEnabled(bool distanceFieldEnabled);
bool isDistanceFieldEnabled() const { return _distanceFieldEnabled;}

View File

@ -69,6 +69,65 @@ FontAtlas * FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName)
return tempAtlas;
}
FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile)
{
std::string atlasName = generateFontName(plistFile, 0, GlyphCollection::CUSTOM,false);
FontAtlas *tempAtlas = _atlasMap[atlasName];
if ( !tempAtlas )
{
tempAtlas = FontAtlasFactory::createAtlasFromCharMap(plistFile);
if (tempAtlas)
_atlasMap[atlasName] = tempAtlas;
}
else
{
tempAtlas->retain();
}
return tempAtlas;
}
FontAtlas * FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
{
char tmp[30];
sprintf(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap);
std::string atlasName = generateFontName(tmp, 0, GlyphCollection::CUSTOM,false);
FontAtlas *tempAtlas = _atlasMap[atlasName];
if ( !tempAtlas )
{
tempAtlas = FontAtlasFactory::createAtlasFromCharMap(texture,itemWidth,itemHeight,startCharMap);
if (tempAtlas)
_atlasMap[atlasName] = tempAtlas;
}
else
{
tempAtlas->retain();
}
return tempAtlas;
}
FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
{
std::string atlasName = generateFontName(charMapFile, 0, GlyphCollection::CUSTOM,false);
FontAtlas *tempAtlas = _atlasMap[atlasName];
if ( !tempAtlas )
{
tempAtlas = FontAtlasFactory::createAtlasFromCharMap(charMapFile,itemWidth,itemHeight,startCharMap);
if (tempAtlas)
_atlasMap[atlasName] = tempAtlas;
}
else
{
tempAtlas->retain();
}
return tempAtlas;
}
std::string FontAtlasCache::generateFontName(const std::string& fontFileName, int size, GlyphCollection theGlyphs, bool useDistanceField)
{
std::string tempName(fontFileName);

View File

@ -36,16 +36,17 @@ NS_CC_BEGIN
class CC_DLL FontAtlasCache
{
public:
static FontAtlas * getFontAtlasTTF(const std::string& fontFileName, int size, GlyphCollection glyphs, const char *customGlyphs = 0, bool useDistanceField = false);
static FontAtlas * getFontAtlasFNT(const std::string& fontFileName);
static FontAtlas * getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
static FontAtlas * getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
static FontAtlas * getFontAtlasCharMap(const std::string& plistFile);
static bool releaseFontAtlas(FontAtlas *atlas);
private:
static std::string generateFontName(const std::string& fontFileName, int size, GlyphCollection theGlyphs, bool useDistanceField);
static std::unordered_map<std::string, FontAtlas *> _atlasMap;
};

View File

@ -60,4 +60,46 @@ FontAtlas * FontAtlasFactory::createAtlasFromFNT(const std::string& fntFilePath)
}
}
FontAtlas * FontAtlasFactory::createAtlasFromCharMap(const std::string& plistFile)
{
Font *font = Font::createWithCharMap(plistFile);
if(font)
{
return font->createFontAtlas();
}
else
{
return nullptr;
}
}
FontAtlas * FontAtlasFactory::createAtlasFromCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
{
Font *font = Font::createWithCharMap(texture,itemWidth,itemHeight,startCharMap);
if(font)
{
return font->createFontAtlas();
}
else
{
return nullptr;
}
}
FontAtlas * FontAtlasFactory::createAtlasFromCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
{
Font *font = Font::createWithCharMap(charMapFile,itemWidth,itemHeight,startCharMap);
if(font)
{
return font->createFontAtlas();
}
else
{
return nullptr;
}
}
NS_CC_END

View File

@ -40,6 +40,10 @@ public:
static FontAtlas * createAtlasFromTTF(const std::string& fntFilePath, int fontSize, GlyphCollection glyphs, const char *customGlyphs = 0, bool useDistanceField = false);
static FontAtlas * createAtlasFromFNT(const std::string& fntFilePath);
static FontAtlas * createAtlasFromCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
static FontAtlas * createAtlasFromCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
static FontAtlas * createAtlasFromCharMap(const std::string& plistFile);
private:
};

171
cocos/2d/CCFontCharMap.cpp Normal file
View File

@ -0,0 +1,171 @@
/****************************************************************************
Copyright (c) 2013 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies 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.
****************************************************************************/
#include "CCFontCharMap.h"
#include "CCFontAtlas.h"
NS_CC_BEGIN
FontCharMap * FontCharMap::create(const std::string& plistFile)
{
std::string pathStr = FileUtils::getInstance()->fullPathForFilename(plistFile);
std::string relPathStr = pathStr.substr(0, pathStr.find_last_of("/"))+"/";
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(pathStr.c_str());
CCASSERT(dict["version"].asInt() == 1, "Unsupported version. Upgrade cocos2d version");
std::string textureFilename = relPathStr + dict["textureFilename"].asString();
unsigned int width = dict["itemWidth"].asInt() / CC_CONTENT_SCALE_FACTOR();
unsigned int height = dict["itemHeight"].asInt() / CC_CONTENT_SCALE_FACTOR();
unsigned int startChar = dict["firstChar"].asInt();
Texture2D *tempTexture = Director::getInstance()->getTextureCache()->addImage(textureFilename);
if (!tempTexture)
{
return nullptr;
}
FontCharMap *tempFont = new FontCharMap(tempTexture,width,height,startChar);
if (!tempFont)
{
return nullptr;
}
tempFont->autorelease();
return tempFont;
}
FontCharMap* FontCharMap::create(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
{
Texture2D *tempTexture = Director::getInstance()->getTextureCache()->addImage(charMapFile);
if (!tempTexture)
{
return nullptr;
}
FontCharMap *tempFont = new FontCharMap(tempTexture,itemWidth,itemHeight,startCharMap);
if (!tempFont)
{
return nullptr;
}
tempFont->autorelease();
return tempFont;
}
FontCharMap* FontCharMap::create(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
{
FontCharMap *tempFont = new FontCharMap(texture,itemWidth,itemHeight,startCharMap);
if (!tempFont)
{
return nullptr;
}
tempFont->autorelease();
return tempFont;
}
FontCharMap::~FontCharMap()
{
}
Size * FontCharMap::getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const
{
if (!text)
return 0;
outNumLetters = cc_wcslen(text);
if (!outNumLetters)
return 0;
Size *sizes = new Size[outNumLetters];
if (!sizes)
return 0;
int advance = _itemWidth * CC_CONTENT_SCALE_FACTOR();
for (int c = 0; c < outNumLetters; ++c)
{
sizes[c].width = advance;
}
return sizes;
}
Rect FontCharMap::getRectForChar(unsigned short theChar) const
{
return _charRect;
}
FontAtlas * FontCharMap::createFontAtlas()
{
FontAtlas *tempAtlas = new FontAtlas(*this);
if (!tempAtlas)
return nullptr;
Size s = _texture->getContentSize();
int itemsPerColumn = (int)(s.height / _itemHeight);
int itemsPerRow = (int)(s.width / _itemWidth);
tempAtlas->setCommonLineHeight(_itemHeight);
FontLetterDefinition tempDefinition;
tempDefinition.textureID = 0;
tempDefinition.anchorX = 0.5f;
tempDefinition.anchorY = 0.5f;
tempDefinition.offsetX = 0.0f;
tempDefinition.offsetY = 0.0f;
tempDefinition.validDefinition = true;
tempDefinition.width = _itemWidth;
tempDefinition.height = _itemHeight;
int charId = _mapStartChar;
float itemWidthInPixels = _itemWidth * CC_CONTENT_SCALE_FACTOR();
float itemHeightInPixels = _itemHeight * CC_CONTENT_SCALE_FACTOR();
for (int row = 0; row < itemsPerColumn; ++row)
{
for (int col = 0; col < itemsPerRow; ++col)
{
tempDefinition.letteCharUTF16 = charId;
tempDefinition.U = _itemWidth * col;
tempDefinition.V = _itemHeight * row;
tempAtlas->addLetterDefinition(tempDefinition);
charId++;
}
}
tempAtlas->addTexture(*_texture,0);
return tempAtlas;
}
NS_CC_END

65
cocos/2d/CCFontCharMap.h Normal file
View File

@ -0,0 +1,65 @@
/****************************************************************************
Copyright (c) 2013 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies 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.
****************************************************************************/
#ifndef _CCFontCharMap_h_
#define _CCFontCharMap_h_
#include "cocos2d.h"
#include "CCFont.h"
NS_CC_BEGIN
class FontCharMap : public Font
{
public:
static FontCharMap * create(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
static FontCharMap * create(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
static FontCharMap * create(const std::string& plistFile);
virtual Size* getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const override;
virtual Rect getRectForChar(unsigned short theChar) const override;
virtual FontAtlas *createFontAtlas() override;
protected:
FontCharMap(Texture2D* texture,int itemWidth, int itemHeight, int startCharMap) :
_texture(texture),_itemWidth(itemWidth),_itemHeight(itemHeight),_mapStartChar(startCharMap),_charRect(0,0,itemWidth,itemHeight){}
/**
* @js NA
* @lua NA
*/
virtual ~FontCharMap();
private:
Texture2D* _texture;
int _mapStartChar;
int _itemWidth;
int _itemHeight;
Rect _charRect;
};
NS_CC_END
#endif /* defined(_CCFontCharMap_h_) */

View File

@ -92,6 +92,93 @@ Label* Label::createWithBMFont(const std::string& bmfontFilePath, const std::str
}
}
Label* Label::createWithCharMap(const std::string& plistFile)
{
Label *ret = new Label();
if (!ret)
return nullptr;
if (ret->setCharMap(plistFile))
{
ret->autorelease();
return ret;
}
else
{
delete ret;
return nullptr;
}
}
Label* Label::createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
{
Label *ret = new Label();
if (!ret)
return nullptr;
if (ret->setCharMap(texture,itemWidth,itemHeight,startCharMap))
{
ret->autorelease();
return ret;
}
else
{
delete ret;
return nullptr;
}
}
Label* Label::createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
{
Label *ret = new Label();
if (!ret)
return nullptr;
if (ret->setCharMap(charMapFile,itemWidth,itemHeight,startCharMap))
{
ret->autorelease();
return ret;
}
else
{
delete ret;
return nullptr;
}
}
bool Label::setCharMap(const std::string& plistFile)
{
FontAtlas *newAtlas = FontAtlasCache::getFontAtlasCharMap(plistFile);
if (!newAtlas)
return false;
return initWithFontAtlas(newAtlas);
}
bool Label::setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
{
FontAtlas *newAtlas = FontAtlasCache::getFontAtlasCharMap(texture,itemWidth,itemHeight,startCharMap);
if (!newAtlas)
return false;
return initWithFontAtlas(newAtlas);
}
bool Label::setCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
{
FontAtlas *newAtlas = FontAtlasCache::getFontAtlasCharMap(charMapFile,itemWidth,itemHeight,startCharMap);
if (!newAtlas)
return false;
return initWithFontAtlas(newAtlas);
}
Label::Label(FontAtlas *atlas, TextHAlignment alignment, bool useDistanceField,bool useA8Shader)
: _reusedLetter(nullptr)
, _commonLineHeight(0.0f)

View File

@ -62,12 +62,12 @@ typedef struct _ttfConfig
const char *customGlyphs;
bool distanceFieldEnabled;
_ttfConfig(const char* filePath,int fontSize = 36, const GlyphCollection& glyphs = GlyphCollection::NEHE,
const char *customGlyphs = nullptr,bool useDistanceField = false)
_ttfConfig(const char* filePath,int size = 36, const GlyphCollection& glyphCollection = GlyphCollection::NEHE,
const char *customGlyphCollection = nullptr,bool useDistanceField = false)
:fontFilePath(filePath)
,fontSize(fontSize)
,glyphs(glyphs)
,customGlyphs(customGlyphs)
,fontSize(size)
,glyphs(glyphCollection)
,customGlyphs(customGlyphCollection)
,distanceFieldEnabled(useDistanceField)
{}
}TTFConfig;
@ -82,10 +82,18 @@ public:
static Label* createWithBMFont(const std::string& bmfontFilePath, const std::string& text,const TextHAlignment& alignment = TextHAlignment::CENTER, int lineWidth = 0);
static Label * createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
static Label * createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
static Label * createWithCharMap(const std::string& plistFile);
bool setTTFConfig(const TTFConfig& ttfConfig);
bool setBMFontFilePath(const std::string& bmfontFilePath);
bool setCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
bool setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
bool setCharMap(const std::string& plistFile);
bool setString(const std::string& text, const TextHAlignment& alignment = TextHAlignment::CENTER, float lineWidth = -1, bool lineBreakWithoutSpaces = false);
//only support for TTF

View File

@ -353,7 +353,7 @@ bool LabelTextFormatter::createStringSprites(LabelTextFormatProtocol *theLabel)
Point fontPos = Point((float)nextFontPositionX + charXOffset + charRect.size.width * 0.5f + kerningAmount,
(float)nextFontPositionY + yOffset - charRect.size.height * 0.5f);
(float)nextFontPositionY + yOffset - charRect.size.height * 0.5f);
if( theLabel->recordLetterInfo(CC_POINT_PIXELS_TO_POINTS(fontPos),c,i) == false)
{

View File

@ -71,6 +71,7 @@ set(COCOS2D_SRC
CCFontDefinition.cpp
CCFontFNT.cpp
CCFontFreeType.cpp
CCFontCharMap.cpp
CCLabel.cpp
CCLabelAtlas.cpp
CCLabelBMFont.cpp

View File

@ -248,6 +248,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou
<ClCompile Include="CCFontAtlas.cpp" />
<ClCompile Include="CCFontAtlasCache.cpp" />
<ClCompile Include="CCFontAtlasFactory.cpp" />
<ClCompile Include="CCFontCharMap.cpp" />
<ClCompile Include="CCFontDefinition.cpp" />
<ClCompile Include="CCFontFNT.cpp" />
<ClCompile Include="CCFontFreeType.cpp" />
@ -429,6 +430,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou
<ClInclude Include="CCFontAtlas.h" />
<ClInclude Include="CCFontAtlasCache.h" />
<ClInclude Include="CCFontAtlasFactory.h" />
<ClInclude Include="CCFontCharMap.h" />
<ClInclude Include="CCFontDefinition.h" />
<ClInclude Include="CCFontFNT.h" />
<ClInclude Include="CCFontFreeType.h" />

View File

@ -601,6 +601,9 @@
<ClCompile Include="renderer\CCBatchCommand.cpp">
<Filter>renderer</Filter>
</ClCompile>
<ClCompile Include="CCFontCharMap.cpp">
<Filter>label_nodes</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\physics\CCPhysicsBody.h">
@ -1213,5 +1216,8 @@
<ClInclude Include="renderer\CCBatchCommand.h">
<Filter>renderer</Filter>
</ClInclude>
<ClInclude Include="CCFontCharMap.h">
<Filter>label_nodes</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -67,6 +67,7 @@ static std::function<Layer*()> createFunctions[] =
CL(LabelBMFontTestNew),
CL(LabelTTFDistanceField),
CL(LabelTTFDistanceFieldEffect),
CL(LabelCharMapTest),
CL(LabelCrashTest)
};
@ -1244,6 +1245,52 @@ std::string LabelTTFDistanceFieldEffect::subtitle() const
return "Testing effect base on DistanceField";
}
LabelCharMapTest::LabelCharMapTest()
{
_time = 0.0f;
auto label1 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.plist");
addChild(label1, 0, kTagSprite1);
label1->setPosition( Point(10,100) );
label1->setOpacity( 200 );
auto label2 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.plist");
addChild(label2, 0, kTagSprite2);
label2->setPosition( Point(10,160) );
label2->setOpacity( 32 );
auto label3 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.png", 48, 64, ' ');
label3->setString("123 Test");
addChild(label3, 0, kTagSprite3);
label3->setPosition( Point(10,220) );
schedule(schedule_selector(LabelCharMapTest::step));
}
void LabelCharMapTest::step(float dt)
{
_time += dt;
char string[12] = {0};
sprintf(string, "%2.2f Test", _time);
auto label1 = (Label*)getChildByTag(kTagSprite1);
label1->setString(string);
auto label2 = (Label*)getChildByTag(kTagSprite2);
sprintf(string, "%d", (int)_time);
label2->setString(string);
}
std::string LabelCharMapTest::title() const
{
return "New Label + char map file";
}
std::string LabelCharMapTest::subtitle() const
{
return "Updating label should be fast.";
}
LabelCrashTest::LabelCrashTest()
{
auto size = Director::getInstance()->getWinSize();

View File

@ -347,6 +347,22 @@ public:
virtual std::string subtitle() const override;
};
class LabelCharMapTest : public AtlasDemoNew
{
public:
CREATE_FUNC(LabelCharMapTest);
LabelCharMapTest();
virtual std::string title() const override;
virtual std::string subtitle() const override;
void step(float dt);
private:
float _time;
};
class LabelCrashTest : public AtlasDemoNew
{
public:

View File

@ -1 +1 @@
5ae50c3f2080b46e18ba3f5aee4c5c686d54e13a
f056b511a4fb5efc921792e46fe57597f669de83