From f0f8663fa9f8d310d34107cb6c26dc3deb231061 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 16 Jun 2016 14:15:00 +0800 Subject: [PATCH] fix TTF and BMFont word wrap --- cocos/2d/CCLabel.h | 2 + cocos/2d/CCLabelTextFormatter.cpp | 75 +++++++++++++++++++------------ 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index 4f2ae5ae8d..9c5a5650c9 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -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(); diff --git a/cocos/2d/CCLabelTextFormatter.cpp b/cocos/2d/CCLabelTextFormatter.cpp index 1e4f4e2dd9..f3a4221371 100644 --- a/cocos/2d/CCLabelTextFormatter.cpp +++ b/cocos/2d/CCLabelTextFormatter.cpp @@ -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::functionupdateBMFontScale(); - + for (int index = 0; index < textLen; ) { auto character = _utf16Text[index]; @@ -144,7 +163,7 @@ bool Label::multilineTextWrap(std::function 0.f && nextTokenX > 0.f && letterX + letterDef.width * _bmfontScale > _maxLineWidth && !StringUtils::isUnicodeSpace(character) && nextChangeSize) @@ -192,28 +211,28 @@ bool Label::multilineTextWrap(std::function 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 1) @@ -238,25 +257,25 @@ bool Label::multilineTextWrap(std::function 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 lambda) { float fontSize = this->getRenderingFontSize(); - + int i = 0; auto letterDefinition = _fontAtlas->_letterDefinitions; auto tempLetterDefinition = letterDefinition;