2013-07-19 04:13:41 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-07-19 04:13:41 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2013-07-11 02:59:05 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
#include "2d/CCLabel.h"
|
2013-07-11 02:59:05 +08:00
|
|
|
#include <vector>
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2015-07-24 18:38:45 +08:00
|
|
|
#include "2d/CCFontAtlas.h"
|
2013-07-11 02:59:05 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
void Label::computeAlignmentOffset()
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_linesOffsetX.clear();
|
|
|
|
switch (_hAlignment)
|
|
|
|
{
|
|
|
|
case cocos2d::TextHAlignment::LEFT:
|
|
|
|
_linesOffsetX.assign(_numberOfLines, 0);
|
|
|
|
break;
|
|
|
|
case cocos2d::TextHAlignment::CENTER:
|
|
|
|
for (auto lineWidth : _linesWidth)
|
2014-01-21 17:55:49 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_linesOffsetX.push_back((_contentSize.width - lineWidth) / 2.f);
|
2014-01-21 17:55:49 +08:00
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
break;
|
|
|
|
case cocos2d::TextHAlignment::RIGHT:
|
|
|
|
for (auto lineWidth : _linesWidth)
|
2014-01-21 17:55:49 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_linesOffsetX.push_back(_contentSize.width - lineWidth);
|
2014-01-21 17:55:49 +08:00
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-01-21 17:55:49 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
switch (_vAlignment)
|
|
|
|
{
|
|
|
|
case cocos2d::TextVAlignment::TOP:
|
|
|
|
_letterOffsetY = _contentSize.height;
|
|
|
|
break;
|
|
|
|
case cocos2d::TextVAlignment::CENTER:
|
|
|
|
_letterOffsetY = (_contentSize.height + _textDesiredHeight) / 2.f;
|
|
|
|
break;
|
|
|
|
case cocos2d::TextVAlignment::BOTTOM:
|
|
|
|
_letterOffsetY = _textDesiredHeight;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 12:38:32 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
static int getFirstWordLen(const std::u16string& utf16Text, int startIndex, int textLen)
|
|
|
|
{
|
|
|
|
auto character = utf16Text[startIndex];
|
|
|
|
if (StringUtils::isCJKUnicode(character) || StringUtils::isUnicodeSpace(character) || character == '\n')
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2015-05-28 11:56:43 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
int len = 1;
|
|
|
|
for (int index = startIndex + 1; index < textLen; ++index)
|
|
|
|
{
|
|
|
|
character = utf16Text[index];
|
|
|
|
if (character == '\n' || StringUtils::isUnicodeSpace(character) || StringUtils::isCJKUnicode(character))
|
2014-01-21 17:55:49 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
break;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
len++;
|
2013-07-23 02:40:39 +08:00
|
|
|
}
|
2014-05-08 11:12:59 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
return len;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
bool Label::multilineTextWrapByWord()
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
int textLen = getStringLength();
|
|
|
|
int lineIndex = 0;
|
|
|
|
float nextWordX = 0.f;
|
|
|
|
float nextWordY = 0.f;
|
|
|
|
float longestLine = 0.f;
|
|
|
|
float letterRight = 0.f;
|
|
|
|
|
|
|
|
auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
|
|
|
|
float highestY = 0.f;
|
|
|
|
float lowestY = 0.f;
|
|
|
|
FontLetterDefinition letterDef;
|
|
|
|
Vec2 letterPosition;
|
2013-07-11 02:59:05 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
for (int index = 0; index < textLen; )
|
2014-03-07 14:58:44 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
auto character = _utf16Text[index];
|
|
|
|
if (character == '\n')
|
|
|
|
{
|
|
|
|
_linesWidth.push_back(letterRight);
|
|
|
|
letterRight = 0.f;
|
|
|
|
lineIndex++;
|
|
|
|
nextWordX = 0.f;
|
|
|
|
nextWordY -= _lineHeight;
|
|
|
|
recordPlaceholderInfo(index, character);
|
|
|
|
index++;
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-22 14:57:11 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
auto wordLen = getFirstWordLen(_utf16Text, index, textLen);
|
|
|
|
float wordHighestY = highestY;;
|
|
|
|
float wordLowestY = lowestY;
|
|
|
|
float wordRight = letterRight;
|
|
|
|
float nextLetterX = nextWordX;
|
|
|
|
bool newLine = false;
|
|
|
|
for (int tmp = 0; tmp < wordLen;++tmp)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
int letterIndex = index + tmp;
|
|
|
|
character = _utf16Text[letterIndex];
|
2015-08-14 11:07:34 +08:00
|
|
|
if (character == '\r')
|
|
|
|
{
|
|
|
|
recordPlaceholderInfo(letterIndex, character);
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
if (_fontAtlas->getLetterDefinitionForChar(character, letterDef) == false)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
recordPlaceholderInfo(letterIndex, character);
|
|
|
|
CCLOG("LabelTextFormatter error:can't find letter definition in font file for letter: %c", character);
|
2013-07-11 02:59:05 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
|
|
|
|
auto letterX = (nextLetterX + letterDef.offsetX) / contentScaleFactor;
|
|
|
|
if (_maxLineWidth > 0.f && nextWordX > 0.f && letterX + letterDef.width > _maxLineWidth)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_linesWidth.push_back(letterRight);
|
|
|
|
letterRight = 0.f;
|
|
|
|
lineIndex++;
|
|
|
|
nextWordX = 0.f;
|
|
|
|
nextWordY -= _lineHeight;
|
|
|
|
newLine = true;
|
|
|
|
break;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
else
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
letterPosition.x = letterX;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
letterPosition.y = (nextWordY - letterDef.offsetY) / contentScaleFactor;
|
|
|
|
recordLetterInfo(letterPosition, character, letterIndex, lineIndex);
|
|
|
|
|
|
|
|
if (_horizontalKernings && letterIndex < textLen - 1)
|
|
|
|
nextLetterX += _horizontalKernings[letterIndex + 1];
|
|
|
|
nextLetterX += letterDef.xAdvance + _additionalKerning;
|
|
|
|
|
|
|
|
wordRight = letterPosition.x + letterDef.width;
|
|
|
|
|
|
|
|
if (wordHighestY < letterPosition.y)
|
|
|
|
wordHighestY = letterPosition.y;
|
|
|
|
if (wordLowestY > letterPosition.y - letterDef.height)
|
|
|
|
wordLowestY = letterPosition.y - letterDef.height;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
if (newLine)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextWordX = nextLetterX;
|
|
|
|
letterRight = wordRight;
|
|
|
|
if (highestY < wordHighestY)
|
|
|
|
highestY = wordHighestY;
|
|
|
|
if (lowestY > wordLowestY)
|
|
|
|
lowestY = wordLowestY;
|
|
|
|
if (longestLine < letterRight)
|
|
|
|
longestLine = letterRight;
|
|
|
|
|
|
|
|
index += wordLen;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
|
|
|
|
_linesWidth.push_back(letterRight);
|
|
|
|
|
|
|
|
_numberOfLines = lineIndex + 1;
|
|
|
|
_textDesiredHeight = (_numberOfLines * _lineHeight) / contentScaleFactor;
|
|
|
|
Size contentSize(_labelWidth, _labelHeight);
|
|
|
|
if (_labelWidth <= 0.f)
|
|
|
|
contentSize.width = longestLine;
|
|
|
|
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;
|
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
bool Label::multilineTextWrapByChar()
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
int textLen = getStringLength();
|
|
|
|
int lineIndex = 0;
|
|
|
|
float nextLetterX = 0.f;
|
|
|
|
float nextLetterY = 0.f;
|
|
|
|
float longestLine = 0.f;
|
|
|
|
float letterRight = 0.f;
|
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
|
2015-07-24 18:38:45 +08:00
|
|
|
float highestY = 0.f;
|
|
|
|
float lowestY = 0.f;
|
|
|
|
FontLetterDefinition letterDef;
|
|
|
|
Vec2 letterPosition;
|
2014-03-07 14:58:44 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
for (int index = 0; index < textLen; index++)
|
2014-03-07 14:58:44 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
auto character = _utf16Text[index];
|
2015-08-14 11:07:34 +08:00
|
|
|
if (character == '\r')
|
|
|
|
{
|
|
|
|
recordPlaceholderInfo(index, character);
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
if (character == '\n')
|
2014-03-07 14:58:44 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_linesWidth.push_back(letterRight);
|
|
|
|
letterRight = 0.f;
|
|
|
|
lineIndex++;
|
|
|
|
nextLetterX = 0.f;
|
|
|
|
nextLetterY -= _lineHeight;
|
|
|
|
recordPlaceholderInfo(index, character);
|
|
|
|
continue;
|
2014-03-07 14:58:44 +08:00
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
|
|
|
|
if (_fontAtlas->getLetterDefinitionForChar(character, letterDef) == false)
|
2014-03-07 14:58:44 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
recordPlaceholderInfo(index, character);
|
|
|
|
CCLOG("LabelTextFormatter error:can't find letter definition in font file for letter: %c", character);
|
|
|
|
continue;
|
2014-03-07 14:58:44 +08:00
|
|
|
}
|
2014-03-24 14:16:27 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
auto letterX = (nextLetterX + letterDef.offsetX) / contentScaleFactor;
|
|
|
|
if (_maxLineWidth > 0.f && nextLetterX > 0.f && letterX + letterDef.width > _maxLineWidth)
|
2014-01-22 14:57:11 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_linesWidth.push_back(letterRight);
|
|
|
|
letterRight = 0.f;
|
|
|
|
lineIndex++;
|
|
|
|
nextLetterX = 0.f;
|
|
|
|
nextLetterY -= _lineHeight;
|
|
|
|
letterPosition.x = letterDef.offsetX / contentScaleFactor;
|
2014-01-22 14:57:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
letterPosition.x = letterX;
|
2014-01-22 14:57:11 +08:00
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
letterPosition.y = (nextLetterY - letterDef.offsetY) / contentScaleFactor;
|
|
|
|
recordLetterInfo(letterPosition, character, index, lineIndex);
|
2014-03-24 14:16:27 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
if (_horizontalKernings && index < textLen - 1)
|
|
|
|
nextLetterX += _horizontalKernings[index + 1];
|
|
|
|
nextLetterX += letterDef.xAdvance + _additionalKerning;
|
2014-03-07 14:58:44 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
letterRight = letterPosition.x + letterDef.width;
|
2014-03-24 14:16:27 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
if (highestY < letterPosition.y)
|
|
|
|
highestY = letterPosition.y;
|
|
|
|
if (lowestY > letterPosition.y - letterDef.height)
|
|
|
|
lowestY = letterPosition.y - letterDef.height;
|
2015-05-28 11:56:43 +08:00
|
|
|
if (longestLine < letterRight)
|
|
|
|
longestLine = letterRight;
|
2014-01-24 14:04:08 +08:00
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
|
|
|
|
_linesWidth.push_back(letterRight);
|
|
|
|
|
|
|
|
_numberOfLines = lineIndex + 1;
|
|
|
|
_textDesiredHeight = (_numberOfLines * _lineHeight) / contentScaleFactor;
|
|
|
|
Size contentSize(_labelWidth, _labelHeight);
|
|
|
|
if (_labelWidth <= 0.f)
|
|
|
|
contentSize.width = longestLine;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::recordLetterInfo(const cocos2d::Vec2& point, char16_t utf16Char, int letterIndex, int lineIndex)
|
|
|
|
{
|
|
|
|
if (static_cast<std::size_t>(letterIndex) >= _lettersInfo.size())
|
2014-03-07 14:58:44 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
LetterInfo tmpInfo;
|
|
|
|
_lettersInfo.push_back(tmpInfo);
|
2014-03-07 14:58:44 +08:00
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
_lettersInfo[letterIndex].lineIndex = lineIndex;
|
|
|
|
_lettersInfo[letterIndex].utf16Char = utf16Char;
|
|
|
|
_lettersInfo[letterIndex].valid = _fontAtlas->_letterDefinitions[utf16Char].validDefinition;
|
|
|
|
_lettersInfo[letterIndex].positionX = point.x;
|
|
|
|
_lettersInfo[letterIndex].positionY = point.y;
|
|
|
|
}
|
2014-03-24 14:16:27 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
void Label::recordPlaceholderInfo(int letterIndex, char16_t utf16Char)
|
|
|
|
{
|
|
|
|
if (static_cast<std::size_t>(letterIndex) >= _lettersInfo.size())
|
2014-03-24 14:16:27 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
LetterInfo tmpInfo;
|
|
|
|
_lettersInfo.push_back(tmpInfo);
|
2014-03-24 14:16:27 +08:00
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
_lettersInfo[letterIndex].utf16Char = utf16Char;
|
|
|
|
_lettersInfo[letterIndex].valid = false;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
2014-02-08 15:05:36 +08:00
|
|
|
NS_CC_END
|