Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix text hit testing for invisible runs #13135

Merged
merged 5 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/Avalonia.Base/Media/TextFormatting/TextLineImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ public override CharacterHit GetCharacterHitFromDistance(double distance)
continue;
}
}
else
{
currentPosition += currentRun.Length;

continue;
}

break;
}
Expand Down Expand Up @@ -990,6 +996,12 @@ private TextRunBounds GetRunBoundsLeftToRight(ShapedTextRun currentRun, double s

var characterLength = Math.Abs(startHit.FirstCharacterIndex + startHit.TrailingLength - endHit.FirstCharacterIndex - endHit.TrailingLength);

//Make sure we properly deal with zero width space runs
if (characterLength == 0 && currentRun.Length > 0 && currentRun.GlyphRun.Metrics.WidthIncludingTrailingWhitespace == 0)
{
characterLength = currentRun.Length;
}

if (endX < startX)
{
(endX, startX) = (startX, endX);
Expand All @@ -1003,7 +1015,9 @@ private TextRunBounds GetRunBoundsLeftToRight(ShapedTextRun currentRun, double s

var runWidth = endX - startX;

return new TextRunBounds(new Rect(startX, 0, runWidth, Height), currentPosition, characterLength, currentRun);
var textSourceIndex = offset + startHit.FirstCharacterIndex;

return new TextRunBounds(new Rect(startX, 0, runWidth, Height), textSourceIndex, characterLength, currentRun);
}

private TextRunBounds GetRunBoundsRightToLeft(ShapedTextRun currentRun, double endX,
Expand Down Expand Up @@ -1038,6 +1052,17 @@ private TextRunBounds GetRunBoundsRightToLeft(ShapedTextRun currentRun, double e

var characterLength = Math.Abs(startHit.FirstCharacterIndex + startHit.TrailingLength - endHit.FirstCharacterIndex - endHit.TrailingLength);

//Make sure we properly deal with zero width space runs
if (characterLength == 0 && currentRun.Length > 0 && currentRun.GlyphRun.Metrics.WidthIncludingTrailingWhitespace == 0)
{
characterLength = currentRun.Length;
}

if(startHit.FirstCharacterIndex > endHit.FirstCharacterIndex)
{
startHit = endHit;
}

if (endX < startX)
{
(endX, startX) = (startX, endX);
Expand All @@ -1051,7 +1076,9 @@ private TextRunBounds GetRunBoundsRightToLeft(ShapedTextRun currentRun, double e

var runWidth = endX - startX;

return new TextRunBounds(new Rect(startX, 0, runWidth, Height), currentPosition, characterLength, currentRun);
var textSourceIndex = offset + startHit.FirstCharacterIndex;

return new TextRunBounds(new Rect(startX, 0, runWidth, Height), textSourceIndex, characterLength, currentRun);
}

public override void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,64 @@ public void Should_Retain_TextEndOfParagraph_With_TextWrapping()
Assert.NotNull(textLine.TextLineBreak.TextEndOfLine);
}
}

[Fact]
public void Should_HitTestStringWithInvisibleRuns()
{
var defaultRunProperties = new GenericTextRunProperties(Typeface.Default, foregroundBrush: Brushes.Black);
var paragraphProperties = new GenericTextParagraphProperties(defaultRunProperties);
//var textSource = new ListTextSource(



using (Start())
{
var hello = new TextCharacters("Hello",
new GenericTextRunProperties(Typeface.Default, foregroundBrush: Brushes.Black));
var world = new TextCharacters("world",
new GenericTextRunProperties(Typeface.Default, foregroundBrush: Brushes.Red));

var source = new ListTextSource(new InvisibleRun(1), hello, new InvisibleRun(1), world);

var textLine =
TextFormatter.Current.FormatLine(source, 0, double.PositiveInfinity, paragraphProperties);

void VerifyHit(int offset)
{
var glyphCenter = textLine.GetTextBounds(offset, 1)[0].Rectangle.Center;
var hit = textLine.GetCharacterHitFromDistance(glyphCenter.X);
Assert.Equal(offset, hit.FirstCharacterIndex);
}
VerifyHit(3);
VerifyHit(8);
}
}

[Fact]
public void GetTextBounds_For_TextLine_With_ZeroWidthSpaces_Does_Not_Freeze()
{
var defaultRunProperties = new GenericTextRunProperties(Typeface.Default, foregroundBrush: Brushes.Black);
var paragraphProperties = new GenericTextParagraphProperties(defaultRunProperties);

using (Start())
{
var text = new TextCharacters("\u200B\u200B",
new GenericTextRunProperties(Typeface.Default, foregroundBrush: Brushes.Black));

var source = new ListTextSource(text, new InvisibleRun(1), new TextEndOfParagraph());

var textLine =
TextFormatter.Current.FormatLine(source, 0, double.PositiveInfinity, paragraphProperties);

var bounds = textLine.GetTextBounds(0, 3);

Assert.Equal(1, bounds.Count);

var runBounds = bounds[0].TextRunBounds;

Assert.Equal(2, runBounds.Count);
}
}

protected readonly record struct SimpleTextSource : ITextSource
{
Expand Down Expand Up @@ -776,6 +834,32 @@ public TextRun GetTextRun(int textSourceIndex)
return new TextCharacters(_text, new GenericTextRunProperties(Typeface.Default, foregroundBrush: Brushes.Black));
}
}

private class ListTextSource : ITextSource
{
private Dictionary<int, TextRun> _runs = new();

public ListTextSource(params TextRun[] runs) : this((IEnumerable<TextRun>)runs)
{

}

public ListTextSource(IEnumerable<TextRun> runs)
{
var off = 0;
foreach (var r in runs)
{
_runs[off] = r;
off += r.Length;
}
}

public TextRun GetTextRun(int textSourceIndex)
{
_runs.TryGetValue(textSourceIndex, out var rv);
return rv;
}
}

private class RectangleRun : DrawableTextRun
{
Expand All @@ -798,6 +882,15 @@ public override void Draw(DrawingContext drawingContext, Point origin)
}
}
}

private class InvisibleRun : TextRun
{
public InvisibleRun(int length)
{
Length = length;
}
public override int Length { get; }
}

public static IDisposable Start()
{
Expand Down