From 5f0ae7fccb24bb769aae8b4de7ff99587108d49e Mon Sep 17 00:00:00 2001 From: solveEM <131154097+solveEM@users.noreply.github.com> Date: Thu, 30 Jan 2025 08:48:11 +0100 Subject: [PATCH] TextLayout: TextStyleOverrides aren't applied correctly (#17922) * Added FormattedTextSource->GetTextRun failing test * Ensure that TextLayout-TextRuns are correctly generated from the passed TextStyleOverrides using the FormattedTextSource->GetTextRun method. --------- Co-authored-by: Benedikt Stebner --- .../TextFormatting/FormattedTextSource.cs | 10 +----- .../FormattedTextSourceTests.cs | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 tests/Avalonia.Base.UnitTests/Media/TextFormatting/FormattedTextSourceTests.cs diff --git a/src/Avalonia.Base/Media/TextFormatting/FormattedTextSource.cs b/src/Avalonia.Base/Media/TextFormatting/FormattedTextSource.cs index a4fc1edd14..1586639fbd 100644 --- a/src/Avalonia.Base/Media/TextFormatting/FormattedTextSource.cs +++ b/src/Avalonia.Base/Media/TextFormatting/FormattedTextSource.cs @@ -58,7 +58,6 @@ namespace Avalonia.Media.TextFormatting var currentProperties = defaultProperties; - var hasOverride = false; var i = 0; @@ -92,15 +91,8 @@ namespace Avalonia.Media.TextFormatting } length = Math.Max(0, textRange.Start + textRange.Length - firstTextSourceIndex); - - if (hasOverride) - { - continue; - } - - hasOverride = true; - currentProperties = propertiesOverride.Value; + break; } if (length < text.Length && i == textModifier.Count) diff --git a/tests/Avalonia.Base.UnitTests/Media/TextFormatting/FormattedTextSourceTests.cs b/tests/Avalonia.Base.UnitTests/Media/TextFormatting/FormattedTextSourceTests.cs new file mode 100644 index 0000000000..d6eb4c98d9 --- /dev/null +++ b/tests/Avalonia.Base.UnitTests/Media/TextFormatting/FormattedTextSourceTests.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; +using Avalonia.Media; +using Avalonia.Media.TextFormatting; +using Avalonia.Utilities; +using Xunit; + +namespace Avalonia.Base.UnitTests.Media.TextFormatting +{ + + public class FormattedTextSourceTests + { + [Fact] + public void GetTextRun_WithTwoTextStyleOverrides_ShouldGenerateCorrectFirstRun() + { + //Prepare a sample text: The two "He" at the beginning of each line should be displayed with other TextRunProperties + string text = "Hello World\r\nHello"; + Typeface typeface = new Typeface(); + GenericTextRunProperties defaultTextRunProperties = new GenericTextRunProperties(typeface); + IReadOnlyList> textStyleOverrides = new List>() + { + new ValueSpan(0, 2, new GenericTextRunProperties(typeface, backgroundBrush: Brushes.Aqua)), + new ValueSpan(13, 2, new GenericTextRunProperties(typeface, backgroundBrush: Brushes.Aqua)), + }; + + FormattedTextSource textSource = new FormattedTextSource(text, defaultTextRunProperties, textStyleOverrides); + TextRun textRun = textSource.GetTextRun(0); + + Assert.Equal(2, textRun.Length); + Assert.Equal("He", textRun.Text.ToString()); + } + } +}