diff --git a/CSharpMath.Editor/Extensions/DisplayEditingExtensions.TextLine.cs b/CSharpMath.Editor/Extensions/DisplayEditingExtensions.TextLine.cs index 1884f7232..099e5036e 100644 --- a/CSharpMath.Editor/Extensions/DisplayEditingExtensions.TextLine.cs +++ b/CSharpMath.Editor/Extensions/DisplayEditingExtensions.TextLine.cs @@ -70,16 +70,24 @@ public static int MathListIndexToStringIndex(this TextLineDisplay public static MathListIndex IndexForPoint(this TextLineDisplay self, TypesettingContext context, PointF point) where TFont : IFont { // Convert the point to the reference of the CTLine var relativePoint = new PointF(point.X - self.Position.X, point.Y - self.Position.Y); - var indices = self.Runs.Select(run => run.Run.GlyphIndexForXOffset(context, relativePoint.Plus(run.Position).X)).Where(x => x.HasValue).ToArray(); - if (indices.Length == 0) + var runsAndIndicies = + self.Runs + .Select(run => ValueTuple.Create(run, run.Run.GlyphIndexForXOffset(context, relativePoint.Plus(run.Position).X))) + .Where(x => x.Item2.HasValue) + .ToArray(); + if (runsAndIndicies.Length == 0) return null; - var index = indices.Single().GetValueOrDefault(); - - // index will be between 0 and _range.length inclusive - if (index < 0 || index > self.Range.Length) + var (r, nindex) = runsAndIndicies.Single(); + var index = nindex.GetValueOrDefault(); + var diffLng = r.Run.Length != r.Range.Length; + if (index < 0 || (!diffLng && index > self.Range.Length) || (diffLng && index > r.Run.Length)) throw new InvalidCodePathException($"Returned index out of range: {index}, range ({self.Range.Location}, {self.Range.Length})"); - // translate to the current index - return MathListIndex.Level0Index(self.Range.Location + index); + if (!diffLng) + return MathListIndex.Level0Index(self.Range.Location + index); + if (index > r.Run.Length / 2) + return MathListIndex.Level0Index(self.Range.End); + + return MathListIndex.Level0Index(self.Range.Location); } public static (TextRunDisplay run, int charIndex) GetRunAndCharIndexFromStringIndex(this TextLineDisplay self, int lineCharIndex) where TFont : IFont {