Function caret_x_at

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

Maps a UTF-8 byte position to the preceding recorded X advance.

Positions inside a multi-byte scalar snap backward to that scalar’s leading boundary. Values beyond the line snap to its width. Malformed layouts are tolerated by returning zero if the matching advance is absent.

§Examples

use ailloli_ui_text::{text_layout::caret_x_at, LineLayout};
let layout = LineLayout { byte_offsets: vec![0, 2, 3], advances: vec![0.0, 6.0, 12.0] };
assert_eq!(caret_x_at(&layout, 1), 0.0);
assert_eq!(caret_x_at(&layout, usize::MAX), 12.0);