diff --git a/cocos/2d/CCFont.cpp b/cocos/2d/CCFont.cpp index 69616fd316..e69de29bb2 100644 --- a/cocos/2d/CCFont.cpp +++ b/cocos/2d/CCFont.cpp @@ -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 diff --git a/cocos/2d/CCFont.h b/cocos/2d/CCFont.h index a23119f986..3953070691 100644 --- a/cocos/2d/CCFont.h +++ b/cocos/2d/CCFont.h @@ -29,7 +29,7 @@ /// @cond DO_NOT_SHOW #include -#include "2d/CCLabel.h" +#include "base/ccTypes.h" NS_CC_BEGIN @@ -38,31 +38,11 @@ class FontAtlas; class CC_DLL Font : public Ref { public: - virtual FontAtlas *createFontAtlas() = 0; + virtual FontAtlas* createFontAtlas() = 0; virtual int* getHorizontalKerningForTextUTF16(const std::u16string& text, int &outNumLetters) const = 0; - virtual const char* getCurrentGlyphCollection() const; - 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 diff --git a/cocos/2d/CCFontAtlas.cpp b/cocos/2d/CCFontAtlas.cpp index d962f31f7c..e90bf0e116 100644 --- a/cocos/2d/CCFontAtlas.cpp +++ b/cocos/2d/CCFontAtlas.cpp @@ -1,6 +1,6 @@ /**************************************************************************** Copyright (c) 2013 Zynga Inc. - Copyright (c) 2013-2014 Chukong Technologies Inc. + Copyright (c) 2013-2015 Chukong Technologies Inc. http://www.cocos2d-x.org diff --git a/cocos/2d/CCFontAtlasCache.cpp b/cocos/2d/CCFontAtlasCache.cpp index f8c81a3b30..8ca4464c57 100644 --- a/cocos/2d/CCFontAtlasCache.cpp +++ b/cocos/2d/CCFontAtlasCache.cpp @@ -1,6 +1,6 @@ /**************************************************************************** Copyright (c) 2013 Zynga Inc. - Copyright (c) 2013-2014 Chukong Technologies Inc. + Copyright (c) 2013-2015 Chukong Technologies Inc. http://www.cocos2d-x.org @@ -26,12 +26,12 @@ #include #include - +#include "base/CCDirector.h" #include "2d/CCFontFNT.h" #include "2d/CCFontFreeType.h" #include "2d/CCFontAtlas.h" #include "2d/CCFontCharMap.h" -#include "base/CCDirector.h" +#include "2d/CCLabel.h" 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; - if(config.outlineSize > 0) + bool useDistanceField = config->distanceFieldEnabled; + if(config->outlineSize > 0) { useDistanceField = false; } - auto atlasName = generateFontName(config.fontFilePath, config.fontSize, GlyphCollection::DYNAMIC, useDistanceField); + auto atlasName = generateFontName(config->fontFilePath, config->fontSize, useDistanceField); atlasName.append("_outline_"); std::stringstream ss; - ss << config.outlineSize; + ss << config->outlineSize; atlasName.append(ss.str()); auto it = _atlasMap.find(atlasName); if ( it == _atlasMap.end() ) { - auto font = FontFreeType::create(config.fontFilePath, config.fontSize, config.glyphs, - config.customGlyphs, useDistanceField, config.outlineSize); + auto font = FontFreeType::create(config->fontFilePath, config->fontSize, config->glyphs, + config->customGlyphs, useDistanceField, config->outlineSize); if (font) { auto tempAtlas = font->createFontAtlas(); @@ -84,9 +84,9 @@ FontAtlas * FontAtlasCache::getFontAtlasTTF(const TTFConfig & config) 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); if ( it == _atlasMap.end() ) @@ -112,9 +112,9 @@ FontAtlas * FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, con 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); if ( it == _atlasMap.end() ) @@ -140,11 +140,11 @@ FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& plistFile) 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]; 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); if ( it == _atlasMap.end() ) @@ -170,12 +170,12 @@ FontAtlas * FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidt 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]; 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); if ( it == _atlasMap.end() ) @@ -201,31 +201,10 @@ FontAtlas * FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, 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); - 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) tempName.append("df"); // std::to_string is not supported on android, using std::stringstream instead. diff --git a/cocos/2d/CCFontAtlasCache.h b/cocos/2d/CCFontAtlasCache.h index 2bdd93ad3d..028edba0a5 100644 --- a/cocos/2d/CCFontAtlasCache.h +++ b/cocos/2d/CCFontAtlasCache.h @@ -29,22 +29,23 @@ /// @cond DO_NOT_SHOW #include - -#include "2d/CCLabel.h" +#include "base/ccTypes.h" NS_CC_BEGIN class FontAtlas; +class Texture2D; +struct _ttfConfig; class CC_DLL FontAtlasCache { public: - static FontAtlas * getFontAtlasTTF(const TTFConfig & config); - static FontAtlas * getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset = Vec2::ZERO); + static FontAtlas* getFontAtlasTTF(const _ttfConfig* config); + 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(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap); - static FontAtlas * getFontAtlasCharMap(const std::string& plistFile); + 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); @@ -53,8 +54,8 @@ public: */ static void purgeCachedData(); -private: - static std::string generateFontName(const std::string& fontFileName, int size, GlyphCollection theGlyphs, bool useDistanceField); +private: + static std::string generateFontName(const std::string& fontFileName, int size, bool useDistanceField); static std::unordered_map _atlasMap; }; diff --git a/cocos/2d/CCFontCharMap.cpp b/cocos/2d/CCFontCharMap.cpp index dcc4c40b7c..985a9c8f54 100644 --- a/cocos/2d/CCFontCharMap.cpp +++ b/cocos/2d/CCFontCharMap.cpp @@ -1,6 +1,6 @@ /**************************************************************************** Copyright (c) 2013 Zynga Inc. - Copyright (c) 2013-2014 Chukong Technologies Inc. + Copyright (c) 2013-2015 Chukong Technologies Inc. http://www.cocos2d-x.org diff --git a/cocos/2d/CCFontCharMap.h b/cocos/2d/CCFontCharMap.h index 78ad9abbd9..32987b2bed 100644 --- a/cocos/2d/CCFontCharMap.h +++ b/cocos/2d/CCFontCharMap.h @@ -32,6 +32,7 @@ NS_CC_BEGIN +class Texture2D; class FontCharMap : public Font { public: diff --git a/cocos/2d/CCFontFNT.cpp b/cocos/2d/CCFontFNT.cpp index e89f02fd5c..9cbb6cd014 100644 --- a/cocos/2d/CCFontFNT.cpp +++ b/cocos/2d/CCFontFNT.cpp @@ -1,6 +1,6 @@ /**************************************************************************** Copyright (c) 2013 Zynga Inc. - Copyright (c) 2013-2014 Chukong Technologies Inc. + Copyright (c) 2013-2015 Chukong Technologies Inc. http://www.cocos2d-x.org diff --git a/cocos/2d/CCFontFreeType.cpp b/cocos/2d/CCFontFreeType.cpp index 0e4c1cb98e..651080778d 100644 --- a/cocos/2d/CCFontFreeType.cpp +++ b/cocos/2d/CCFontFreeType.cpp @@ -38,6 +38,9 @@ FT_Library FontFreeType::_FTlibrary; bool FontFreeType::_FTInitialized = false; const int FontFreeType::DistanceMapSpread = 3; +const char* FontFreeType::_glyphASCII = "\"!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ "; +const char* FontFreeType::_glyphNEHE = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ "; + typedef struct _DataRef { Data data; @@ -53,7 +56,7 @@ FontFreeType * FontFreeType::create(const std::string &fontName, int fontSize, G if (!tempFont) return nullptr; - tempFont->setCurrentGlyphCollection(glyphs, customGlyphs); + tempFont->setGlyphCollection(glyphs, customGlyphs); if (!tempFont->createFontObject(fontName, fontSize)) { @@ -100,6 +103,7 @@ FontFreeType::FontFreeType(bool distanceFieldEnabled /* = false */,int outline / , _lineHeight(0) , _fontAtlas(nullptr) , _encoding(FT_ENCODING_UNICODE) +, _usedGlyphs(GlyphCollection::ASCII) { if (outline > 0) { @@ -202,7 +206,7 @@ FontAtlas * FontFreeType::createFontAtlas() if (_fontAtlas && _usedGlyphs != GlyphCollection::DYNAMIC) { std::u16string utf16; - if (StringUtils::UTF8ToUTF16(getCurrentGlyphCollection(), utf16)) + if (StringUtils::UTF8ToUTF16(getGlyphCollection(), 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 diff --git a/cocos/2d/CCFontFreeType.h b/cocos/2d/CCFontFreeType.h index 19ccf7de54..cc8f54fa11 100644 --- a/cocos/2d/CCFontFreeType.h +++ b/cocos/2d/CCFontFreeType.h @@ -75,6 +75,11 @@ public: virtual FontAtlas* createFontAtlas() override; virtual int getFontMaxHeight() const override { return _lineHeight; } private: + static const char* _glyphASCII; + static const char* _glyphNEHE; + static FT_Library _FTlibrary; + static bool _FTInitialized; + FontFreeType(bool distanceFieldEnabled = false, int outline = 0); virtual ~FontFreeType(); @@ -85,18 +90,22 @@ private: int getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar) const; unsigned char* getGlyphBitmapWithOutline(unsigned short code, FT_BBox &bbox); + + void setGlyphCollection(GlyphCollection glyphs, const char* customGlyphs = nullptr); + const char* getGlyphCollection() const; - static FT_Library _FTlibrary; - static bool _FTInitialized; - FT_Face _fontRef; - FT_Stroker _stroker; - std::string _fontName; - bool _distanceFieldEnabled; - float _outlineSize; + FT_Face _fontRef; + FT_Stroker _stroker; + FT_Encoding _encoding; + + std::string _fontName; + bool _distanceFieldEnabled; + float _outlineSize; int _lineHeight; FontAtlas* _fontAtlas; - FT_Encoding _encoding; + GlyphCollection _usedGlyphs; + std::string _customGlyphs; }; /// @endcond diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index 36650ee696..d5f9d211aa 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -528,7 +528,7 @@ void Label::setFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = false * bool Label::setTTFConfig(const TTFConfig& ttfConfig) { - FontAtlas *newAtlas = FontAtlasCache::getFontAtlasTTF(ttfConfig); + FontAtlas *newAtlas = FontAtlasCache::getFontAtlasTTF(&ttfConfig); if (!newAtlas) { diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index e76902fc18..bbfeb34dee 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -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 diff --git a/cocos/base/ccTypes.h b/cocos/base/ccTypes.h index c6a2e6d3b7..a4ef98a322 100644 --- a/cocos/base/ccTypes.h +++ b/cocos/base/ccTypes.h @@ -427,6 +427,19 @@ enum class CC_DLL TextHAlignment 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 /** @struct T2F_Quad