offsetAt method
The logical offset nearest to point, or null when nothing is within maxDistance.
Implementation
int? offsetAt(Offset point, {double maxDistance = 48}) {
int cursor = 0;
int? bestRun;
int bestCursor = 0;
double bestDistance = double.infinity;
for (int i = 0; i < runs.length; i++) {
final UDocTextRun run = runs[i];
final Rect area = run.rect;
final double dx = point.dx < area.left ? area.left - point.dx : (point.dx > area.right ? point.dx - area.right : 0);
final double dy = point.dy < area.top ? area.top - point.dy : (point.dy > area.bottom ? point.dy - area.bottom : 0);
final double distance = dx * dx + dy * dy * 4;
if (distance < bestDistance) {
bestDistance = distance;
bestRun = i;
bestCursor = cursor;
}
cursor += run.text.length + 1;
}
if (bestRun == null || bestDistance > maxDistance * maxDistance) return null;
return bestCursor + runs[bestRun].offsetAt(point);
}