Merge pull request #15863 from zilongshanren/fix-word-clamp-with-only-one-word-left

fix TTF and BMFont word wrap
This commit is contained in:
minggo 2016-06-16 15:01:27 +08:00 committed by GitHub
commit 0db0a45fa0
2 changed files with 49 additions and 28 deletions

View File

@ -676,6 +676,8 @@ protected:
bool isHorizontalClamped(float letterPositionX, int lineInex);
void restoreFontSize();
void updateLetterSpriteScale(Sprite* sprite);
int getFirstCharLen(const std::u16string& utf16Text, int startIndex, int textLen);
int getFirstWordLen(const std::u16string& utf16Text, int startIndex, int textLen);
void reset();

View File

@ -1,19 +1,19 @@
/****************************************************************************
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
@ -72,12 +72,12 @@ void Label::computeAlignmentOffset()
}
}
static int getFirstCharLen(const std::u16string& utf16Text, int startIndex, int textLen)
int Label::getFirstCharLen(const std::u16string& utf16Text, int startIndex, int textLen)
{
return 1;
}
static int getFirstWordLen(const std::u16string& utf16Text, int startIndex, int textLen)
int Label::getFirstWordLen(const std::u16string& utf16Text, int startIndex, int textLen)
{
auto character = utf16Text[startIndex];
if (StringUtils::isCJKUnicode(character) || StringUtils::isUnicodeSpace(character) || character == (char16_t)TextFormatter::NewLine)
@ -86,16 +86,35 @@ static int getFirstWordLen(const std::u16string& utf16Text, int startIndex, int
}
int len = 1;
FontLetterDefinition letterDef;
auto nextLetterX = 0;
auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
for (int index = startIndex + 1; index < textLen; ++index)
{
character = utf16Text[index];
if (_fontAtlas->getLetterDefinitionForChar(character, letterDef) == false)
{
break;
}
auto letterX = (nextLetterX + letterDef.offsetX * _bmfontScale) / contentScaleFactor;
if (_maxLineWidth > 0.f && letterX + letterDef.width * _bmfontScale > _maxLineWidth
&& !StringUtils::isUnicodeSpace(character))
{
if(len >= 2) {
return len -1;
}
}
nextLetterX += letterDef.xAdvance * _bmfontScale + _additionalKerning;
if (character == (char16_t)TextFormatter::NewLine || StringUtils::isUnicodeSpace(character) || StringUtils::isCJKUnicode(character))
{
break;
}
len++;
}
return len;
}
@ -119,7 +138,7 @@ bool Label::multilineTextWrap(std::function<int(const std::u16string&, int, int)
float nextTokenY = 0.f;
float longestLine = 0.f;
float letterRight = 0.f;
auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
float lineSpacing = _lineSpacing * contentScaleFactor;
float highestY = 0.f;
@ -127,9 +146,9 @@ bool Label::multilineTextWrap(std::function<int(const std::u16string&, int, int)
FontLetterDefinition letterDef;
Vec2 letterPosition;
bool nextChangeSize = true;
this->updateBMFontScale();
for (int index = 0; index < textLen; )
{
auto character = _utf16Text[index];
@ -144,7 +163,7 @@ bool Label::multilineTextWrap(std::function<int(const std::u16string&, int, int)
index++;
continue;
}
auto tokenLen = nextTokenLen(_utf16Text, index, textLen);
float tokenHighestY = highestY;;
float tokenLowestY = lowestY;
@ -173,7 +192,7 @@ bool Label::multilineTextWrap(std::function<int(const std::u16string&, int, int)
CCLOG("LabelTextFormatter error:can't find letter definition in font file for letter: %c", character);
continue;
}
auto letterX = (nextLetterX + letterDef.offsetX * _bmfontScale) / contentScaleFactor;
if (_enableWrap && _maxLineWidth > 0.f && nextTokenX > 0.f && letterX + letterDef.width * _bmfontScale > _maxLineWidth
&& !StringUtils::isUnicodeSpace(character) && nextChangeSize)
@ -192,28 +211,28 @@ bool Label::multilineTextWrap(std::function<int(const std::u16string&, int, int)
}
letterPosition.y = (nextTokenY - letterDef.offsetY * _bmfontScale) / contentScaleFactor;
recordLetterInfo(letterPosition, character, letterIndex, lineIndex);
if (nextChangeSize)
{
if (_horizontalKernings && letterIndex < textLen - 1)
nextLetterX += _horizontalKernings[letterIndex + 1];
nextLetterX += letterDef.xAdvance * _bmfontScale + _additionalKerning;
tokenRight = letterPosition.x + letterDef.width * _bmfontScale;
}
nextChangeSize = true;
if (tokenHighestY < letterPosition.y)
tokenHighestY = letterPosition.y;
if (tokenLowestY > letterPosition.y - letterDef.height * _bmfontScale)
tokenLowestY = letterPosition.y - letterDef.height * _bmfontScale;
}
if (newLine)
{
continue;
}
nextTokenX = nextLetterX;
letterRight = tokenRight;
if (highestY < tokenHighestY)
@ -222,12 +241,12 @@ bool Label::multilineTextWrap(std::function<int(const std::u16string&, int, int)
lowestY = tokenLowestY;
if (longestLine < letterRight)
longestLine = letterRight;
index += tokenLen;
}
_linesWidth.push_back(letterRight);
_numberOfLines = lineIndex + 1;
_textDesiredHeight = (_numberOfLines * _lineHeight * _bmfontScale) / contentScaleFactor;
if (_numberOfLines > 1)
@ -238,25 +257,25 @@ bool Label::multilineTextWrap(std::function<int(const std::u16string&, int, int)
if (_labelHeight <= 0.f)
contentSize.height = _textDesiredHeight;
setContentSize(contentSize);
_tailoredTopY = contentSize.height;
_tailoredBottomY = 0.f;
if (highestY > 0.f)
_tailoredTopY = contentSize.height + highestY;
if (lowestY < -_textDesiredHeight)
_tailoredBottomY = _textDesiredHeight + lowestY;
return true;
}
bool Label::multilineTextWrapByWord()
{
return multilineTextWrap(std::bind(getFirstWordLen, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
return multilineTextWrap(std::bind(CC_CALLBACK_3(Label::getFirstWordLen, this), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
}
bool Label::multilineTextWrapByChar()
{
return multilineTextWrap(std::bind(getFirstCharLen, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
return multilineTextWrap(std::bind(CC_CALLBACK_3(Label::getFirstCharLen, this) , std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
}
bool Label::isVerticalClamp()
@ -279,7 +298,7 @@ bool Label::isHorizontalClamp()
if (_lettersInfo[ctr].valid)
{
auto& letterDef = _fontAtlas->_letterDefinitions[_lettersInfo[ctr].utf16Char];
auto px = _lettersInfo[ctr].positionX + letterDef.width/2 * _bmfontScale;
auto lineIndex = _lettersInfo[ctr].lineIndex;
@ -298,18 +317,18 @@ bool Label::isHorizontalClamp()
}
}
}
}
}
return letterClamp;
}
void Label::shrinkLabelToContentSize(std::function<bool(void)> lambda)
{
float fontSize = this->getRenderingFontSize();
int i = 0;
auto letterDefinition = _fontAtlas->_letterDefinitions;
auto tempLetterDefinition = letterDefinition;