Browse Source

Prevent infinite loop

pull/11667/head
Benedikt Stebner 3 years ago
parent
commit
bfdd3fa325
  1. 1
      samples/Sandbox/MainWindow.axaml
  2. 22
      src/Avalonia.Base/Media/TextFormatting/TextLineImpl.cs
  3. 28
      tests/Avalonia.Skia.UnitTests/Media/TextFormatting/TextLineTests.cs

1
samples/Sandbox/MainWindow.axaml

@ -1,4 +1,5 @@
<Window xmlns="https://github.com/avaloniaui" <Window xmlns="https://github.com/avaloniaui"
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
x:Class="Sandbox.MainWindow"> x:Class="Sandbox.MainWindow">
<TextBox Text="אבג ABC אבג 123"/>
</Window> </Window>

22
src/Avalonia.Base/Media/TextFormatting/TextLineImpl.cs

@ -371,14 +371,16 @@ namespace Avalonia.Media.TextFormatting
IndexedTextRun currentIndexedRun = _indexedTextRuns[i]; IndexedTextRun currentIndexedRun = _indexedTextRuns[i];
while(currentIndexedRun.TextSourceCharacterIndex != currentPosition) while (currentIndexedRun.TextSourceCharacterIndex != currentPosition)
{ {
if(i + 1 < _indexedTextRuns.Count) if (i + 1 == _indexedTextRuns.Count)
{ {
i++; break;
currentIndexedRun = _indexedTextRuns[i];
} }
i++;
currentIndexedRun = _indexedTextRuns[i];
} }
return currentIndexedRun; return currentIndexedRun;
@ -604,12 +606,14 @@ namespace Avalonia.Media.TextFormatting
while (currentIndexedRun.TextSourceCharacterIndex != currentPosition) while (currentIndexedRun.TextSourceCharacterIndex != currentPosition)
{ {
if (i + 1 < _indexedTextRuns.Count) if (i + 1 == _indexedTextRuns.Count)
{ {
i++; break;
currentIndexedRun = _indexedTextRuns[i];
} }
i++;
currentIndexedRun = _indexedTextRuns[i];
} }
return currentIndexedRun; return currentIndexedRun;

28
tests/Avalonia.Skia.UnitTests/Media/TextFormatting/TextLineTests.cs

@ -9,6 +9,7 @@ using Avalonia.Media.TextFormatting;
using Avalonia.UnitTests; using Avalonia.UnitTests;
using Avalonia.Utilities; using Avalonia.Utilities;
using Xunit; using Xunit;
using static System.Net.Mime.MediaTypeNames;
namespace Avalonia.Skia.UnitTests.Media.TextFormatting namespace Avalonia.Skia.UnitTests.Media.TextFormatting
{ {
@ -1072,7 +1073,7 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
} }
[Fact] [Fact]
public void Should_GetTextBounds_BiDi() public void Should_GetTextBounds_Bidi()
{ {
var text = "אבגדה 12345 ABCDEF אבגדה"; var text = "אבגדה 12345 ABCDEF אבגדה";
@ -1120,6 +1121,31 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
} }
} }
[Fact]
public void Should_GetTextBounds_Bidi_2()
{
var text = "אבג ABC אבג 123";
using (Start())
{
var defaultProperties = new GenericTextRunProperties(Typeface.Default);
var textSource = new SingleBufferTextSource(text, defaultProperties, true);
var formatter = new TextFormatterImpl();
var textLine =
formatter.FormatLine(textSource, 0, double.PositiveInfinity,
new GenericTextParagraphProperties(FlowDirection.LeftToRight, TextAlignment.Left,
true, true, defaultProperties, TextWrapping.NoWrap, 0, 0, 0));
var bounds = textLine.GetTextBounds(0, text.Length);
Assert.Equal(5, bounds.Count);
Assert.Equal(textLine.WidthIncludingTrailingWhitespace, bounds.Last().Rectangle.Right);
}
}
private class FixedRunsTextSource : ITextSource private class FixedRunsTextSource : ITextSource
{ {
private readonly IReadOnlyList<TextRun> _textRuns; private readonly IReadOnlyList<TextRun> _textRuns;

Loading…
Cancel
Save