mirror of https://github.com/axmolengine/axmol.git
fix TTF and BMFont word wrap
This commit is contained in:
parent
d7b495c3d1
commit
f0f8663fa9
|
@ -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();
|
||||
|
||||
|
|
|
@ -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,9 +86,28 @@ 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;
|
||||
|
@ -251,12 +270,12 @@ bool Label::multilineTextWrap(std::function<int(const std::u16string&, int, int)
|
|||
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue