Skip to content

Commit e011861

Browse files
GillibaldMrJul
authored andcommitted
Fix TextWrapping in combination with TextEndOfLine runs (#18523)
1 parent 408ca45 commit e011861

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/Avalonia.Base/Media/TextFormatting/TextFormatterImpl.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,11 @@ private static int MeasureLength(IReadOnlyList<TextRun> textRuns, double paragra
574574
{
575575
var measuredLength = 0;
576576
var currentWidth = 0.0;
577+
var runIndex = 0;
577578

578-
for (var i = 0; i < textRuns.Count; ++i)
579+
for (; runIndex < textRuns.Count; ++runIndex)
579580
{
580-
var currentRun = textRuns[i];
581+
var currentRun = textRuns[runIndex];
581582

582583
switch (currentRun)
583584
{
@@ -630,7 +631,14 @@ private static int MeasureLength(IReadOnlyList<TextRun> textRuns, double paragra
630631
runLength = clusterLength;
631632
}
632633

633-
return measuredLength + runLength;
634+
measuredLength += runLength;
635+
636+
if (runIndex < textRuns.Count - 1 && runLength == currentRun.Length && textRuns[runIndex + 1] is TextEndOfLine endOfLine)
637+
{
638+
measuredLength += endOfLine.Length;
639+
}
640+
641+
return measuredLength;
634642
}
635643

636644
currentWidth += clusterWidth;

tests/Avalonia.Skia.UnitTests/Media/TextFormatting/TextLayoutTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,30 @@ public void Should_Measure_TextLayoutSymbolWithAndWidthIncludingTrailingWhitespa
11601160
}
11611161
}
11621162

1163+
[Fact]
1164+
public void Should_Wrap_With_LineEnd()
1165+
{
1166+
using (Start())
1167+
{
1168+
var defaultProperties =
1169+
new GenericTextRunProperties(Typeface.Default, 72, foregroundBrush: Brushes.Black);
1170+
1171+
var paragraphProperties = new GenericTextParagraphProperties(defaultProperties, textWrap: TextWrapping.Wrap);
1172+
1173+
var textLayout = new TextLayout(new SingleBufferTextSource("01", defaultProperties, true), paragraphProperties, maxWidth: 36);
1174+
1175+
Assert.Equal(2, textLayout.TextLines.Count);
1176+
1177+
var lastLine = textLayout.TextLines.Last();
1178+
1179+
Assert.Equal(2, lastLine.TextRuns.Count);
1180+
1181+
var lastRun = lastLine.TextRuns.Last();
1182+
1183+
Assert.IsAssignableFrom<TextEndOfLine>(lastRun);
1184+
}
1185+
}
1186+
11631187
private static IDisposable Start()
11641188
{
11651189
var disposable = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface

0 commit comments

Comments
 (0)