use super::page::Page;
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Span {
pub line: usize,
pub x0: f32,
pub x1: f32,
pub first_glyph: usize,
pub glyph_count: usize,
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct Extent {
pub width: f32,
pub lines: usize,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct MarkRun {
pub id: String,
pub first_glyph: usize,
pub glyph_count: usize,
}
impl Page {
pub fn spans(&self, first: usize, count: usize) -> Vec<Span> {
let end = first.saturating_add(count).min(self.glyphs.len());
let mut out: Vec<Span> = Vec::new();
for (i, g) in self.glyphs.iter().enumerate().take(end).skip(first) {
match out.last_mut() {
Some(s) if s.line == g.line && s.first_glyph + s.glyph_count == i => {
s.x0 = s.x0.min(g.x);
s.x1 = s.x1.max(g.x + g.width);
s.glyph_count += 1;
}
_ => out.push(Span {
line: g.line,
x0: g.x,
x1: g.x + g.width,
first_glyph: i,
glyph_count: 1,
}),
}
}
out
}
pub fn hit_test(&self, x: f32, line: f32) -> Option<usize> {
if line.is_nan() || line < 0.0 || !x.is_finite() {
return None;
}
let ln = self.lines.get(line as usize)?;
let range = ln.first_glyph..ln.first_glyph + ln.glyph_count;
range.into_iter().find(|&i| {
self.glyphs
.get(i)
.is_some_and(|g| x >= g.x && x < g.x + g.width)
})
}
pub fn glyph_at_source(&self, byte: usize) -> Option<usize> {
self.glyphs.iter().position(|g| g.source.contains(&byte))
}
pub fn extent(&self) -> Extent {
Extent {
width: self.lines.iter().map(|l| l.width).fold(0.0, f32::max),
lines: self.lines.len(),
}
}
}