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

Text processing fixes #8094

Merged
merged 12 commits into from
May 31, 2022
Merged
11 changes: 9 additions & 2 deletions src/Avalonia.Base/Media/TextFormatting/TextBounds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ public sealed class TextBounds
/// <summary>
/// Constructing TextBounds object
/// </summary>
internal TextBounds(Rect bounds, FlowDirection flowDirection)
internal TextBounds(Rect bounds, FlowDirection flowDirection, IList<TextRunBounds> runBounds)
{
Rectangle = bounds;
FlowDirection = flowDirection;
TextRunBounds = runBounds;
}

/// <summary>
/// Bounds rectangle
/// </summary>
public Rect Rectangle { get; }
public Rect Rectangle { get; internal set; }

/// <summary>
/// Text flow direction inside the boundary rectangle
/// </summary>
public FlowDirection FlowDirection { get; }

/// <summary>
/// Get a list of run bounding rectangles
/// </summary>
/// <returns>Array of text run bounds</returns>
public IList<TextRunBounds> TextRunBounds { get; }
}
}
29 changes: 19 additions & 10 deletions src/Avalonia.Base/Media/TextFormatting/TextLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public IEnumerable<Rect> HitTestTextRange(int start, int length)
foreach (var textLine in TextLines)
{
//Current line isn't covered.
if (textLine.FirstTextSourceIndex + textLine.Length <= start)
if (textLine.FirstTextSourceIndex + textLine.Length < start)
{
currentY += textLine.Height;

Expand All @@ -239,18 +239,27 @@ public IEnumerable<Rect> HitTestTextRange(int start, int length)

var textBounds = textLine.GetTextBounds(start, length);

foreach (var bounds in textBounds)
if(textBounds.Count > 0)
{
Rect? last = result.Count > 0 ? result[result.Count - 1] : null;

if (last.HasValue && MathUtilities.AreClose(last.Value.Right, bounds.Rectangle.Left) && MathUtilities.AreClose(last.Value.Top, currentY))
foreach (var bounds in textBounds)
{
result[result.Count - 1] = last.Value.WithWidth(last.Value.Width + bounds.Rectangle.Width);
Rect? last = result.Count > 0 ? result[result.Count - 1] : null;

if (last.HasValue && MathUtilities.AreClose(last.Value.Right, bounds.Rectangle.Left) && MathUtilities.AreClose(last.Value.Top, currentY))
{
result[result.Count - 1] = last.Value.WithWidth(last.Value.Width + bounds.Rectangle.Width);
}
else
{
result.Add(bounds.Rectangle.WithY(currentY));
}

foreach (var runBounds in bounds.TextRunBounds)
{
start += runBounds.Length;
length -= runBounds.Length;
}
}
else
{
result.Add(bounds.Rectangle.WithY(currentY));
}
}

if(textLine.FirstTextSourceIndex + textLine.Length >= start + length)
Expand Down
Loading