pub mod columns;
pub mod extract;
pub mod furniture;
pub mod group;
pub mod hyphen;
pub mod table;
pub use columns::Region;
use pdfium_render::prelude::{PdfDocument, PdfPageIndex};
use crate::tags::RawDoc;
pub fn analyze_document(doc: &PdfDocument, raw: Option<&RawDoc>) -> Vec<Page> {
let pages = doc
.pages()
.iter()
.enumerate()
.map(|(i, page)| {
let rp = PdfPageIndex::try_from(i)
.ok()
.and_then(|i| raw.and_then(|r| r.page(i)));
group::PagePrimitives {
width: page.width().value,
height: page.height().value,
chars: extract::page_chars(&page, rp.as_ref()),
path_marks: extract::page_path_marks(&page),
rules: extract::page_rules(&page),
}
})
.collect();
group::layout_document(pages)
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Rect {
pub left: f32,
pub top: f32,
pub right: f32,
pub bottom: f32,
}
impl Rect {
pub fn new(left: f32, top: f32, right: f32, bottom: f32) -> Self {
Rect {
left: left.min(right),
top: top.min(bottom),
right: left.max(right),
bottom: top.max(bottom),
}
}
pub fn width(&self) -> f32 {
self.right - self.left
}
pub fn height(&self) -> f32 {
self.bottom - self.top
}
pub fn center_x(&self) -> f32 {
(self.left + self.right) / 2.0
}
pub fn union(&self, o: &Rect) -> Rect {
Rect {
left: self.left.min(o.left),
top: self.top.min(o.top),
right: self.right.max(o.right),
bottom: self.bottom.max(o.bottom),
}
}
pub fn h_overlap(&self, o: &Rect) -> f32 {
(self.right.min(o.right) - self.left.max(o.left)).max(0.0)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct Style {
pub bold: bool,
pub italic: bool,
pub mono: bool,
}
impl Style {
pub fn label(&self) -> String {
let mut parts = Vec::new();
if self.bold {
parts.push("bold");
}
if self.italic {
parts.push("italic");
}
if self.mono {
parts.push("mono");
}
if parts.is_empty() {
"regular".into()
} else {
parts.join("+")
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Char {
pub ch: char,
pub bbox: Rect,
pub baseline: f32,
pub size: f32,
pub font: String,
pub style: Style,
pub generated: bool,
pub kind: CharKind,
pub source: CharSource,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum CharKind {
#[default]
Text,
LineEndHyphen,
SoftHyphen,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CharSource {
pub object: Option<u32>,
pub mcid: Option<i32>,
pub actual_text: Option<String>,
pub struct_actual_text: Option<String>,
pub struct_heading: Option<u8>,
pub artifact: Option<Artifact>,
pub hidden_text_before: Option<String>,
pub figure: Option<Figure>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Figure {
pub object: u32,
pub rect: Rect,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Artifact {
pub subtype: Option<String>,
pub has_params: bool,
pub pagination: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Word {
pub text: String,
pub bbox: Rect,
pub baseline: f32,
pub size: f32,
pub font: String,
pub style: Style,
pub glued: bool,
pub len: usize,
pub chars: std::ops::Range<usize>,
pub figure: Option<Figure>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Line {
pub words: Vec<Word>,
pub bbox: Rect,
pub baseline: f32,
pub size: f32,
pub style: Style,
}
impl Line {
pub fn text(&self) -> String {
let mut s = String::new();
for (i, w) in self.words.iter().enumerate() {
if i > 0 && !w.glued {
s.push(' ');
}
s.push_str(&w.text);
}
s
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Block {
pub lines: Vec<Line>,
pub bbox: Rect,
pub size: f32,
pub style: Style,
pub region: usize,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Page {
pub number: usize,
pub width: f32,
pub height: f32,
pub regions: Vec<Region>,
pub blocks: Vec<Block>,
pub furniture: Vec<furniture::Furniture>,
pub chars: Vec<Char>,
}