A fast, helpful, and open-source document parser
merge main
Logan Markewich09d47d966b62
diff --cc crates/liteparse/src/extract.rsindex 27937b9,b8fedf6..070a493--- a/crates/liteparse/src/extract.rs+++ b/crates/liteparse/src/extract.rs@@@ -1,18 -1,18 +1,23 @@@use crate::error::LiteParseError;-use crate::types::{Page as LitePage, PdfInput, TextItem};-use pdfium::{Document, Font, FontType, Library, Page, RectF, TextPage};+use crate::glyph_names::resolve_glyph_name;+use crate::render::encode_png;+use crate::types::{+ ExtractedImage, GraphicPrimitive, ImageRef, OutlineTarget, Page as LitePage, PdfInput, Rect,+ StructNode, TextItem,+};+use pdfium::{Document, Font, FontType, Library, Page, PathObject, RectF, SegmentKind, TextPage};/// Open a PDF from path or bytes with an optional password.- pub(crate) fn load_document_from_input(+ ///+ /// The returned [`Document`] borrows from the provided [`Library`], which+ /// holds the process-global PDFium lock. The lock is released when the+ /// `Library` is dropped, so callers must keep `lib` alive for as long as any+ /// `Document` / `Page` / `TextPage` etc. derived from it is in use.+ pub(crate) fn load_document_from_input<'lib>(+ lib: &'lib Library,input: &PdfInput,password: Option<&str>,- ) -> Result<Document, LiteParseError> {- let lib = Library::init();+ ) -> Result<Document<'lib>, LiteParseError> {match input {PdfInput::Path(path) => Ok(lib.load_document(path, password)?),PdfInput::Bytes(data) => Ok(lib.load_document_from_bytes(data, password)?),@@@ -1155,9 -581,9 +1169,10 @@@ impl SegmentBuildervp_top: f32::MAX,vp_bottom: f32::MIN,last_char_right: f32::MIN,+ last_char_loose_right: f32::MIN,last_char_bottom: f32::MIN,char_count: 0,+ unmapped_char_count: 0,font_name: None,font_size: 0.0,font_height: None,@@@ -1209,9 -635,9 +1224,10 @@@self.vp_top = vp_loose.top;self.vp_bottom = vp_loose.bottom;self.last_char_right = vp_strict.right;+ self.last_char_loose_right = vp_loose.right;self.last_char_bottom = vp_strict.bottom;self.char_count = 1;+ self.unmapped_char_count = if ch.has_unicode_map_error() { 1 } else { 0 };self.has_content = true;self.pending_space = false;self.text_width = 0.0;@@@ -1303,9 -729,11 +1319,12 @@@self.vp_top = self.vp_top.min(vp_loose.top);self.vp_bottom = self.vp_bottom.max(vp_loose.bottom);self.last_char_right = vp_strict.right;+ self.last_char_loose_right = vp_loose.right;self.last_char_bottom = vp_strict.bottom;self.char_count += 1;+ if ch.has_unicode_map_error() {+ self.unmapped_char_count += 1;+ }// Accumulate glyph widthif let Some(ref font) = self.font {diff --cc crates/liteparse/src/parser.rsindex 98bdf67,47159a6..76afe02--- a/crates/liteparse/src/parser.rs+++ b/crates/liteparse/src/parser.rs@@@ -13,7 -12,8 +13,8 @@@ use crate::output::markdownuse crate::projection;#[cfg(not(target_arch = "wasm32"))]use crate::render;-use crate::types::{ParsedPage, PdfInput};+use crate::types::{ExtractedImage, OutlineTarget, ParsedPage, PdfInput};+ use pdfium::Library;/// Result of parsing a document.pub struct ParseResult {@@@ -107,42 -113,53 +122,46 @@@ impl LiteParse.map_err(|e| format!("invalid --target-pages: {}", e))?;// Extract text (and pre-render OCR pages in one PDF load when OCR is on).- // The Document handle isn't Send, so we open it, pull every bit of- // document-level state we need, and drop it before any `.await`.- //+ // The PDFium lock is acquired for this entire critical section and+ // released before any `.await` below — OCR (network / CPU) and grid+ // projection (pure Rust) do not touch PDFium, so they can run+ // concurrently with other `LiteParse` calls.let password = self.config.password.as_deref();- let (mut pages, ocr_rendered) = {+ let render_images = matches!(self.config.image_mode, crate::config::ImageMode::Embed);+ let (pages, ocr_rendered, outline, images) = {- let document = extract::load_document_from_input(&validated_input, password)?;+ let lib = Library::init();- if self.config.ocr_enabled {- let document = extract::load_document_from_input(&lib, &validated_input, password)?;- let pages = extract::extract_pages_from_document(- &document,- target_pages.as_deref(),- self.config.max_pages,- )?;- let t_extract = web_time::Instant::now();- log(&format!(- "[liteparse] extract: {:.1}ms ({} pages)",- t_extract.duration_since(t0).as_secs_f64() * 1000.0,- pages.len()- ));- let rendered = ocr_merge::render_pages_for_ocr(&document, &pages, self.config.dpi)?;++ let document = extract::load_document_from_input(&lib, &validated_input, password)?;+ let outline = extract::extract_outline(&document);+ let (pages, images) = extract::extract_pages_and_images(+ &document,+ target_pages.as_deref(),+ self.config.max_pages,+ render_images,+ )?;+ let t_extract = web_time::Instant::now();+ log(&format!(+ "[liteparse] extract: {:.1}ms ({} pages)",+ t_extract.duration_since(t0).as_secs_f64() * 1000.0,+ pages.len()+ ));+ let rendered = if self.config.ocr_enabled {+ let r = ocr_merge::render_pages_for_ocr(&document, &pages, self.config.dpi)?;log(&format!("[liteparse] ocr render: {:.1}ms ({} pages)",web_time::Instant::now().duration_since(t_extract).as_secs_f64()* 1000.0,- rendered.len()+ r.len()));- (pages, rendered)+ r} else {- let document = extract::load_document_from_input(&lib, &validated_input, password)?;- let pages = extract::extract_pages_from_document(- &document,- target_pages.as_deref(),- self.config.max_pages,- )?;- log(&format!(- "[liteparse] extract: {:.1}ms ({} pages)",- web_time::Instant::now().duration_since(t0).as_secs_f64() * 1000.0,- pages.len()- ));- (pages, Vec::new())- }+ Vec::new()+ };+ // `lib` is dropped here, releasing the PDFium lock.+ (pages, rendered, outline, images)};+ let mut pages = pages;let t1 = web_time::Instant::now();// OCR passdiff --cc crates/liteparse/src/projection.rsindex 8f54601,d56107c..bae0ace--- a/crates/liteparse/src/projection.rs+++ b/crates/liteparse/src/projection.rs@@@ -695,35 -665,10 +727,36 @@@ fn form_lineslet y_diff = (item.item.y - prev.item.y).abs();let y_compatible = y_diff <= 1.5;- if y_compatible && !both_are_numbers && delta_x <= MERGE_THRESHOLD {+ // Word-boundary guard: when prev ends with an alphabetic+ // character and item starts with one, a positive delta_x is+ // very likely a missing-space gap (PDFium omitted the space+ // glyph between two words), not a deliberate silent join.+ // Silent-merging here fuses "of the" → "ofthe". Skip the+ // silent path so the items stay separate and the line+ // builder inserts a space based on `gap > space_threshold`.+ // Punctuation joins (`Standards`+`.`, `width`+`)`, etc.)+ // and overlapping/negative-gap merges still go through.+ let alpha_alpha_gap = delta_x > 0.0+ && prev+ .item+ .text+ .chars()+ .last()+ .is_some_and(|c| c.is_alphabetic())+ && item+ .item+ .text+ .chars()+ .next()+ .is_some_and(|c| c.is_alphabetic());+ if y_compatible+ && !both_are_numbers+ && !alpha_alpha_gap+ && delta_x <= MERGE_THRESHOLD+ {prev.item.width = item.item.x + item.item.width - prev.item.x;prev.item.text.push_str(&item.item.text);+ merge_orig_bbox(prev, &item);continue;}@@@ -5040,6 -2783,85 +5074,91 @@@ mod testsassert!(parsed[0].text_items.is_empty());}+ #[test]+ fn project_pages_to_grid_unions_original_bbox_when_word_items_merge() {+ let y = 50.25;+ let pages = vec![Page {+ page_number: 1,+ page_width: 612.0,+ page_height: 792.0,+ text_items: vec![+ TextItem {+ text: "A".to_string(),+ x: 10.0,+ y,+ width: 10.0,+ height: 8.0,+ ..Default::default()+ },+ TextItem {+ text: "B".to_string(),+ x: 24.0,+ y: y + 0.01,+ width: 8.0,+ height: 7.5,+ ..Default::default()+ },+ ],++ graphics: Vec::new(),++ struct_nodes: Vec::new(),++ image_refs: Vec::new(),+ }];++ let parsed = project_pages_to_grid(pages);+ let item = parsed[0]+ .text_items+ .iter()+ .find(|item| item.text == "A B")+ .expect("merged text item");++ assert!((item.x - 10.0).abs() < 0.001);+ assert!((item.y - y).abs() < 0.001);+ assert!((item.width - 22.0).abs() < 0.001);+ assert!((item.height - 8.0).abs() < 0.001);+ }++ #[test]+ fn project_pages_to_grid_unions_original_bbox_when_continuous_items_merge() {+ let pages = vec![Page {+ page_number: 1,+ page_width: 612.0,+ page_height: 792.0,+ text_items: vec![+ TextItem {+ text: "ab".to_string(),+ x: 40.0,+ y: 100.0,+ width: 10.2,+ height: 9.0,+ ..Default::default()+ },+ TextItem {+ text: "cd".to_string(),+ x: 50.2,+ y: 100.0,+ width: 12.3,+ height: 9.0,+ ..Default::default()+ },+ ],++ graphics: Vec::new(),++ struct_nodes: Vec::new(),++ image_refs: Vec::new(),+ }];++ let parsed = project_pages_to_grid(pages);+ let item = parsed[0]+ .text_items+ .iter()- .find(|item| item.text == "abcd")++ .find(|item| item.text == "ab cd")+ .expect("merged continuous text item");++ assert!((item.x - 40.0).abs() < 0.001);+ assert!((item.y - 100.0).abs() < 0.001);+ assert!((item.width - 22.5).abs() < 0.01);+ assert!((item.height - 9.0).abs() < 0.01);+ }+#[test]fn canonical_rotation_snaps_cardinals_and_near_cardinals() {// Exact cardinals are unchanged.diff --cc crates/pdfium/src/document.rsindex 4eb7758,d511706..c47bdc0--- a/crates/pdfium/src/document.rs+++ b/crates/pdfium/src/document.rs@@@ -1,29 -1,19 +1,36 @@@use crate::error::PdfiumError;use crate::ffi;+ use crate::library::Library;use crate::page::Page;- pub struct Document {+ /// An open PDF document.+ ///+ /// The `'lib` lifetime ties this `Document` to the [`Library`] that opened+ /// it, statically guaranteeing that no PDFium calls happen after the+ /// process-wide PDFium lock has been released.+ pub struct Document<'lib> {pub(crate) handle: pdfium_sys::FPDF_DOCUMENT,+ pub(crate) _lib: std::marker::PhantomData<&'lib Library>,}+/// One entry in the document's outline (bookmarks tree).+#[derive(Debug, Clone)]+pub struct OutlineEntry {+ /// Hierarchy depth, 1-based (top-level entries are level 1).+ pub level: u8,+ /// Bookmark title.+ pub title: String,+ /// Zero-based page index of the destination, or `None` if the destination+ /// isn't a page in this document (external link, missing dest, etc).+ pub page_index: Option<i32>,+ /// Y coordinate of the destination on the page in PDF user space (origin+ /// bottom-left), or `None` if the destination doesn't specify one. To+ /// compare against viewport-space line bboxes (origin top-left) use+ /// `page_height - y`.+ pub y: Option<f32>,+}+- impl Document {+ impl<'lib> Document<'lib> {pub fn page_count(&self) -> i32 {unsafe { ffi!(FPDF_GetPageCount(self.handle)) }}@@@ -39,114 -29,9 +46,114 @@@_doc: std::marker::PhantomData,})}++ /// Walk the document outline (bookmarks). Returns entries in pre-order+ /// (depth-first), so parents precede their children. Empty when the+ /// document has no outline.+ pub fn outline(&self) -> Vec<OutlineEntry> {+ let mut out = Vec::new();+ let root = unsafe {+ ffi!(FPDFBookmark_GetFirstChild(+ self.handle,+ std::ptr::null_mut()+ ))+ };+ if !root.is_null() {+ self.walk_bookmark(root, 1, &mut out);+ }+ out+ }++ fn walk_bookmark(+ &self,+ bookmark: pdfium_sys::FPDF_BOOKMARK,+ level: u8,+ out: &mut Vec<OutlineEntry>,+ ) {+ let mut cur = bookmark;+ while !cur.is_null() {+ let title = read_bookmark_title(cur);+ let (page_index, y) = resolve_dest(self.handle, cur);+ out.push(OutlineEntry {+ level,+ title,+ page_index,+ y,+ });++ let child = unsafe { ffi!(FPDFBookmark_GetFirstChild(self.handle, cur)) };+ if !child.is_null() {+ self.walk_bookmark(child, level.saturating_add(1), out);+ }++ cur = unsafe { ffi!(FPDFBookmark_GetNextSibling(self.handle, cur)) };+ }+ }+}++fn read_bookmark_title(bookmark: pdfium_sys::FPDF_BOOKMARK) -> String {+ let needed = unsafe { ffi!(FPDFBookmark_GetTitle(bookmark, std::ptr::null_mut(), 0)) } as usize;+ if needed < 2 {+ return String::new();+ }+ // `needed` is byte length including a trailing UTF-16 NUL terminator.+ let mut buf: Vec<u16> = vec![0; needed / 2];+ let written = unsafe {+ ffi!(FPDFBookmark_GetTitle(+ bookmark,+ buf.as_mut_ptr() as *mut std::os::raw::c_void,+ needed as std::os::raw::c_ulong,+ ))+ } as usize;+ if written < 2 {+ return String::new();+ }+ let chars = written / 2;+ let end = if buf.get(chars - 1) == Some(&0) {+ chars - 1+ } else {+ chars+ };+ String::from_utf16_lossy(&buf[..end])+}++fn resolve_dest(+ doc: pdfium_sys::FPDF_DOCUMENT,+ bookmark: pdfium_sys::FPDF_BOOKMARK,+) -> (Option<i32>, Option<f32>) {+ let mut dest = unsafe { ffi!(FPDFBookmark_GetDest(doc, bookmark)) };+ if dest.is_null() {+ let action = unsafe { ffi!(FPDFBookmark_GetAction(bookmark)) };+ if !action.is_null() {+ dest = unsafe { ffi!(FPDFAction_GetDest(doc, action)) };+ }+ }+ if dest.is_null() {+ return (None, None);+ }+ let page_index = unsafe { ffi!(FPDFDest_GetDestPageIndex(doc, dest)) };+ let page_index = if page_index >= 0 {+ Some(page_index)+ } else {+ None+ };++ let mut has_x: pdfium_sys::FPDF_BOOL = 0;+ let mut has_y: pdfium_sys::FPDF_BOOL = 0;+ let mut has_z: pdfium_sys::FPDF_BOOL = 0;+ let mut x: f32 = 0.0;+ let mut y: f32 = 0.0;+ let mut z: f32 = 0.0;+ let ok = unsafe {+ ffi!(FPDFDest_GetLocationInPage(+ dest, &mut has_x, &mut has_y, &mut has_z, &mut x, &mut y, &mut z+ ))+ };+ let y_out = if ok != 0 && has_y != 0 { Some(y) } else { None };+ (page_index, y_out)}- impl Drop for Document {+ impl Drop for Document<'_> {fn drop(&mut self) {unsafe { ffi!(FPDF_CloseDocument(self.handle)) };}diff --cc crates/pdfium/src/page.rsindex acb37b3,9322e30..1d8d915--- a/crates/pdfium/src/page.rs+++ b/crates/pdfium/src/page.rs@@@ -17,47 -17,18 +17,52 @@@ pub struct ImageBoundspub height: f32,}+/// One segment of a vector path. Coordinates are in viewport space+/// (top-left origin, 72 DPI) after the object's matrix has been applied.+#[derive(Debug, Clone, Copy)]+pub struct PathSegment {+ pub kind: SegmentKind,+ pub x: f32,+ pub y: f32,+ /// Whether this segment closes the current subpath back to its MoveTo.+ pub close: bool,+}++#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum SegmentKind {+ MoveTo,+ LineTo,+ BezierTo,+}++/// A vector path object extracted from a page. Used by the markdown emitter+/// for ruled-table, horizontal-rule, and figure-cluster detection.+#[derive(Debug, Clone)]+pub struct PathObject {+ /// Object bbox in viewport space (after matrix; from FPDFPageObj_GetBounds).+ pub bbox: RectF,+ pub stroke_color: Option<Color>,+ pub fill_color: Option<Color>,+ pub stroke_width: f32,+ /// True when the path is stroked per its draw mode.+ pub is_stroked: bool,+ /// True when the path is filled (draw-mode fill ≠ NONE).+ pub is_filled: bool,+ pub segments: Vec<PathSegment>,+}+- pub struct Page<'doc> {+ /// A loaded page within a [`Document`].+ ///+ /// The `'doc` lifetime ties the page to its owning document; `'lib` carries+ /// the PDFium-lock lifetime through, ensuring no PDFium calls can occur+ /// after the lock is released.+ pub struct Page<'doc, 'lib: 'doc> {pub(crate) handle: pdfium_sys::FPDF_PAGE,pub(crate) doc_handle: pdfium_sys::FPDF_DOCUMENT,- pub(crate) _doc: PhantomData<&'doc Document>,+ pub(crate) _doc: PhantomData<&'doc Document<'lib>>,}- impl Page<'_> {+ impl<'doc, 'lib: 'doc> Page<'doc, 'lib> {pub fn width(&self) -> f32 {unsafe { ffi!(FPDF_GetPageWidthF(self.handle)) }}@@@ -276,228 -283,175 +317,395 @@@Err(PdfiumError::OperationFailed)}++ /// Enumerate vector path objects on this page. Segment points are+ /// transformed into viewport space (top-left origin, 72 DPI) by composing+ /// the object's matrix with the page→viewport transform. Recurses into+ /// Form XObjects (composing each form's matrix) — table rules and other+ /// vector art are frequently wrapped in a form container, invisible to a+ /// top-level-only walk.+ pub fn path_objects(&self, view_box: &RectF) -> Vec<PathObject> {+ let vp = self.viewport_transform(view_box);+ let obj_count = unsafe { ffi!(FPDFPage_CountObjects(self.handle)) };+ let mut out = Vec::new();+ let identity = pdfium_sys::FS_MATRIX {+ a: 1.0,+ b: 0.0,+ c: 0.0,+ d: 1.0,+ e: 0.0,+ f: 0.0,+ };++ for i in 0..obj_count {+ let obj = unsafe { ffi!(FPDFPage_GetObject(self.handle, i)) };+ if obj.is_null() {+ continue;+ }+ collect_path_objects(obj, &identity, &vp, 0, &mut out);+ }+ out+ }+}++const FS_IDENTITY: pdfium_sys::FS_MATRIX = pdfium_sys::FS_MATRIX {+ a: 1.0,+ b: 0.0,+ c: 0.0,+ d: 1.0,+ e: 0.0,+ f: 0.0,+};++/// Compose two affine matrices: `result(p) = outer(inner(p))`.+fn compose_matrix(+ outer: &pdfium_sys::FS_MATRIX,+ inner: &pdfium_sys::FS_MATRIX,+) -> pdfium_sys::FS_MATRIX {+ pdfium_sys::FS_MATRIX {+ a: outer.a * inner.a + outer.c * inner.b,+ b: outer.b * inner.a + outer.d * inner.b,+ c: outer.a * inner.c + outer.c * inner.d,+ d: outer.b * inner.c + outer.d * inner.d,+ e: outer.a * inner.e + outer.c * inner.f + outer.e,+ f: outer.b * inner.e + outer.d * inner.f + outer.f,+ }+}++/// Recursively collect path objects, descending into Form XObjects. `parent`+/// is the accumulated form matrix mapping this object's content space into+/// page space (identity at the top level).+fn collect_path_objects(+ obj: pdfium_sys::FPDF_PAGEOBJECT,+ parent: &pdfium_sys::FS_MATRIX,+ vp: &ViewportTransform,+ depth: usize,+ out: &mut Vec<PathObject>,+) {+ const MAX_FORM_DEPTH: usize = 6;+ let obj_type = unsafe { ffi!(FPDFPageObj_GetType(obj)) };++ if obj_type == pdfium_sys::FPDF_PAGEOBJ_FORM as i32 {+ if depth >= MAX_FORM_DEPTH {+ return;+ }+ let mut fm = FS_IDENTITY;+ unsafe { ffi!(FPDFPageObj_GetMatrix(obj, &mut fm)) };+ let combined = compose_matrix(parent, &fm);+ let n = unsafe { ffi!(FPDFFormObj_CountObjects(obj)) };+ for i in 0..n {+ let child = unsafe { ffi!(FPDFFormObj_GetObject(obj, i as std::os::raw::c_ulong)) };+ if child.is_null() {+ continue;+ }+ collect_path_objects(child, &combined, vp, depth + 1, out);+ }+ return;+ }++ if obj_type != pdfium_sys::FPDF_PAGEOBJ_PATH as i32 {+ return;+ }++ // Object → content-space matrix, composed with the accumulated form+ // matrix to reach page space.+ let mut m = FS_IDENTITY;+ unsafe { ffi!(FPDFPageObj_GetMatrix(obj, &mut m)) };+ let m = compose_matrix(parent, &m);++ // GetBounds reports bounds in the object's content-stream space (its own+ // matrix applied, ancestor form matrices not). Lift the corners through+ // the parent matrix, then to viewport.+ let mut left = 0.0f32;+ let mut bottom = 0.0f32;+ let mut right = 0.0f32;+ let mut top = 0.0f32;+ let ok = unsafe {+ ffi!(FPDFPageObj_GetBounds(+ obj,+ &mut left,+ &mut bottom,+ &mut right,+ &mut top+ ))+ };+ if ok == 0 {+ return;+ }+ let corners = [(left, bottom), (left, top), (right, bottom), (right, top)];+ let mut min_x = f32::INFINITY;+ let mut min_y = f32::INFINITY;+ let mut max_x = f32::NEG_INFINITY;+ let mut max_y = f32::NEG_INFINITY;+ for (x, y) in corners {+ let px = parent.a * x + parent.c * y + parent.e;+ let py = parent.b * x + parent.d * y + parent.f;+ min_x = min_x.min(px);+ max_x = max_x.max(px);+ min_y = min_y.min(py);+ max_y = max_y.max(py);+ }+ let bbox = vp.transform_bounds(&RectF {+ left: min_x,+ top: max_y,+ right: max_x,+ bottom: min_y,+ });++ // Draw mode → is_filled / is_stroked.+ let mut fill_mode = 0i32;+ let mut stroke_bool = 0i32;+ let dm_ok = unsafe { ffi!(FPDFPath_GetDrawMode(obj, &mut fill_mode, &mut stroke_bool)) };+ let (is_filled, is_stroked) = if dm_ok != 0 {+ (+ fill_mode != pdfium_sys::FPDF_FILLMODE_NONE as i32,+ stroke_bool != 0,+ )+ } else {+ (false, false)+ };++ // Colors are reported as RGBA channels in 0..=255 cuint.+ let stroke_color =+ read_color(|r, g, b, a| unsafe { ffi!(FPDFPageObj_GetStrokeColor(obj, r, g, b, a)) });+ let fill_color =+ read_color(|r, g, b, a| unsafe { ffi!(FPDFPageObj_GetFillColor(obj, r, g, b, a)) });++ let mut stroke_width = 0.0f32;+ unsafe { ffi!(FPDFPageObj_GetStrokeWidth(obj, &mut stroke_width)) };++ // Walk segments. Points are in the object's local coords; apply the+ // composed matrix → page, then viewport transform.+ let n_segs = unsafe { ffi!(FPDFPath_CountSegments(obj)) };+ let mut segments = Vec::with_capacity(n_segs.max(0) as usize);+ for si in 0..n_segs {+ let seg = unsafe { ffi!(FPDFPath_GetPathSegment(obj, si)) };+ if seg.is_null() {+ continue;+ }+ let mut sx = 0.0f32;+ let mut sy = 0.0f32;+ let pt_ok = unsafe { ffi!(FPDFPathSegment_GetPoint(seg, &mut sx, &mut sy)) };+ if pt_ok == 0 {+ continue;+ }+ let ty = unsafe { ffi!(FPDFPathSegment_GetType(seg)) };+ let close = unsafe { ffi!(FPDFPathSegment_GetClose(seg)) } != 0;+ let kind = match ty as u32 {+ pdfium_sys::FPDF_SEGMENT_MOVETO => SegmentKind::MoveTo,+ pdfium_sys::FPDF_SEGMENT_LINETO => SegmentKind::LineTo,+ pdfium_sys::FPDF_SEGMENT_BEZIERTO => SegmentKind::BezierTo,+ _ => continue,+ };++ // Apply the composed matrix (FS_MATRIX is column-major a/b/c/d/e/f+ // matching the PDF text-matrix convention used elsewhere).+ let page_x = m.a * sx + m.c * sy + m.e;+ let page_y = m.b * sx + m.d * sy + m.f;+ let (x, y) = vp.transform_point(page_x, page_y);+ segments.push(PathSegment { kind, x, y, close });+ }++ out.push(PathObject {+ bbox,+ stroke_color,+ fill_color,+ stroke_width,+ is_stroked,+ is_filled,+ segments,+ });+}++/// Helper: call a PDFium getter for RGBA color channels and pack into our `Color`.+/// Returns None when the FFI call reports failure.+fn read_color<F>(getter: F) -> Option<Color>+where+ F: FnOnce(*mut u32, *mut u32, *mut u32, *mut u32) -> i32,+{+ let mut r = 0u32;+ let mut g = 0u32;+ let mut b = 0u32;+ let mut a = 0u32;+ let ok = getter(&mut r, &mut g, &mut b, &mut a);+ if ok == 0 {+ return None;+ }+ Some(Color {+ r: r as u8,+ g: g as u8,+ b: b as u8,+ a: a as u8,+ })}+ /// Recursion limit for nested form XObjects in `filled_path_bounds`.+ const MAX_FORM_DEPTH: u32 = 4;++ /// Compose two FS_MATRIX transforms: the result applies `inner` first,+ /// then `outer` (i.e. `outer ∘ inner`).+ fn compose_matrices(+ outer: &pdfium_sys::FS_MATRIX,+ inner: &pdfium_sys::FS_MATRIX,+ ) -> pdfium_sys::FS_MATRIX {+ pdfium_sys::FS_MATRIX {+ a: outer.a * inner.a + outer.c * inner.b,+ b: outer.b * inner.a + outer.d * inner.b,+ c: outer.a * inner.c + outer.c * inner.d,+ d: outer.b * inner.c + outer.d * inner.d,+ e: outer.a * inner.e + outer.c * inner.f + outer.e,+ f: outer.b * inner.e + outer.d * inner.f + outer.f,+ }+ }++ #[allow(clippy::too_many_arguments)]+ fn collect_filled_paths(+ obj: pdfium_sys::FPDF_PAGEOBJECT,+ transform: Option<&pdfium_sys::FS_MATRIX>,+ page_width: f32,+ page_height: f32,+ min_size_pt: f32,+ max_page_coverage: f32,+ depth: u32,+ out: &mut Vec<ImageBounds>,+ ) {+ let obj_type = unsafe { ffi!(FPDFPageObj_GetType(obj)) };++ if obj_type == pdfium_sys::FPDF_PAGEOBJ_FORM as i32 {+ if depth >= MAX_FORM_DEPTH {+ return;+ }+ // Child bounds are reported in the form's coordinate space, so the+ // form matrix (composed with any outer form transforms) must be+ // applied to map them into page space.+ let mut m = pdfium_sys::FS_MATRIX {+ a: 1.0,+ b: 0.0,+ c: 0.0,+ d: 1.0,+ e: 0.0,+ f: 0.0,+ };+ let has_m = unsafe { ffi!(FPDFPageObj_GetMatrix(obj, &mut m)) } != 0;+ let combined = match (transform, has_m) {+ (Some(outer), true) => Some(compose_matrices(outer, &m)),+ (Some(outer), false) => Some(*outer),+ (None, true) => Some(m),+ (None, false) => None,+ };++ let child_count = unsafe { ffi!(FPDFFormObj_CountObjects(obj)) };+ for i in 0..child_count {+ let child = unsafe { ffi!(FPDFFormObj_GetObject(obj, i as std::os::raw::c_ulong)) };+ if child.is_null() {+ continue;+ }+ collect_filled_paths(+ child,+ combined.as_ref(),+ page_width,+ page_height,+ min_size_pt,+ max_page_coverage,+ depth + 1,+ out,+ );+ }+ return;+ }++ if obj_type != pdfium_sys::FPDF_PAGEOBJ_PATH as i32 {+ return;+ }++ // Only filled paths can be glyph outlines; skip stroke-only paths+ // (table borders, rules, underlines).+ let mut fill_mode: std::os::raw::c_int = 0;+ let mut stroke: pdfium_sys::FPDF_BOOL = 0;+ let ok = unsafe { ffi!(FPDFPath_GetDrawMode(obj, &mut fill_mode, &mut stroke)) };+ if ok == 0 || fill_mode == pdfium_sys::FPDF_FILLMODE_NONE as i32 {+ return;+ }++ // Skip light or transparent fills: glyph outlines are drawn in ink-like+ // (dark, opaque) colors, while table zebra striping and section shading+ // use light pastels. Light-on-dark text still gets caught because the+ // dark background rect itself is a dark filled path. Paths whose fill+ // color can't be read (pattern/shading fills) are kept conservatively.+ let mut r: std::os::raw::c_uint = 0;+ let mut g: std::os::raw::c_uint = 0;+ let mut b: std::os::raw::c_uint = 0;+ let mut a: std::os::raw::c_uint = 0;+ let ok = unsafe {+ ffi!(FPDFPageObj_GetFillColor(+ obj, &mut r, &mut g, &mut b, &mut a+ ))+ };+ if ok != 0 {+ if a < 128 {+ return;+ }+ let luminance = 0.299 * r as f32 + 0.587 * g as f32 + 0.114 * b as f32;+ if luminance > 140.0 {+ return;+ }+ }++ let mut left: f32 = 0.0;+ let mut bottom: f32 = 0.0;+ let mut right: f32 = 0.0;+ let mut top: f32 = 0.0;+ let ok = unsafe {+ ffi!(FPDFPageObj_GetBounds(+ obj,+ &mut left,+ &mut bottom,+ &mut right,+ &mut top+ ))+ };+ if ok == 0 {+ return;+ }++ if let Some(m) = transform {+ let corners = [(left, bottom), (right, bottom), (left, top), (right, top)];+ let mut min_x = f32::INFINITY;+ let mut max_x = f32::NEG_INFINITY;+ let mut min_y = f32::INFINITY;+ let mut max_y = f32::NEG_INFINITY;+ for (x, y) in corners {+ let tx = m.a * x + m.c * y + m.e;+ let ty = m.b * x + m.d * y + m.f;+ min_x = min_x.min(tx);+ max_x = max_x.max(tx);+ min_y = min_y.min(ty);+ max_y = max_y.max(ty);+ }+ left = min_x;+ right = max_x;+ bottom = min_y;+ top = max_y;+ }++ let w = right - left;+ let h = top - bottom;++ if w < min_size_pt || h < min_size_pt {+ return;+ }+ if w > page_width * max_page_coverage && h > page_height * max_page_coverage {+ return;+ }++ out.push(ImageBounds {+ x: left,+ y: page_height - top,+ width: w,+ height: h,+ });+ }+/// Pre-computed affine transform from PDF page space to viewport space./// Avoids repeated FFI calls to `FPDF_PageToDevice` by probing 3 points/// once and deriving the 6 affine coefficients.diff --cc crates/pdfium/src/struct_tree.rsindex e948d82,0000000..6767fe5mode 100644,000000..100644--- a/crates/pdfium/src/struct_tree.rs+++ b/crates/pdfium/src/struct_tree.rs@@@ -1,222 -1,0 +1,225 @@@+//! Walk the PDF structure tree (tagged-PDF tree) for a page.+//!+//! Each struct element has:+//! - a role (string: "H1", "Figure", "Table", ...)+//! - zero or more marked content ids (mcids) that tie the element to+//! page-content stream blocks+//! - zero or more child elements+//!+//! For LiteParse's purposes we want a flat list of nodes with:+//! - the role string+//! - viewport-space bbox derived from joining page objects whose+//! `FPDFPageObj_GetMarkedContentID` matches one of this node's mcids+//! - the mcid list itself (so the layout pass can map text runs to nodes)++use crate::ffi;+use crate::page::{Page, ViewportTransform};+use crate::types::RectF;++/// One node in the structure tree, flattened for downstream use.+#[derive(Debug, Clone)]+pub struct StructNode {+ /// Role string from `FPDF_StructElement_GetType` (e.g. "H1", "P", "Figure").+ pub role: String,+ /// Marked content ids attached to this element (and its non-struct child markers).+ pub mcids: Vec<i32>,+ /// Union bbox of page objects tagged with any of `mcids`, in viewport+ /// coordinates (top-left origin, 72 DPI). `None` when none of the mcids+ /// resolved to a bbox on the page.+ pub bbox: Option<RectF>,+ /// Optional alt text (set for Figure / Formula elements when present).+ pub alt_text: Option<String>,+}+- impl Page<'_> {++impl Page<'_, '_> {+ /// Walk this page's structure tree (tagged-PDF tree). Returns an empty+ /// vec when the page is untagged or the document has no struct tree.+ /// Nodes are returned in pre-order (parent before children).+ pub fn struct_tree(&self, view_box: &RectF) -> Vec<StructNode> {+ let tree = unsafe { ffi!(FPDF_StructTree_GetForPage(self.handle)) };+ if tree.is_null() {+ return Vec::new();+ }++ let mcid_bboxes = collect_mcid_bboxes(self, view_box);+ let mut out = Vec::new();++ let count = unsafe { ffi!(FPDF_StructTree_CountChildren(tree)) };+ for i in 0..count {+ let elem = unsafe { ffi!(FPDF_StructTree_GetChildAtIndex(tree, i)) };+ if !elem.is_null() {+ walk_element(elem, &mcid_bboxes, &mut out);+ }+ }++ unsafe { ffi!(FPDF_StructTree_Close(tree)) };++ out+ }+}++/// Pre-scan all page objects on the page, building `mcid → union(bbox)` in+/// viewport space. Each struct node then unions the bboxes for its own mcids.- fn collect_mcid_bboxes(page: &Page<'_>, view_box: &RectF) -> std::collections::HashMap<i32, RectF> {++fn collect_mcid_bboxes(++ page: &Page<'_, '_>,++ view_box: &RectF,++) -> std::collections::HashMap<i32, RectF> {+ let vp = page.viewport_transform(view_box);+ let obj_count = unsafe { ffi!(FPDFPage_CountObjects(page.handle)) };+ let mut map: std::collections::HashMap<i32, RectF> = std::collections::HashMap::new();++ for i in 0..obj_count {+ let obj = unsafe { ffi!(FPDFPage_GetObject(page.handle, i)) };+ if obj.is_null() {+ continue;+ }+ let mcid = unsafe { ffi!(FPDFPageObj_GetMarkedContentID(obj)) };+ if mcid < 0 {+ continue;+ }+ let bbox = page_object_bbox(obj, &vp);+ if let Some(b) = bbox {+ map.entry(mcid)+ .and_modify(|cur| *cur = union_rect(cur, &b))+ .or_insert(b);+ }+ }++ map+}++fn page_object_bbox(obj: pdfium_sys::FPDF_PAGEOBJECT, vp: &ViewportTransform) -> Option<RectF> {+ let mut left = 0.0f32;+ let mut bottom = 0.0f32;+ let mut right = 0.0f32;+ let mut top = 0.0f32;+ let ok = unsafe {+ ffi!(FPDFPageObj_GetBounds(+ obj,+ &mut left,+ &mut bottom,+ &mut right,+ &mut top+ ))+ };+ if ok == 0 {+ return None;+ }+ Some(vp.transform_bounds(&RectF {+ left,+ top,+ right,+ bottom,+ }))+}++fn union_rect(a: &RectF, b: &RectF) -> RectF {+ RectF {+ left: a.left.min(b.left),+ top: a.top.min(b.top),+ right: a.right.max(b.right),+ bottom: a.bottom.max(b.bottom),+ }+}++fn walk_element(+ elem: pdfium_sys::FPDF_STRUCTELEMENT,+ mcid_bboxes: &std::collections::HashMap<i32, RectF>,+ out: &mut Vec<StructNode>,+) {+ let role = read_element_type(elem);+ let alt_text = read_alt_text(elem);++ // Collect mcids: the multi-mcid getters + per-child marked-content-only children.+ let mut mcids: Vec<i32> = Vec::new();+ let n_mcids = unsafe { ffi!(FPDF_StructElement_GetMarkedContentIdCount(elem)) };+ for i in 0..n_mcids {+ let m = unsafe { ffi!(FPDF_StructElement_GetMarkedContentIdAtIndex(elem, i)) };+ if m >= 0 {+ mcids.push(m);+ }+ }+ // Also legacy: single direct mcid getter (older tag-trees expose it here).+ let single = unsafe { ffi!(FPDF_StructElement_GetMarkedContentID(elem)) };+ if single >= 0 && !mcids.contains(&single) {+ mcids.push(single);+ }++ let n_children = unsafe { ffi!(FPDF_StructElement_CountChildren(elem)) };+ for i in 0..n_children {+ // Non-struct children expose their mcid via GetChildMarkedContentID+ // (returns -1 when the child is itself a struct element).+ let child_mcid = unsafe { ffi!(FPDF_StructElement_GetChildMarkedContentID(elem, i)) };+ if child_mcid >= 0 && !mcids.contains(&child_mcid) {+ mcids.push(child_mcid);+ }+ }++ let bbox = union_mcid_bboxes(&mcids, mcid_bboxes);+ out.push(StructNode {+ role,+ mcids,+ bbox,+ alt_text,+ });++ for i in 0..n_children {+ let child_elem = unsafe { ffi!(FPDF_StructElement_GetChildAtIndex(elem, i)) };+ if !child_elem.is_null() {+ walk_element(child_elem, mcid_bboxes, out);+ }+ }+}++fn union_mcid_bboxes(+ mcids: &[i32],+ mcid_bboxes: &std::collections::HashMap<i32, RectF>,+) -> Option<RectF> {+ let mut acc: Option<RectF> = None;+ for m in mcids {+ if let Some(b) = mcid_bboxes.get(m) {+ acc = Some(match acc {+ Some(a) => union_rect(&a, b),+ None => *b,+ });+ }+ }+ acc+}++fn read_element_type(elem: pdfium_sys::FPDF_STRUCTELEMENT) -> String {+ read_widestring(|buf, len| unsafe { ffi!(FPDF_StructElement_GetType(elem, buf, len)) })+}++fn read_alt_text(elem: pdfium_sys::FPDF_STRUCTELEMENT) -> Option<String> {+ let s =+ read_widestring(|buf, len| unsafe { ffi!(FPDF_StructElement_GetAltText(elem, buf, len)) });+ if s.is_empty() { None } else { Some(s) }+}++/// Read a PDFium UTF-16LE widestring out-param via the "call once for size,+/// allocate, call again" pattern. `getter` is `(buf, buflen) -> bytes_written`.+fn read_widestring<F>(getter: F) -> String+where+ F: Fn(*mut std::os::raw::c_void, std::os::raw::c_ulong) -> std::os::raw::c_ulong,+{+ let needed = getter(std::ptr::null_mut(), 0) as usize;+ if needed < 2 {+ return String::new();+ }+ let mut buf: Vec<u16> = vec![0; needed / 2];+ let written = getter(+ buf.as_mut_ptr() as *mut std::os::raw::c_void,+ needed as std::os::raw::c_ulong,+ ) as usize;+ if written < 2 {+ return String::new();+ }+ let chars = written / 2;+ let end = if buf.get(chars - 1) == Some(&0) {+ chars - 1+ } else {+ chars+ };+ String::from_utf16_lossy(&buf[..end])+}