2013-07-19 04:13:41 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2013-07-19 04:13:41 +08:00
|
|
|
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:
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2013-07-19 04:13:41 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2013-07-19 04:13:41 +08:00
|
|
|
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"
|
2015-11-19 16:06:13 +08:00
|
|
|
#include "2d/CCFontFNT.h"
|
2013-07-11 02:59:05 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2013-07-11 02:59:05 +08:00
|
|
|
|
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)
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
case axis::TextHAlignment::LEFT:
|
2015-07-24 18:38:45 +08:00
|
|
|
_linesOffsetX.assign(_numberOfLines, 0);
|
|
|
|
break;
|
2022-07-11 17:50:21 +08:00
|
|
|
case axis::TextHAlignment::CENTER:
|
2015-07-24 18:38:45 +08:00
|
|
|
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;
|
2022-07-11 17:50:21 +08:00
|
|
|
case axis::TextHAlignment::RIGHT:
|
2015-07-24 18:38:45 +08:00
|
|
|
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)
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
case axis::TextVAlignment::TOP:
|
2015-07-24 18:38:45 +08:00
|
|
|
_letterOffsetY = _contentSize.height;
|
|
|
|
break;
|
2022-07-11 17:50:21 +08:00
|
|
|
case axis::TextVAlignment::CENTER:
|
2015-07-24 18:38:45 +08:00
|
|
|
_letterOffsetY = (_contentSize.height + _textDesiredHeight) / 2.f;
|
|
|
|
break;
|
2022-07-11 17:50:21 +08:00
|
|
|
case axis::TextVAlignment::BOTTOM:
|
2015-07-24 18:38:45 +08:00
|
|
|
_letterOffsetY = _textDesiredHeight;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 12:38:32 +08:00
|
|
|
|
2018-03-13 09:40:24 +08:00
|
|
|
int Label::getFirstCharLen(const std::u32string& /*utf32Text*/, int /*startIndex*/, int /*textLen*/) const
|
2015-11-23 17:47:30 +08:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-03-13 09:40:24 +08:00
|
|
|
int Label::getFirstWordLen(const std::u32string& utf32Text, int startIndex, int textLen) const
|
2015-07-24 18:38:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int len = 0;
|
2018-03-13 09:40:24 +08:00
|
|
|
auto nextLetterX = 0;
|
2016-06-16 14:15:00 +08:00
|
|
|
FontLetterDefinition letterDef;
|
2022-07-15 19:17:01 +08:00
|
|
|
auto contentScaleFactor = AX_CONTENT_SCALE_FACTOR();
|
2018-03-13 09:40:24 +08:00
|
|
|
|
|
|
|
for (int index = startIndex; index < textLen; ++index)
|
2015-07-24 18:38:45 +08:00
|
|
|
{
|
2018-03-13 09:40:24 +08:00
|
|
|
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))))
|
2016-06-16 14:15:00 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-13 09:40:24 +08:00
|
|
|
if (!getFontLetterDef(character, letterDef))
|
2016-06-16 14:15:00 +08:00
|
|
|
{
|
2018-03-13 09:40:24 +08:00
|
|
|
break;
|
2016-06-16 14:15:00 +08:00
|
|
|
}
|
|
|
|
|
2018-03-13 09:40:24 +08:00
|
|
|
if (_maxLineWidth > 0.f)
|
2014-01-21 17:55:49 +08:00
|
|
|
{
|
2018-03-13 09:40:24 +08:00
|
|
|
auto letterX = (nextLetterX + letterDef.offsetX * _bmfontScale) / contentScaleFactor;
|
|
|
|
|
|
|
|
if (letterX + letterDef.width * _bmfontScale > _maxLineWidth)
|
|
|
|
break;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
2018-03-13 09:40:24 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
nextLetterX += static_cast<int>(letterDef.xAdvance * _bmfontScale + _additionalKerning);
|
2018-03-13 09:40:24 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
len++;
|
2013-07-23 02:40:39 +08:00
|
|
|
}
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2018-03-13 09:40:24 +08:00
|
|
|
if (len == 0 && textLen)
|
|
|
|
len = 1;
|
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
return len;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
2018-03-13 09:40:24 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
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();
|
2022-07-15 19:17:01 +08:00
|
|
|
_bmfontScale = _bmFontSize * AX_CONTENT_SCALE_FACTOR() / originalFontSize;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-19 16:06:13 +08:00
|
|
|
_bmfontScale = 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 16:41:52 +08:00
|
|
|
bool Label::multilineTextWrap(const std::function<int(const std::u32string&, int, int)>& nextTokenLen)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
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;
|
2018-06-08 17:06:29 +08:00
|
|
|
float nextWhitespaceWidth = 0.f;
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
auto contentScaleFactor = AX_CONTENT_SCALE_FACTOR();
|
2021-12-25 10:04:45 +08:00
|
|
|
float lineSpacing = _lineSpacing * contentScaleFactor;
|
|
|
|
float highestY = 0.f;
|
|
|
|
float lowestY = 0.f;
|
2015-07-24 18:38:45 +08:00
|
|
|
FontLetterDefinition letterDef;
|
|
|
|
Vec2 letterPosition;
|
2016-03-07 19:07:36 +08:00
|
|
|
bool nextChangeSize = true;
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
this->updateBMFontScale();
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int index = 0; index < textLen;)
|
2014-03-07 14:58:44 +08:00
|
|
|
{
|
2018-03-13 09:40:24 +08:00
|
|
|
char32_t character = _utf32Text[index];
|
|
|
|
if (character == StringUtils::UnicodeCharacters::NewLine)
|
2015-07-24 18:38:45 +08:00
|
|
|
{
|
|
|
|
_linesWidth.push_back(letterRight);
|
|
|
|
letterRight = 0.f;
|
|
|
|
lineIndex++;
|
2015-11-23 17:47:30 +08:00
|
|
|
nextTokenX = 0.f;
|
2021-12-25 10:04:45 +08:00
|
|
|
nextTokenY -= _lineHeight * _bmfontScale + lineSpacing;
|
2015-07-24 18:38:45 +08:00
|
|
|
recordPlaceholderInfo(index, character);
|
|
|
|
index++;
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-16 14:15:00 +08:00
|
|
|
|
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;
|
2018-06-08 17:06:29 +08:00
|
|
|
float whitespaceWidth = nextWhitespaceWidth;
|
2021-12-25 10:04:45 +08:00
|
|
|
bool newLine = false;
|
|
|
|
for (int tmp = 0; tmp < tokenLen; ++tmp)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
int letterIndex = index + tmp;
|
2021-12-25 10:04:45 +08:00
|
|
|
character = _utf32Text[letterIndex];
|
2018-03-13 09:40:24 +08:00
|
|
|
if (character == StringUtils::UnicodeCharacters::CarriageReturn)
|
2015-08-14 11:07:34 +08:00
|
|
|
{
|
|
|
|
recordPlaceholderInfo(letterIndex, character);
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-08 17:06:29 +08:00
|
|
|
|
2016-03-07 19:07:36 +08:00
|
|
|
// \b - Next char not change x position
|
2018-03-13 09:40:24 +08:00
|
|
|
if (character == StringUtils::UnicodeCharacters::NextCharNoChangeX)
|
2016-03-07 19:07:36 +08:00
|
|
|
{
|
|
|
|
nextChangeSize = false;
|
|
|
|
recordPlaceholderInfo(letterIndex, character);
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-13 09:40:24 +08:00
|
|
|
|
|
|
|
if (!getFontLetterDef(character, letterDef))
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
recordPlaceholderInfo(letterIndex, character);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("LabelTextFormatter error: can't find letter definition in font file for letter: 0x%x",
|
2021-12-25 10:04:45 +08:00
|
|
|
character);
|
2013-07-11 02:59:05 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
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)
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2018-06-08 17:06:29 +08:00
|
|
|
_linesWidth.push_back(letterRight - whitespaceWidth);
|
|
|
|
nextWhitespaceWidth = 0.f;
|
2021-12-25 10:04:45 +08:00
|
|
|
letterRight = 0.f;
|
2015-07-24 18:38:45 +08:00
|
|
|
lineIndex++;
|
2015-11-23 17:47:30 +08:00
|
|
|
nextTokenX = 0.f;
|
2021-12-25 10:04:45 +08:00
|
|
|
nextTokenY -= (_lineHeight * _bmfontScale + lineSpacing);
|
2015-07-24 18:38:45 +08:00
|
|
|
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-11-23 17:47:30 +08:00
|
|
|
letterPosition.y = (nextTokenY - letterDef.offsetY * _bmfontScale) / contentScaleFactor;
|
2015-07-24 18:38:45 +08:00
|
|
|
recordLetterInfo(letterPosition, character, letterIndex, lineIndex);
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2016-03-07 19:07:36 +08:00
|
|
|
if (nextChangeSize)
|
|
|
|
{
|
2018-06-08 17:06:29 +08:00
|
|
|
float newLetterWidth = 0.f;
|
2016-03-07 19:07:36 +08:00
|
|
|
if (_horizontalKernings && letterIndex < textLen - 1)
|
2021-12-26 23:26:34 +08:00
|
|
|
newLetterWidth = static_cast<float>(_horizontalKernings[letterIndex + 1]);
|
2018-06-08 17:06:29 +08:00
|
|
|
newLetterWidth += letterDef.xAdvance * _bmfontScale + _additionalKerning;
|
|
|
|
|
|
|
|
nextLetterX += newLetterWidth;
|
|
|
|
tokenRight = nextLetterX / contentScaleFactor;
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2018-06-08 17:06:29 +08:00
|
|
|
if (StringUtils::isUnicodeSpace(character))
|
2018-04-04 11:53:16 +08:00
|
|
|
{
|
2018-06-08 17:06:29 +08:00
|
|
|
nextWhitespaceWidth += newLetterWidth / contentScaleFactor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nextWhitespaceWidth = 0;
|
2018-04-04 11:53:16 +08:00
|
|
|
}
|
2016-03-07 19:07:36 +08:00
|
|
|
}
|
|
|
|
nextChangeSize = true;
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2015-11-23 17:47:30 +08:00
|
|
|
if (tokenHighestY < letterPosition.y)
|
|
|
|
tokenHighestY = letterPosition.y;
|
|
|
|
if (tokenLowestY > letterPosition.y - letterDef.height * _bmfontScale)
|
|
|
|
tokenLowestY = letterPosition.y - letterDef.height * _bmfontScale;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
if (newLine)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
nextTokenX = nextLetterX;
|
2015-11-23 17:47:30 +08:00
|
|
|
letterRight = tokenRight;
|
|
|
|
if (highestY < tokenHighestY)
|
|
|
|
highestY = tokenHighestY;
|
|
|
|
if (lowestY > tokenLowestY)
|
|
|
|
lowestY = tokenLowestY;
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2015-11-23 17:47:30 +08:00
|
|
|
index += tokenLen;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2018-06-08 17:06:29 +08:00
|
|
|
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)
|
2018-06-08 17:06:29 +08:00
|
|
|
{
|
|
|
|
if (longestLine < lineWidth)
|
|
|
|
longestLine = lineWidth;
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_numberOfLines = lineIndex + 1;
|
2015-11-19 16:06:13 +08:00
|
|
|
_textDesiredHeight = (_numberOfLines * _lineHeight * _bmfontScale) / contentScaleFactor;
|
2015-09-29 00:02:17 +08:00
|
|
|
if (_numberOfLines > 1)
|
|
|
|
_textDesiredHeight += (_numberOfLines - 1) * _lineSpacing;
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 contentSize(_labelWidth, _labelHeight);
|
2015-07-24 18:38:45 +08:00
|
|
|
if (_labelWidth <= 0.f)
|
|
|
|
contentSize.width = longestLine;
|
|
|
|
if (_labelHeight <= 0.f)
|
|
|
|
contentSize.height = _textDesiredHeight;
|
|
|
|
setContentSize(contentSize);
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_tailoredTopY = contentSize.height;
|
2015-07-24 18:38:45 +08:00
|
|
|
_tailoredBottomY = 0.f;
|
|
|
|
if (highestY > 0.f)
|
|
|
|
_tailoredTopY = contentSize.height + highestY;
|
|
|
|
if (lowestY < -_textDesiredHeight)
|
|
|
|
_tailoredBottomY = _textDesiredHeight + lowestY;
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2013-07-11 02:59:05 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-23 17:47:30 +08:00
|
|
|
bool Label::multilineTextWrapByWord()
|
2013-07-11 02:59:05 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
return multilineTextWrap(AX_CALLBACK_3(Label::getFirstWordLen, this));
|
2015-11-23 17:47:30 +08:00
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
|
2015-11-23 17:47:30 +08:00
|
|
|
bool Label::multilineTextWrapByChar()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
return multilineTextWrap(AX_CALLBACK_3(Label::getFirstCharLen, this));
|
2015-07-24 18:38:45 +08:00
|
|
|
}
|
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
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)
|
|
|
|
{
|
2017-02-06 16:41:52 +08:00
|
|
|
auto& letterDef = _fontAtlas->_letterDefinitions[_lettersInfo[ctr].utf32Char];
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto px = _lettersInfo[ctr].positionX + letterDef.width / 2 * _bmfontScale;
|
2015-12-07 18:13:03 +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)
|
|
|
|
{
|
2015-12-07 18:13:03 +08:00
|
|
|
letterClamp = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2015-12-07 18:13:03 +08:00
|
|
|
auto wordWidth = this->_linesWidth[lineIndex];
|
2021-12-25 10:04:45 +08:00
|
|
|
if (wordWidth > this->_contentSize.width && (px > _contentSize.width))
|
|
|
|
{
|
2015-12-07 18:13:03 +08:00
|
|
|
letterClamp = true;
|
|
|
|
break;
|
|
|
|
}
|
2015-11-19 16:06:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
return letterClamp;
|
|
|
|
}
|
|
|
|
|
2016-07-22 18:37:43 +08:00
|
|
|
void Label::shrinkLabelToContentSize(const std::function<bool(void)>& lambda)
|
2015-11-19 16:06:13 +08:00
|
|
|
{
|
|
|
|
float fontSize = this->getRenderingFontSize();
|
2016-06-16 14:15:00 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int i = 0;
|
|
|
|
auto letterDefinition = _fontAtlas->_letterDefinitions;
|
2015-11-19 16:06:13 +08:00
|
|
|
auto tempLetterDefinition = letterDefinition;
|
2021-12-25 10:04:45 +08:00
|
|
|
float originalLineHeight = _lineHeight;
|
|
|
|
bool flag = true;
|
|
|
|
while (lambda())
|
|
|
|
{
|
2015-11-19 16:06:13 +08:00
|
|
|
++i;
|
|
|
|
float newFontSize = fontSize - i;
|
2021-12-25 10:04:45 +08:00
|
|
|
flag = false;
|
|
|
|
if (newFontSize <= 0)
|
|
|
|
{
|
2015-11-19 16:06:13 +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)
|
|
|
|
{
|
2022-01-03 13:57:40 +08:00
|
|
|
this->scaleFontSize(fontSize - i);
|
2015-11-19 16:06:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void Label::recordLetterInfo(const axis::Vec2& point, char32_t utf32Char, int letterIndex, int lineIndex)
|
2015-07-24 18:38:45 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
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;
|
2017-05-05 22:52:15 +08:00
|
|
|
_lettersInfo[letterIndex].atlasIndex = -1;
|
2015-07-24 18:38:45 +08:00
|
|
|
}
|
2014-03-24 14:16:27 +08:00
|
|
|
|
2017-02-06 16:41:52 +08:00
|
|
|
void Label::recordPlaceholderInfo(int letterIndex, char32_t utf32Char)
|
2015-07-24 18:38:45 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2017-02-06 16:41:52 +08:00
|
|
|
_lettersInfo[letterIndex].utf32Char = utf32Char;
|
2021-12-25 10:04:45 +08:00
|
|
|
_lettersInfo[letterIndex].valid = false;
|
2013-07-11 02:59:05 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|