From 538c01e76ee8035a1183a95493ea3fcd1e79fdc5 Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Mon, 24 Mar 2014 14:16:27 +0800 Subject: [PATCH 1/3] label:support clip blank of upper and lower margin. --- cocos/2d/CCFontAtlas.cpp | 4 +++ cocos/2d/CCFontAtlas.h | 2 ++ cocos/2d/CCLabel.cpp | 2 ++ cocos/2d/CCLabel.h | 6 ++++ cocos/2d/CCLabelTextFormatter.cpp | 50 ++++++++++++++++++++++++++++++- 5 files changed, 63 insertions(+), 1 deletion(-) diff --git a/cocos/2d/CCFontAtlas.cpp b/cocos/2d/CCFontAtlas.cpp index 3e40e41419..11f9998d6c 100644 --- a/cocos/2d/CCFontAtlas.cpp +++ b/cocos/2d/CCFontAtlas.cpp @@ -231,6 +231,8 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String) auto pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8; bool existNewLetter = false; + int bottomHeight = _commonLineHeight - _fontAscender; + for (int i = 0; i < length; ++i) { auto outIterator = _fontLetterDefinitions.find(utf16String[i]); @@ -248,6 +250,7 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String) tempDef.height = tempRect.size.height + _letterPadding; tempDef.offsetX = tempRect.origin.x + offsetAdjust; tempDef.offsetY = _fontAscender + tempRect.origin.y - offsetAdjust; + tempDef.clipBottom = bottomHeight - (tempDef.height + tempRect.origin.y + offsetAdjust); if (_currentPageOrigX + tempDef.width > CacheTextureWidth) { @@ -290,6 +293,7 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String) tempDef.offsetX = 0; tempDef.offsetY = 0; tempDef.textureID = 0; + tempDef.clipBottom = 0; _currentPageOrigX += 1; } diff --git a/cocos/2d/CCFontAtlas.h b/cocos/2d/CCFontAtlas.h index 2d3eb615a3..a10fe9eb9f 100644 --- a/cocos/2d/CCFontAtlas.h +++ b/cocos/2d/CCFontAtlas.h @@ -50,6 +50,8 @@ struct FontLetterDefinition int textureID; bool validDefinition; int xAdvance; + + int clipBottom; }; class CC_DLL FontAtlas : public Ref diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index 3d6bc68508..644ceeec06 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -339,6 +339,8 @@ void Label::reset() _textColor = Color4B::WHITE; _textColorF = Color4F::WHITE; setColor(Color3B::WHITE); + + _clipEnabled = false; } void Label::updateShaderProgram() diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index 93130c4e10..dc9271d8ab 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -217,6 +217,10 @@ public: virtual Sprite * getLetter(int lettetIndex); + /** clip upper and lower margin for reduce height of label. + */ + void setClipMarginEnabled(bool clipEnabled) { _clipEnabled = clipEnabled; } + // font related stuff int getCommonLineHeight() const; @@ -365,6 +369,8 @@ protected: Color4B _textColor; Color4F _textColorF; + bool _clipEnabled; + private: CC_DISALLOW_COPY_AND_ASSIGN(Label); diff --git a/cocos/2d/CCLabelTextFormatter.cpp b/cocos/2d/CCLabelTextFormatter.cpp index 9f0efe1eaa..119456caa6 100644 --- a/cocos/2d/CCLabelTextFormatter.cpp +++ b/cocos/2d/CCLabelTextFormatter.cpp @@ -321,6 +321,16 @@ bool LabelTextFormatter::createStringSprites(Label *theLabel) FontLetterDefinition tempDefinition; Point letterPosition; const auto& kernings = theLabel->_horizontalKernings; + + float clipTop = 0; + float clipBottom = 0; + int lineIndex = 0; + bool lineStart = true; + bool clip = false; + if (theLabel->_currentLabelType == Label::LabelType::TTF && theLabel->_clipEnabled) + { + clip = true; + } for (unsigned int i = 0; i < stringLen; i++) { @@ -337,9 +347,10 @@ bool LabelTextFormatter::createStringSprites(Label *theLabel) charYOffset = -1; charAdvance = -1; } - + if (c == '\n') { + lineIndex++; nextFontPositionX = 0; nextFontPositionY -= theLabel->_commonLineHeight; @@ -347,8 +358,30 @@ bool LabelTextFormatter::createStringSprites(Label *theLabel) if(nextFontPositionY < theLabel->_commonLineHeight) break; + lineStart = true; continue; } + else if (clip && tempDefinition.height > 0.0f) + { + if (lineStart) + { + if (lineIndex == 0) + { + clipTop = charYOffset; + } + lineStart = false; + clipBottom = tempDefinition.clipBottom; + } + else if(tempDefinition.clipBottom < clipBottom) + { + clipBottom = tempDefinition.clipBottom; + } + + if (lineIndex == 0 && charYOffset < clipTop) + { + clipTop = charYOffset; + } + } letterPosition.x = (nextFontPositionX + charXOffset + kernings[i]) / contentScaleFactor; letterPosition.y = (nextFontPositionY - charYOffset) / contentScaleFactor; @@ -382,11 +415,26 @@ bool LabelTextFormatter::createStringSprites(Label *theLabel) } tmpSize.height = totalHeight; + if (theLabel->_labelHeight > 0) { tmpSize.height = theLabel->_labelHeight * contentScaleFactor; } + + if (clip) + { + int clipTotal = (clipTop + clipBottom) / contentScaleFactor; + tmpSize.height -= clipTotal * contentScaleFactor; + clipBottom /= contentScaleFactor; + + for (int i = 0; i < theLabel->_limitShowCount; i++) + { + theLabel->_lettersInfo[i].position.y -= clipBottom; + } + } + theLabel->setContentSize(CC_SIZE_PIXELS_TO_POINTS(tmpSize)); + return true; } From 19341ceaf42fe3bbb8fed7e9ca8a6f08d58bab8c Mon Sep 17 00:00:00 2001 From: LinWenhai Date: Tue, 25 Mar 2014 14:54:42 +0800 Subject: [PATCH 2/3] add getter of ClipMarginEnabled. --- cocos/2d/CCLabel.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index dc9271d8ab..c453112cb4 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -220,7 +220,7 @@ public: /** clip upper and lower margin for reduce height of label. */ void setClipMarginEnabled(bool clipEnabled) { _clipEnabled = clipEnabled; } - + bool getClipMarginEnabled() const { return _clipEnabled; } // font related stuff int getCommonLineHeight() const; From 4b454fe7bbb5a067dda9d4305912d4d8a078d183 Mon Sep 17 00:00:00 2001 From: LinWenhai Date: Tue, 25 Mar 2014 16:37:34 +0800 Subject: [PATCH 3/3] Update getter of ClipMarginEnabled. --- cocos/2d/CCLabel.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index c453112cb4..c2e2be23b8 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -220,7 +220,7 @@ public: /** clip upper and lower margin for reduce height of label. */ void setClipMarginEnabled(bool clipEnabled) { _clipEnabled = clipEnabled; } - bool getClipMarginEnabled() const { return _clipEnabled; } + bool isClipMarginEnabled() const { return _clipEnabled; } // font related stuff int getCommonLineHeight() const;