hitTextSelectionGlyphs function
TextSelectionHit?
hitTextSelectionGlyphs(
- Iterable<
TextSelectionGlyph> glyphs, - Mat2D transform,
- Vec2D point, {
- bool nearest = false,
Maps the existing displayed glyphs to endpoints without reshaping text.
Implementation
TextSelectionHit? hitTextSelectionGlyphs(
Iterable<TextSelectionGlyph> glyphs,
Mat2D transform,
Vec2D point, {
bool nearest = false,
}) {
if (!point.x.isFinite || !point.y.isFinite) return null;
final inverse = Mat2D();
if (!Mat2D.invert(inverse, transform)) return null;
final local = inverse.mapXY(point.x, point.y);
if (!local.x.isFinite || !local.y.isFinite) return null;
TextSelectionHit? result;
for (final glyph in glyphs) {
final rect = glyph.bounds;
if (rect.isEmpty || glyph.count <= 0) continue;
final x = local.x.clamp(rect.left, rect.right);
final y = local.y.clamp(rect.top, rect.bottom);
if (!nearest && (x != local.x || y != local.y)) continue;
final root = transform.mapXY(x, y);
final dx = root.x - point.x, dy = root.y - point.y;
final distance = dx * dx + dy * dy;
if (result == null || distance < result.distanceSquared) {
var fraction = (x - rect.left) / rect.width;
if (glyph.rtl) fraction = 1 - fraction;
result = TextSelectionHit(
glyph.start + (fraction * glyph.count).round(),
distance,
);
}
}
return result;
}