Merge pull request #5931 from Dhilan007/develop_label

label: Support to clip transparent area of Label, content size will be updated with only non-transparent area.
This commit is contained in:
James Chen 2014-03-25 16:47:12 +08:00
commit b6589b0b5a
5 changed files with 63 additions and 1 deletions

View File

@ -231,6 +231,8 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
auto pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8; auto pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8;
bool existNewLetter = false; bool existNewLetter = false;
int bottomHeight = _commonLineHeight - _fontAscender;
for (int i = 0; i < length; ++i) for (int i = 0; i < length; ++i)
{ {
auto outIterator = _fontLetterDefinitions.find(utf16String[i]); auto outIterator = _fontLetterDefinitions.find(utf16String[i]);
@ -248,6 +250,7 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
tempDef.height = tempRect.size.height + _letterPadding; tempDef.height = tempRect.size.height + _letterPadding;
tempDef.offsetX = tempRect.origin.x + offsetAdjust; tempDef.offsetX = tempRect.origin.x + offsetAdjust;
tempDef.offsetY = _fontAscender + tempRect.origin.y - offsetAdjust; tempDef.offsetY = _fontAscender + tempRect.origin.y - offsetAdjust;
tempDef.clipBottom = bottomHeight - (tempDef.height + tempRect.origin.y + offsetAdjust);
if (_currentPageOrigX + tempDef.width > CacheTextureWidth) if (_currentPageOrigX + tempDef.width > CacheTextureWidth)
{ {
@ -290,6 +293,7 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
tempDef.offsetX = 0; tempDef.offsetX = 0;
tempDef.offsetY = 0; tempDef.offsetY = 0;
tempDef.textureID = 0; tempDef.textureID = 0;
tempDef.clipBottom = 0;
_currentPageOrigX += 1; _currentPageOrigX += 1;
} }

View File

@ -50,6 +50,8 @@ struct FontLetterDefinition
int textureID; int textureID;
bool validDefinition; bool validDefinition;
int xAdvance; int xAdvance;
int clipBottom;
}; };
class CC_DLL FontAtlas : public Ref class CC_DLL FontAtlas : public Ref

View File

@ -340,6 +340,8 @@ void Label::reset()
_textColor = Color4B::WHITE; _textColor = Color4B::WHITE;
_textColorF = Color4F::WHITE; _textColorF = Color4F::WHITE;
setColor(Color3B::WHITE); setColor(Color3B::WHITE);
_clipEnabled = false;
} }
void Label::updateShaderProgram() void Label::updateShaderProgram()

View File

@ -217,6 +217,10 @@ public:
virtual Sprite * getLetter(int lettetIndex); virtual Sprite * getLetter(int lettetIndex);
/** clip upper and lower margin for reduce height of label.
*/
void setClipMarginEnabled(bool clipEnabled) { _clipEnabled = clipEnabled; }
bool isClipMarginEnabled() const { return _clipEnabled; }
// font related stuff // font related stuff
int getCommonLineHeight() const; int getCommonLineHeight() const;
@ -365,6 +369,8 @@ protected:
Color4B _textColor; Color4B _textColor;
Color4F _textColorF; Color4F _textColorF;
bool _clipEnabled;
private: private:
CC_DISALLOW_COPY_AND_ASSIGN(Label); CC_DISALLOW_COPY_AND_ASSIGN(Label);

View File

@ -322,6 +322,16 @@ bool LabelTextFormatter::createStringSprites(Label *theLabel)
Point letterPosition; Point letterPosition;
const auto& kernings = theLabel->_horizontalKernings; 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++) for (unsigned int i = 0; i < stringLen; i++)
{ {
unsigned short c = strWhole[i]; unsigned short c = strWhole[i];
@ -340,6 +350,7 @@ bool LabelTextFormatter::createStringSprites(Label *theLabel)
if (c == '\n') if (c == '\n')
{ {
lineIndex++;
nextFontPositionX = 0; nextFontPositionX = 0;
nextFontPositionY -= theLabel->_commonLineHeight; nextFontPositionY -= theLabel->_commonLineHeight;
@ -347,8 +358,30 @@ bool LabelTextFormatter::createStringSprites(Label *theLabel)
if(nextFontPositionY < theLabel->_commonLineHeight) if(nextFontPositionY < theLabel->_commonLineHeight)
break; break;
lineStart = true;
continue; 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.x = (nextFontPositionX + charXOffset + kernings[i]) / contentScaleFactor;
letterPosition.y = (nextFontPositionY - charYOffset) / contentScaleFactor; letterPosition.y = (nextFontPositionY - charYOffset) / contentScaleFactor;
@ -382,11 +415,26 @@ bool LabelTextFormatter::createStringSprites(Label *theLabel)
} }
tmpSize.height = totalHeight; tmpSize.height = totalHeight;
if (theLabel->_labelHeight > 0) if (theLabel->_labelHeight > 0)
{ {
tmpSize.height = theLabel->_labelHeight * contentScaleFactor; 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)); theLabel->setContentSize(CC_SIZE_PIXELS_TO_POINTS(tmpSize));
return true; return true;
} }