use serde::Serialize;
use crate::extract::PageRuns;
mod columns;
mod flow;
mod furniture;
mod rows;
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Line {
pub text: String,
pub x0: f32,
pub baseline: f32,
pub indent: u8,
pub font_size: f32,
pub bold: bool,
pub centered: bool,
pub column: u8,
pub pages: Vec<usize>,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Block {
pub lines: Vec<Line>,
}
#[derive(Debug, Clone, Serialize)]
pub struct Layout {
pub blocks: Vec<Block>,
pub furniture: Vec<Line>,
}
impl Layout {
pub fn text(&self) -> String {
let mut out = String::new();
for (i, block) in self.blocks.iter().enumerate() {
if i > 0 {
out.push('\n');
}
for line in &block.lines {
out.push_str(line.text.trim_end());
out.push('\n');
}
}
out
}
}
#[derive(Debug, Clone)]
pub struct LayoutOptions {
pub strip_furniture: bool,
}
impl Default for LayoutOptions {
fn default() -> Self {
Self {
strip_furniture: true,
}
}
}
pub fn reconstruct(pages: &[PageRuns]) -> Layout {
reconstruct_with(pages, &LayoutOptions::default())
}
pub fn reconstruct_with(pages: &[PageRuns], options: &LayoutOptions) -> Layout {
let mut lines: Vec<Line> = Vec::new();
for (page_index, page) in pages.iter().enumerate() {
let page_rows = rows::assemble(page, page_index);
lines.extend(columns::lines_in_reading_order(page_rows, page, page_index));
}
let stripped = if options.strip_furniture {
furniture::strip(&mut lines, pages)
} else {
Vec::new()
};
flow::quantize_indents(&mut lines);
let blocks = flow::into_blocks(lines);
Layout {
blocks,
furniture: stripped,
}
}
#[cfg(test)]
pub(crate) mod testutil {
use crate::extract::{PageRuns, TextRun};
pub fn run(text: &str, x: f32, baseline: f32, size: f32, page: usize) -> TextRun {
TextRun {
text: text.into(),
bbox: (
x,
baseline,
x + text.chars().count() as f32 * 0.5 * size,
baseline + size,
),
font_size: size,
bold: false,
italic: false,
font: None,
page,
}
}
pub fn page(runs: Vec<TextRun>) -> PageRuns {
PageRuns {
width: 612.0,
height: 792.0,
runs,
}
}
pub fn body_lines(lines: &[&str], x: f32, top: f32, leading: f32, page: usize) -> Vec<TextRun> {
lines
.iter()
.enumerate()
.map(|(i, t)| run(t, x, top - i as f32 * leading, 10.0, page))
.collect()
}
}
#[cfg(test)]
mod tests {
use super::testutil::{body_lines, page, run};
use super::*;
#[test]
fn empty_input_yields_empty_layout() {
let layout = reconstruct(&[]);
assert!(layout.blocks.is_empty());
assert!(layout.furniture.is_empty());
assert_eq!(layout.text(), "");
}
#[test]
fn single_paragraph_reads_top_to_bottom() {
let runs = body_lines(
&["first line of text here", "second line follows it"],
72.0,
700.0,
12.0,
0,
);
let layout = reconstruct(&[page(runs)]);
assert_eq!(layout.blocks.len(), 1);
assert_eq!(
layout.text(),
"first line of text here\nsecond line follows it\n"
);
}
#[test]
fn same_baseline_runs_join_with_gap_spacing() {
let mut runs = Vec::new();
let mut x = 100.0;
for (text, size, gap) in [
("T", 9.0, 0.0),
("ITLE ", 7.0, 0.0),
("1:", 9.0, 2.0),
("G", 9.0, 2.5),
("OVERNMENT", 7.0, 0.0),
] {
x += gap;
let r = run(text, x, 700.0, size, 0);
x += rows::width_est(&r);
runs.push(r);
}
let layout = reconstruct(&[page(runs)]);
let text = layout.text();
assert_eq!(layout.blocks.len(), 1);
assert_eq!(layout.blocks[0].lines.len(), 1, "one baseline = one line");
assert!(
text.contains("TITLE 1: GOVERNMENT"),
"expected joined small-caps line, got {text:?}"
);
}
#[test]
fn layout_serializes_for_json_output() {
let runs = body_lines(&["hello world"], 72.0, 700.0, 12.0, 0);
let layout = reconstruct(&[page(runs)]);
let v: serde_json::Value = serde_json::to_value(&layout).unwrap();
let line = &v["blocks"][0]["lines"][0];
assert_eq!(line["text"], "hello world");
assert_eq!(line["x0"], 72.0);
assert_eq!(line["indent"], 0);
assert_eq!(line["column"], 0);
assert_eq!(line["pages"][0], 0);
}
#[test]
fn keep_furniture_leaves_lines_inline() {
let mut pages_vec = Vec::new();
for p in 0..4 {
let mut runs = vec![run("RUNNING HEAD", 250.0, 770.0, 9.0, p)];
let second = format!("is never furniture, page {p}");
runs.extend(body_lines(
&["body text well below the band so it", &second],
72.0,
620.0,
12.0,
p,
));
pages_vec.push(page(runs));
}
let stripped = reconstruct(&pages_vec);
assert_eq!(stripped.furniture.len(), 4);
assert!(!stripped.text().contains("RUNNING HEAD"));
let kept = reconstruct_with(
&pages_vec,
&LayoutOptions {
strip_furniture: false,
},
);
assert!(kept.furniture.is_empty());
assert!(kept.text().contains("RUNNING HEAD"));
}
}