diff --git a/cocos/2d/CCFontAtlas.cpp b/cocos/2d/CCFontAtlas.cpp index 6256350abd8b..5f9e73c25712 100644 --- a/cocos/2d/CCFontAtlas.cpp +++ b/cocos/2d/CCFontAtlas.cpp @@ -436,6 +436,7 @@ bool FontAtlas::prepareLetterDefinitions(const std::u32string& utf32Text) tempDef.height = tempDef.height / scaleFactor; tempDef.U = tempDef.U / scaleFactor; tempDef.V = tempDef.V / scaleFactor; + tempDef.rotated = false; } else{ delete[] bitmap; @@ -451,6 +452,7 @@ bool FontAtlas::prepareLetterDefinitions(const std::u32string& utf32Text) tempDef.offsetX = 0; tempDef.offsetY = 0; tempDef.textureID = 0; + tempDef.rotated = false; _currentPageOrigX += 1; } diff --git a/cocos/2d/CCFontAtlas.h b/cocos/2d/CCFontAtlas.h index 5d05bcd13ba6..04ad3767a3b0 100644 --- a/cocos/2d/CCFontAtlas.h +++ b/cocos/2d/CCFontAtlas.h @@ -55,6 +55,7 @@ struct FontLetterDefinition int textureID; bool validDefinition; int xAdvance; + bool rotated; }; class CC_DLL FontAtlas : public Ref diff --git a/cocos/2d/CCFontCharMap.cpp b/cocos/2d/CCFontCharMap.cpp index ab958166aba4..2a1f14b27272 100644 --- a/cocos/2d/CCFontCharMap.cpp +++ b/cocos/2d/CCFontCharMap.cpp @@ -126,6 +126,7 @@ FontAtlas * FontCharMap::createFontAtlas() tempDefinition.width = _itemWidth / contentScaleFactor; tempDefinition.height = _itemHeight / contentScaleFactor; tempDefinition.xAdvance = _itemWidth; + tempDefinition.rotated = false; int charId = _mapStartChar; for (int row = 0; row < itemsPerColumn; ++row) diff --git a/cocos/2d/CCFontFNT.cpp b/cocos/2d/CCFontFNT.cpp index daa572081020..ec8e43f45764 100644 --- a/cocos/2d/CCFontFNT.cpp +++ b/cocos/2d/CCFontFNT.cpp @@ -34,8 +34,6 @@ #include "renderer/CCTextureCache.h" #include -#include -#include NS_CC_BEGIN @@ -52,99 +50,6 @@ enum { struct _FontDefHashElement; -/** -@struct BMFontDef -BMFont definition -*/ -typedef struct _BMFontDef { - //! ID of the character - unsigned int charID; - //! origin and size of the font - Rect rect; - //! The X amount the image should be offset when drawing the image (in pixels) - short xOffset; - //! The Y amount the image should be offset when drawing the image (in pixels) - short yOffset; - //! The amount to move the current position after drawing the character (in pixels) - short xAdvance; -} BMFontDef; - -/** @struct BMFontPadding -BMFont padding -@since v0.8.2 -*/ -typedef struct _BMFontPadding { - /// padding left - int left; - /// padding top - int top; - /// padding right - int right; - /// padding bottom - int bottom; -} BMFontPadding; - -/** @brief BMFontConfiguration has parsed configuration of the .fnt file -@since v0.8 -*/ -class CC_DLL BMFontConfiguration : public Ref -{ - // FIXME: Creating a public interface so that the bitmapFontArray[] is accessible -public://@public - // BMFont definitions - std::unordered_map _fontDefDictionary; - - //! FNTConfig: Common Height Should be signed (issue #1343) - int _commonHeight; - //! Padding - BMFontPadding _padding; - //! atlas name - std::string _atlasName; - //! values for kerning - std::unordered_map _kerningDictionary; - - // Character Set defines the letters that actually exist in the font - std::set *_characterSet; - //! Font Size - int _fontSize; -public: - /** - * @js ctor - */ - BMFontConfiguration(); - /** - * @js NA - * @lua NA - */ - virtual ~BMFontConfiguration(); - /** - * @js NA - * @lua NA - */ - std::string description() const; - - /** allocates a BMFontConfiguration with a FNT file */ - static BMFontConfiguration * create(const std::string& FNTfile); - - /** initializes a BitmapFontConfiguration with a FNT file */ - bool initWithFNTfile(const std::string& FNTfile); - - const std::string& getAtlasName() { return _atlasName; } - void setAtlasName(const std::string& atlasName) { _atlasName = atlasName; } - - std::set* getCharacterSet() const; -private: - std::set* parseConfigFile(const std::string& controlFile); - std::set* parseBinaryConfigFile(unsigned char* pData, unsigned long size, const std::string& controlFile); - unsigned int parseCharacterDefinition(const char* line); - void parseInfoArguments(const char* line); - void parseCommonArguments(const char* line); - void parseImageFileName(const char* line, const std::string& fntFile); - void parseKerningEntry(const char* line); - void purgeKerningDictionary(); - void purgeFontDefDictionary(); -}; - // //FNTConfig Cache - free functions // @@ -748,6 +653,8 @@ FontAtlas * FontFNT::createFontAtlas() tempDefinition.validDefinition = true; tempDefinition.xAdvance = fontDef.xAdvance; + tempDefinition.rotated = false; + // add the new definition if (65535 < fontDef.charID) { CCLOGWARN("Warning: 65535 < fontDef.charID (%u), ignored", fontDef.charID); diff --git a/cocos/2d/CCFontFNT.h b/cocos/2d/CCFontFNT.h index d0b932fd4f86..457de4a5f114 100644 --- a/cocos/2d/CCFontFNT.h +++ b/cocos/2d/CCFontFNT.h @@ -30,10 +30,106 @@ /// @cond DO_NOT_SHOW #include "2d/CCFont.h" +#include +#include NS_CC_BEGIN -class BMFontConfiguration; +/** +@struct BMFontDef +BMFont definition +*/ +typedef struct _BMFontDef { + //! ID of the character + unsigned int charID; + //! origin and size of the font + Rect rect; + //! The X amount the image should be offset when drawing the image (in pixels) + short xOffset; + //! The Y amount the image should be offset when drawing the image (in pixels) + short yOffset; + //! The amount to move the current position after drawing the character (in pixels) + short xAdvance; +} BMFontDef; + +/** @struct BMFontPadding +BMFont padding +@since v0.8.2 +*/ +typedef struct _BMFontPadding { + /// padding left + int left; + /// padding top + int top; + /// padding right + int right; + /// padding bottom + int bottom; +} BMFontPadding; + +/** @brief BMFontConfiguration has parsed configuration of the .fnt file +@since v0.8 +*/ +class CC_DLL BMFontConfiguration : public Ref +{ + // FIXME: Creating a public interface so that the bitmapFontArray[] is accessible +public://@public + // BMFont definitions + std::unordered_map _fontDefDictionary; + + //! FNTConfig: Common Height Should be signed (issue #1343) + int _commonHeight; + //! Padding + BMFontPadding _padding; + //! atlas name + std::string _atlasName; + //! values for kerning + std::unordered_map _kerningDictionary; + + // Character Set defines the letters that actually exist in the font + std::set* _characterSet; + //! Font Size + int _fontSize; +public: + /** + * @js ctor + */ + BMFontConfiguration(); + /** + * @js NA + * @lua NA + */ + virtual ~BMFontConfiguration(); + /** + * @js NA + * @lua NA + */ + std::string description() const; + + /** allocates a BMFontConfiguration with a FNT file */ + static BMFontConfiguration* create(const std::string& FNTfile); + + /** initializes a BitmapFontConfiguration with a FNT file */ + bool initWithFNTfile(const std::string& FNTfile); + + const std::string& getAtlasName() { return _atlasName; } + void setAtlasName(const std::string& atlasName) { _atlasName = atlasName; } + + std::set* getCharacterSet() const; + +protected: + virtual std::set* parseConfigFile(const std::string& controlFile); + virtual std::set* parseBinaryConfigFile(unsigned char* pData, unsigned long size, const std::string& controlFile); + +private: + unsigned int parseCharacterDefinition(const char* line); + void parseInfoArguments(const char* line); + void parseCommonArguments(const char* line); + void parseImageFileName(const char* line, const std::string& fntFile); + void parseKerningEntry(const char* line); + void purgeKerningDictionary(); + void purgeFontDefDictionary(); +}; class CC_DLL FontFNT : public Font { @@ -47,7 +143,10 @@ class CC_DLL FontFNT : public Font static void purgeCachedData(); virtual int* getHorizontalKerningForTextUTF32(const std::u32string& text, int &outNumLetters) const override; virtual FontAtlas *createFontAtlas() override; + void setFontSize(float fontSize); + float getFontSize() const { return _fontSize; } + int getOriginalFontSize()const; static void reloadBMFontResource(const std::string& fntFilePath); @@ -60,12 +159,10 @@ class CC_DLL FontFNT : public Font * @lua NA */ virtual ~FontFNT(); - -private: - + + BMFontConfiguration* _configuration; int getHorizontalKerningForChars(char32_t firstChar, char32_t secondChar) const; - BMFontConfiguration * _configuration; Vec2 _imageOffset; //User defined font size float _fontSize; diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index fa2b2d2dd73d..e488bdec630d 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -790,7 +790,7 @@ void Label::updateLabelLetters() } else { - letterSprite->setTextureRect(uvRect, false, uvRect.size); + letterSprite->setTextureRect(uvRect, letterDef.rotated, uvRect.size); letterSprite->setTextureAtlas(_batchNodes.at(letterDef.textureID)->getTextureAtlas()); letterSprite->setAtlasIndex(_lettersInfo[letterIndex].atlasIndex); } @@ -971,7 +971,7 @@ bool Label::updateQuads() if (_reusedRect.size.height > 0.f && _reusedRect.size.width > 0.f) { - _reusedLetter->setTextureRect(_reusedRect, false, _reusedRect.size); + _reusedLetter->setTextureRect(_reusedRect, letterDef.rotated, _reusedRect.size); float letterPositionX = _lettersInfo[ctr].positionX + _linesOffsetX[_lettersInfo[ctr].lineIndex]; _reusedLetter->setPosition(letterPositionX, py); auto index = static_cast(_batchNodes.at(letterDef.textureID)->getTextureAtlas()->getTotalQuads()); @@ -1799,7 +1799,7 @@ Sprite* Label::getLetter(int letterIndex) else { this->updateBMFontScale(); - letter = LabelLetter::createWithTexture(_fontAtlas->getTexture(textureID), uvRect); + letter = LabelLetter::createWithTexture(_fontAtlas->getTexture(textureID), uvRect, letterDef.rotated); letter->setTextureAtlas(_batchNodes.at(textureID)->getTextureAtlas()); letter->setAtlasIndex(letterInfo.atlasIndex); auto px = letterInfo.positionX + _bmfontScale * uvRect.size.width / 2 + _linesOffsetX[letterInfo.lineIndex];