Function caret_index_at_point

Source
pub fn caret_index_at_point(laid: &LaidOutText, x: f32, y: f32) -> usize
Expand description

Maps layout-space (x, y) to a UTF-8 byte index (cluster-aware).

Coordinates are logical pixels relative to the layout origin. Parley resolves points outside glyph bounds to its nearest valid cursor; the returned index is therefore suitable for slicing only after ordinary UTF-8 boundary checks required by the caller’s text handling.

§Examples

use ailloli_ui_core::{Color, FontId, TextStyle};
use ailloli_ui_text::{caret_index_at_point, layout_text, ParleyEngine, TextLayoutParams};
#[allow(deprecated)] let mut engine = ParleyEngine::new();
let laid = layout_text(&mut engine, TextLayoutParams::new("abc", TextStyle::new(FontId::Ui, 16, Color::WHITE)));
assert_eq!(caret_index_at_point(&laid, -100.0, 0.0), 0);