Browse Source

Fix TextLayout overflow (#17914)

* Update TextLayout only if finalSize is different from _constraint

* Added comment
release/11.2.4
Compunet 1 year ago
committed by Max Katz
parent
commit
68ab3225cb
  1. 18
      src/Avalonia.Controls/Presenters/TextPresenter.cs

18
src/Avalonia.Controls/Presenters/TextPresenter.cs

@ -634,6 +634,8 @@ namespace Avalonia.Controls.Presenters
protected override Size ArrangeOverride(Size finalSize) protected override Size ArrangeOverride(Size finalSize)
{ {
var finalWidth = finalSize.Width;
var textWidth = Math.Ceiling(TextLayout.OverhangLeading + TextLayout.WidthIncludingTrailingWhitespace + TextLayout.OverhangTrailing); var textWidth = Math.Ceiling(TextLayout.OverhangLeading + TextLayout.WidthIncludingTrailingWhitespace + TextLayout.OverhangTrailing);
if (finalSize.Width < textWidth) if (finalSize.Width < textWidth)
@ -641,15 +643,17 @@ namespace Avalonia.Controls.Presenters
finalSize = finalSize.WithWidth(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(finalWidth), double.PositiveInfinity);
}
_constraint = new Size(Math.Ceiling(finalSize.Width), double.PositiveInfinity);
_textLayout?.Dispose(); _textLayout?.Dispose();
_textLayout = null; _textLayout = null;
}
return finalSize; return finalSize;
} }

Loading…
Cancel
Save