Skip to content
Merged
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
24 changes: 16 additions & 8 deletions CSharpMath.Editor/Extensions/DisplayEditingExtensions.TextLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,24 @@ public static int MathListIndexToStringIndex<TFont, TGlyph>(this TextLineDisplay
public static MathListIndex IndexForPoint<TFont, TGlyph>(this TextLineDisplay<TFont, TGlyph> self, TypesettingContext<TFont, TGlyph> context, PointF point) where TFont : IFont<TGlyph> {
// 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<TFont, TGlyph> run, int charIndex) GetRunAndCharIndexFromStringIndex<TFont, TGlyph>(this TextLineDisplay<TFont, TGlyph> self, int lineCharIndex) where TFont : IFont<TGlyph> {
Expand Down