use cosmic_text::Buffer;
use crate::node::text_selection::{CaretIndex, CaretPosition, CaretPositions, glyph_carets};
pub(super) fn buffer_caret_positions(buffer: &Buffer, text_len: usize) -> CaretPositions {
let mut line_offsets = Vec::with_capacity(buffer.lines.len());
let mut offset = 0usize;
for line in &buffer.lines {
line_offsets.push(offset);
offset += line.text().len() + line.ending().as_str().len();
}
let mut positions = Vec::with_capacity(text_len + buffer.lines.len().max(1));
let mut index = CaretIndex::default();
for run in buffer.layout_runs() {
let line_offset = line_offsets.get(run.line_i).copied().unwrap_or(0);
let y = run.line_top.max(0.0);
let start = positions.len();
if run.glyphs.is_empty() {
positions.push(CaretPosition {
index: line_offset,
x: 0.0,
y,
});
index.push_line(start, positions.len(), line_offset, line_offset, y);
continue;
}
for glyph in run.glyphs {
positions.extend(glyph_carets(glyph, run.text).map(|(index, x)| CaretPosition {
index: line_offset + index,
x,
y,
}));
}
index.push_line(
start,
positions.len(),
line_offset,
line_offset + buffer.lines[run.line_i].text().len(),
y,
);
}
if positions.is_empty() {
positions.push(CaretPosition {
index: 0,
x: 0.0,
y: 0.0,
});
index.push_line(0, 1, 0, 0, 0.0);
}
if !positions.iter().any(|position| position.index == text_len) {
let last_y = positions.last().map(|position| position.y).unwrap_or(0.0);
positions.push(CaretPosition {
index: text_len,
x: positions.last().map(|position| position.x).unwrap_or(0.0),
y: last_y,
});
index.push_line(positions.len() - 1, positions.len(), text_len, text_len, last_y);
}
CaretPositions::with_index(positions, index)
}