From 8110d1bc64073afbcd165e3206b93c4df136e760 Mon Sep 17 00:00:00 2001 From: donandren Date: Wed, 15 Jun 2016 02:06:14 +0300 Subject: [PATCH] fixed HitTestTextRange in skia FormattedTextImpl --- src/Skia/Avalonia.Skia/FormattedTextImpl.cs | 27 +++++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Skia/Avalonia.Skia/FormattedTextImpl.cs b/src/Skia/Avalonia.Skia/FormattedTextImpl.cs index 7722dbf7ef..55f5286bfa 100644 --- a/src/Skia/Avalonia.Skia/FormattedTextImpl.cs +++ b/src/Skia/Avalonia.Skia/FormattedTextImpl.cs @@ -3,15 +3,14 @@ using Avalonia.Platform; using SkiaSharp; using System; using System.Collections.Generic; +using System.Linq; using System.Runtime.InteropServices; using System.Text; -using System.Linq; namespace Avalonia.Skia { unsafe class FormattedTextImpl : IFormattedTextImpl { - public FormattedTextImpl(string text, TextWrapping wrapping = TextWrapping.NoWrap) { _text = text ?? string.Empty; @@ -100,7 +99,6 @@ namespace Avalonia.Skia { offset = line.TextLength > line.Length ? line.Length : line.Length - 1; - } return new TextHitTestResult @@ -131,7 +129,6 @@ namespace Avalonia.Skia return new Rect(r.X + r.Width, r.Y, 0, LineHeight); } - if (rects.Count == 0) { //empty text @@ -149,10 +146,25 @@ namespace Avalonia.Skia public IEnumerable HitTestTextRange(int index, int length) { + List result = new List(); + var rects = GetRects(); - for (var c = 0; c < length; c++) - yield return rects[c + index]; + int lastIndex = index + length - 1; + + foreach (var line in _skiaLines.Where(l => + (l.Start + l.Length) > index && + lastIndex >= l.Start)) + { + int lineEndIndex = line.Start + (line.Length > 0 ? line.Length - 1 : 0); + + double left = rects[line.Start > index ? line.Start : index].X; + double right = rects[lineEndIndex > lastIndex ? lastIndex : lineEndIndex].Right; + + result.Add(new Rect(left, line.Top, right - left, line.Height)); + } + + return result; } public Size Measure() @@ -344,7 +356,7 @@ namespace Avalonia.Skia //this is a quick fix so we have skia rendering //properly right and center align - //TODO: find a better implementation including + //TODO: find a better implementation including //hittesting and text selection working properly //paint.TextAlign = SKTextAlign.Right; @@ -365,7 +377,6 @@ namespace Avalonia.Skia } } - canvas.DrawText(subString, x, origin.Y + line.Top + LineOffset, paint); } }