Function caret_index_at_x

Source
pub fn caret_index_at_x(layout: &LineLayout, x: f32) -> usize
Expand description

Returns the first cumulative-advance index at or beyond x.

The result indexes layout.advances and layout.byte_offsets; callers can obtain the UTF-8 offset with layout.byte_offsets[result]. Negative and NaN X values behave as zero. Values beyond the width return the last advance index. An empty layout returns zero.

ยงExamples

use ailloli_ui_text::{caret_index_at_x, LineLayout};
let layout = LineLayout { byte_offsets: vec![0, 2, 3], advances: vec![0.0, 6.0, 12.0] };
let position = caret_index_at_x(&layout, 7.0);
assert_eq!(position, 2);
assert_eq!(layout.byte_offsets[position], 3);