A fast, helpful, and open-source document parser
fix: calculate rotation with transforms into account
Logan Markewich541b01325480
diff --git a/Cargo.lock b/Cargo.lockindex 6d99854..2524765 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2016,7 +2016,7 @@ checksum = "92daf443525c4cce67b150400bc2316076100ce0b3686209eb8cf3c31612e6f0"[[package]]name = "liteparse"-version = "2.14.3"+version = "2.14.4"dependencies = ["blake3","clap",@@ -2048,7 +2048,7 @@ dependencies = [[[package]]name = "liteparse-napi"-version = "2.14.3"+version = "2.14.4"dependencies = ["image","liteparse",@@ -2083,7 +2083,7 @@ dependencies = [[[package]]name = "liteparse-python"-version = "2.14.3"+version = "2.14.4"dependencies = ["anyhow","clap",@@ -2099,7 +2099,7 @@ dependencies = [[[package]]name = "liteparse-wasm"-version = "2.14.3"+version = "2.14.4"dependencies = ["console_error_panic_hook","js-sys",diff --git a/crates/liteparse-napi/Cargo.toml b/crates/liteparse-napi/Cargo.tomlindex 3fb6499..494d2c0 100644--- a/crates/liteparse-napi/Cargo.toml+++ b/crates/liteparse-napi/Cargo.toml@@ -1,6 +1,6 @@[package]name = "liteparse-napi"-version = "2.14.3"+version = "2.14.4"edition.workspace = truelicense.workspace = truerepository.workspace = true@@ -14,7 +14,7 @@ default = ["tesseract"]tesseract = ["liteparse/tesseract"][dependencies]-liteparse = { package = "liteparse", version = "2.14.3", path = "../liteparse", default-features = false }+liteparse = { package = "liteparse", version = "2.14.4", path = "../liteparse", default-features = false }pdfium-sys = { package = "liteparse-pdfium-sys", version = "1.9.0", path = "../pdfium-sys" }napi = { version = "2", features = ["async", "serde-json", "napi9"] }napi-derive = "2"diff --git a/crates/liteparse-python/Cargo.toml b/crates/liteparse-python/Cargo.tomlindex c94c496..42e5568 100644--- a/crates/liteparse-python/Cargo.toml+++ b/crates/liteparse-python/Cargo.toml@@ -1,6 +1,6 @@[package]name = "liteparse-python"-version = "2.14.3"+version = "2.14.4"edition.workspace = truelicense.workspace = truerepository.workspace = true@@ -15,7 +15,7 @@ default = ["tesseract"]tesseract = ["liteparse/tesseract"][dependencies]-liteparse = { package = "liteparse", version = "2.14.3", path = "../liteparse", default-features = false }+liteparse = { package = "liteparse", version = "2.14.4", path = "../liteparse", default-features = false }pdfium-sys = { package = "liteparse-pdfium-sys", version = "1.9.0", path = "../pdfium-sys" }pdfium = { package = "liteparse-pdfium", version = "1.9.0", path = "../pdfium" }clap = { version = "4.5.55", features = ["derive"] }diff --git a/crates/liteparse-wasm/Cargo.toml b/crates/liteparse-wasm/Cargo.tomlindex 70b683c..50f66eb 100644--- a/crates/liteparse-wasm/Cargo.toml+++ b/crates/liteparse-wasm/Cargo.toml@@ -1,6 +1,6 @@[package]name = "liteparse-wasm"-version = "2.14.3"+version = "2.14.4"edition.workspace = truelicense.workspace = truerepository.workspace = truediff --git a/crates/liteparse/Cargo.toml b/crates/liteparse/Cargo.tomlindex 348a10b..b1d3c71 100644--- a/crates/liteparse/Cargo.toml+++ b/crates/liteparse/Cargo.toml@@ -1,6 +1,6 @@[package]name = "liteparse"-version = "2.14.3"+version = "2.14.4"edition.workspace = truelicense.workspace = truerepository.workspace = truediff --git a/crates/liteparse/src/extract.rs b/crates/liteparse/src/extract.rsindex b314184..c8a4a04 100644--- a/crates/liteparse/src/extract.rs+++ b/crates/liteparse/src/extract.rs@@ -2126,6 +2126,11 @@ fn adjust_angle_for_rotation(angle_rad: f32, page_rotation: i32) -> f32 {_ => {}}a = a.rem_euclid(2.0 * PI);+ // Float error can leave a full turn a hair under 2π (e.g. π + PDFium's+ // 3.1415925), which would surface as 359.99998° instead of 0°.+ if 2.0 * PI - a < 1e-4 {+ a = 0.0;+ }a}@@ -3170,10 +3175,23 @@ impl SegmentBuilder {self.font_weight = meta.font_weight;- // Angle adjusted for page rotation+ // Angle adjusted for page rotation.+ //+ // PDFium's per-char angle comes from the char matrix (Tm × CTM,+ // including horizontal scaling) and ignores the sign of the font+ // size. A negative `Tf` operand scales glyph space by -1 on both+ // axes, i.e. a 180° rotation, so a page that pairs a y-flipped CTM+ // and `-100 Tz` with a negative font size renders perfectly upright+ // while PDFium reports 180°. Fold the font-size sign back in so+ // `rotation` describes what the reader sees.let angle_rad = cv.ch.angle();self.rotation_deg = if angle_rad >= 0.0 {- adjust_angle_for_rotation(angle_rad, page_rotation).to_degrees()+ let visual = if meta.font_size < 0.0 {+ angle_rad + std::f32::consts::PI+ } else {+ angle_rad+ };+ adjust_angle_for_rotation(visual, page_rotation).to_degrees()} else {0.0};diff --git a/crates/liteparse/tests/integration_test.rs b/crates/liteparse/tests/integration_test.rsindex 507ac94..183e399 100644--- a/crates/liteparse/tests/integration_test.rs+++ b/crates/liteparse/tests/integration_test.rs@@ -947,3 +947,38 @@ fn raw_text_items_account_for_every_glyph() {"sample text survives: {text:?}");}++/// A page whose content stream pairs a y-flipped CTM and `-100 Tz` with a+/// negative `Tf` operand renders upright, but PDFium's per-char angle only+/// sees the mirrored char matrix and reports 180°. `rotation` must describe+/// the visual orientation, otherwise callers that "correct" page rotation+/// from it flip an upright page upside down.+#[tokio::test]+async fn test_negative_font_size_is_not_reported_as_rotated() {+ let lit = LiteParse::new(LiteParseConfig {+ ocr_enabled: false,+ ..LiteParseConfig::default()+ });+ let parsed = lit+ .parse("../../integration_tests_data/negative_font_size.pdf")+ .await+ .expect("Should parse PDF");++ let items: Vec<_> = parsed+ .pages+ .iter()+ .flat_map(|p| p.text_items.iter())+ .collect();+ assert!(!items.is_empty(), "expected native text items");+ for item in &items {+ assert!(+ item.rotation.abs() < 1.0,+ "item {:?} reported rotation {} on an upright page",+ item.text,+ item.rotation+ );+ }+ let text = parsed.pages[0].text.as_str();+ assert!(text.contains("Upright text drawn with a negative font size"));+ assert!(text.contains("Second line stays upright too"));+}diff --git a/packages/node/package.json b/packages/node/package.jsonindex 2f92839..b934390 100644--- a/packages/node/package.json+++ b/packages/node/package.json@@ -1,6 +1,6 @@{"name": "@llamaindex/liteparse",- "version": "2.14.3",+ "version": "2.14.4","description": "Fast, lightweight PDF and document parsing with spatial text extraction","type": "module","main": "./dist/lib.cjs",@@ -64,13 +64,13 @@"typescript": "~5.9.2"},"optionalDependencies": {- "@llamaindex/liteparse-darwin-arm64": "2.14.3",- "@llamaindex/liteparse-darwin-x64": "2.14.3",- "@llamaindex/liteparse-linux-arm64-gnu": "2.14.3",- "@llamaindex/liteparse-linux-x64-gnu": "2.14.3",- "@llamaindex/liteparse-win32-arm64-msvc": "2.14.3",- "@llamaindex/liteparse-win32-x64-msvc": "2.14.3",- "@llamaindex/liteparse-linux-x64-musl": "2.14.3"+ "@llamaindex/liteparse-darwin-arm64": "2.14.4",+ "@llamaindex/liteparse-darwin-x64": "2.14.4",+ "@llamaindex/liteparse-linux-arm64-gnu": "2.14.4",+ "@llamaindex/liteparse-linux-x64-gnu": "2.14.4",+ "@llamaindex/liteparse-win32-arm64-msvc": "2.14.4",+ "@llamaindex/liteparse-win32-x64-msvc": "2.14.4",+ "@llamaindex/liteparse-linux-x64-musl": "2.14.4"},"engines": {"node": ">=18.0.0"diff --git a/packages/python/pyproject.toml b/packages/python/pyproject.tomlindex 4adbdc5..5733307 100644--- a/packages/python/pyproject.toml+++ b/packages/python/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "maturin"[project]name = "liteparse"-version = "2.14.3"+version = "2.14.4"description = "Python bindings for LiteParse - fast, lightweight PDF and document parsing"readme = "README.md"license = "Apache-2.0"diff --git a/packages/wasm/package.json b/packages/wasm/package.jsonindex bfc6b88..d08ff40 100644--- a/packages/wasm/package.json+++ b/packages/wasm/package.json@@ -1,6 +1,6 @@{"name": "@llamaindex/liteparse-wasm",- "version": "2.14.3",+ "version": "2.14.4","description": "Fast, lightweight PDF parsing with spatial text extraction — WebAssembly build for browsers","type": "module","main": "./pkg/liteparse_wasm.js",