Skip to content

A fast, helpful, and open-source document parser

wip

Logan Markewichf43924bdcc2a
diff --git a/Cargo.lock b/Cargo.lock
index 698724e..4072aa8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2076,6 +2076,7 @@ version = "0.1.0"
dependencies = [
"bitflags 2.13.1",
"fontdb",
+ "image",
"log",
"quick-xml",
"rustc-hash",
@@ -2083,6 +2084,7 @@ dependencies = [
"serde_json",
"skrifa",
"thiserror 2.0.19",
+ "tiny-skia",
"unicode-normalization",
"unicode-properties",
"unicode-segmentation",
diff --git a/crates/liteparse-docx/ATTRIBUTION.md b/crates/liteparse-docx/ATTRIBUTION.md
index 4eb1c11..150ea64 100644
--- a/crates/liteparse-docx/ATTRIBUTION.md
+++ b/crates/liteparse-docx/ATTRIBUTION.md
@@ -80,6 +80,23 @@ render/mod.rs sections: estimate_cursor_y, resolve_and_layout,
field inherits checkpoint/replay and `Continuous`-continuation semantics
for free. Verified behaviour-preserving: `layout_probe` command censuses
over the 48-doc corpus + long documents are byte-identical before/after.
+10. **`src/render/raster.rs` (liteparse-only, no upstream equivalent;
+ `raster` cargo feature).** Rasterizes `LayoutedPage` draw commands to
+ RGBA over tiny-skia + skrifa outlines — the role upstream's
+ `painter.rs`+Skia plays for PDF emission, rebuilt for PNG screenshots
+ without Skia. Glyph pen advances re-derive the measurer's arithmetic
+ against the same `FontRegistry` faces, so raster and `TextItem` geometry
+ share one coordinate space by construction. Paints in four layers
+ (shape < shading rect < image < ink), each in stream order:
+ `DrawCommand` carries no z-order and §20.4.2.3 `behindDoc` is honored by
+ emission position only within a header/footer run, so a body-anchored
+ behind-doc shape lands mid-stream after the header's text — upstream's
+ sequential painter has the same artifact. The command stream itself is
+ untouched (censuses, image-FIFO naming and text-item order all
+ unaffected). Optional deps: `tiny-skia` 0.11 (the line resvg already
+ pins in the liteparse tree), `image` 0.25 (decode only). The
+ `cmd_dump` example prints a page's command stream compactly — it is the
+ debug tool for z-order/ordering questions.
## Layout parity (measured 2026-08-13, macOS host)
diff --git a/crates/liteparse-docx/Cargo.toml b/crates/liteparse-docx/Cargo.toml
index fed2b1f..c9ae531 100644
--- a/crates/liteparse-docx/Cargo.toml
+++ b/crates/liteparse-docx/Cargo.toml
@@ -24,6 +24,21 @@ skrifa = "0.45"
unicode-segmentation = "1.13"
unicode-properties = "0.1"
unicode-normalization = "0.1"
+# `raster` feature only — pinned to the tiny-skia line resvg 0.44 already
+# drags into the liteparse tree, so the feature adds ~no supply chain.
+tiny-skia = { version = "0.11", optional = true }
+image = { version = "0.25", optional = true, default-features = false, features = [
+ "png",
+ "jpeg",
+ "gif",
+ "bmp",
+ "tiff",
+ "webp",
+] }
+
+[features]
+# Rasterize LayoutedPage draw commands to RGBA (native screenshot path).
+raster = ["dep:tiny-skia", "dep:image"]
[dev-dependencies]
serde_json = "1"
diff --git a/crates/liteparse-docx/src/render/mod.rs b/crates/liteparse-docx/src/render/mod.rs
index 939a781..a7ccfd9 100644
--- a/crates/liteparse-docx/src/render/mod.rs
+++ b/crates/liteparse-docx/src/render/mod.rs
@@ -14,6 +14,8 @@ pub mod error;
pub mod fonts;
pub mod geometry;
pub mod layout;
+#[cfg(feature = "raster")]
+pub mod raster;
pub mod resolve;
use crate::model::Document;
diff --git a/crates/liteparse/Cargo.toml b/crates/liteparse/Cargo.toml
index d7c831b..e575716 100644
--- a/crates/liteparse/Cargo.toml
+++ b/crates/liteparse/Cargo.toml
@@ -30,7 +30,7 @@ oar-ocr-webgpu = ["oar-ocr", "oar-ocr/webgpu"]
oar-ocr-openvino = ["oar-ocr", "oar-ocr/openvino"]
tesseract = ["dep:tesseract-rs"]
# Native DOCX structure reading (no LibreOffice, no projection).
-docx-native = ["dep:liteparse-docx"]
+docx-native = ["dep:liteparse-docx", "liteparse-docx/raster"]
[dependencies]
blake3 = "1"
diff --git a/crates/liteparse/src/main.rs b/crates/liteparse/src/main.rs
index 5fd2181..3c5d05e 100644
--- a/crates/liteparse/src/main.rs
+++ b/crates/liteparse/src/main.rs
@@ -190,6 +190,11 @@ struct ScreenshotCommand {
#[arg(long)]
password: Option<String>,
+ /// Disable the native office parser (DOCX renders via LibreOffice
+ /// conversion instead of the native layout raster)
+ #[arg(long)]
+ no_office_native: bool,
+
/// Suppress progress output
#[arg(short, long)]
quiet: bool,
@@ -468,6 +473,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
target_pages: cmd.target_pages.clone(),
dpi: cmd.dpi,
password: cmd.password.clone(),
+ office_native: !cmd.no_office_native,
quiet: cmd.quiet,
..Default::default()
};
diff --git a/crates/liteparse/src/parser.rs b/crates/liteparse/src/parser.rs
index 15f1b11..a696d37 100644
--- a/crates/liteparse/src/parser.rs
+++ b/crates/liteparse/src/parser.rs
@@ -913,6 +913,93 @@ impl LiteParse {
Ok(Some(result))
}
+ /// Native DOCX screenshot: rasterize the layout's own draw commands
+ /// (tiny-skia + skrifa) instead of converting through LibreOffice.
+ ///
+ /// Error contract, relied on by the `screenshot_input` call site:
+ /// `Conversion` means the engine failed and the caller should fall back
+ /// to the conversion path; any other error (bad page range, PNG encode)
+ /// is the caller's and surfaces directly. Panics from the vendored
+ /// engine map to `Conversion` like the parse side.
+ #[cfg(all(feature = "docx-native", not(target_arch = "wasm32")))]
+ fn try_screenshot_docx_native(
+ &self,
+ data: &[u8],
+ page_numbers: Option<&[u32]>,
+ ) -> Result<Vec<ScreenshotResult>, LiteParseError> {
+ std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
+ self.screenshot_docx_native_inner(data, page_numbers)
+ }))
+ .unwrap_or_else(|_| {
+ Err(LiteParseError::Conversion(
+ "native docx layout panicked".to_string(),
+ ))
+ })
+ }
+
+ #[cfg(all(feature = "docx-native", not(target_arch = "wasm32")))]
+ fn screenshot_docx_native_inner(
+ &self,
+ data: &[u8],
+ page_numbers: Option<&[u32]>,
+ ) -> Result<Vec<ScreenshotResult>, LiteParseError> {
+ let parsed = liteparse_docx::docx::parse(data)
+ .map_err(|e| LiteParseError::Conversion(format!("docx parse failed: {e}")))?;
+ let resolved = liteparse_docx::render::resolve::resolve(parsed);
+ let registry = liteparse_docx::render::fonts::FontRegistry::build(
+ &resolved.embedded_fonts,
+ &resolved.font_families,
+ )
+ .map_err(|e| LiteParseError::Conversion(format!("font registry: {e}")))?;
+ let layouted = liteparse_docx::render::layout_document(&resolved, &registry);
+
+ let page_count = layouted.len() as u32;
+ let pages: Vec<u32> = match page_numbers {
+ Some(nums) => nums.to_vec(),
+ None => (1..=page_count).collect(),
+ };
+ let scale = self.config.dpi / 72.0;
+ let mut results = Vec::with_capacity(pages.len());
+ for page_num in pages {
+ if page_num < 1 || page_num > page_count {
+ return Err(LiteParseError::Other(format!(
+ "page {page_num} out of range (document has {page_count} pages)"
+ )));
+ }
+ let raster = liteparse_docx::render::raster::rasterize_page(
+ &layouted[(page_num - 1) as usize],
+ &registry,
+ scale,
+ )
+ .map_err(LiteParseError::Conversion)?;
+ let (w, h) = (raster.width as usize, raster.height as usize);
+ let is_solid_fill = render::is_solid_fill_rgba(&raster.rgba, w, h);
+ // Same skip rule as the PDFium path: a solid-fill page has no
+ // structure to find.
+ let rects = if self.config.detect_screenshot_rects && !is_solid_fill {
+ render::find_solid_rects_rgba(
+ &raster.rgba,
+ w,
+ h,
+ raster.page_width_pt,
+ raster.page_height_pt,
+ )
+ } else {
+ Vec::new()
+ };
+ let png = crate::extract::encode_png(&raster.rgba, raster.width, raster.height)?;
+ results.push(ScreenshotResult {
+ page_num,
+ width: raster.width,
+ height: raster.height,
+ image_bytes: png,
+ is_solid_fill,
+ rects,
+ });
+ }
+ Ok(results)
+ }
+
/// `parse_from_pages`'s native sibling: no projection — the block model
/// arrives from the DOCX source, so markdown is `render_blocks` per page
/// and page text is a straight reading-order join of the text items.
@@ -1035,6 +1122,27 @@ impl LiteParse {
}
};
+ // Native DOCX raster: same layout engine as the native parse, so the
+ // screenshot shares the native TextItem coordinate space — the
+ // LibreOffice raster never did, which is why highlight-on-screenshot
+ // was blocked. Engine failures (`Conversion` errors / panics) fall
+ // back to the conversion path, same net as the parse side; other
+ // errors (bad page range) are the caller's and surface directly.
+ // `render_form_fields` is irrelevant here: the LibreOffice-converted
+ // PDF carries no interactive fields either.
+ #[cfg(all(feature = "docx-native", not(target_arch = "wasm32")))]
+ if self.config.office_native
+ && let Some(bytes) = crate::office::docx_bytes(&input)
+ {
+ match self.try_screenshot_docx_native(&bytes, page_numbers.as_deref()) {
+ Ok(results) => return Ok(results),
+ Err(LiteParseError::Conversion(e)) => log(&format!(
+ "[liteparse] native docx screenshot failed ({e}); falling back to conversion"
+ )),
+ Err(e) => return Err(e),
+ }
+ }
+
let (validated_input, _guard) =
conversion::resolve_pdf_input(input, self.config.password.as_deref(), true).await?;