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); bool isHorizontalClamped(float letterPositionX, int lineInex);
void restoreFontSize(); void restoreFontSize();
void updateLetterSpriteScale(Sprite* sprite); 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(); void reset();

View File

@ -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; 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]; auto character = utf16Text[startIndex];
if (StringUtils::isCJKUnicode(character) || StringUtils::isUnicodeSpace(character) || character == (char16_t)TextFormatter::NewLine) if (StringUtils::isCJKUnicode(character) || StringUtils::isUnicodeSpace(character) || character == (char16_t)TextFormatter::NewLine)
@ -86,9 +86,28 @@ static int getFirstWordLen(const std::u16string& utf16Text, int startIndex, int
} }
int len = 1; int len = 1;
FontLetterDefinition letterDef;
auto nextLetterX = 0;
auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
for (int index = startIndex + 1; index < textLen; ++index) for (int index = startIndex + 1; index < textLen; ++index)
{ {
character = utf16Text[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)) if (character == (char16_t)TextFormatter::NewLine || StringUtils::isUnicodeSpace(character) || StringUtils::isCJKUnicode(character))
{ {
break; break;
@ -251,12 +270,12 @@ bool Label::multilineTextWrap(std::function<int(const std::u16string&, int, int)
bool Label::multilineTextWrapByWord() 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() 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() bool Label::isVerticalClamp()