axmol/cocos/2d/CCLabelTextFormatter.cpp

451 lines
14 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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.
****************************************************************************/
#include "2d/CCLabel.h"
#include <vector>
#include "base/ccUTF8.h"
#include "base/CCDirector.h"
#include "2d/CCFontAtlas.h"
#include "2d/CCFontFNT.h"
NS_CC_BEGIN
void Label::computeAlignmentOffset()
{
_linesOffsetX.clear();
switch (_hAlignment)
{
case cocos2d::TextHAlignment::LEFT:
_linesOffsetX.assign(_numberOfLines, 0);
break;
case cocos2d::TextHAlignment::CENTER:
for (auto lineWidth : _linesWidth)
{
_linesOffsetX.push_back((_contentSize.width - lineWidth) / 2.f);
}
break;
case cocos2d::TextHAlignment::RIGHT:
for (auto lineWidth : _linesWidth)
{
_linesOffsetX.push_back(_contentSize.width - lineWidth);
}
break;
default:
break;
}
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;
}
}
int Label::getFirstCharLen(const std::u32string& /*utf32Text*/, int /*startIndex*/, int /*textLen*/) const
{
return 1;
}
int Label::getFirstWordLen(const std::u32string& utf32Text, int startIndex, int textLen) const
{
2021-12-25 10:04:45 +08:00
int len = 0;
2019-11-23 20:27:39 +08:00
auto nextLetterX = 0;
FontLetterDefinition letterDef;
auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
for (int index = startIndex; index < textLen; ++index)
{
char32_t character = utf32Text[index];
2021-12-25 10:04:45 +08:00
if (character == StringUtils::UnicodeCharacters::NewLine ||
(!StringUtils::isUnicodeNonBreaking(character) &&
(StringUtils::isUnicodeSpace(character) || StringUtils::isCJKUnicode(character))))
2019-11-23 20:27:39 +08:00
{
break;
}
if (!getFontLetterDef(character, letterDef))
{
break;
}
if (_maxLineWidth > 0.f)
{
auto letterX = (nextLetterX + letterDef.offsetX * _bmfontScale) / contentScaleFactor;
if (letterX + letterDef.width * _bmfontScale > _maxLineWidth)
break;
}
2021-12-26 23:26:34 +08:00
nextLetterX += static_cast<int>(letterDef.xAdvance * _bmfontScale + _additionalKerning);
2019-11-23 20:27:39 +08:00
len++;
}
if (len == 0 && textLen)
len = 1;
return len;
}
bool Label::getFontLetterDef(char32_t character, FontLetterDefinition& letterDef) const
{
if (character == StringUtils::UnicodeCharacters::NoBreakSpace)
{
// change no-break space to regular space
// reason: some fonts have issue with no-break space:
// * no letter definition
// * not normal big width
character = StringUtils::UnicodeCharacters::Space;
}
return _fontAtlas->getLetterDefinitionForChar(character, letterDef);
}
void Label::updateBMFontScale()
{
auto font = _fontAtlas->getFont();
2021-12-25 10:04:45 +08:00
if (_currentLabelType == LabelType::BMFONT)
{
2021-12-28 11:00:34 +08:00
FontFNT* bmFont = (FontFNT*)font;
2021-12-26 23:26:34 +08:00
auto originalFontSize = bmFont->getOriginalFontSize();
2021-12-28 11:00:34 +08:00
_bmfontScale = _bmFontSize * CC_CONTENT_SCALE_FACTOR() / originalFontSize;
2021-12-25 10:04:45 +08:00
}
else
{
2019-11-23 20:27:39 +08:00
_bmfontScale = 1.0f;
}
}
bool Label::multilineTextWrap(const std::function<int(const std::u32string&, int, int)>& nextTokenLen)
{
2021-12-25 10:04:45 +08:00
int textLen = getStringLength();
int lineIndex = 0;
float nextTokenX = 0.f;
float nextTokenY = 0.f;
float longestLine = 0.f;
float letterRight = 0.f;
2019-11-23 20:27:39 +08:00
float nextWhitespaceWidth = 0.f;
auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
2021-12-25 10:04:45 +08:00
float lineSpacing = _lineSpacing * contentScaleFactor;
float highestY = 0.f;
float lowestY = 0.f;
2019-11-23 20:27:39 +08:00
FontLetterDefinition letterDef;
Vec2 letterPosition;
bool nextChangeSize = true;
this->updateBMFontScale();
2021-12-25 10:04:45 +08:00
for (int index = 0; index < textLen;)
2019-11-23 20:27:39 +08:00
{
char32_t character = _utf32Text[index];
if (character == StringUtils::UnicodeCharacters::NewLine)
{
_linesWidth.push_back(letterRight);
letterRight = 0.f;
lineIndex++;
nextTokenX = 0.f;
2021-12-25 10:04:45 +08:00
nextTokenY -= _lineHeight * _bmfontScale + lineSpacing;
2019-11-23 20:27:39 +08:00
recordPlaceholderInfo(index, character);
index++;
continue;
}
2021-12-25 10:04:45 +08:00
auto tokenLen = nextTokenLen(_utf32Text, index, textLen);
float tokenHighestY = highestY;
float tokenLowestY = lowestY;
float tokenRight = letterRight;
float nextLetterX = nextTokenX;
2019-11-23 20:27:39 +08:00
float whitespaceWidth = nextWhitespaceWidth;
2021-12-25 10:04:45 +08:00
bool newLine = false;
for (int tmp = 0; tmp < tokenLen; ++tmp)
2019-11-23 20:27:39 +08:00
{
int letterIndex = index + tmp;
2021-12-25 10:04:45 +08:00
character = _utf32Text[letterIndex];
2019-11-23 20:27:39 +08:00
if (character == StringUtils::UnicodeCharacters::CarriageReturn)
{
recordPlaceholderInfo(letterIndex, character);
continue;
}
// \b - Next char not change x position
if (character == StringUtils::UnicodeCharacters::NextCharNoChangeX)
{
nextChangeSize = false;
recordPlaceholderInfo(letterIndex, character);
continue;
}
if (!getFontLetterDef(character, letterDef))
{
recordPlaceholderInfo(letterIndex, character);
2021-12-25 10:04:45 +08:00
CCLOG("LabelTextFormatter error: can't find letter definition in font file for letter: 0x%x",
character);
2019-11-23 20:27:39 +08:00
continue;
}
auto letterX = (nextLetterX + letterDef.offsetX * _bmfontScale) / contentScaleFactor;
2021-12-25 10:04:45 +08:00
if (_enableWrap && _maxLineWidth > 0.f && nextTokenX > 0.f &&
letterX + letterDef.width * _bmfontScale > _maxLineWidth && !StringUtils::isUnicodeSpace(character) &&
nextChangeSize)
2019-11-23 20:27:39 +08:00
{
_linesWidth.push_back(letterRight - whitespaceWidth);
nextWhitespaceWidth = 0.f;
2021-12-25 10:04:45 +08:00
letterRight = 0.f;
2019-11-23 20:27:39 +08:00
lineIndex++;
nextTokenX = 0.f;
2021-12-25 10:04:45 +08:00
nextTokenY -= (_lineHeight * _bmfontScale + lineSpacing);
2019-11-23 20:27:39 +08:00
newLine = true;
break;
}
else
{
letterPosition.x = letterX;
}
letterPosition.y = (nextTokenY - letterDef.offsetY * _bmfontScale) / contentScaleFactor;
recordLetterInfo(letterPosition, character, letterIndex, lineIndex);
if (nextChangeSize)
{
float newLetterWidth = 0.f;
if (_horizontalKernings && letterIndex < textLen - 1)
2021-12-26 23:26:34 +08:00
newLetterWidth = static_cast<float>(_horizontalKernings[letterIndex + 1]);
2019-11-23 20:27:39 +08:00
newLetterWidth += letterDef.xAdvance * _bmfontScale + _additionalKerning;
nextLetterX += newLetterWidth;
tokenRight = nextLetterX / contentScaleFactor;
if (StringUtils::isUnicodeSpace(character))
{
nextWhitespaceWidth += newLetterWidth / contentScaleFactor;
}
else
{
nextWhitespaceWidth = 0;
}
}
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;
}
2021-12-25 10:04:45 +08:00
nextTokenX = nextLetterX;
2019-11-23 20:27:39 +08:00
letterRight = tokenRight;
if (highestY < tokenHighestY)
highestY = tokenHighestY;
if (lowestY > tokenLowestY)
lowestY = tokenLowestY;
index += tokenLen;
}
if (_linesWidth.empty())
{
_linesWidth.push_back(letterRight);
longestLine = letterRight;
}
else
{
_linesWidth.push_back(letterRight - nextWhitespaceWidth);
2021-12-25 10:04:45 +08:00
for (auto&& lineWidth : _linesWidth)
2019-11-23 20:27:39 +08:00
{
if (longestLine < lineWidth)
longestLine = lineWidth;
}
}
2021-12-25 10:04:45 +08:00
_numberOfLines = lineIndex + 1;
2019-11-23 20:27:39 +08:00
_textDesiredHeight = (_numberOfLines * _lineHeight * _bmfontScale) / contentScaleFactor;
if (_numberOfLines > 1)
_textDesiredHeight += (_numberOfLines - 1) * _lineSpacing;
2021-10-23 23:27:14 +08:00
Vec2 contentSize(_labelWidth, _labelHeight);
2019-11-23 20:27:39 +08:00
if (_labelWidth <= 0.f)
contentSize.width = longestLine;
if (_labelHeight <= 0.f)
contentSize.height = _textDesiredHeight;
setContentSize(contentSize);
2021-12-25 10:04:45 +08:00
_tailoredTopY = contentSize.height;
2019-11-23 20:27:39 +08:00
_tailoredBottomY = 0.f;
if (highestY > 0.f)
_tailoredTopY = contentSize.height + highestY;
if (lowestY < -_textDesiredHeight)
_tailoredBottomY = _textDesiredHeight + lowestY;
return true;
}
bool Label::multilineTextWrapByWord()
{
return multilineTextWrap(CC_CALLBACK_3(Label::getFirstWordLen, this));
}
bool Label::multilineTextWrapByChar()
{
return multilineTextWrap(CC_CALLBACK_3(Label::getFirstCharLen, this));
}
bool Label::isVerticalClamp()
{
if (_textDesiredHeight > _contentSize.height)
{
return true;
}
else
{
return false;
}
}
bool Label::isHorizontalClamp()
{
bool letterClamp = false;
for (int ctr = 0; ctr < _lengthOfString; ++ctr)
{
if (_lettersInfo[ctr].valid)
{
auto& letterDef = _fontAtlas->_letterDefinitions[_lettersInfo[ctr].utf32Char];
2021-12-25 10:04:45 +08:00
auto px = _lettersInfo[ctr].positionX + letterDef.width / 2 * _bmfontScale;
2019-11-23 20:27:39 +08:00
auto lineIndex = _lettersInfo[ctr].lineIndex;
2021-12-25 10:04:45 +08:00
if (_labelWidth > 0.f)
{
if (!this->_enableWrap)
{
if (px > _contentSize.width)
{
2019-11-23 20:27:39 +08:00
letterClamp = true;
break;
}
}
2021-12-25 10:04:45 +08:00
else
{
2019-11-23 20:27:39 +08:00
auto wordWidth = this->_linesWidth[lineIndex];
2021-12-25 10:04:45 +08:00
if (wordWidth > this->_contentSize.width && (px > _contentSize.width))
{
2019-11-23 20:27:39 +08:00
letterClamp = true;
break;
}
}
}
}
}
return letterClamp;
}
void Label::shrinkLabelToContentSize(const std::function<bool(void)>& lambda)
{
float fontSize = this->getRenderingFontSize();
2021-12-25 10:04:45 +08:00
int i = 0;
auto letterDefinition = _fontAtlas->_letterDefinitions;
2019-11-23 20:27:39 +08:00
auto tempLetterDefinition = letterDefinition;
2021-12-25 10:04:45 +08:00
float originalLineHeight = _lineHeight;
bool flag = true;
while (lambda())
{
2019-11-23 20:27:39 +08:00
++i;
float newFontSize = fontSize - i;
2021-12-25 10:04:45 +08:00
flag = false;
if (newFontSize <= 0)
{
2019-11-23 20:27:39 +08:00
break;
}
float scale = newFontSize / fontSize;
std::swap(_fontAtlas->_letterDefinitions, tempLetterDefinition);
_fontAtlas->scaleFontLetterDefinition(scale);
this->setLineHeight(originalLineHeight * scale);
if (_maxLineWidth > 0.f && !_lineBreakWithoutSpaces)
{
multilineTextWrapByWord();
}
else
{
multilineTextWrapByChar();
}
computeAlignmentOffset();
tempLetterDefinition = letterDefinition;
}
this->setLineHeight(originalLineHeight);
std::swap(_fontAtlas->_letterDefinitions, letterDefinition);
2021-12-25 10:04:45 +08:00
if (!flag)
{
if (fontSize - i >= 0)
{
2019-11-23 20:27:39 +08:00
this->scaleFontSizeDown(fontSize - i);
}
}
}
void Label::recordLetterInfo(const cocos2d::Vec2& point, char32_t utf32Char, int letterIndex, int lineIndex)
{
if (static_cast<std::size_t>(letterIndex) >= _lettersInfo.size())
{
LetterInfo tmpInfo;
_lettersInfo.push_back(tmpInfo);
}
2021-12-25 10:04:45 +08:00
_lettersInfo[letterIndex].lineIndex = lineIndex;
_lettersInfo[letterIndex].utf32Char = utf32Char;
_lettersInfo[letterIndex].valid = _fontAtlas->_letterDefinitions[utf32Char].validDefinition;
_lettersInfo[letterIndex].positionX = point.x;
_lettersInfo[letterIndex].positionY = point.y;
2019-11-23 20:27:39 +08:00
_lettersInfo[letterIndex].atlasIndex = -1;
}
void Label::recordPlaceholderInfo(int letterIndex, char32_t utf32Char)
{
if (static_cast<std::size_t>(letterIndex) >= _lettersInfo.size())
{
LetterInfo tmpInfo;
_lettersInfo.push_back(tmpInfo);
}
_lettersInfo[letterIndex].utf32Char = utf32Char;
2021-12-25 10:04:45 +08:00
_lettersInfo[letterIndex].valid = false;
2019-11-23 20:27:39 +08:00
}
NS_CC_END