Skip to content

Commit 5624d1c

Browse files
authored
Fix Calling Label::setString after Label::getLetter causes layout issues (#2572)
1 parent 44a5391 commit 5624d1c

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

core/2d/Label.cpp

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ void Label::reset()
663663
_lineDrawNode = nullptr;
664664
}
665665
_strikethroughEnabled = false;
666-
_underlineEnabled = false;
666+
_underlineEnabled = false;
667667
setRotationSkewX(0); // reverse italics
668668
}
669669

@@ -1067,9 +1067,11 @@ void Label::updateLabelLetters()
10671067
letterSprite->setAtlasIndex(_lettersInfo[letterIndex].atlasIndex);
10681068
}
10691069

1070-
auto px = letterInfo.positionX + letterDef.width / 2 + _linesOffsetX[letterInfo.lineIndex];
1071-
auto py = letterInfo.positionY - letterDef.height / 2 + _letterOffsetY;
1070+
auto px =
1071+
letterInfo.positionX + _fontScale * uvRect.size.width / 2 + _linesOffsetX[letterInfo.lineIndex];
1072+
auto py = letterInfo.positionY - _fontScale * uvRect.size.height / 2 + _letterOffsetY;
10721073
letterSprite->setPosition(px, py);
1074+
letterSprite->setOpacity(_realOpacity);
10731075
}
10741076
else
10751077
{
@@ -1507,7 +1509,7 @@ void Label::enableUnderline()
15071509
_lineDrawNode->setGlobalZOrder(getGlobalZOrder());
15081510
_lineDrawNode->setOpacity(_displayedOpacity);
15091511
_lineDrawNode->properties.setFactor(_lineDrawNode->properties.getFactor() *
1510-
2.0f); // 2.0f: Makes the line smaller
1512+
2.0f); // 2.0f: Makes the line smaller
15111513
addChild(_lineDrawNode, 100000);
15121514
}
15131515
}
@@ -1526,8 +1528,8 @@ void Label::enableStrikethrough()
15261528
_lineDrawNode->setGlobalZOrder(getGlobalZOrder());
15271529
_lineDrawNode->setOpacity(_displayedOpacity);
15281530
_lineDrawNode->properties.setFactor(_lineDrawNode->properties.getFactor() *
1529-
2.0f); // 2.0f: Makes the line smaller
1530-
addChild(_lineDrawNode, 100000);
1531+
2.0f); // 2.0f: Makes the line smaller
1532+
addChild(_lineDrawNode, 100000);
15311533
}
15321534
}
15331535

@@ -1811,12 +1813,13 @@ void Label::updateContent()
18111813
nextY -= lineHeight + lineSpacing;
18121814
}
18131815
}
1814-
else if (_textSprite) // ...and is the logic for System fonts
1816+
else if (_textSprite) // ...and is the logic for System fonts
18151817
{
18161818
computeStringNumLines();
18171819
const auto spriteSize = _textSprite->getContentSize();
18181820

1819-
// FIXME: system fonts don't report the height of the font correctly. only the size of the texture, which is POT
1821+
// FIXME: system fonts don't report the height of the font correctly. only the size of the texture, which is
1822+
// POT
18201823
// FIXME: Might not work with different vertical alignments
18211824
const auto lineSize = spriteSize.height / static_cast<float>(_numberOfLines);
18221825
const auto thickness = std::max(std::ceil(lineSize * 0.12f * 2) / 2.f, 2.f);
@@ -1832,9 +1835,9 @@ void Label::updateContent()
18321835
}
18331836

18341837
if (_strikethroughEnabled)
1835-
{
1838+
{
18361839
const auto baseY = lineSize - lineSize / 2;
1837-
1840+
18381841
for (int i = 0; i < _numberOfLines; ++i)
18391842
{
18401843
float y = baseY + lineSize * i;
@@ -2495,8 +2498,8 @@ void Label::setTextColor(const Color4B& color)
24952498
_textColorF.a = _textColor.a / 255.0f;
24962499

24972500
// System font and TTF using setColor for Outline/Glow!");
2498-
if (_currentLabelType != LabelType::TTF && _currentLabelType != LabelType::STRING_TEXTURE)
2499-
setColor(Color3B(color));
2501+
if (_currentLabelType != LabelType::TTF && _currentLabelType != LabelType::STRING_TEXTURE)
2502+
setColor(Color3B(color));
25002503
}
25012504

25022505
void Label::updateColor()
@@ -2968,7 +2971,7 @@ bool Label::multilineTextWrap(bool breakOnChar, bool ignoreOverflow)
29682971
{
29692972
recordPlaceholderInfo(letterIndex, character);
29702973
AXLOGW("LabelTextFormatter error: can't find letter definition in font file for letter: 0x{:x}",
2971-
static_cast<uint32_t>(character));
2974+
static_cast<uint32_t>(character));
29722975
continue;
29732976
}
29742977

@@ -3015,7 +3018,7 @@ bool Label::multilineTextWrap(bool breakOnChar, bool ignoreOverflow)
30153018
nextChangeSize = true;
30163019

30173020
tokenHighestY = std::max(tokenHighestY, letterPosition.y);
3018-
tokenLowestY = std::min(tokenLowestY, letterPosition.y - letterDef.height * _fontScale);
3021+
tokenLowestY = std::min(tokenLowestY, letterPosition.y - letterDef.height * _fontScale);
30193022
}
30203023

30213024
if (newLine)
@@ -3025,8 +3028,8 @@ bool Label::multilineTextWrap(bool breakOnChar, bool ignoreOverflow)
30253028

30263029
nextTokenX = nextLetterX;
30273030
letterRight = tokenRight;
3028-
highestY = std::max(highestY, tokenHighestY);
3029-
lowestY = std::min(lowestY, tokenLowestY);
3031+
highestY = std::max(highestY, tokenHighestY);
3032+
lowestY = std::min(lowestY, tokenLowestY);
30303033

30313034
index += tokenLen;
30323035
}
@@ -3134,18 +3137,23 @@ bool Label::isHorizontalClamp()
31343137
return letterClamp;
31353138
}
31363139

3137-
void Label::recordLetterInfo(const ax::Vec2& point, char32_t utf32Char, int letterIndex, int lineIndex, float offsetX, float offsetY)
3140+
void Label::recordLetterInfo(const ax::Vec2& point,
3141+
char32_t utf32Char,
3142+
int letterIndex,
3143+
int lineIndex,
3144+
float offsetX,
3145+
float offsetY)
31383146
{
31393147
if (static_cast<std::size_t>(letterIndex) >= _lettersInfo.size())
31403148
{
31413149
LetterInfo tmpInfo;
31423150
_lettersInfo.emplace_back(tmpInfo);
31433151
}
3144-
_lettersInfo[letterIndex].lineIndex = lineIndex;
3145-
_lettersInfo[letterIndex].utf32Char = utf32Char;
3146-
_lettersInfo[letterIndex].valid = _fontAtlas->_letterDefinitions[utf32Char].validDefinition && utf32Char != ' ';
3147-
_lettersInfo[letterIndex].positionX = point.x;
3148-
_lettersInfo[letterIndex].positionY = point.y;
3152+
_lettersInfo[letterIndex].lineIndex = lineIndex;
3153+
_lettersInfo[letterIndex].utf32Char = utf32Char;
3154+
_lettersInfo[letterIndex].valid = _fontAtlas->_letterDefinitions[utf32Char].validDefinition && utf32Char != ' ';
3155+
_lettersInfo[letterIndex].positionX = point.x;
3156+
_lettersInfo[letterIndex].positionY = point.y;
31493157
_lettersInfo[letterIndex].atlasIndex = -1;
31503158
_lettersInfo[letterIndex].offsetX = offsetX;
31513159
_lettersInfo[letterIndex].offsetY = offsetY;
@@ -3162,4 +3170,4 @@ void Label::recordPlaceholderInfo(int letterIndex, char32_t utf32Char)
31623170
_lettersInfo[letterIndex].valid = false;
31633171
}
31643172

3165-
}
3173+
} // namespace ax

0 commit comments

Comments
 (0)