use crate::backend::{Dir, PdfFile};
use anyhow::Result;
const SAME_BASELINE_PT: f32 = 1.0;
const SPLIT_GAP_PT: f32 = 30.0;
const SPACE_GAP_EM: f32 = 0.15;
#[derive(Debug, Clone)]
pub struct TextLine {
pub page: usize,
pub baseline: f32,
pub left: f32,
pub right: f32,
pub text: String,
}
pub fn pdf_lines(data: Vec<u8>) -> Result<(Vec<TextLine>, usize)> {
let pdf = PdfFile::open(data)?;
let (pages, _) = pdf.extract(0);
let count = pages.len();
let mut out = Vec::new();
for page in pages {
let mut glyphs: Vec<_> = page
.glyphs
.iter()
.filter(|glyph| glyph.dir == Dir::Horizontal && !glyph.invisible && glyph.angle.abs() < 1.0)
.collect();
glyphs.sort_by(|a, b| a.y.total_cmp(&b.y).then(a.x.total_cmp(&b.x)));
let mut rows: Vec<Vec<&crate::backend::Glyph>> = Vec::new();
for glyph in glyphs {
match rows.last_mut() {
Some(row) if (row[0].y - glyph.y).abs() < SAME_BASELINE_PT => row.push(glyph),
_ => rows.push(vec![glyph]),
}
}
for mut row in rows {
row.sort_by(|a, b| a.x.total_cmp(&b.x));
let mut current: Option<TextLine> = None;
let mut end = f32::MIN;
for glyph in row {
let gap = glyph.x - end;
if let Some(line) = &mut current
&& gap > SPLIT_GAP_PT
{
finish(line);
out.push(current.take().expect("a line is open"));
}
let line = current.get_or_insert_with(|| TextLine {
page: page.index,
baseline: glyph.y,
left: glyph.x,
right: glyph.x,
text: String::new(),
});
if !line.text.is_empty() && gap > SPACE_GAP_EM * glyph.size {
line.text.push(' ');
}
line.text.push_str(&glyph.text);
line.right = glyph.x + glyph.adv;
end = glyph.x + glyph.adv;
}
if let Some(mut line) = current {
finish(&mut line);
out.push(line);
}
}
}
out.retain(|line| !line.text.is_empty());
Ok((out, count))
}
fn finish(line: &mut TextLine) {
line.text = line.text.split_whitespace().collect::<Vec<_>>().join(" ");
}