Skip to content

Commit

Permalink
Fix TextLayout overflow (#17914)
Browse files Browse the repository at this point in the history
* Update TextLayout only if finalSize is different from _constraint

* Added comment
  • Loading branch information
dme-compunet authored Jan 9, 2025
1 parent f2f017c commit 8fe22dc
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Avalonia.Controls/Presenters/TextPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -634,22 +634,26 @@ protected override Size MeasureOverride(Size availableSize)

protected override Size ArrangeOverride(Size finalSize)
{
var finalWidth = finalSize.Width;

var textWidth = Math.Ceiling(TextLayout.OverhangLeading + TextLayout.WidthIncludingTrailingWhitespace + TextLayout.OverhangTrailing);

if (finalSize.Width < textWidth)
{
finalSize = finalSize.WithWidth(textWidth);
}

if (MathUtilities.AreClose(_constraint.Width, finalSize.Width))
// Check if the '_constraint' has changed since the last measure,
// if so recalculate the TextLayout according to the new size
// NOTE: It is important to check this against the actual final size
// (excluding the trailing whitespace) to avoid TextLayout overflow.
if (MathUtilities.AreClose(_constraint.Width, finalWidth) == false)
{
return finalSize;
}

_constraint = new Size(Math.Ceiling(finalSize.Width), double.PositiveInfinity);
_constraint = new Size(Math.Ceiling(finalWidth), double.PositiveInfinity);

_textLayout?.Dispose();
_textLayout = null;
_textLayout?.Dispose();
_textLayout = null;
}

return finalSize;
}
Expand Down

0 comments on commit 8fe22dc

Please sign in to comment.