Merge pull request #13101 from WenhaiLin/v3.8-label-code-style

Improve coding style
This commit is contained in:
子龙山人 2015-07-30 13:44:53 +08:00
commit cd4d8b7880
13 changed files with 104 additions and 202 deletions

View File

@ -1,104 +0,0 @@
/****************************************************************************
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 "2d/CCFont.h"
#include "base/ccUTF8.h"
NS_CC_BEGIN
const char * Font::_glyphASCII = "\"!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ ";
const char * Font::_glyphNEHE = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ";
Font::Font() :
_usedGlyphs(GlyphCollection::ASCII)
, _customGlyphs(nullptr)
{
}
const char * Font::getGlyphCollection(GlyphCollection glyphs) const
{
switch (glyphs)
{
case GlyphCollection::NEHE:
return _glyphNEHE;
break;
case GlyphCollection::ASCII:
return _glyphASCII;
break;
default:
return 0;
break;
}
}
void Font::setCurrentGlyphCollection(GlyphCollection glyphs, const char *customGlyphs)
{
if (_customGlyphs)
delete [] _customGlyphs;
switch (glyphs)
{
case GlyphCollection::NEHE:
_customGlyphs = 0;
break;
case GlyphCollection::ASCII:
_customGlyphs = 0;
break;
default:
if (customGlyphs)
{
size_t length = strlen(customGlyphs);
_customGlyphs = new char [length + 2];
memcpy(_customGlyphs, customGlyphs, length);
_customGlyphs[length] = 0;
_customGlyphs[length+1] = 0;
}
break;
}
_usedGlyphs = glyphs;
}
const char * Font::getCurrentGlyphCollection() const
{
if (_customGlyphs)
{
return _customGlyphs;
}
else
{
return getGlyphCollection(_usedGlyphs);
}
}
NS_CC_END

View File

@ -29,7 +29,7 @@
/// @cond DO_NOT_SHOW /// @cond DO_NOT_SHOW
#include <string> #include <string>
#include "2d/CCLabel.h" #include "base/ccTypes.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -38,31 +38,11 @@ class FontAtlas;
class CC_DLL Font : public Ref class CC_DLL Font : public Ref
{ {
public: public:
virtual FontAtlas *createFontAtlas() = 0; virtual FontAtlas* createFontAtlas() = 0;
virtual int* getHorizontalKerningForTextUTF16(const std::u16string& text, int &outNumLetters) const = 0; virtual int* getHorizontalKerningForTextUTF16(const std::u16string& text, int &outNumLetters) const = 0;
virtual const char* getCurrentGlyphCollection() const;
virtual int getFontMaxHeight() const { return 0; } virtual int getFontMaxHeight() const { return 0; }
protected:
Font();
/**
* @js NA
* @lua NA
*/
virtual ~Font() {}
void setCurrentGlyphCollection(GlyphCollection glyphs, const char *customGlyphs = 0);
const char * getGlyphCollection(GlyphCollection glyphs) const;
GlyphCollection _usedGlyphs;
char * _customGlyphs;
static const char * _glyphASCII;
static const char * _glyphNEHE;
}; };
NS_CC_END NS_CC_END

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2013 Zynga Inc. Copyright (c) 2013 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc. Copyright (c) 2013-2015 Chukong Technologies Inc.
http://www.cocos2d-x.org http://www.cocos2d-x.org

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2013 Zynga Inc. Copyright (c) 2013 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc. Copyright (c) 2013-2015 Chukong Technologies Inc.
http://www.cocos2d-x.org http://www.cocos2d-x.org
@ -26,12 +26,12 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include "base/CCDirector.h"
#include "2d/CCFontFNT.h" #include "2d/CCFontFNT.h"
#include "2d/CCFontFreeType.h" #include "2d/CCFontFreeType.h"
#include "2d/CCFontAtlas.h" #include "2d/CCFontAtlas.h"
#include "2d/CCFontCharMap.h" #include "2d/CCFontCharMap.h"
#include "base/CCDirector.h" #include "2d/CCLabel.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -45,26 +45,26 @@ void FontAtlasCache::purgeCachedData()
} }
} }
FontAtlas * FontAtlasCache::getFontAtlasTTF(const TTFConfig & config) FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config)
{ {
bool useDistanceField = config.distanceFieldEnabled; bool useDistanceField = config->distanceFieldEnabled;
if(config.outlineSize > 0) if(config->outlineSize > 0)
{ {
useDistanceField = false; useDistanceField = false;
} }
auto atlasName = generateFontName(config.fontFilePath, config.fontSize, GlyphCollection::DYNAMIC, useDistanceField); auto atlasName = generateFontName(config->fontFilePath, config->fontSize, useDistanceField);
atlasName.append("_outline_"); atlasName.append("_outline_");
std::stringstream ss; std::stringstream ss;
ss << config.outlineSize; ss << config->outlineSize;
atlasName.append(ss.str()); atlasName.append(ss.str());
auto it = _atlasMap.find(atlasName); auto it = _atlasMap.find(atlasName);
if ( it == _atlasMap.end() ) if ( it == _atlasMap.end() )
{ {
auto font = FontFreeType::create(config.fontFilePath, config.fontSize, config.glyphs, auto font = FontFreeType::create(config->fontFilePath, config->fontSize, config->glyphs,
config.customGlyphs, useDistanceField, config.outlineSize); config->customGlyphs, useDistanceField, config->outlineSize);
if (font) if (font)
{ {
auto tempAtlas = font->createFontAtlas(); auto tempAtlas = font->createFontAtlas();
@ -84,9 +84,9 @@ FontAtlas * FontAtlasCache::getFontAtlasTTF(const TTFConfig & config)
return nullptr; return nullptr;
} }
FontAtlas * FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset /* = Vec2::ZERO */) FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset /* = Vec2::ZERO */)
{ {
std::string atlasName = generateFontName(fontFileName, 0, GlyphCollection::CUSTOM,false); std::string atlasName = generateFontName(fontFileName, 0,false);
auto it = _atlasMap.find(atlasName); auto it = _atlasMap.find(atlasName);
if ( it == _atlasMap.end() ) if ( it == _atlasMap.end() )
@ -112,9 +112,9 @@ FontAtlas * FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, con
return nullptr; return nullptr;
} }
FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile) FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile)
{ {
std::string atlasName = generateFontName(plistFile, 0, GlyphCollection::CUSTOM,false); std::string atlasName = generateFontName(plistFile, 0,false);
auto it = _atlasMap.find(atlasName); auto it = _atlasMap.find(atlasName);
if ( it == _atlasMap.end() ) if ( it == _atlasMap.end() )
@ -140,11 +140,11 @@ FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile)
return nullptr; return nullptr;
} }
FontAtlas * FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
{ {
char tmp[30]; char tmp[30];
sprintf(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap); sprintf(tmp,"name:%u_%d_%d_%d",texture->getName(),itemWidth,itemHeight,startCharMap);
std::string atlasName = generateFontName(tmp, 0, GlyphCollection::CUSTOM,false); std::string atlasName = generateFontName(tmp, 0,false);
auto it = _atlasMap.find(atlasName); auto it = _atlasMap.find(atlasName);
if ( it == _atlasMap.end() ) if ( it == _atlasMap.end() )
@ -170,12 +170,12 @@ FontAtlas * FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidt
return nullptr; return nullptr;
} }
FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
{ {
char tmp[255]; char tmp[255];
snprintf(tmp,250,"name:%s_%d_%d_%d",charMapFile.c_str(),itemWidth,itemHeight,startCharMap); snprintf(tmp,250,"name:%s_%d_%d_%d",charMapFile.c_str(),itemWidth,itemHeight,startCharMap);
std::string atlasName = generateFontName(tmp, 0, GlyphCollection::CUSTOM,false); std::string atlasName = generateFontName(tmp, 0,false);
auto it = _atlasMap.find(atlasName); auto it = _atlasMap.find(atlasName);
if ( it == _atlasMap.end() ) if ( it == _atlasMap.end() )
@ -201,31 +201,10 @@ FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile,
return nullptr; return nullptr;
} }
std::string FontAtlasCache::generateFontName(const std::string& fontFileName, int size, GlyphCollection theGlyphs, bool useDistanceField) std::string FontAtlasCache::generateFontName(const std::string& fontFileName, int size, bool useDistanceField)
{ {
std::string tempName(fontFileName); std::string tempName(fontFileName);
switch (theGlyphs)
{
case GlyphCollection::DYNAMIC:
tempName.append("_DYNAMIC_");
break;
case GlyphCollection::NEHE:
tempName.append("_NEHE_");
break;
case GlyphCollection::ASCII:
tempName.append("_ASCII_");
break;
case GlyphCollection::CUSTOM:
tempName.append("_CUSTOM_");
break;
default:
break;
}
if(useDistanceField) if(useDistanceField)
tempName.append("df"); tempName.append("df");
// std::to_string is not supported on android, using std::stringstream instead. // std::to_string is not supported on android, using std::stringstream instead.

View File

@ -29,22 +29,23 @@
/// @cond DO_NOT_SHOW /// @cond DO_NOT_SHOW
#include <unordered_map> #include <unordered_map>
#include "base/ccTypes.h"
#include "2d/CCLabel.h"
NS_CC_BEGIN NS_CC_BEGIN
class FontAtlas; class FontAtlas;
class Texture2D;
struct _ttfConfig;
class CC_DLL FontAtlasCache class CC_DLL FontAtlasCache
{ {
public: public:
static FontAtlas * getFontAtlasTTF(const TTFConfig & config); static FontAtlas* getFontAtlasTTF(const _ttfConfig* config);
static FontAtlas * getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset = Vec2::ZERO); static FontAtlas* getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset = Vec2::ZERO);
static FontAtlas * getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap); 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(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
static FontAtlas * getFontAtlasCharMap(const std::string& plistFile); static FontAtlas* getFontAtlasCharMap(const std::string& plistFile);
static bool releaseFontAtlas(FontAtlas *atlas); static bool releaseFontAtlas(FontAtlas *atlas);
@ -54,7 +55,7 @@ public:
static void purgeCachedData(); static void purgeCachedData();
private: private:
static std::string generateFontName(const std::string& fontFileName, int size, GlyphCollection theGlyphs, bool useDistanceField); static std::string generateFontName(const std::string& fontFileName, int size, bool useDistanceField);
static std::unordered_map<std::string, FontAtlas *> _atlasMap; static std::unordered_map<std::string, FontAtlas *> _atlasMap;
}; };

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2013 Zynga Inc. Copyright (c) 2013 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc. Copyright (c) 2013-2015 Chukong Technologies Inc.
http://www.cocos2d-x.org http://www.cocos2d-x.org

View File

@ -32,6 +32,7 @@
NS_CC_BEGIN NS_CC_BEGIN
class Texture2D;
class FontCharMap : public Font class FontCharMap : public Font
{ {
public: public:

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
Copyright (c) 2013 Zynga Inc. Copyright (c) 2013 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc. Copyright (c) 2013-2015 Chukong Technologies Inc.
http://www.cocos2d-x.org http://www.cocos2d-x.org

View File

@ -38,6 +38,9 @@ FT_Library FontFreeType::_FTlibrary;
bool FontFreeType::_FTInitialized = false; bool FontFreeType::_FTInitialized = false;
const int FontFreeType::DistanceMapSpread = 3; const int FontFreeType::DistanceMapSpread = 3;
const char* FontFreeType::_glyphASCII = "\"!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ ";
const char* FontFreeType::_glyphNEHE = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ";
typedef struct _DataRef typedef struct _DataRef
{ {
Data data; Data data;
@ -53,7 +56,7 @@ FontFreeType * FontFreeType::create(const std::string &fontName, int fontSize, G
if (!tempFont) if (!tempFont)
return nullptr; return nullptr;
tempFont->setCurrentGlyphCollection(glyphs, customGlyphs); tempFont->setGlyphCollection(glyphs, customGlyphs);
if (!tempFont->createFontObject(fontName, fontSize)) if (!tempFont->createFontObject(fontName, fontSize))
{ {
@ -100,6 +103,7 @@ FontFreeType::FontFreeType(bool distanceFieldEnabled /* = false */,int outline /
, _lineHeight(0) , _lineHeight(0)
, _fontAtlas(nullptr) , _fontAtlas(nullptr)
, _encoding(FT_ENCODING_UNICODE) , _encoding(FT_ENCODING_UNICODE)
, _usedGlyphs(GlyphCollection::ASCII)
{ {
if (outline > 0) if (outline > 0)
{ {
@ -202,7 +206,7 @@ FontAtlas * FontFreeType::createFontAtlas()
if (_fontAtlas && _usedGlyphs != GlyphCollection::DYNAMIC) if (_fontAtlas && _usedGlyphs != GlyphCollection::DYNAMIC)
{ {
std::u16string utf16; std::u16string utf16;
if (StringUtils::UTF8ToUTF16(getCurrentGlyphCollection(), utf16)) if (StringUtils::UTF8ToUTF16(getGlyphCollection(), utf16))
{ {
_fontAtlas->prepareLetterDefinitions(utf16); _fontAtlas->prepareLetterDefinitions(utf16);
} }
@ -601,4 +605,36 @@ void FontFreeType::renderCharAt(unsigned char *dest,int posX, int posY, unsigned
} }
} }
void FontFreeType::setGlyphCollection(GlyphCollection glyphs, const char* customGlyphs /* = nullptr */)
{
_usedGlyphs = glyphs;
if (glyphs == GlyphCollection::CUSTOM)
{
_customGlyphs = customGlyphs;
}
}
const char* FontFreeType::getGlyphCollection() const
{
const char* glyphCollection = nullptr;
switch (_usedGlyphs)
{
case cocos2d::GlyphCollection::DYNAMIC:
break;
case cocos2d::GlyphCollection::NEHE:
glyphCollection = _glyphNEHE;
break;
case cocos2d::GlyphCollection::ASCII:
glyphCollection = _glyphASCII;
break;
case cocos2d::GlyphCollection::CUSTOM:
glyphCollection = _customGlyphs.c_str();
break;
default:
break;
}
return glyphCollection;
}
NS_CC_END NS_CC_END

View File

@ -75,6 +75,11 @@ public:
virtual FontAtlas* createFontAtlas() override; virtual FontAtlas* createFontAtlas() override;
virtual int getFontMaxHeight() const override { return _lineHeight; } virtual int getFontMaxHeight() const override { return _lineHeight; }
private: private:
static const char* _glyphASCII;
static const char* _glyphNEHE;
static FT_Library _FTlibrary;
static bool _FTInitialized;
FontFreeType(bool distanceFieldEnabled = false, int outline = 0); FontFreeType(bool distanceFieldEnabled = false, int outline = 0);
virtual ~FontFreeType(); virtual ~FontFreeType();
@ -86,17 +91,21 @@ private:
int getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar) const; int getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar) const;
unsigned char* getGlyphBitmapWithOutline(unsigned short code, FT_BBox &bbox); unsigned char* getGlyphBitmapWithOutline(unsigned short code, FT_BBox &bbox);
static FT_Library _FTlibrary; void setGlyphCollection(GlyphCollection glyphs, const char* customGlyphs = nullptr);
static bool _FTInitialized; const char* getGlyphCollection() const;
FT_Face _fontRef; FT_Face _fontRef;
FT_Stroker _stroker; FT_Stroker _stroker;
FT_Encoding _encoding;
std::string _fontName; std::string _fontName;
bool _distanceFieldEnabled; bool _distanceFieldEnabled;
float _outlineSize; float _outlineSize;
int _lineHeight; int _lineHeight;
FontAtlas* _fontAtlas; FontAtlas* _fontAtlas;
FT_Encoding _encoding; GlyphCollection _usedGlyphs;
std::string _customGlyphs;
}; };
/// @endcond /// @endcond

View File

@ -528,7 +528,7 @@ void Label::setFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = false *
bool Label::setTTFConfig(const TTFConfig& ttfConfig) bool Label::setTTFConfig(const TTFConfig& ttfConfig)
{ {
FontAtlas *newAtlas = FontAtlasCache::getFontAtlasTTF(ttfConfig); FontAtlas *newAtlas = FontAtlasCache::getFontAtlasTTF(&ttfConfig);
if (!newAtlas) if (!newAtlas)
{ {

View File

@ -38,19 +38,6 @@ NS_CC_BEGIN
* @{ * @{
*/ */
/**
* @brief Possible GlyphCollection used by Label.
*
* Specify a collections of characters to be load when Label created.
* Consider using DYNAMIC.
*/
enum class GlyphCollection {
DYNAMIC,
NEHE,
ASCII,
CUSTOM
};
/** /**
* @struct TTFConfig * @struct TTFConfig

View File

@ -427,6 +427,19 @@ enum class CC_DLL TextHAlignment
RIGHT RIGHT
}; };
/**
* @brief Possible GlyphCollection used by Label.
*
* Specify a collections of characters to be load when Label created.
* Consider using DYNAMIC.
*/
enum class GlyphCollection {
DYNAMIC,
NEHE,
ASCII,
CUSTOM
};
// Types for animation in particle systems // Types for animation in particle systems
/** @struct T2F_Quad /** @struct T2F_Quad