A fast, helpful, and open-source document parser
port the grid projection
Logan Markewich2e03b3ca653e
diff --git a/packages/rust/src/extract.rs b/packages/rust/src/extract.rsindex 7c93e2a..3ab975d 100644--- a/packages/rust/src/extract.rs+++ b/packages/rust/src/extract.rs@@ -1,45 +1,24 @@use pdfium_render::prelude::*;-use serde::Serialize;+use crate::types::{TextItem, Page};// If gap is < fontSize * CHAR_X_GAP_THRESHOLD, consider it part of the same word-const CHAR_X_GAP_THRESHOLD: f32 = 0.3;+const CHAR_X_GAP_THRESHOLD: f32 = 0.3;// If gap is < fontSize * NEGATIVE_X_GAP_THRESHOLD, consider it a negative gap (e.g. kerning) and start a new wordconst NEGATIVE_X_GAP_THRESHOLD: f32 = -0.2;// If gap is > fontSize * CHAR_Y_GAP_THRESHOLD, consider it a new item-const CHAR_Y_GAP_THRESHOLD: f32 = 0.25;--#[derive(Debug, Serialize)]-struct TextItem {- text: String,- x: f32,- y: f32,- width: f32,- height: f32,- rotation: f32,- font_name: Option<String>,- font_size: Option<f32>,-}--#[derive(Debug, Serialize)]-struct Page {- page_number: usize,- page_width: f32,- page_height: f32,- text_items: Vec<TextItem>,-}+const CHAR_Y_GAP_THRESHOLD: f32 = 0.25;-pub fn extract(pdf_path: &str, page_num: Option<u32>) -> Result<(), Box<dyn std::error::Error>> {- // Initialize PDFium+/// Extract pages from a PDF file and return them as structured data.+pub fn extract_pages(pdf_path: &str, page_num: Option<u32>) -> Result<Vec<Page>, Box<dyn std::error::Error>> {let pdfium = Pdfium::new(Pdfium::bind_to_statically_linked_library().unwrap());- // Load the PDF documentlet document = pdfium.load_pdf_from_file(pdf_path, None)?;+ let mut pages = Vec::new();- // iterate over pages to extract textfor (page_index, page) in document.pages().iter().enumerate() {if let Some(target_page) = page_num {if page_index as u32 + 1 != target_page {@@ -47,113 +26,123 @@ pub fn extract(pdf_path: &str, page_num: Option<u32>) -> Result<(), Box<dyn std:}}- let mut text_items = Vec::new();- let mut cur_item: Option<TextItem> = None;-- for object in page.objects().iter() {- if let Some(object) = object.as_text_object() {- let bounds = object.bounds().unwrap();- let obj_x = bounds.x1.value;- let obj_y = bounds.y1.value;- // Get rotation and normalize to 0-360- let obj_rotation = object.get_rotation_counter_clockwise_degrees();- let obj_rotation = if obj_rotation < 0.0 { obj_rotation + 360.0 } else { obj_rotation };-- // scaled_font_size() = unscaled * matrix.d (vertical scale).- // For 90° rotated text, matrix.d ≈ 0, so scaled_font_size returns 0.- // In that case, compute from the matrix horizontal scale: sqrt(a² + b²) * unscaled.- let unscaled_fs = object.unscaled_font_size().value;- let scaled_fs = object.scaled_font_size().value;- let obj_font_size = if scaled_fs > 0.0 {- scaled_fs- } else {- // Use horizontal scale factor from the transformation matrix- let matrix = object.matrix().unwrap();- let h_scale = (matrix.a() * matrix.a() + matrix.b() * matrix.b()).sqrt();- unscaled_fs * h_scale- };+ let text_items = extract_page_text_items(&page)?;++ pages.push(Page {+ page_number: page_index + 1,+ page_width: page.width().value,+ page_height: page.height().value,+ text_items,+ });+ }++ Ok(pages)+}- let is_vertical = (obj_rotation > 45.0 && obj_rotation < 135.0)- || (obj_rotation > 225.0 && obj_rotation < 315.0);- let (obj_w, obj_h) = if is_vertical {- // 90°/270° — object.width()/height() return 0, use bounds + font size- let bounds_h = (bounds.y2.value - bounds.y1.value).abs();- (obj_font_size, f32::max(bounds_h, obj_font_size))+/// Extract raw text items and print each page as a JSON-line object to stdout.+pub fn extract(pdf_path: &str, page_num: Option<u32>) -> Result<(), Box<dyn std::error::Error>> {+ let pages = extract_pages(pdf_path, page_num)?;+ for page in &pages {+ println!("{}", serde_json::to_string(page)?);+ }+ Ok(())+}++fn extract_page_text_items(page: &PdfPage) -> Result<Vec<TextItem>, Box<dyn std::error::Error>> {+ let mut text_items = Vec::new();+ let mut cur_item: Option<TextItem> = None;++ for object in page.objects().iter() {+ if let Some(object) = object.as_text_object() {+ let bounds = object.bounds().unwrap();+ let obj_x = bounds.x1.value;+ let obj_y = bounds.y1.value;+ // Get rotation and normalize to 0-360+ let obj_rotation = object.get_rotation_counter_clockwise_degrees();+ let obj_rotation = if obj_rotation < 0.0 { obj_rotation + 360.0 } else { obj_rotation };++ // scaled_font_size() = unscaled * matrix.d (vertical scale).+ // For 90° rotated text, matrix.d ≈ 0, so scaled_font_size returns 0.+ // In that case, compute from the matrix horizontal scale: sqrt(a² + b²) * unscaled.+ let unscaled_fs = object.unscaled_font_size().value;+ let scaled_fs = object.scaled_font_size().value;+ let obj_font_size = if scaled_fs > 0.0 {+ scaled_fs+ } else {+ // Use horizontal scale factor from the transformation matrix+ let matrix = object.matrix().unwrap();+ let h_scale = (matrix.a() * matrix.a() + matrix.b() * matrix.b()).sqrt();+ unscaled_fs * h_scale+ };++ let is_vertical = (obj_rotation > 45.0 && obj_rotation < 135.0)+ || (obj_rotation > 225.0 && obj_rotation < 315.0);+ let (obj_w, obj_h) = if is_vertical {+ // 90°/270° — object.width()/height() return 0, use bounds + font size+ let bounds_h = (bounds.y2.value - bounds.y1.value).abs();+ (obj_font_size, f32::max(bounds_h, obj_font_size))+ } else {+ // 0°/180° — normal: object.width() works, font size for height+ (object.width().unwrap().value, obj_font_size)+ };++ if obj_rotation > 1.0 {+ eprintln!(+ "ROTATED: text='{}' rot={:.1} bounds=({:.2},{:.2})-({:.2},{:.2}) scaled_fs={:.2}",+ object.text(), obj_rotation,+ bounds.x1.value, bounds.y1.value, bounds.x2.value, bounds.y2.value,+ obj_font_size+ );+ }++ if let Some(ref mut item) = cur_item {+ let cur_font_size = item.font_size.unwrap_or(obj_font_size);+ let rotation_matches = (item.rotation - obj_rotation).abs() < 1.0;+ let cur_is_vertical = (item.rotation > 45.0 && item.rotation < 135.0)+ || (item.rotation > 225.0 && item.rotation < 315.0);++ // For vertical text, "reading direction" is along y-axis, so swap gap roles:+ // - reading_gap: distance along reading direction (x for horizontal, y for vertical)+ // - cross_gap: distance across lines (y for horizontal, x for vertical)+ let (reading_gap, cross_gap) = if cur_is_vertical {+ let y_gap = obj_y - (item.y + item.height);+ let x_gap = (item.x - obj_x).abs();+ (y_gap, x_gap)} else {- // 0°/180° — normal: object.width() works, font size for height- (object.width().unwrap().value, obj_font_size)+ let x_gap = obj_x - (item.x + item.width);+ let y_gap = (item.y - obj_y).abs();+ (x_gap, y_gap)};- if obj_rotation > 1.0 {- eprintln!(- "ROTATED: text='{}' rot={:.1} bounds=({:.2},{:.2})-({:.2},{:.2}) scaled_fs={:.2}",- object.text(), obj_rotation,- bounds.x1.value, bounds.y1.value, bounds.x2.value, bounds.y2.value,- obj_font_size- );- }-- if let Some(ref mut item) = cur_item {- let cur_font_size = item.font_size.unwrap_or(obj_font_size);- let rotation_matches = (item.rotation - obj_rotation).abs() < 1.0;- let cur_is_vertical = (item.rotation > 45.0 && item.rotation < 135.0)- || (item.rotation > 225.0 && item.rotation < 315.0);-- // For vertical text, "reading direction" is along y-axis, so swap gap roles:- // - reading_gap: distance along reading direction (x for horizontal, y for vertical)- // - cross_gap: distance across lines (y for horizontal, x for vertical)- let (reading_gap, cross_gap) = if cur_is_vertical {- let y_gap = obj_y - (item.y + item.height);- let x_gap = (item.x - obj_x).abs();- (y_gap, x_gap)- } else {- let x_gap = obj_x - (item.x + item.width);- let y_gap = (item.y - obj_y).abs();- (x_gap, y_gap)- };-- // Vertical text glyphs commonly overlap along the reading axis,- // so use a more lenient negative threshold to allow merging.- let neg_threshold = if cur_is_vertical { -0.5 } else { NEGATIVE_X_GAP_THRESHOLD };- if reading_gap < cur_font_size * neg_threshold || !rotation_matches {- // Negative gap or rotation difference — start new item- text_items.push(cur_item.take().unwrap());- cur_item = Some(TextItem {- text: object.text().to_string(),- x: obj_x,- y: obj_y,- width: obj_w,- height: obj_h,- rotation: obj_rotation,- font_name: Some(object.font().family().to_string()),- font_size: Some(obj_font_size),- });- } else if reading_gap < cur_font_size * CHAR_X_GAP_THRESHOLD && cross_gap < cur_font_size * CHAR_Y_GAP_THRESHOLD {- // Same word — merge directly- item.text.push_str(object.text().trim_start());- if cur_is_vertical {- item.height = (obj_y + obj_h) - item.y;- item.width = f32::max(item.width, obj_w);- } else {- item.width = (obj_x + obj_w) - item.x;- item.height = f32::max(item.height, obj_h);- }+ // Vertical text glyphs commonly overlap along the reading axis,+ // so use a more lenient negative threshold to allow merging.+ let neg_threshold = if cur_is_vertical { -0.5 } else { NEGATIVE_X_GAP_THRESHOLD };+ if reading_gap < cur_font_size * neg_threshold || !rotation_matches {+ // Negative gap or rotation difference — start new item+ text_items.push(cur_item.take().unwrap());+ cur_item = Some(TextItem {+ text: object.text().to_string(),+ x: obj_x,+ y: obj_y,+ width: obj_w,+ height: obj_h,+ rotation: obj_rotation,+ font_name: Some(object.font().family().to_string()),+ font_size: Some(obj_font_size),+ });+ } else if reading_gap < cur_font_size * CHAR_X_GAP_THRESHOLD && cross_gap < cur_font_size * CHAR_Y_GAP_THRESHOLD {+ // Same word — merge directly+ item.text.push_str(object.text().trim_start());+ if cur_is_vertical {+ item.height = (obj_y + obj_h) - item.y;+ item.width = f32::max(item.width, obj_w);} else {- // Large gap — flush current item and start new one- text_items.push(cur_item.take().unwrap());- cur_item = Some(TextItem {- text: object.text().to_string(),- x: obj_x,- y: obj_y,- width: obj_w,- height: obj_h,- rotation: obj_rotation,- font_name: Some(object.font().family().to_string()),- font_size: Some(obj_font_size),- });+ item.width = (obj_x + obj_w) - item.x;+ item.height = f32::max(item.height, obj_h);}} else {- // First text object on this page+ // Large gap — flush current item and start new one+ text_items.push(cur_item.take().unwrap());cur_item = Some(TextItem {text: object.text().to_string(),x: obj_x,@@ -165,26 +154,28 @@ pub fn extract(pdf_path: &str, page_num: Option<u32>) -> Result<(), Box<dyn std:font_size: Some(obj_font_size),});}+ } else {+ // First text object on this page+ cur_item = Some(TextItem {+ text: object.text().to_string(),+ x: obj_x,+ y: obj_y,+ width: obj_w,+ height: obj_h,+ rotation: obj_rotation,+ font_name: Some(object.font().family().to_string()),+ font_size: Some(obj_font_size),+ });}}-- // Push the last item if it has text- if let Some(item) = cur_item.take() {- if !item.text.is_empty() {- text_items.push(item);- }- }-- let page_data = Page {- page_number: page_index + 1,- page_width: page.width().value,- page_height: page.height().value,- text_items,- };+ }- // Print the page data as a JSON-line object- println!("{}", serde_json::to_string(&page_data)?);+ // Push the last item if it has text+ if let Some(item) = cur_item.take() {+ if !item.text.is_empty() {+ text_items.push(item);+ }}- Ok(())+ Ok(text_items)}diff --git a/packages/rust/src/lib.rs b/packages/rust/src/lib.rsindex 16e7036..e01f64a 100644--- a/packages/rust/src/lib.rs+++ b/packages/rust/src/lib.rs@@ -1 +1,3 @@pub mod extract;+pub mod projection;+pub mod types;\ No newline at end of filediff --git a/packages/rust/src/main.rs b/packages/rust/src/main.rsindex 14c6ef5..4cd0ffc 100644--- a/packages/rust/src/main.rs+++ b/packages/rust/src/main.rs@@ -1,5 +1,6 @@use clap::{Parser, Subcommand, Args};use liteparse_rs::extract;+use liteparse_rs::projection;#[derive(Parser, Debug)]@@ -9,16 +10,17 @@ struct Cli {command: Commands,}-// 2. Define the top-level commands as an Enum.#[derive(Subcommand, Debug)]enum Commands {- /// Extract text and metadata from a PDF file- Extract(ExtractCommand),+ /// Extract raw text items from a PDF file (no grid projection)+ Extract(PdfCommand),+ /// Parse a PDF file: extract + grid projection, output projected pages as JSON+ Parse(PdfCommand),}#[derive(Args, Debug)]-struct ExtractCommand {+struct PdfCommand {/// Specify the path to the PDF file#[arg(long)]pdf_path: String,@@ -35,7 +37,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {Commands::Extract(cmd) => {extract::extract(&cmd.pdf_path, cmd.page_num)?;}+ Commands::Parse(cmd) => {+ let pages = extract::extract_pages(&cmd.pdf_path, cmd.page_num)?;+ let parsed_pages = projection::project_pages_to_grid(pages);+ // Output all parsed pages as a single JSON array+ println!("{}", serde_json::to_string(&parsed_pages)?);+ }}Ok(())-}\ No newline at end of file+}diff --git a/packages/rust/src/projection.rs b/packages/rust/src/projection.rsnew file mode 100644index 0000000..0e3d07d--- /dev/null+++ b/packages/rust/src/projection.rs@@ -0,0 +1,1421 @@+use std::cmp::Ordering;+use std::collections::{BTreeMap, HashMap, HashSet};+use crate::types::*;++const FLOATING_SPACES: usize = 2;+const COLUMN_SPACES: usize = 4;++#[derive(Clone, Copy, Debug, PartialEq, Eq)]+enum SnapKind {+ Left,+ Right,+ Center,+}++fn compute_median_textbox_size(items: &[ProjectedTextItem]) -> (f32, f32) {+ if items.is_empty() {+ return (0.0, 0.0);+ }++ // Match TS behavior: median width is computed as average character width.+ let mut widths: Vec<f32> = items+ .iter()+ .filter_map(|item| {+ if item.item.width <= 0.0 {+ return None;+ }+ let char_len = item.item.text.chars().count();+ if char_len == 0 {+ return None;+ }+ Some(item.item.width / char_len as f32)+ })+ .collect();+ let mut heights: Vec<f32> = items+ .iter()+ .filter_map(|item| {+ if item.item.height > 0.0 {+ Some(item.item.height)+ } else {+ None+ }+ })+ .collect();++ if widths.is_empty() {+ widths.push(1.0);+ }+ if heights.is_empty() {+ heights.push(1.0);+ }++ widths.sort_by(|a, b| a.total_cmp(b));+ heights.sort_by(|a, b| a.total_cmp(b));++ let mid = widths.len() / 2;++ let median_width = if widths.len().is_multiple_of(2) {+ (widths[mid - 1] + widths[mid]) / 2.0+ } else {+ widths[mid]+ };++ let median_height = if heights.len().is_multiple_of(2) {+ (heights[mid - 1] + heights[mid]) / 2.0+ } else {+ heights[mid]+ };++ (median_width, median_height)+}++fn canonical_rotation(rotation: f32) -> i32 {+ let mut r = rotation;+ while r < 0.0 {+ r += 360.0;+ }+ while r >= 360.0 {+ r -= 360.0;+ }++ let candidates = [0.0f32, 90.0, 180.0, 270.0];+ let mut best = 0.0f32;+ let mut best_delta = f32::INFINITY;+ for c in candidates {+ let delta = (r - c).abs();+ if delta < best_delta {+ best_delta = delta;+ best = c;+ }+ }++ if best_delta <= 2.0 {+ best as i32+ } else {+ r.round() as i32+ }+}++fn handle_rotation_reading_order(items: &mut [ProjectedTextItem], page_height: f32) {+ if !items.iter().any(|b| canonical_rotation(b.item.rotation) != 0) {+ return;+ }++ // Group all items by rotation value.+ let mut groups_by_rotation: HashMap<i32, Vec<usize>> = HashMap::new();+ for (idx, bbox) in items.iter().enumerate() {+ let r = canonical_rotation(bbox.item.rotation);+ groups_by_rotation.entry(r).or_default().push(idx);+ }++ // Build group list sorted by each group's minimum X.+ let mut bbox_groups: Vec<Vec<usize>> = groups_by_rotation.into_values().collect();+ for group in &mut bbox_groups {+ group.sort_by(|a, b| {+ items[*a]+ .item+ .y+ .total_cmp(&items[*b].item.y)+ });+ }++ bbox_groups.sort_by(|a, b| {+ let min_x_a = a+ .iter()+ .map(|idx| items[*idx].item.x)+ .fold(f32::INFINITY, |acc, v| acc.min(v));+ let min_x_b = b+ .iter()+ .map(|idx| items[*idx].item.x)+ .fold(f32::INFINITY, |acc, v| acc.min(v));+ min_x_a.total_cmp(&min_x_b)+ });++ for group_idx in 0..bbox_groups.len() {+ let group = bbox_groups[group_idx].clone();+ if group.is_empty() {+ continue;+ }++ let group_rotation = canonical_rotation(items[group[0]].item.rotation);+ if group_rotation != 90 && group_rotation != 270 {+ continue;+ }++ // Check if non-rotated/other-rotated items visually overlap this group (X and Y overlap).+ let mut global_overlap = false;+ 'outer: for (other_idx, other_bbox) in items.iter().enumerate() {+ let other_rot = canonical_rotation(other_bbox.item.rotation);+ if other_rot == group_rotation {+ continue;+ }++ for group_item_idx in &group {+ if *group_item_idx == other_idx {+ continue;+ }+ let b = &items[*group_item_idx].item;+ let x_overlap = b.x >= other_bbox.item.x && b.x <= other_bbox.item.x + other_bbox.item.width;+ let y_overlap = b.y < other_bbox.item.y + other_bbox.item.height+ && b.y + b.height > other_bbox.item.y;+ if x_overlap && y_overlap {+ global_overlap = true;+ break 'outer;+ }+ }+ }++ if global_overlap {+ for idx in &group {+ if items[*idx].d != 0.0 {+ items[*idx].item.y += items[*idx].d;+ items[*idx].d = 0.0;+ }+ items[*idx].item.rotation = 0.0;+ items[*idx].rotated = true;+ }+ } else {+ let group_max_x = group+ .iter()+ .map(|idx| items[*idx].item.x + items[*idx].item.width)+ .fold(f32::NEG_INFINITY, |acc, v| acc.max(v));++ let mut delta_y = 0.0f32;+ if group_idx != 0 {+ let previous_group = &bbox_groups[group_idx - 1];+ let previous_group_max_y = previous_group+ .iter()+ .map(|idx| items[*idx].item.y + items[*idx].item.height)+ .fold(f32::NEG_INFINITY, |acc, v| acc.max(v));+ delta_y = previous_group_max_y + page_height;+ }++ if group_rotation == 90 {+ for idx in &group {+ let new_x = items[*idx].item.y.round();+ let new_y = items[*idx].item.x + delta_y;+ let new_w = items[*idx].item.height;+ let new_h = items[*idx].item.width;+ items[*idx].item.x = new_x;+ items[*idx].item.y = new_y;+ items[*idx].item.width = new_w;+ items[*idx].item.height = new_h;+ items[*idx].item.rotation = 0.0;+ items[*idx].rotated = true;+ }+ }++ if group_rotation == 270 {+ let max_y = group+ .iter()+ .map(|idx| items[*idx].item.y + items[*idx].item.height)+ .fold(f32::NEG_INFINITY, |acc, v| acc.max(v));+ for idx in &group {+ let new_x = (max_y - items[*idx].item.y - items[*idx].item.height).round();+ let new_y = items[*idx].item.x + delta_y;+ let new_w = items[*idx].item.height;+ let new_h = items[*idx].item.width;+ items[*idx].item.x = new_x;+ items[*idx].item.y = new_y;+ items[*idx].item.width = new_w;+ items[*idx].item.height = new_h;+ items[*idx].item.rotation = 0.0;+ items[*idx].rotated = true;+ }+ }++ let global_delta = delta_y + group_max_x + page_height;+ for other_group_idx in (group_idx + 1)..bbox_groups.len() {+ for idx in &bbox_groups[other_group_idx] {+ let rot = canonical_rotation(items[*idx].item.rotation);+ if rot == 90 || rot == 270 {+ items[*idx].d += global_delta;+ } else {+ items[*idx].item.y += global_delta;+ }+ }+ }+ }+ }++ // Handle 180-degree rotation conservatively.+ // Unlike TS, we don't have extractor-provided rx/ry fields, so normalize to unrotated+ // and preserve local ordering by x.+ for group in &bbox_groups {+ if group.is_empty() {+ continue;+ }+ let rotation = canonical_rotation(items[group[0]].item.rotation);+ if rotation == 180 {+ let mut sorted = group.clone();+ sorted.sort_by(|a, b| {+ items[*a]+ .item+ .x+ .total_cmp(&items[*b].item.x)+ });+ for idx in sorted {+ items[idx].item.rotation = 0.0;+ items[idx].rotated = true;+ }+ }+ }++ items.sort_by(|a, b| a.item.y.total_cmp(&b.item.y));+}++fn clean_projected_items(items: &mut Vec<ProjectedTextItem>, page_width: f32) {+ // Rust equivalent of cleanRawText margin cleanup.+ // Keep this conservative: only remove likely margin line numbers when they appear isolated.+ let midpoint = page_width * 0.5;+ let margin_left = midpoint - 5.0;+ let margin_right = midpoint + 20.0;++ let mut has_non_margin_by_line: HashMap<i32, bool> = HashMap::new();+ for item in items.iter() {+ let line_key = item.item.y.round() as i32;+ if !item.is_margin_line_number {+ has_non_margin_by_line.insert(line_key, true);+ }+ }++ items.retain(|item| {+ let line_key = item.item.y.round() as i32;+ let line_has_content = has_non_margin_by_line.get(&line_key).copied().unwrap_or(false);+ let center = item.item.x + item.item.width * 0.5;+ let text = item.item.text.trim();+ let looks_like_line_number = {+ let chars: Vec<char> = text.chars().collect();+ if chars.is_empty() || chars.len() > 3 {+ false+ } else {+ let mut digit_count = 0usize;+ let mut valid = true;+ for (idx, c) in chars.iter().enumerate() {+ if c.is_ascii_digit() {+ digit_count += 1;+ } else if *c == 'O' && idx == chars.len() - 1 {+ // OCR confusion 0->O+ } else {+ valid = false;+ break;+ }+ }+ valid && (1..=2).contains(&digit_count)+ }+ };++ let likely_margin = item.is_margin_line_number+ || (center > margin_left+ && center < margin_right+ && looks_like_line_number+ && item.item.width < 15.0);++ !(likely_margin && !line_has_content)+ });+}++fn form_lines(+ items: &mut Vec<ProjectedTextItem>,+ median_width: f32,+ median_height: f32,+ page_width: f32,+) -> Vec<Vec<ProjectedTextItem>> {+ // Y-tolerance for sorting: items within this threshold are considered same line+ // This handles:+ // 1. Floating point precision issues between columns (e.g., 334.7400 vs 334.7399)+ // 2. Subscripts/superscripts which are typically offset by 3-5 units from their base characters+ // Using a fraction of medianHeight to scale with document font size.+ let y_sort_tolerance: f32 = (median_height * 0.5).max(5.0);++ // Note: We keep whitespace items as they may be needed for proper word separation.+ // The spacing calculation handles gaps between items.++ // For two-column documents, detect and mark margin line numbers+ // These are short numeric items positioned between columns (near the page midpoint)+ // They should not be merged with column content+ if page_width > 0.0 {+ let midpoint = page_width / 2.0;+ let margin_left = midpoint - 5.0;+ let margin_right = midpoint + 20.0;++ fn is_margin_line_number_text(text: &str) -> bool {+ let trimmed = text.trim();+ if trimmed.is_empty() {+ return false;+ }+ let chars: Vec<char> = trimmed.chars().collect();+ if chars.len() > 3 {+ return false;+ }++ let mut digit_count = 0usize;+ for (idx, c) in chars.iter().enumerate() {+ if c.is_ascii_digit() {+ digit_count += 1;+ } else if *c == 'O' && idx == chars.len() - 1 {+ // OCR confusion: 0 -> O+ } else {+ return false;+ }+ }+ (1..=2).contains(&digit_count)+ }++ for item in items.iter_mut() {+ let center = item.item.x + item.item.width / 2.0;++ // Check if item is in the margin zone and looks like a number+ if center > margin_left &&+ center < margin_right &&+ is_margin_line_number_text(&item.item.text) &&+ item.item.width < 15.0+ {+ item.is_margin_line_number = true;+ }+ }+ }++ // Sort by y then x. Snap y to a grid so items on the same visual line+ // get identical keys — this keeps the comparator transitive (total order),+ // which Rust's sort requires.+ let snap_y = |y: f32| -> i64 {+ if y_sort_tolerance > 0.0 {+ (y / y_sort_tolerance).round() as i64+ } else {+ (y * 1000.0).round() as i64+ }+ };+ items.sort_by(|a, b| {+ let ya = snap_y(a.item.y);+ let yb = snap_y(b.item.y);+ ya.cmp(&yb).then_with(|| a.item.x.total_cmp(&b.item.x))+ });++ fn can_merge(prev: &ProjectedTextItem, cur: &ProjectedTextItem, y_tolerance: f32, h_tolerance: f32) -> bool {+ if (cur.item.y - prev.item.y).abs() <= y_tolerance &&+ (cur.item.height - prev.item.height).abs() <= h_tolerance {+ let delta_x = cur.item.x - (prev.item.x + prev.item.width);+ return (-0.5..0.0).contains(&delta_x) || (0.0..0.1).contains(&delta_x);+ }++ false+ }++ fn merge_bbox(prev: &ProjectedTextItem, cur: &ProjectedTextItem) -> (f32, f32, f32, f32) {+ let x1 = prev.item.x.min(cur.item.x);+ let y1 = prev.item.y.min(cur.item.y);+ let x2 = (prev.item.x + prev.item.width).max(cur.item.x + cur.item.width);+ let y2 = (prev.item.y + prev.item.height).max(cur.item.y + cur.item.height);+ (x1, y1, x2 - x1, y2 - y1)+ }++ // Merge continuous bbox items in a single linear pass.+ // This avoids repeated middle removals (O(n^2) in worst case) on large OCR outputs.+ let merge_y_tolerance = 0.1;+ let merge_h_tolerance = 0.1;++ let mut merged_items: Vec<ProjectedTextItem> = Vec::with_capacity(items.len());+ for cur in items.drain(..) {+ let should_merge = merged_items+ .last()+ .map(|prev| can_merge(prev, &cur, merge_y_tolerance, merge_h_tolerance))+ .unwrap_or(false);++ if should_merge {+ if let Some(prev) = merged_items.last_mut() {+ let merged = merge_bbox(prev, &cur);+ prev.item.text.push_str(&cur.item.text);+ prev.item.x = merged.0;+ prev.item.y = merged.1;+ prev.item.width = merged.2;+ prev.item.height = merged.3;+ }+ } else {+ merged_items.push(cur);+ }+ }++ *items = merged_items;++ // try to find the bounding box that forms a line and group items by line+ let mut lines: Vec<Vec<ProjectedTextItem>> = Vec::new();+ let mut current_line: Vec<ProjectedTextItem> = Vec::new();+ let mut current_line_min_y = f32::INFINITY;+ let mut current_line_max_y = f32::NEG_INFINITY;+ for item in items.drain(..) {+ if !current_line.is_empty() {+ let mut line_collide = false;+ for line_item in current_line.iter() {+ let overlap_length = (line_item.item.x + line_item.item.width).min(item.item.x + item.item.width) - line_item.item.x.max(item.item.x);++ // Use a minimum threshold to tolerate small overalps common in PDFs due to:+ // - character spacing and kerning+ // - floating point precision issues in text extraction+ // - adjacent items with slightly overlapping boxes+ // We want to detect true collisions, not adjacent text+ if overlap_length > f32::max(5.0, median_width / 3.0) {+ line_collide = true;+ break;+ }+ }++ // Don't merge margin line numbers with regular content+ let cur_line_has_margin = current_line.iter().any(|i| i.is_margin_line_number);+ let cur_item_has_margin = item.is_margin_line_number;+ let margin_mismatch = cur_line_has_margin != cur_item_has_margin;++ // For rotated text, use y-tolerance based merging since heights may be inconsistent+ let y_tolerance_merge = if item.rotated {+ (median_height * 2.0).max(20.0)+ } else {+ 0.0+ };+ let y_within_tolerance =+ item.rotated && (item.item.y - current_line_min_y).abs() < y_tolerance_merge;++ if !line_collide && !margin_mismatch &&+ (+ y_within_tolerance ||+ (item.item.y + item.item.height * 0.5 >= current_line_min_y && item.item.y + item.item.height * 0.5 <= current_line_max_y) ||+ (item.item.y >= current_line_min_y && item.item.y <= current_line_max_y)+ )+ {+ current_line_min_y = current_line_min_y.min(item.item.y);+ current_line_max_y = current_line_max_y.max(item.item.y + item.item.height);+ current_line.push(item);+ } else {+ lines.push(std::mem::take(&mut current_line));+ current_line_min_y = item.item.y;+ current_line_max_y = item.item.y + item.item.height;+ current_line.push(item);+ }+ } else {+ current_line_min_y = item.item.y;+ current_line_max_y = item.item.y + item.item.height;+ current_line.push(item);+ }+ }++ if !current_line.is_empty() {+ lines.push(current_line);+ }++ // sort each line by x+ for line in lines.iter_mut() {+ line.sort_by(|a, b| a.item.x.total_cmp(&b.item.x));+ }++ // sort lines by y+ lines.sort_by(|a, b| {+ let ay = a.first().map(|v| v.item.y).unwrap_or(f32::INFINITY);+ let by = b.first().map(|v| v.item.y).unwrap_or(f32::INFINITY);+ ay.total_cmp(&by)+ });++ // merge 'words'+ const MERGE_THRESHOLD: f32 = 1.0;++ // Pattern to detect standalone numeric values.+ // Matches: numbers with optional commas, decimal points, dollar signs, percentages, negatives.+ fn looks_like_table_number(text: &str) -> bool {+ let trimmed = text.trim();+ if trimmed.chars().count() < 2 {+ return false;+ }++ let mut chars = trimmed.chars().peekable();+ if matches!(chars.peek(), Some('$')) {+ chars.next();+ }+ if matches!(chars.peek(), Some('-')) {+ chars.next();+ }++ let mut has_digit = false;+ let mut has_decimal = false;+ for c in chars {+ if c.is_ascii_digit() {+ has_digit = true;+ } else if c == ',' {+ continue;+ } else if c == '.' {+ if has_decimal {+ return false;+ }+ has_decimal = true;+ } else if c == '%' {+ // Percent is only valid as trailing char.+ return has_digit && trimmed.ends_with('%');+ } else {+ return false;+ }+ }++ has_digit+ }++ for line in lines.iter_mut() {+ let mut merged_line: Vec<ProjectedTextItem> = Vec::with_capacity(line.len());+ for item in line.drain(..) {+ if let Some(prev) = merged_line.last_mut() {+ let both_are_numbers = looks_like_table_number(&prev.item.text)+ && looks_like_table_number(&item.item.text);++ let delta_x = item.item.x - prev.item.x - prev.item.width;++ if !both_are_numbers && delta_x <= MERGE_THRESHOLD {+ prev.item.width = item.item.x + item.item.width - prev.item.x;+ prev.item.text.push_str(&item.item.text);+ continue;+ }++ let prev_len = prev.item.text.chars().count().max(1) as f32;+ let avg_char_width = prev.item.width / prev_len;+ if !both_are_numbers && delta_x < avg_char_width {+ prev.item.width = item.item.x + item.item.width - prev.item.x;+ if !prev.item.text.ends_with(' ') {+ prev.item.text.push(' ');+ }+ prev.item.text.push_str(&item.item.text);+ continue;+ }+ }++ merged_line.push(item);+ }++ *line = merged_line;+ }++ // Merge overlapping lines when there is no horizontal bbox overlap.+ let mut i = 1usize;+ while i < lines.len() {+ let (previous_min_y, previous_max_y) = {+ let previous = &lines[i - 1];+ let min_y = previous+ .iter()+ .map(|v| v.item.y)+ .fold(f32::INFINITY, |a, b| a.min(b));+ let max_y = previous+ .iter()+ .map(|v| v.item.y + v.item.height)+ .fold(f32::NEG_INFINITY, |a, b| a.max(b));+ (min_y, max_y)+ };++ let (current_min_y, current_max_y) = {+ let current = &lines[i];+ let min_y = current+ .iter()+ .map(|v| v.item.y)+ .fold(f32::INFINITY, |a, b| a.min(b));+ let max_y = current+ .iter()+ .map(|v| v.item.y + v.item.height)+ .fold(f32::NEG_INFINITY, |a, b| a.max(b));+ (min_y, max_y)+ };++ // Do the two lines overlap vertically?+ let lines_overlap = previous_max_y > current_min_y && previous_min_y < current_max_y;++ if lines_overlap {+ let bbox_overlap = {+ let previous = &lines[i - 1];+ let current = &lines[i];+ current.iter().any(|bbox| {+ previous.iter().any(|prev_bbox| {+ (bbox.item.x >= prev_bbox.item.x+ && bbox.item.x <= prev_bbox.item.x + prev_bbox.item.width)+ || (prev_bbox.item.x >= bbox.item.x+ && prev_bbox.item.x <= bbox.item.x + bbox.item.width)+ })+ })+ };++ if !bbox_overlap {+ let mut current = lines.remove(i);+ lines[i - 1].append(&mut current);+ lines[i - 1].sort_by(|a, b| a.item.x.total_cmp(&b.item.x));+ continue;+ }+ }++ i += 1;+ }++ lines+}++#[derive(Clone, Debug, Default)]+struct BoxMeta {+ left_anchor: Option<i32>,+ right_anchor: Option<i32>,+ center_anchor: Option<i32>,+ snap: Option<SnapKind>,+ should_space: usize,+ force_unsnapped: bool,+ rendered: bool,+ projected_x: usize,+}++fn anchor_key(x: f32) -> i32 {+ (x * 4.0).round() as i32+}++fn anchor_to_x(key: i32) -> f32 {+ key as f32 / 4.0+}++fn trim_end_len(s: &str) -> usize {+ s.trim_end().len()+}++fn trim_end_in_place(s: &mut String) {+ let n = trim_end_len(s);+ s.truncate(n);+}++fn line_space_end(raw_line: &str, should_space: usize) -> usize {+ let mut space_end = 0usize;+ if !raw_line.ends_with(' ') {+ space_end = should_space;+ }+ if should_space > 1 {+ let trailing_spaces = raw_line.len().saturating_sub(trim_end_len(raw_line));+ if trailing_spaces < should_space {+ space_end = should_space - trailing_spaces;+ }+ }+ space_end+}++fn can_render_bbox(meta_line: &[BoxMeta], idx: usize) -> bool {+ for m in meta_line.iter().take(idx) {+ if !m.rendered {+ return false;+ }+ }+ true+}++fn merge_nearby_anchor_groups(collection: &mut HashMap<i32, Vec<(usize, usize)>>) {+ const MERGE_TOLERANCE: i32 = 8; // 2 units in quarter-point anchor key space++ let sorted_keys: Vec<i32> = {+ let mut keys: Vec<i32> = collection.keys().copied().collect();+ keys.sort_unstable();+ keys+ };++ for (i, anchor) in sorted_keys.iter().enumerate() {+ if !collection.contains_key(anchor) {+ continue;+ }+ for next_anchor in sorted_keys.iter().skip(i + 1) {+ if !collection.contains_key(next_anchor) {+ continue;+ }+ if next_anchor - anchor > MERGE_TOLERANCE {+ break;+ }++ let current_len = collection.get(anchor).map(|v| v.len()).unwrap_or(0);+ let next_len = collection.get(next_anchor).map(|v| v.len()).unwrap_or(0);++ if next_len > current_len {+ if let Some(cur_items) = collection.remove(anchor) {+ if let Some(next_items) = collection.get_mut(next_anchor) {+ next_items.extend(cur_items);+ }+ }+ break;+ } else if let Some(next_items) = collection.remove(next_anchor)+ && let Some(cur_items) = collection.get_mut(anchor)+ {+ cur_items.extend(next_items);+ }+ }+ }+}++fn update_forward_anchor_right_bound(+ snap_map: &[i32],+ forward_anchor: &mut BTreeMap<i32, usize>,+ right_bound: i32,+ anchor_target: usize,+) {+ const POSITION_TOLERANCE: i32 = 8; // 2 units in quarter-point anchor key space++ for (idx, anchor) in snap_map.iter().enumerate().rev() {+ if *anchor < right_bound {+ return;+ }++ let entry = forward_anchor.entry(*anchor).or_insert(0);+ if anchor_target > *entry {+ *entry = anchor_target;+ }++ let mut j = idx;+ while j > 0 {+ let nearby_anchor = snap_map[j - 1];+ if *anchor - nearby_anchor > POSITION_TOLERANCE {+ break;+ }+ let nearby_entry = forward_anchor.entry(nearby_anchor).or_insert(0);+ if anchor_target > *nearby_entry {+ *nearby_entry = anchor_target;+ }+ j -= 1;+ }+ }+}++fn compress_wide_spaces(line: &str, min_run: usize, replace_with: usize) -> String {+ let mut out = String::with_capacity(line.len());+ let bytes = line.as_bytes();+ let mut i = 0usize;+ while i < bytes.len() {+ if bytes[i] == b' ' {+ let start = i;+ while i < bytes.len() && bytes[i] == b' ' {+ i += 1;+ }+ let run_len = i - start;+ if run_len >= min_run {+ out.push_str(&" ".repeat(replace_with));+ } else {+ out.push_str(&" ".repeat(run_len));+ }+ } else {+ out.push(bytes[i] as char);+ i += 1;+ }+ }+ out+}++fn fix_sparse_blocks(raw_lines: &mut [String], start: usize, end: usize) {+ let mut total = 0usize;+ let mut whitespace = 0usize;++ for line in raw_lines.iter_mut().take(end).skip(start) {+ trim_end_in_place(line);+ if line.is_empty() {+ continue;+ }+ total += line.len();+ whitespace += line.chars().filter(|c| c.is_whitespace()).count();+ }++ if total >= 500 && (whitespace as f32 / total as f32) > 0.8 {+ for line in raw_lines.iter_mut().take(end).skip(start) {+ if line.is_empty() {+ continue;+ }+ *line = compress_wide_spaces(line, COLUMN_SPACES, FLOATING_SPACES);+ }+ }+}++fn project_to_grid(page: &Page, mut projection_boxes: Vec<ProjectedTextItem>) -> (Vec<ProjectedTextItem>, String) {+ // Step 1a: Filter out items that are purely dots+ let mut dot_count = 0usize;+ projection_boxes.iter().for_each(|item| {+ if item.item.text.chars().all(|c| c == '.' || c == '·' || c == '•') {+ dot_count += 1;+ }+ });++ // If there are many dot-only items (likely a dotted ruler), remove them in-place.+ // Use a relative threshold (5%) and an absolute threshold (100) to avoid removing+ // dots on small pages.+ if dot_count > 100 && (dot_count as f64) > (projection_boxes.len() as f64) * 0.05 {+ projection_boxes.retain(|item| {+ !item.item.text.chars().all(|c| c == '.' || c == '·' || c == '•')+ });+ }++ // Step 1b: Compute median distances+ let (median_width, median_height) = compute_median_textbox_size(&projection_boxes);++ // Step 1c: Handle reading order rotations+ handle_rotation_reading_order(&mut projection_boxes, page.page_height);++ // Step 1d: Form lines of boxes+ let mut lines = form_lines(&mut projection_boxes, median_width, median_height, page.page_width);+ if lines.is_empty() {+ return (Vec::new(), String::new());+ }++ let mut meta: Vec<Vec<BoxMeta>> = lines+ .iter()+ .map(|line| vec![BoxMeta::default(); line.len()])+ .collect();++ // Compute spacing hints between neighboring boxes, similar to TS shouldSpace.+ for line_idx in 0..lines.len() {+ for box_idx in 0..lines[line_idx].len() {+ if box_idx == 0 {+ meta[line_idx][box_idx].should_space = 0;+ continue;+ }+ let prev = &lines[line_idx][box_idx - 1].item;+ let cur = &lines[line_idx][box_idx].item;+ let x_delta = cur.x - (prev.x + prev.width);++ let mut should_space = 0usize;+ if x_delta > 2.0 {+ should_space = 1;+ let prev_len = prev.text.chars().count().max(1) as f32;+ let prev_char_width = (prev.width / prev_len).max(0.1);+ if x_delta > prev_char_width * 2.0 {+ let column_gap_threshold = page.page_width * 0.1;+ let same_column = x_delta < column_gap_threshold;+ if x_delta > prev_char_width * 8.0 {+ should_space = if same_column { FLOATING_SPACES } else { COLUMN_SPACES };+ } else {+ should_space = if same_column { 1 } else { FLOATING_SPACES };+ }+ }+ }+ meta[line_idx][box_idx].should_space = should_space;+ }+ }++ // Anchor extraction+ let mut anchor_left: HashMap<i32, Vec<(usize, usize)>> = HashMap::new();+ let mut anchor_right: HashMap<i32, Vec<(usize, usize)>> = HashMap::new();+ let mut anchor_center: HashMap<i32, Vec<(usize, usize)>> = HashMap::new();++ for (line_idx, line) in lines.iter().enumerate() {+ for (box_idx, bbox) in line.iter().enumerate() {+ let left_key = anchor_key(bbox.item.x);+ let right_key = anchor_key(bbox.item.x + bbox.item.width);+ let center_key = anchor_key(bbox.item.x + bbox.item.width * 0.5);+ anchor_left.entry(left_key).or_default().push((line_idx, box_idx));+ anchor_right.entry(right_key).or_default().push((line_idx, box_idx));+ anchor_center.entry(center_key).or_default().push((line_idx, box_idx));+ }+ }++ merge_nearby_anchor_groups(&mut anchor_left);+ merge_nearby_anchor_groups(&mut anchor_right);+ merge_nearby_anchor_groups(&mut anchor_center);++ // Keep non-singletons only.+ anchor_left.retain(|_, v| v.len() >= 2);+ anchor_right.retain(|_, v| v.len() >= 2);+ anchor_center.retain(|_, v| v.len() >= 2);++ // Populate per-item anchor candidates.+ for (anchor, members) in &anchor_left {+ for (li, bi) in members {+ meta[*li][*bi].left_anchor = Some(*anchor);+ }+ }+ for (anchor, members) in &anchor_right {+ for (li, bi) in members {+ meta[*li][*bi].right_anchor = Some(*anchor);+ }+ }+ for (anchor, members) in &anchor_center {+ for (li, bi) in members {+ meta[*li][*bi].center_anchor = Some(*anchor);+ }+ }++ // Resolve duplicates by choosing strongest anchor (tie-break: left > right > center).+ for line_idx in 0..lines.len() {+ for box_idx in 0..lines[line_idx].len() {+ let left_count = meta[line_idx][box_idx]+ .left_anchor+ .and_then(|k| anchor_left.get(&k).map(|v| v.len()))+ .unwrap_or(0);+ let right_count = meta[line_idx][box_idx]+ .right_anchor+ .and_then(|k| anchor_right.get(&k).map(|v| v.len()))+ .unwrap_or(0);+ let center_count = meta[line_idx][box_idx]+ .center_anchor+ .and_then(|k| anchor_center.get(&k).map(|v| v.len()))+ .unwrap_or(0);++ if left_count == 0 && right_count == 0 && center_count == 0 {+ continue;+ }++ let kind = if left_count >= right_count && left_count >= center_count {+ SnapKind::Left+ } else if right_count >= left_count && right_count >= center_count {+ SnapKind::Right+ } else {+ SnapKind::Center+ };+ meta[line_idx][box_idx].snap = Some(kind);+ }+ }++ let mut left_snaps: Vec<i32> = anchor_left.keys().copied().collect();+ let mut right_snaps: Vec<i32> = anchor_right.keys().copied().collect();+ let mut center_snaps: Vec<i32> = anchor_center.keys().copied().collect();+ left_snaps.sort_unstable();+ right_snaps.sort_unstable();+ center_snaps.sort_unstable();++ let mut floating_set: HashSet<i32> = HashSet::new();+ for (line_idx, line) in lines.iter().enumerate() {+ for (box_idx, bbox) in line.iter().enumerate() {+ if meta[line_idx][box_idx].snap.is_none() {+ floating_set.insert(anchor_key(bbox.item.x));+ }+ }+ }+ let mut floating_snaps: Vec<i32> = floating_set.into_iter().collect();+ floating_snaps.sort_unstable();++ let mut forward_left: BTreeMap<i32, usize> = BTreeMap::new();+ let mut forward_right: BTreeMap<i32, usize> = BTreeMap::new();+ let mut forward_center: BTreeMap<i32, usize> = BTreeMap::new();+ let mut forward_floating: BTreeMap<i32, usize> = BTreeMap::new();++ let mut raw_lines = vec![String::new(); lines.len()];++ let mut has_changed = true;+ while has_changed || !left_snaps.is_empty() || !right_snaps.is_empty() || !center_snaps.is_empty() {+ has_changed = false;++ // Render floating/unsnapped first when they are not blocked by earlier pending snap columns.+ for line_idx in 0..lines.len() {+ for box_idx in 0..lines[line_idx].len() {+ if meta[line_idx][box_idx].rendered {+ continue;+ }++ if !meta[line_idx][box_idx].force_unsnapped {+ if meta[line_idx][box_idx].snap.is_some() {+ continue;+ }++ let x_key = anchor_key(lines[line_idx][box_idx].item.x);+ let center_key = anchor_key(+ lines[line_idx][box_idx].item.x + lines[line_idx][box_idx].item.width * 0.5,+ );+ if left_snaps.first().copied().is_some_and(|v| v < x_key)+ || right_snaps.first().copied().is_some_and(|v| v < x_key)+ || center_snaps.first().copied().is_some_and(|v| v < center_key)+ {+ continue;+ }+ }++ if !can_render_bbox(&meta[line_idx], box_idx) {+ break;+ }++ let (bbox_x, bbox_w, bbox_text) = {+ let b = &lines[line_idx][box_idx].item;+ (b.x, b.width, b.text.clone())+ };+ let mut target_x = ((bbox_x / median_width).round() as isize)+ .max(0)+ .min(COLUMN_SPACES as isize) as usize;++ let x_key = anchor_key(bbox_x);+ let last_snap_left = forward_left+ .range(..=x_key)+ .map(|(_, v)| *v)+ .max()+ .unwrap_or(0);++ let line_max = last_snap_left.max(trim_end_len(&raw_lines[line_idx]) + meta[line_idx][box_idx].should_space);+ if target_x < line_max {+ target_x = line_max;+ }++ if !meta[line_idx][box_idx].force_unsnapped {+ let floating_key = anchor_key(bbox_x);+ if let Some(floating_anchor) = forward_floating.get(&floating_key).copied()+ && target_x < floating_anchor+ {+ let adjusted = floating_anchor.min(target_x + 4);+ if adjusted > target_x {+ target_x = adjusted;+ }+ }+ }++ trim_end_in_place(&mut raw_lines[line_idx]);+ let before_len = raw_lines[line_idx].len();+ if target_x > before_len {+ raw_lines[line_idx].push_str(&" ".repeat(target_x - before_len));+ }+ let start_x = raw_lines[line_idx].len();+ raw_lines[line_idx].push_str(&bbox_text);++ meta[line_idx][box_idx].rendered = true;+ meta[line_idx][box_idx].projected_x = start_x;+ lines[line_idx][box_idx].rendered = true;+ lines[line_idx][box_idx].num_spaces = start_x.saturating_sub(before_len);+ has_changed = true;++ let next_should_space = if box_idx + 1 < lines[line_idx].len() {+ meta[line_idx][box_idx + 1].should_space+ } else {+ 0+ };+ let right_bound = anchor_key(bbox_x + bbox_w);+ let target_len = raw_lines[line_idx].len() + next_should_space;++ update_forward_anchor_right_bound(&left_snaps, &mut forward_left, right_bound, target_len);+ update_forward_anchor_right_bound(&right_snaps, &mut forward_right, right_bound, target_len);+ update_forward_anchor_right_bound(&floating_snaps, &mut forward_floating, right_bound, target_len);+ }+ }++ let left_first = left_snaps.first().copied();+ let right_first = right_snaps.first().copied();+ let center_first = center_snaps.first().copied();++ let next_kind = match (left_first, right_first, center_first) {+ (None, None, None) => None,+ (Some(_), None, None) => Some(SnapKind::Left),+ (None, Some(_), None) => Some(SnapKind::Right),+ (None, None, Some(_)) => Some(SnapKind::Center),+ (Some(l), Some(r), None) => Some(if l <= r { SnapKind::Left } else { SnapKind::Right }),+ (Some(l), None, Some(c)) => Some(if l <= c { SnapKind::Left } else { SnapKind::Center }),+ (None, Some(r), Some(c)) => Some(if r <= c { SnapKind::Right } else { SnapKind::Center }),+ (Some(l), Some(r), Some(c)) => {+ if l <= r && l <= c {+ Some(SnapKind::Left)+ } else if r <= l && r <= c {+ Some(SnapKind::Right)+ } else {+ Some(SnapKind::Center)+ }+ }+ };++ let Some(kind) = next_kind else {+ continue;+ };++ let current_anchor = match kind {+ SnapKind::Left => left_snaps.first().copied(),+ SnapKind::Right => right_snaps.first().copied(),+ SnapKind::Center => center_snaps.first().copied(),+ };++ let Some(current_anchor) = current_anchor else {+ continue;+ };++ let mut turn_items: Vec<(usize, usize)> = Vec::new();+ for line_idx in 0..lines.len() {+ for box_idx in 0..lines[line_idx].len() {+ if meta[line_idx][box_idx].rendered {+ continue;+ }+ let matches = match kind {+ SnapKind::Left => meta[line_idx][box_idx].left_anchor == Some(current_anchor),+ SnapKind::Right => meta[line_idx][box_idx].right_anchor == Some(current_anchor),+ SnapKind::Center => meta[line_idx][box_idx].center_anchor == Some(current_anchor),+ };+ if matches {+ turn_items.push((line_idx, box_idx));+ }+ }+ }++ if turn_items.is_empty() {+ match kind {+ SnapKind::Left => {+ left_snaps.remove(0);+ }+ SnapKind::Right => {+ right_snaps.remove(0);+ }+ SnapKind::Center => {+ center_snaps.remove(0);+ }+ }+ continue;+ }++ has_changed = true;++ let mut target_x = ((anchor_to_x(current_anchor) / median_width).round() as isize)+ .max(0)+ .min(COLUMN_SPACES as isize) as usize;++ let line_max = match kind {+ SnapKind::Left => turn_items+ .iter()+ .map(|(li, bi)| raw_lines[*li].len() + line_space_end(&raw_lines[*li], meta[*li][*bi].should_space) + 1)+ .max()+ .unwrap_or(0),+ SnapKind::Right => turn_items+ .iter()+ .map(|(li, bi)| {+ let bbox = &lines[*li][*bi].item;+ let x_key = anchor_key(bbox.x);+ let last_snap_left = forward_left+ .range(..=x_key)+ .map(|(_, v)| *v)+ .max()+ .unwrap_or(0);+ let left_bound = last_snap_left.max(trim_end_len(&raw_lines[*li]) + meta[*li][*bi].should_space);+ left_bound + bbox.text.chars().count()+ })+ .max()+ .unwrap_or(0),+ SnapKind::Center => turn_items+ .iter()+ .map(|(li, bi)| {+ let text_half = lines[*li][*bi].item.text.chars().count() / 2;+ raw_lines[*li].len() + text_half + line_space_end(&raw_lines[*li], meta[*li][*bi].should_space)+ })+ .max()+ .unwrap_or(0),+ };++ if target_x < line_max {+ target_x = line_max;+ }++ match kind {+ SnapKind::Left => {+ if let Some(v) = forward_left.get(¤t_anchor).copied() {+ target_x = target_x.max(v);+ }+ forward_left.insert(current_anchor, target_x);+ }+ SnapKind::Right => {+ if let Some(v) = forward_right.get(¤t_anchor).copied() {+ target_x = target_x.max(v);+ }+ forward_right.insert(current_anchor, target_x);+ }+ SnapKind::Center => {+ if let Some(v) = forward_center.get(¤t_anchor).copied() {+ target_x = target_x.max(v);+ }+ forward_center.insert(current_anchor, target_x);+ }+ }++ for (line_idx, box_idx) in turn_items {+ let (bbox_x, bbox_w, bbox_text) = {+ let b = &lines[line_idx][box_idx].item;+ (b.x, b.width, b.text.clone())+ };+ match kind {+ SnapKind::Left => {+ let before = raw_lines[line_idx].len();+ if target_x > before {+ raw_lines[line_idx].push_str(&" ".repeat(target_x - before));+ }+ let start_x = raw_lines[line_idx].len();+ raw_lines[line_idx].push_str(&bbox_text);+ meta[line_idx][box_idx].projected_x = start_x;+ lines[line_idx][box_idx].num_spaces = start_x.saturating_sub(before);+ }+ SnapKind::Right => {+ trim_end_in_place(&mut raw_lines[line_idx]);+ let text_len = bbox_text.chars().count();+ let before = raw_lines[line_idx].len();+ let trim_len = trim_end_len(&raw_lines[line_idx]);+ if target_x > trim_len + text_len {+ let pad = target_x - raw_lines[line_idx].len() - text_len;+ raw_lines[line_idx].push_str(&" ".repeat(pad));+ }+ let start_x = raw_lines[line_idx].len();+ raw_lines[line_idx].push_str(&bbox_text);+ meta[line_idx][box_idx].projected_x = start_x;+ lines[line_idx][box_idx].num_spaces = start_x.saturating_sub(before);+ }+ SnapKind::Center => {+ let text_half = bbox_text.chars().count() / 2;+ let before = raw_lines[line_idx].len();+ if target_x > raw_lines[line_idx].len() + text_half {+ let pad = target_x - raw_lines[line_idx].len() - text_half;+ raw_lines[line_idx].push_str(&" ".repeat(pad));+ }+ let start_x = raw_lines[line_idx].len();+ raw_lines[line_idx].push_str(&bbox_text);+ meta[line_idx][box_idx].projected_x = start_x;+ lines[line_idx][box_idx].num_spaces = start_x.saturating_sub(before);+ }+ }++ meta[line_idx][box_idx].rendered = true;+ lines[line_idx][box_idx].rendered = true;++ let next_should_space = if box_idx + 1 < lines[line_idx].len() {+ meta[line_idx][box_idx + 1].should_space+ } else {+ 0+ };+ let right_bound = anchor_key(bbox_x + bbox_w);+ let target_len = raw_lines[line_idx].len() + next_should_space;+ update_forward_anchor_right_bound(&left_snaps, &mut forward_left, right_bound, target_len);+ update_forward_anchor_right_bound(&right_snaps, &mut forward_right, right_bound, target_len);+ update_forward_anchor_right_bound(&floating_snaps, &mut forward_floating, right_bound, target_len);+ }++ match kind {+ SnapKind::Left => {+ left_snaps.remove(0);+ }+ SnapKind::Right => {+ right_snaps.remove(0);+ }+ SnapKind::Center => {+ center_snaps.remove(0);+ }+ }+ }++ // Fallback: render anything still not rendered to avoid data loss.+ for line_idx in 0..lines.len() {+ for box_idx in 0..lines[line_idx].len() {+ if meta[line_idx][box_idx].rendered {+ continue;+ }+ if !raw_lines[line_idx].is_empty() && !raw_lines[line_idx].ends_with(' ') {+ raw_lines[line_idx].push(' ');+ }+ let start_x = raw_lines[line_idx].len();+ raw_lines[line_idx].push_str(&lines[line_idx][box_idx].item.text);+ meta[line_idx][box_idx].rendered = true;+ meta[line_idx][box_idx].projected_x = start_x;+ lines[line_idx][box_idx].rendered = true;+ }+ }++ let raw_line_count = raw_lines.len();+ fix_sparse_blocks(&mut raw_lines, 0, raw_line_count);++ // Persist projected positions and flatten in line order.+ let mut flattened: Vec<ProjectedTextItem> = Vec::with_capacity(lines.iter().map(|l| l.len()).sum());+ for (line_idx, line) in lines.into_iter().enumerate() {+ for (box_idx, mut item) in line.into_iter().enumerate() {+ item.item.x = meta[line_idx][box_idx].projected_x as f32;+ item.item.y = line_idx as f32;+ item.force_unsnapped = meta[line_idx][box_idx].force_unsnapped;+ item.num_spaces = meta[line_idx][box_idx].should_space;++ if let Some(snap) = meta[line_idx][box_idx].snap {+ match snap {+ SnapKind::Left => {+ item.snap = Snap::Left;+ item.anchor = Anchor::Left;+ }+ SnapKind::Right => {+ item.snap = Snap::Right;+ item.anchor = Anchor::Right;+ }+ SnapKind::Center => {+ item.snap = Snap::Center;+ item.anchor = Anchor::Center;+ }+ }+ }+ flattened.push(item);+ }+ }++ clean_projected_items(&mut flattened, page.page_width);++ let text = raw_lines.into_iter()+ .map(|l| l.trim_end().to_string())+ .collect::<Vec<_>>()+ .join("\n");++ let text = clean_rendered_text(&text);++ (flattened, text)+}++/// Post-rendering text cleanup:+/// - Remove top margin (leading empty lines)+/// - Remove bottom margin (trailing empty lines)+/// - Remove left margin (consistent leading whitespace)+/// - Replace null characters with spaces+fn clean_rendered_text(text: &str) -> String {+ let text = text.replace('\0', " ");+ let lines: Vec<&str> = text.split('\n').collect();++ // Find bounds of content and minimum left indentation+ let mut min_x: Option<usize> = None;+ let mut min_y: Option<usize> = None;+ let mut max_y: Option<usize> = None;++ for (i, line) in lines.iter().enumerate() {+ if line.trim().is_empty() {+ continue;+ }+ let leading = line.len() - line.trim_start().len();+ min_x = Some(min_x.map_or(leading, |m: usize| m.min(leading)));+ if min_y.is_none() {+ min_y = Some(i);+ }+ max_y = Some(i);+ }++ let (min_x, min_y, max_y) = match (min_x, min_y, max_y) {+ (Some(x), Some(y1), Some(y2)) => (x, y1, y2),+ _ => return String::new(),+ };++ lines[min_y..=max_y]+ .iter()+ .map(|line| {+ if line.len() > min_x {+ &line[min_x..]+ } else {+ line.trim_end()+ }+ })+ .collect::<Vec<_>>()+ .join("\n")+}++pub fn project_pages_to_grid(pages: Vec<Page>) -> Vec<ParsedPage> {+ pages.into_iter().map(|page| {+ let projection_boxes = page.text_items.iter().map(|item| {+ ProjectedTextItem {+ item: item.clone(),+ snap: Snap::Left,+ anchor: Anchor::Left,+ is_dup: false,+ rendered: false,+ num_spaces: 0,+ force_unsnapped: false,+ is_margin_line_number: false,+ rotated: false,+ d: 0.0,+ }+ }).collect();++ let (projected_items, text) = project_to_grid(&page, projection_boxes);+ ParsedPage {+ page_number: page.page_number,+ page_width: page.page_width,+ page_height: page.page_height,+ text,+ text_items: projected_items.into_iter().map(|proj| TextItem {+ text: proj.item.text,+ x: proj.item.x,+ y: proj.item.y,+ width: proj.item.width,+ height: proj.item.height,+ rotation: proj.item.rotation,+ font_name: proj.item.font_name,+ font_size: proj.item.font_size,+ }).collect(),+ }+ }).collect()+}\ No newline at end of filediff --git a/packages/rust/src/types.rs b/packages/rust/src/types.rsnew file mode 100644index 0000000..06fd272--- /dev/null+++ b/packages/rust/src/types.rs@@ -0,0 +1,81 @@+use serde::{Deserialize, Serialize};++/// Supported output formats for parsed documents.+/// - `"json"` — Structured JSON with per-page text items, bounding boxes, and metadata.+/// - `"text"` — Plain text with spatial layout preserved.+#[derive(Debug, Serialize, Deserialize)]+pub enum OutputFormat {+ Json,+ Text,+}++/// Accepted input types for input documents.+/// - `FilePath(String)` — A file path to a local PDF document.+/// - `Buffer(Vec<u8>)` — A byte buffer containing the PDF data.+#[derive(Debug, Serialize, Deserialize)]+pub enum InputType {+ FilePath(String),+ Buffer(Vec<u8>),+}++/// Represents a single text item extracted from a PDF page,+/// including its content, position, size, rotation, and font metadata.+#[derive(Debug, Clone, Serialize)]+pub struct TextItem {+ pub text: String,+ pub x: f32,+ pub y: f32,+ pub width: f32,+ pub height: f32,+ pub rotation: f32,+ pub font_name: Option<String>,+ pub font_size: Option<f32>,+}++/// Represents a single page in a PDF document, including its dimensions and extracted text items.+#[derive(Debug, Serialize)]+pub struct Page {+ pub page_number: usize,+ pub page_width: f32,+ pub page_height: f32,+ pub text_items: Vec<TextItem>,+}++/// Represents a fully parsed page with projected text layout.+#[derive(Debug, Serialize)]+pub struct ParsedPage {+ pub page_number: usize,+ pub page_width: f32,+ pub page_height: f32,+ pub text: String,+ pub text_items: Vec<TextItem>,+}++#[derive(Debug, Serialize)]+pub enum Snap {+ Left,+ Right,+ Center,+}++#[derive(Debug, Serialize)]+pub enum Anchor {+ Left,+ Right,+ Center,+}++/// Represents a Projected piece of text, responsible for keeping track of projection related data+#[derive(Debug, Serialize)]+pub struct ProjectedTextItem {+ pub item: TextItem,+ pub snap: Snap,+ pub anchor: Anchor,+ pub is_dup: bool,+ pub rendered: bool,+ pub num_spaces: usize,+ pub force_unsnapped: bool,+ pub is_margin_line_number: bool,+ pub rotated: bool,+ pub d: f32,+}diff --git a/src/core/parser.ts b/src/core/parser.tsindex cfb3bcf..a711aca 100644--- a/src/core/parser.ts+++ b/src/core/parser.ts@@ -160,20 +160,31 @@ export class LiteParse {log(`Loaded PDF with ${doc.numPages} pages`);- // Extract pages- const pages = await this.pdfEngine.extractAllPages(- doc,- this.config.maxPages,- this.config.targetPages- );+ let processedPages: import("./types.js").ParsedPage[];- // run BEFORE grid projection- if (this.ocrEngine) {- await this.runOCR(doc, pages, log);- }+ if (this.config.experimental && this.pdfEngine instanceof LiteParseRsEngine) {+ // Rust pipeline: extract + grid projection in one shot+ processedPages = await this.pdfEngine.parseDocument(+ doc,+ this.config.maxPages,+ this.config.targetPages+ );+ } else {+ // TypeScript pipeline: extract, OCR, then grid projection+ const pages = await this.pdfEngine.extractAllPages(+ doc,+ this.config.maxPages,+ this.config.targetPages+ );- // Process pages with complete grid projection (after OCR)- const processedPages = projectPagesToGrid(pages, this.config);+ // run BEFORE grid projection+ if (this.ocrEngine) {+ await this.runOCR(doc, pages, log);+ }++ // Process pages with complete grid projection (after OCR)+ processedPages = projectPagesToGrid(pages, this.config);+ }// Build bounding boxes if enabledif (this.config.preciseBoundingBox) {diff --git a/src/engines/pdf/liteparse-rs.ts b/src/engines/pdf/liteparse-rs.tsindex cabcf11..cb14f7b 100644--- a/src/engines/pdf/liteparse-rs.ts+++ b/src/engines/pdf/liteparse-rs.ts@@ -5,11 +5,11 @@ import { statSync } from "node:fs";import path from "node:path";import os from "node:os";import { PdfEngine, PdfDocument, PageData, Image } from "./interface.js";-import { TextItem } from "../../core/types.js";+import { TextItem, ParsedPage } from "../../core/types.js";const execFileAsync = promisify(execFile);-/** JSON output shape from the Rust CLI for a single page */+/** JSON output shape from the Rust CLI for a single extracted page */interface RustPageOutput {page_number: number;page_width: number;@@ -26,6 +26,11 @@ interface RustPageOutput {}>;}+/** JSON output shape from the Rust CLI `parse` command (extraction + projection) */+interface RustParsedPageOutput extends RustPageOutput {+ text: string;+}+/** Extended PdfDocument that stores the file path for CLI invocations */interface RustPdfDocument extends PdfDocument {_filePath: string;@@ -155,6 +160,32 @@ export class LiteParseRsEngine implements PdfEngine {// Nothing to clean up — each CLI invocation is stateless}+ /**+ * Run the full Rust pipeline (extract + grid projection) and return ParsedPages directly.+ * This skips the TypeScript grid projection entirely.+ */+ async parseDocument(+ doc: PdfDocument,+ maxPages?: number,+ targetPages?: string+ ): Promise<ParsedPage[]> {+ const rustDoc = doc as RustPdfDocument;+ let parsedPages = await this.runParse(rustDoc._filePath);++ // Filter to target pages if specified+ if (targetPages) {+ const targetSet = this.parseTargetPages(targetPages, doc.numPages);+ parsedPages = parsedPages.filter((p) => targetSet.has(p.page_number));+ }++ // Apply maxPages limit+ if (maxPages && parsedPages.length > maxPages) {+ parsedPages = parsedPages.slice(0, maxPages);+ }++ return parsedPages.map((p) => this.convertParsedPage(p));+ }+// ── private helpers ──────────────────────────────────────────────/** Run the Rust CLI and parse JSON-line output */@@ -186,6 +217,57 @@ export class LiteParseRsEngine implements PdfEngine {return pages;}+ /** Run the Rust CLI `parse` command (extract + projection) and parse JSON output */+ private async runParse(filePath: string): Promise<RustParsedPageOutput[]> {+ const args = ["parse", "--pdf-path", filePath];++ let stdout: string;+ try {+ const result = await execFileAsync(BINARY_PATH, args, {+ maxBuffer: 100 * 1024 * 1024, // 100 MB+ });+ stdout = result.stdout;+ } catch (err: unknown) {+ const message = err instanceof Error ? err.message : String(err);+ throw new Error(`liteparse-rs parse failed: ${message}`);+ }++ // `parse` outputs a single JSON array+ return JSON.parse(stdout) as RustParsedPageOutput[];+ }++ /** Convert Rust CLI parsed page output to ParsedPage format */+ private convertParsedPage(raw: RustParsedPageOutput): ParsedPage {+ const pageHeight = raw.page_height;++ const textItems: TextItem[] = raw.text_items.map((item) => {+ // pdfium uses bottom-left origin; LiteParse expects top-left origin.+ // Convert: y_top = page_height - y_bottom - item_height+ const y = pageHeight - item.y - item.height;++ return {+ str: item.text,+ x: item.x,+ y,+ width: item.width,+ height: item.height,+ w: item.width,+ h: item.height,+ fontName: item.font_name ?? undefined,+ fontSize: item.font_size ?? undefined,+ r: item.rotation,+ };+ });++ return {+ pageNum: raw.page_number,+ width: raw.page_width,+ height: raw.page_height,+ text: raw.text,+ textItems,+ };+ }+/** Convert Rust CLI output to the PageData format */private convertPage(raw: RustPageOutput): PageData {const pageWidth = raw.page_width;