Browse Source

Properly handle multiple line breaks

Benedikt Schroeder 5 years ago
parent
commit
cb6785e700

+ 2 - 2
src/Avalonia.Visuals/Media/GlyphRun.cs

@@ -399,14 +399,14 @@ namespace Avalonia.Media
 
                 if (characterIndex > GlyphClusters[GlyphClusters.Length - 1])
                 {
-                    return _glyphClusters.End;
+                    return _glyphClusters.Length - 1;
                 }
             }
             else
             {
                 if (characterIndex < GlyphClusters[GlyphClusters.Length - 1])
                 {
-                    return _glyphClusters.End;
+                    return _glyphClusters.Length - 1;
                 }
 
                 if (characterIndex > GlyphClusters[0])

+ 0 - 8
src/Avalonia.Visuals/Media/TextFormatting/TextFormatterImpl.cs

@@ -339,14 +339,6 @@ namespace Avalonia.Media.TextFormatting
                     return true;
                 }
 
-                //The line breaker isn't treating \n\r as a pair so we have to fix that here.
-                if (textRun.Text[lineBreak.PositionMeasure] == '\n'
-                    && textRun.Text[lineBreak.PositionWrap] == '\r')
-                {
-                    lineBreak = new LineBreak(lineBreak.PositionMeasure, lineBreak.PositionWrap + 1,
-                        lineBreak.Required);
-                }
-
                 return true;
             }
 

+ 21 - 1
tests/Avalonia.Skia.UnitTests/Media/TextFormatting/TextLayoutTests.cs

@@ -417,7 +417,6 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
 
         [Theory]
         [InlineData("abcde\r\n", 7)] // Carriage Return + Line Feed
-        [InlineData("abcde\n\r", 7)] // This isn't valid but we somehow have to support it.
         [InlineData("abcde\u000A", 6)] // Line Feed
         [InlineData("abcde\u000B", 6)] // Vertical Tab
         [InlineData("abcde\u000C", 6)] // Form Feed
@@ -575,6 +574,27 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
             }
         }
 
+        [Fact]
+        public void Should_Process_Multiple_NewLines_Properly()
+        {
+            using (Start())
+            {
+                var text = "123\r\n\r\n456\r\n\r\n";
+                var layout = new TextLayout(
+                    text,
+                    Typeface.Default,
+                    12.0f,
+                    Brushes.Black);
+
+                Assert.Equal(5, layout.TextLines.Count);
+
+                Assert.Equal("123\r\n", layout.TextLines[0].TextRuns[0].Text);
+                Assert.Equal("\r\n", layout.TextLines[1].TextRuns[0].Text);
+                Assert.Equal("456\r\n", layout.TextLines[2].TextRuns[0].Text);
+                Assert.Equal("\r\n", layout.TextLines[3].TextRuns[0].Text);
+            }
+        }
+
         [Fact]
         public void Should_Wrap_Min_OneCharacter_EveryLine()
         {