From d63f554bf2eecebcbdaa33012b9f8e42a6e4bba3 Mon Sep 17 00:00:00 2001 From: Arnold <40414978+PatriceJiang@users.noreply.github.com> Date: Thu, 18 Oct 2018 09:00:23 +0800 Subject: [PATCH] CCLabel: fix fontName attribute (#19095) * add method for FontAtlas * break empty font * reduce string constrution --- cocos/2d/CCFontAtlas.cpp | 13 ++++++++++++- cocos/2d/CCFontAtlas.h | 2 ++ cocos/2d/CCFontFreeType.h | 1 + cocos/2d/CCLabel.cpp | 6 +++++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/cocos/2d/CCFontAtlas.cpp b/cocos/2d/CCFontAtlas.cpp index 4dd07162a9..b03bd37e85 100644 --- a/cocos/2d/CCFontAtlas.cpp +++ b/cocos/2d/CCFontAtlas.cpp @@ -477,11 +477,22 @@ Texture2D* FontAtlas::getTexture(int slot) return _atlasTextures[slot]; } -void FontAtlas::setLineHeight(float newHeight) +void FontAtlas::setLineHeight(float newHeight) { _lineHeight = newHeight; } +std::string FontAtlas::getFontName() const +{ + std::string fontName = _fontFreeType ? _fontFreeType->getFontName() : ""; + if(fontName.empty()) return fontName; + auto idx = fontName.rfind("/"); + if (idx != std::string::npos) { return fontName.substr(idx + 1); } + idx = fontName.rfind("\\"); + if (idx != std::string::npos) { return fontName.substr(idx + 1); } + return fontName; +} + void FontAtlas::setAliasTexParameters() { if (_antialiasEnabled) diff --git a/cocos/2d/CCFontAtlas.h b/cocos/2d/CCFontAtlas.h index 64e01e186e..5d05bcd13b 100644 --- a/cocos/2d/CCFontAtlas.h +++ b/cocos/2d/CCFontAtlas.h @@ -84,6 +84,8 @@ public: float getLineHeight() const { return _lineHeight; } void setLineHeight(float newHeight); + std::string getFontName() const; + Texture2D* getTexture(int slot); const Font* getFont() const { return _font; } diff --git a/cocos/2d/CCFontFreeType.h b/cocos/2d/CCFontFreeType.h index 0832c0e861..1ba97a5854 100644 --- a/cocos/2d/CCFontFreeType.h +++ b/cocos/2d/CCFontFreeType.h @@ -73,6 +73,7 @@ public: int getFontAscender() const; const char* getFontFamily() const; + std::string getFontName() const { return _fontName; } virtual FontAtlas* createFontAtlas() override; virtual int getFontMaxHeight() const override { return _lineHeight; } diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index f500ddb24b..468f175eea 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -2084,7 +2084,11 @@ void Label::removeChild(Node* child, bool cleanup /* = true */) FontDefinition Label::_getFontDefinition() const { FontDefinition systemFontDef; - systemFontDef._fontName = _systemFont; + + std::string fontName = _systemFont; + if (_fontAtlas && !_fontAtlas->getFontName().empty()) fontName = _fontAtlas->getFontName(); + + systemFontDef._fontName = fontName; systemFontDef._fontSize = _systemFontSize; systemFontDef._alignment = _hAlignment; systemFontDef._vertAlignment = _vAlignment;