A fast, helpful, and open-source document parser
revamp pdfium builds
Logan Markewich663ebb78e77f
diff --git a/packages/rust/Cargo.lock b/packages/rust/Cargo.lockindex a5977b8..2c9cb44 100644--- a/packages/rust/Cargo.lock+++ b/packages/rust/Cargo.lock@@ -3,12 +3,12 @@version = 4[[package]]-name = "android_system_properties"-version = "0.1.5"+name = "aho-corasick"+version = "1.1.4"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"+checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"dependencies = [- "libc",+ "memchr",][[package]]@@ -62,10 +62,24 @@ dependencies = [][[package]]-name = "autocfg"-version = "1.5.0"+name = "bindgen"+version = "0.71.1"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"+checksum = "5f58bf3d7db68cfbac37cfc485a8d711e87e064c3d0fe0435b92f7a407f9d6b3"+dependencies = [+ "bitflags",+ "cexpr",+ "clang-sys",+ "itertools",+ "log",+ "prettyplease",+ "proc-macro2",+ "quote",+ "regex",+ "rustc-hash",+ "shlex",+ "syn",+][[package]]name = "bitflags"@@ -74,43 +88,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"[[package]]-name = "bumpalo"-version = "3.20.2"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb"--[[package]]-name = "bytemuck"-version = "1.25.0"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "c8efb64bd706a16a1bdde310ae86b351e4d21550d98d056f22f8a7f7a2183fec"--[[package]]-name = "byteorder"-version = "1.5.0"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"--[[package]]-name = "byteorder-lite"-version = "0.1.0"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"--[[package]]-name = "bytes"-version = "1.11.1"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33"--[[package]]-name = "cc"-version = "1.2.57"+name = "cexpr"+version = "0.6.0"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "7a0dd1ca384932ff3641c8718a02769f1698e7563dc6974ffd03346116310423"+checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"dependencies = [- "find-msvc-tools",- "shlex",+ "nom",][[package]]@@ -120,16 +103,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"[[package]]-name = "chrono"-version = "0.4.44"+name = "clang-sys"+version = "1.8.1"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0"+checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4"dependencies = [- "iana-time-zone",- "js-sys",- "num-traits",- "wasm-bindgen",- "windows-link",+ "glob",+ "libc",+ "libloading",][[package]]@@ -178,32 +159,6 @@ version = "1.0.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"-[[package]]-name = "console_error_panic_hook"-version = "0.1.7"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"-dependencies = [- "cfg-if",- "wasm-bindgen",-]--[[package]]-name = "console_log"-version = "1.0.0"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "be8aed40e4edbf4d3b4431ab260b63fdc40f5780a4766824329ea0f1eefe3c0f"-dependencies = [- "log",- "web-sys",-]--[[package]]-name = "core-foundation-sys"-version = "0.8.7"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"-[[package]]name = "either"version = "1.15.0"@@ -211,34 +166,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"[[package]]-name = "find-msvc-tools"-version = "0.1.9"+name = "glob"+version = "0.3.3"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"--[[package]]-name = "futures-core"-version = "0.3.32"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d"--[[package]]-name = "futures-task"-version = "0.3.32"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393"--[[package]]-name = "futures-util"-version = "0.3.32"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6"-dependencies = [- "futures-core",- "futures-task",- "pin-project-lite",- "slab",-]+checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280"[[package]]name = "heck"@@ -246,44 +177,6 @@ version = "0.5.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"-[[package]]-name = "iana-time-zone"-version = "0.1.65"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470"-dependencies = [- "android_system_properties",- "core-foundation-sys",- "iana-time-zone-haiku",- "js-sys",- "log",- "wasm-bindgen",- "windows-core",-]--[[package]]-name = "iana-time-zone-haiku"-version = "0.1.2"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"-dependencies = [- "cc",-]--[[package]]-name = "image"-version = "0.25.10"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "85ab80394333c02fe689eaf900ab500fbd0c2213da414687ebf995a65d5a6104"-dependencies = [- "bytemuck",- "byteorder-lite",- "moxcms",- "num-traits",- "zune-core",- "zune-jpeg",-]-[[package]]name = "is_terminal_polyfill"version = "1.70.2"@@ -292,9 +185,9 @@ checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"[[package]]name = "itertools"-version = "0.14.0"+version = "0.13.0"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"+checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"dependencies = ["either",]@@ -305,16 +198,6 @@ version = "1.0.18"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"-[[package]]-name = "js-sys"-version = "0.3.91"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "b49715b7073f385ba4bc528e5747d02e66cb39c6146efb66b781f131f0fb399c"-dependencies = [- "once_cell",- "wasm-bindgen",-]-[[package]]name = "libc"version = "0.2.183"@@ -323,9 +206,9 @@ checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d"[[package]]name = "libloading"-version = "0.9.0"+version = "0.8.9"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "754ca22de805bb5744484a5b151a9e1a8e837d5dc232c2d7d8c2e3492edc8b60"+checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"dependencies = ["cfg-if","windows-link",@@ -336,7 +219,7 @@ name = "liteparse-rs"version = "0.1.0"dependencies = ["clap",- "pdfium-render",+ "pdfium","serde","serde_json",]@@ -347,12 +230,6 @@ version = "0.4.29"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"-[[package]]-name = "maybe-owned"-version = "0.3.4"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "4facc753ae494aeb6e3c22f839b158aebd4f9270f55cd3c79906c45476c47ab4"-[[package]]name = "memchr"version = "2.8.0"@@ -360,30 +237,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"[[package]]-name = "moxcms"-version = "0.8.1"+name = "minimal-lexical"+version = "0.2.1"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "bb85c154ba489f01b25c0d36ae69a87e4a1c73a72631fc6c0eb6dde34a73e44b"-dependencies = [- "num-traits",- "pxfm",-]+checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"[[package]]-name = "num-traits"-version = "0.2.19"+name = "nom"+version = "7.1.3"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"+checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"dependencies = [- "autocfg",+ "memchr",+ "minimal-lexical",]-[[package]]-name = "once_cell"-version = "1.21.4"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"-[[package]]name = "once_cell_polyfill"version = "1.70.2"@@ -391,42 +259,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"[[package]]-name = "pdfium-render"-version = "0.8.37"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "6553f6604a52b3203db7b4e9d51eb4dd193cf455af9e56d40cab6575b547b679"+name = "pdfium"+version = "0.1.0"dependencies = [- "bitflags",- "bytemuck",- "bytes",- "chrono",- "console_error_panic_hook",- "console_log",- "image",- "itertools",- "js-sys",- "libloading",- "log",- "maybe-owned",- "once_cell",- "utf16string",- "vecmath",- "wasm-bindgen",- "wasm-bindgen-futures",- "web-sys",+ "pdfium-sys",][[package]]-name = "pin-project-lite"-version = "0.2.17"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"+name = "pdfium-sys"+version = "0.1.0"+dependencies = [+ "bindgen",+][[package]]-name = "piston-float"-version = "1.0.1"+name = "prettyplease"+version = "0.2.37"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "ad78bf43dcf80e8f950c92b84f938a0fc7590b7f6866fbcbeca781609c115590"+checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b"+dependencies = [+ "proc-macro2",+ "syn",+][[package]]name = "proc-macro2"@@ -437,12 +291,6 @@ dependencies = ["unicode-ident",]-[[package]]-name = "pxfm"-version = "0.1.28"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "b5a041e753da8b807c9255f28de81879c78c876392ff2469cde94799b2896b9d"-[[package]]name = "quote"version = "1.0.45"@@ -453,10 +301,39 @@ dependencies = [][[package]]-name = "rustversion"-version = "1.0.22"+name = "regex"+version = "1.12.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276"+dependencies = [+ "aho-corasick",+ "memchr",+ "regex-automata",+ "regex-syntax",+]++[[package]]+name = "regex-automata"+version = "0.4.14"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f"+dependencies = [+ "aho-corasick",+ "memchr",+ "regex-syntax",+]++[[package]]+name = "regex-syntax"+version = "0.8.10"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"++[[package]]+name = "rustc-hash"+version = "2.1.2"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"+checksum = "94300abf3f1ae2e2b8ffb7b58043de3d399c73fa6f4b73826402a5c457614dbe"[[package]]name = "serde"@@ -507,12 +384,6 @@ version = "1.3.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"-[[package]]-name = "slab"-version = "0.4.12"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5"-[[package]]name = "strsim"version = "0.11.1"@@ -536,158 +407,18 @@ version = "1.0.24"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"-[[package]]-name = "utf16string"-version = "0.2.0"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "0b62a1e85e12d5d712bf47a85f426b73d303e2d00a90de5f3004df3596e9d216"-dependencies = [- "byteorder",-]-[[package]]name = "utf8parse"version = "0.2.2"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"-[[package]]-name = "vecmath"-version = "1.0.0"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "956ae1e0d85bca567dee1dcf87fb1ca2e792792f66f87dced8381f99cd91156a"-dependencies = [- "piston-float",-]--[[package]]-name = "wasm-bindgen"-version = "0.2.114"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "6532f9a5c1ece3798cb1c2cfdba640b9b3ba884f5db45973a6f442510a87d38e"-dependencies = [- "cfg-if",- "once_cell",- "rustversion",- "wasm-bindgen-macro",- "wasm-bindgen-shared",-]--[[package]]-name = "wasm-bindgen-futures"-version = "0.4.64"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "e9c5522b3a28661442748e09d40924dfb9ca614b21c00d3fd135720e48b67db8"-dependencies = [- "cfg-if",- "futures-util",- "js-sys",- "once_cell",- "wasm-bindgen",- "web-sys",-]--[[package]]-name = "wasm-bindgen-macro"-version = "0.2.114"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "18a2d50fcf105fb33bb15f00e7a77b772945a2ee45dcf454961fd843e74c18e6"-dependencies = [- "quote",- "wasm-bindgen-macro-support",-]--[[package]]-name = "wasm-bindgen-macro-support"-version = "0.2.114"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "03ce4caeaac547cdf713d280eda22a730824dd11e6b8c3ca9e42247b25c631e3"-dependencies = [- "bumpalo",- "proc-macro2",- "quote",- "syn",- "wasm-bindgen-shared",-]--[[package]]-name = "wasm-bindgen-shared"-version = "0.2.114"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "75a326b8c223ee17883a4251907455a2431acc2791c98c26279376490c378c16"-dependencies = [- "unicode-ident",-]--[[package]]-name = "web-sys"-version = "0.3.91"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "854ba17bb104abfb26ba36da9729addc7ce7f06f5c0f90f3c391f8461cca21f9"-dependencies = [- "js-sys",- "wasm-bindgen",-]--[[package]]-name = "windows-core"-version = "0.62.2"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"-dependencies = [- "windows-implement",- "windows-interface",- "windows-link",- "windows-result",- "windows-strings",-]--[[package]]-name = "windows-implement"-version = "0.60.2"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"-dependencies = [- "proc-macro2",- "quote",- "syn",-]--[[package]]-name = "windows-interface"-version = "0.59.3"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"-dependencies = [- "proc-macro2",- "quote",- "syn",-]-[[package]]name = "windows-link"version = "0.2.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"-[[package]]-name = "windows-result"-version = "0.4.1"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"-dependencies = [- "windows-link",-]--[[package]]-name = "windows-strings"-version = "0.5.1"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"-dependencies = [- "windows-link",-]-[[package]]name = "windows-sys"version = "0.61.2"@@ -702,18 +433,3 @@ name = "zmij"version = "1.0.21"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"--[[package]]-name = "zune-core"-version = "0.5.1"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "cb8a0807f7c01457d0379ba880ba6322660448ddebc890ce29bb64da71fb40f9"--[[package]]-name = "zune-jpeg"-version = "0.5.14"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "0b7a1c0af6e5d8d1363f4994b7a091ccf963d8b694f7da5b0b9cceb82da2c0a6"-dependencies = [- "zune-core",-]diff --git a/packages/rust/Cargo.toml b/packages/rust/Cargo.tomlindex 56e2e8a..7ff6614 100644--- a/packages/rust/Cargo.toml+++ b/packages/rust/Cargo.toml@@ -1,13 +1,7 @@-[package]-name = "liteparse-rs"-version = "0.1.0"-edition = "2024"--[dependencies]-clap = { version = "4.5.55", features = ["derive"] }-pdfium-render = { version = "0.8.37", features = ["static", "libc++", "core_graphics"] }-serde = { version = "1.0.228", features = ["derive"] }-serde_json = "1.0.149"--[env]-PDFIUM_STATIC_LIB_PATH = { value = "vendor/pdfium/release/lib", relative = true }+[workspace]+resolver = "2"+members = [+ "crates/pdfium-sys",+ "crates/pdfium",+ "crates/liteparse",+]diff --git a/packages/rust/crates/liteparse/Cargo.toml b/packages/rust/crates/liteparse/Cargo.tomlnew file mode 100644index 0000000..86dfcdd--- /dev/null+++ b/packages/rust/crates/liteparse/Cargo.toml@@ -0,0 +1,10 @@+[package]+name = "liteparse-rs"+version = "0.1.0"+edition = "2024"++[dependencies]+clap = { version = "4.5.55", features = ["derive"] }+pdfium = { path = "../pdfium" }+serde = { version = "1.0.228", features = ["derive"] }+serde_json = "1.0.149"diff --git a/packages/rust/crates/liteparse/build.rs b/packages/rust/crates/liteparse/build.rsnew file mode 100644index 0000000..32e8221--- /dev/null+++ b/packages/rust/crates/liteparse/build.rs@@ -0,0 +1,16 @@+use std::env;+use std::path::PathBuf;++fn main() {+ // Resolve pdfium dylib path the same way pdfium-sys does+ let lib_path = if let Ok(p) = env::var("PDFIUM_LIB_PATH") {+ PathBuf::from(p)+ } else {+ let manifest = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());+ manifest.join("../../vendor/pdfium/release/lib")+ };++ if let Ok(lib_path) = lib_path.canonicalize() {+ println!("cargo:rustc-link-arg=-Wl,-rpath,{}", lib_path.display());+ }+}diff --git a/packages/rust/crates/liteparse/src/extract.rs b/packages/rust/crates/liteparse/src/extract.rsnew file mode 100644index 0000000..29ed8b1--- /dev/null+++ b/packages/rust/crates/liteparse/src/extract.rs@@ -0,0 +1,137 @@+use pdfium::{Library, TextPage};+use crate::types::{TextItem, Page};++/// 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 lib = Library::init();+ let document = lib.load_document(pdf_path, None)?;+ let page_count = document.page_count();+ let mut pages = Vec::new();++ for page_index in 0..page_count {+ if let Some(target_page) = page_num {+ if page_index as u32 + 1 != target_page {+ continue;+ }+ }++ let page = document.page(page_index)?;+ let text_page = page.text()?;+ let mut text_items = extract_page_text_items(&text_page)?;+ let page_height = page.height();++ // Convert from pdfium bottom-left origin to top-left origin+ for item in &mut text_items {+ item.y = page_height - item.y - item.height;+ }++ pages.push(Page {+ page_number: (page_index + 1) as usize,+ page_width: page.width(),+ page_height,+ text_items,+ });+ }++ Ok(pages)+}++/// 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(())+}++/// Use pdfium's text rect API (FPDFText_CountRects/GetRect/GetBoundedText) to extract+/// text segments. Pdfium auto-merges characters sharing the same baseline and font into+/// rectangular regions — this is the same approach pdfium-render's segments() used.+fn extract_page_text_items(text_page: &TextPage) -> Result<Vec<TextItem>, Box<dyn std::error::Error>> {+ let char_count = text_page.char_count();+ if char_count <= 0 {+ return Ok(Vec::new());+ }++ // Count rects for all characters — this caches the results for subsequent rect() calls+ let rect_count = text_page.count_rects(0, char_count);+ let mut text_items = Vec::new();++ for rect_idx in 0..rect_count {+ let rect = match text_page.rect(rect_idx) {+ Some(r) => r,+ None => continue,+ };++ let seg_text = text_page+ .bounded_text(rect.left, rect.top, rect.right, rect.bottom)+ .replace('\x02', "-");++ if seg_text.trim().is_empty() {+ continue;+ }++ let x = rect.left as f32;+ let y = rect.bottom as f32;+ let width = (rect.right - rect.left) as f32;+ let height = (rect.top - rect.bottom) as f32;++ // Find the first character in this rect to get font info and rotation+ let (font_name, font_size, rotation) = first_char_info_in_rect(text_page, &rect, height);++ text_items.push(TextItem {+ text: seg_text,+ x,+ y,+ width,+ height,+ rotation,+ font_name,+ font_size,+ });+ }++ Ok(text_items)+}++/// Get font name, font size, and rotation angle (degrees) from the first character+/// whose center falls within the given rect.+fn first_char_info_in_rect(+ text_page: &TextPage,+ rect: &pdfium::TextRect,+ fallback_height: f32,+) -> (Option<String>, Option<f32>, f32) {+ let char_count = text_page.char_count();+ for i in 0..char_count {+ let ch = match text_page.char_at(i) {+ Some(c) => c,+ None => continue,+ };++ if let Some(cb) = ch.char_box() {+ let cx = (cb.left + cb.right) / 2.0;+ let cy = (cb.bottom + cb.top) / 2.0;+ if cx >= rect.left && cx <= rect.right && cy >= rect.bottom && cy <= rect.top {+ let fname = ch.font_name();+ let fsize = ch.font_size() as f32;+ let fsize = if fsize > 0.0 { fsize } else { fallback_height };++ // angle() returns radians; convert to degrees+ let angle_rad = ch.angle();+ let mut angle_deg = if angle_rad >= 0.0 {+ angle_rad.to_degrees()+ } else {+ 0.0+ };+ if angle_deg < 0.0 {+ angle_deg += 360.0;+ }++ return (fname, Some(fsize), angle_deg);+ }+ }+ }++ (None, Some(fallback_height), 0.0)+}diff --git a/packages/rust/src/lib.rs b/packages/rust/crates/liteparse/src/lib.rssimilarity index 100%rename from packages/rust/src/lib.rsrename to packages/rust/crates/liteparse/src/lib.rsdiff --git a/packages/rust/src/main.rs b/packages/rust/crates/liteparse/src/main.rssimilarity index 62%rename from packages/rust/src/main.rsrename to packages/rust/crates/liteparse/src/main.rsindex 4cd0ffc..3811ef2 100644--- a/packages/rust/src/main.rs+++ b/packages/rust/crates/liteparse/src/main.rs@@ -38,10 +38,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {extract::extract(&cmd.pdf_path, cmd.page_num)?;}Commands::Parse(cmd) => {+ let t0 = std::time::Instant::now();let pages = extract::extract_pages(&cmd.pdf_path, cmd.page_num)?;+ let t1 = std::time::Instant::now();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)?);+ let t2 = std::time::Instant::now();+ let json = serde_json::to_string(&parsed_pages)?;+ let t3 = std::time::Instant::now();+ println!("{}", json);+ eprintln!(+ "[rust-bin] extract: {:.1}ms, project: {:.1}ms, serialize: {:.1}ms, total: {:.1}ms",+ t1.duration_since(t0).as_secs_f64() * 1000.0,+ t2.duration_since(t1).as_secs_f64() * 1000.0,+ t3.duration_since(t2).as_secs_f64() * 1000.0,+ t3.duration_since(t0).as_secs_f64() * 1000.0,+ );}}diff --git a/packages/rust/src/projection.rs b/packages/rust/crates/liteparse/src/projection.rssimilarity index 94%rename from packages/rust/src/projection.rsrename to packages/rust/crates/liteparse/src/projection.rsindex 0e3d07d..c16edd8 100644--- a/packages/rust/src/projection.rs+++ b/packages/rust/crates/liteparse/src/projection.rs@@ -1,4 +1,3 @@-use std::cmp::Ordering;use std::collections::{BTreeMap, HashMap, HashSet};use crate::types::*;@@ -645,9 +644,56 @@ fn form_lines(i += 1;}+ // Insert blank lines for vertical gaps between lines.+ // Uses the same approach as TS: if the gap between two lines exceeds+ // a reference height, insert proportional blank lines.+ let mut i = 1;+ while i < lines.len() {+ let prev_metrics = representative_line_metrics(&lines[i - 1], median_height);+ let cur_metrics = representative_line_metrics(&lines[i], median_height);+ let y_delta = cur_metrics.0 - prev_metrics.1; // cur_top - prev_bottom+ let reference_height = median_height.max(prev_metrics.2.min(cur_metrics.2));++ if y_delta > reference_height {+ let num_blank = ((y_delta / reference_height).round() as usize).saturating_sub(1);+ let to_insert = num_blank.clamp(1, 10);+ for _ in 0..to_insert {+ lines.insert(i, Vec::new());+ i += 1;+ }+ }+ i += 1;+ }+lines}+/// Returns (top, bottom, height) for representative items in a line,+/// filtering out items much shorter than median height.+fn representative_line_metrics(+ line: &[ProjectedTextItem],+ global_median_height: f32,+) -> (f32, f32, f32) {+ if line.is_empty() {+ return (0.0, 0.0, 0.0);+ }++ let min_representative = global_median_height * 0.5;+ let has_representative = line.iter().any(|b| b.item.height >= min_representative);++ let top = line+ .iter()+ .filter(|b| !has_representative || b.item.height >= min_representative)+ .map(|b| b.item.y)+ .fold(f32::INFINITY, f32::min);+ let bottom = line+ .iter()+ .filter(|b| !has_representative || b.item.height >= min_representative)+ .map(|b| b.item.y + b.item.height)+ .fold(f32::NEG_INFINITY, f32::max);+ (top, bottom, bottom - top)+}+#[derive(Clone, Debug, Default)]struct BoxMeta {left_anchor: Option<i32>,@@ -838,7 +884,13 @@ fn project_to_grid(page: &Page, mut projection_boxes: Vec<ProjectedTextItem>) ->});}- // Step 1b: Compute median distances+ // Step 1b: Round dimensions (matching TS buildBbox Math.round on w/h)+ for item in projection_boxes.iter_mut() {+ item.item.width = item.item.width.round();+ item.item.height = item.item.height.round();+ }++ // Step 1c: Compute median distanceslet (median_width, median_height) = compute_median_textbox_size(&projection_boxes);// Step 1c: Handle reading order rotations@@ -855,36 +907,6 @@ fn project_to_grid(page: &Page, mut projection_boxes: Vec<ProjectedTextItem>) ->.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 extractionlet mut anchor_left: HashMap<i32, Vec<(usize, usize)>> = HashMap::new();let mut anchor_right: HashMap<i32, Vec<(usize, usize)>> = HashMap::new();@@ -958,6 +980,48 @@ fn project_to_grid(page: &Page, mut projection_boxes: Vec<ProjectedTextItem>) ->}}+ // Compute spacing hints between neighboring boxes (after snaps are assigned).+ 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;++ let cur_snap = meta[line_idx][box_idx].snap;+ let prev_snap = meta[line_idx][box_idx - 1].snap;+ let cur_is_left_snap = cur_snap == Some(SnapKind::Left);+ let prev_is_right_snap = prev_snap == Some(SnapKind::Right);+ let both_snapped = cur_snap.is_some() && prev_snap.is_some();+ let force_unsnapped = meta[line_idx][box_idx].force_unsnapped;++ if (!force_unsnapped && x_delta > prev_char_width * 8.0)+ || cur_is_left_snap+ || prev_is_right_snap+ || both_snapped+ {+ 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;+ }+ }+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();diff --git a/packages/rust/src/types.rs b/packages/rust/crates/liteparse/src/types.rssimilarity index 100%rename from packages/rust/src/types.rsrename to packages/rust/crates/liteparse/src/types.rsdiff --git a/packages/rust/crates/pdfium-sys/Cargo.toml b/packages/rust/crates/pdfium-sys/Cargo.tomlnew file mode 100644index 0000000..7521ebd--- /dev/null+++ b/packages/rust/crates/pdfium-sys/Cargo.toml@@ -0,0 +1,8 @@+[package]+name = "pdfium-sys"+version = "0.1.0"+edition = "2024"+links = "pdfium"++[build-dependencies]+bindgen = "0.71"diff --git a/packages/rust/crates/pdfium-sys/build.rs b/packages/rust/crates/pdfium-sys/build.rsnew file mode 100644index 0000000..8c41146--- /dev/null+++ b/packages/rust/crates/pdfium-sys/build.rs@@ -0,0 +1,48 @@+use std::env;+use std::path::PathBuf;++fn main() {+ let lib_path = if let Ok(p) = env::var("PDFIUM_LIB_PATH") {+ PathBuf::from(p)+ } else {+ let manifest = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());+ manifest.join("../../vendor/pdfium/release/lib")+ };++ let include_path = if let Ok(p) = env::var("PDFIUM_INCLUDE_PATH") {+ PathBuf::from(p)+ } else {+ let manifest = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());+ manifest.join("../../vendor/pdfium/release/include")+ };++ let lib_path = lib_path.canonicalize().expect("pdfium lib path does not exist");+ println!("cargo:rustc-link-search=native={}", lib_path.display());+ println!("cargo:rustc-link-lib=dylib=pdfium");++ // Export the lib path so downstream crates can find it+ println!("cargo:lib_path={}", lib_path.display());++ // Run bindgen+ let bindings = bindgen::Builder::default()+ .header("wrapper.h")+ .clang_arg(format!("-I{}", include_path.display()))+ .allowlist_function("FPDF.*")+ .allowlist_function("FPDFText_.*")+ .allowlist_function("FPDFPage.*")+ .allowlist_function("FPDFLink_.*")+ .allowlist_function("FPDFFont_.*")+ .allowlist_type("FPDF.*")+ .allowlist_type("FS_.*")+ .allowlist_var("FPDF.*")+ .derive_debug(true)+ .derive_default(true)+ .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))+ .generate()+ .expect("Unable to generate bindings");++ let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());+ bindings+ .write_to_file(out_path.join("bindings.rs"))+ .expect("Couldn't write bindings!");+}diff --git a/packages/rust/crates/pdfium-sys/src/lib.rs b/packages/rust/crates/pdfium-sys/src/lib.rsnew file mode 100644index 0000000..cd503e4--- /dev/null+++ b/packages/rust/crates/pdfium-sys/src/lib.rs@@ -0,0 +1,6 @@+#![allow(non_upper_case_globals)]+#![allow(non_camel_case_types)]+#![allow(non_snake_case)]+#![allow(dead_code)]++include!(concat!(env!("OUT_DIR"), "/bindings.rs"));diff --git a/packages/rust/crates/pdfium-sys/wrapper.h b/packages/rust/crates/pdfium-sys/wrapper.hnew file mode 100644index 0000000..e35d841--- /dev/null+++ b/packages/rust/crates/pdfium-sys/wrapper.h@@ -0,0 +1,7 @@+#include "fpdfview.h"+#include "fpdf_text.h"+#include "fpdf_edit.h"+#include "fpdf_doc.h"+#include "fpdf_annot.h"+#include "fpdf_structtree.h"+#include "fpdf_transformpage.h"diff --git a/packages/rust/crates/pdfium/Cargo.toml b/packages/rust/crates/pdfium/Cargo.tomlnew file mode 100644index 0000000..d04b7bd--- /dev/null+++ b/packages/rust/crates/pdfium/Cargo.toml@@ -0,0 +1,7 @@+[package]+name = "pdfium"+version = "0.1.0"+edition = "2024"++[dependencies]+pdfium-sys = { path = "../pdfium-sys" }diff --git a/packages/rust/crates/pdfium/src/document.rs b/packages/rust/crates/pdfium/src/document.rsnew file mode 100644index 0000000..a807185--- /dev/null+++ b/packages/rust/crates/pdfium/src/document.rs@@ -0,0 +1,29 @@+use crate::error::PdfiumError;+use crate::page::Page;++pub struct Document {+ pub(crate) handle: pdfium_sys::FPDF_DOCUMENT,+}++impl Document {+ pub fn page_count(&self) -> i32 {+ unsafe { pdfium_sys::FPDF_GetPageCount(self.handle) }+ }++ pub fn page(&self, index: i32) -> Result<Page<'_>, PdfiumError> {+ let handle = unsafe { pdfium_sys::FPDF_LoadPage(self.handle, index) };+ if handle.is_null() {+ return Err(PdfiumError::PageNotFound);+ }+ Ok(Page {+ handle,+ _doc: std::marker::PhantomData,+ })+ }+}++impl Drop for Document {+ fn drop(&mut self) {+ unsafe { pdfium_sys::FPDF_CloseDocument(self.handle) };+ }+}diff --git a/packages/rust/crates/pdfium/src/error.rs b/packages/rust/crates/pdfium/src/error.rsnew file mode 100644index 0000000..bf2fd1f--- /dev/null+++ b/packages/rust/crates/pdfium/src/error.rs@@ -0,0 +1,42 @@+use std::fmt;++#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum PdfiumError {+ Unknown,+ FileNotFound,+ InvalidFormat,+ PasswordRequired,+ UnsupportedSecurity,+ PageNotFound,+ OperationFailed,+}++impl PdfiumError {+ pub(crate) fn from_last_error() -> Self {+ let code = unsafe { pdfium_sys::FPDF_GetLastError() };+ match code as u32 {+ pdfium_sys::FPDF_ERR_FILE => PdfiumError::FileNotFound,+ pdfium_sys::FPDF_ERR_FORMAT => PdfiumError::InvalidFormat,+ pdfium_sys::FPDF_ERR_PASSWORD => PdfiumError::PasswordRequired,+ pdfium_sys::FPDF_ERR_SECURITY => PdfiumError::UnsupportedSecurity,+ pdfium_sys::FPDF_ERR_PAGE => PdfiumError::PageNotFound,+ _ => PdfiumError::Unknown,+ }+ }+}++impl fmt::Display for PdfiumError {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ match self {+ PdfiumError::Unknown => write!(f, "unknown pdfium error"),+ PdfiumError::FileNotFound => write!(f, "file not found"),+ PdfiumError::InvalidFormat => write!(f, "invalid PDF format"),+ PdfiumError::PasswordRequired => write!(f, "password required"),+ PdfiumError::UnsupportedSecurity => write!(f, "unsupported security handler"),+ PdfiumError::PageNotFound => write!(f, "page not found"),+ PdfiumError::OperationFailed => write!(f, "operation failed"),+ }+ }+}++impl std::error::Error for PdfiumError {}diff --git a/packages/rust/crates/pdfium/src/lib.rs b/packages/rust/crates/pdfium/src/lib.rsnew file mode 100644index 0000000..17b5ab0--- /dev/null+++ b/packages/rust/crates/pdfium/src/lib.rs@@ -0,0 +1,13 @@+mod error;+mod types;+mod library;+mod document;+mod page;+mod text_page;++pub use error::PdfiumError;+pub use types::*;+pub use library::Library;+pub use document::Document;+pub use page::Page;+pub use text_page::{TextPage, TextChar, TextCharIter};diff --git a/packages/rust/crates/pdfium/src/library.rs b/packages/rust/crates/pdfium/src/library.rsnew file mode 100644index 0000000..2279a62--- /dev/null+++ b/packages/rust/crates/pdfium/src/library.rs@@ -0,0 +1,64 @@+use std::ffi::CString;+use std::sync::Once;++use crate::document::Document;+use crate::error::PdfiumError;++static INIT: Once = Once::new();++pub struct Library {+ _private: (),+}++impl Library {+ pub fn init() -> Library {+ INIT.call_once(|| {+ unsafe { pdfium_sys::FPDF_InitLibrary() };+ });+ Library { _private: () }+ }++ pub fn load_document(&self, path: &str, password: Option<&str>) -> Result<Document, PdfiumError> {+ let c_path = CString::new(path).map_err(|_| PdfiumError::FileNotFound)?;+ let c_password = password+ .map(|p| CString::new(p).map_err(|_| PdfiumError::OperationFailed))+ .transpose()?;++ let handle = unsafe {+ pdfium_sys::FPDF_LoadDocument(+ c_path.as_ptr(),+ c_password.as_ref().map_or(std::ptr::null(), |p| p.as_ptr()),+ )+ };++ if handle.is_null() {+ return Err(PdfiumError::from_last_error());+ }++ Ok(Document { handle })+ }++ pub fn load_document_from_bytes(&self, data: &[u8], password: Option<&str>) -> Result<Document, PdfiumError> {+ let c_password = password+ .map(|p| CString::new(p).map_err(|_| PdfiumError::OperationFailed))+ .transpose()?;++ let handle = unsafe {+ pdfium_sys::FPDF_LoadMemDocument(+ data.as_ptr() as *const std::ffi::c_void,+ data.len() as i32,+ c_password.as_ref().map_or(std::ptr::null(), |p| p.as_ptr()),+ )+ };++ if handle.is_null() {+ return Err(PdfiumError::from_last_error());+ }++ // SAFETY: pdfium requires the data buffer to outlive the document.+ // The caller must ensure `data` lives long enough. For owned data,+ // consider passing a Vec and having the Document hold it.+ // For now, this is the caller's responsibility.+ Ok(Document { handle })+ }+}diff --git a/packages/rust/crates/pdfium/src/page.rs b/packages/rust/crates/pdfium/src/page.rsnew file mode 100644index 0000000..05dff8d--- /dev/null+++ b/packages/rust/crates/pdfium/src/page.rs@@ -0,0 +1,41 @@+use std::marker::PhantomData;++use crate::document::Document;+use crate::error::PdfiumError;+use crate::text_page::TextPage;++pub struct Page<'doc> {+ pub(crate) handle: pdfium_sys::FPDF_PAGE,+ pub(crate) _doc: PhantomData<&'doc Document>,+}++impl Page<'_> {+ pub fn width(&self) -> f32 {+ unsafe { pdfium_sys::FPDF_GetPageWidthF(self.handle) }+ }++ pub fn height(&self) -> f32 {+ unsafe { pdfium_sys::FPDF_GetPageHeightF(self.handle) }+ }++ pub fn rotation(&self) -> i32 {+ unsafe { pdfium_sys::FPDFPage_GetRotation(self.handle) }+ }++ pub fn text(&self) -> Result<TextPage<'_>, PdfiumError> {+ let handle = unsafe { pdfium_sys::FPDFText_LoadPage(self.handle) };+ if handle.is_null() {+ return Err(PdfiumError::OperationFailed);+ }+ Ok(TextPage {+ handle,+ _page: PhantomData,+ })+ }+}++impl Drop for Page<'_> {+ fn drop(&mut self) {+ unsafe { pdfium_sys::FPDF_ClosePage(self.handle) };+ }+}diff --git a/packages/rust/crates/pdfium/src/text_page.rs b/packages/rust/crates/pdfium/src/text_page.rsnew file mode 100644index 0000000..d964749--- /dev/null+++ b/packages/rust/crates/pdfium/src/text_page.rs@@ -0,0 +1,289 @@+use std::marker::PhantomData;++use crate::page::Page;+use crate::types::{CharBox, Matrix, RectF, TextRect};++pub struct TextPage<'page> {+ pub(crate) handle: pdfium_sys::FPDF_TEXTPAGE,+ pub(crate) _page: PhantomData<&'page Page<'page>>,+}++impl TextPage<'_> {+ pub fn char_count(&self) -> i32 {+ unsafe { pdfium_sys::FPDFText_CountChars(self.handle) }+ }++ pub fn chars(&self) -> TextCharIter<'_> {+ TextCharIter {+ text_page: self,+ index: 0,+ count: self.char_count(),+ }+ }++ pub fn char_at(&self, index: i32) -> Option<TextChar<'_>> {+ if index >= 0 && index < self.char_count() {+ Some(TextChar { text_page: self, index })+ } else {+ None+ }+ }++ /// Count rectangular areas occupied by a text segment.+ /// Must be called before `rect()`.+ pub fn count_rects(&self, start: i32, count: i32) -> i32 {+ unsafe { pdfium_sys::FPDFText_CountRects(self.handle, start, count) }+ }++ /// Get a rectangle from the last `count_rects()` call.+ pub fn rect(&self, index: i32) -> Option<TextRect> {+ let mut left = 0.0;+ let mut top = 0.0;+ let mut right = 0.0;+ let mut bottom = 0.0;+ let ok = unsafe {+ pdfium_sys::FPDFText_GetRect(self.handle, index, &mut left, &mut top, &mut right, &mut bottom)+ };+ if ok != 0 {+ Some(TextRect { left, top, right, bottom })+ } else {+ None+ }+ }++ /// Extract text within a rectangular boundary.+ pub fn bounded_text(&self, left: f64, top: f64, right: f64, bottom: f64) -> String {+ // First call to get required buffer length (in UTF-16 code units, excluding terminator)+ let len = unsafe {+ pdfium_sys::FPDFText_GetBoundedText(+ self.handle,+ left, top, right, bottom,+ std::ptr::null_mut(),+ 0,+ )+ };+ if len <= 0 {+ return String::new();+ }++ // Allocate buffer with space for terminator+ let buf_len = len + 1;+ let mut buf: Vec<u16> = vec![0; buf_len as usize];+ let written = unsafe {+ pdfium_sys::FPDFText_GetBoundedText(+ self.handle,+ left, top, right, bottom,+ buf.as_mut_ptr(),+ buf_len,+ )+ };++ if written <= 0 {+ return String::new();+ }++ // Strip trailing NUL if present+ let text_len = if written > 0 && buf[(written - 1) as usize] == 0 {+ (written - 1) as usize+ } else {+ written as usize+ };++ String::from_utf16_lossy(&buf[..text_len])+ }++ /// Extract text for a range of characters.+ pub fn get_text(&self, start: i32, count: i32) -> String {+ if count <= 0 {+ return String::new();+ }+ let buf_len = count + 1; // +1 for terminator+ let mut buf: Vec<u16> = vec![0; buf_len as usize];+ let written = unsafe {+ pdfium_sys::FPDFText_GetText(self.handle, start, count, buf.as_mut_ptr())+ };+ if written <= 0 {+ return String::new();+ }+ let text_len = if written > 0 && buf[(written - 1) as usize] == 0 {+ (written - 1) as usize+ } else {+ written as usize+ };+ String::from_utf16_lossy(&buf[..text_len])+ }+}++impl Drop for TextPage<'_> {+ fn drop(&mut self) {+ unsafe { pdfium_sys::FPDFText_ClosePage(self.handle) };+ }+}++// -- TextChar: zero-cost view into a TextPage --++pub struct TextChar<'tp> {+ text_page: &'tp TextPage<'tp>,+ pub(crate) index: i32,+}++impl TextChar<'_> {+ pub fn unicode(&self) -> u32 {+ unsafe { pdfium_sys::FPDFText_GetUnicode(self.text_page.handle, self.index) }+ }++ pub fn font_size(&self) -> f64 {+ unsafe { pdfium_sys::FPDFText_GetFontSize(self.text_page.handle, self.index) }+ }++ pub fn font_weight(&self) -> i32 {+ unsafe { pdfium_sys::FPDFText_GetFontWeight(self.text_page.handle, self.index) }+ }++ pub fn font_name(&self) -> Option<String> {+ // First call to get required buffer size+ let len = unsafe {+ pdfium_sys::FPDFText_GetFontInfo(+ self.text_page.handle,+ self.index,+ std::ptr::null_mut(),+ 0,+ std::ptr::null_mut(),+ )+ };+ if len == 0 {+ return None;+ }+ let mut buf: Vec<u8> = vec![0; len as usize];+ let written = unsafe {+ pdfium_sys::FPDFText_GetFontInfo(+ self.text_page.handle,+ self.index,+ buf.as_mut_ptr() as *mut std::ffi::c_void,+ len,+ std::ptr::null_mut(),+ )+ };+ if written == 0 {+ return None;+ }+ // Strip trailing NUL+ let str_len = if written > 0 && buf[(written - 1) as usize] == 0 {+ (written - 1) as usize+ } else {+ written as usize+ };+ Some(String::from_utf8_lossy(&buf[..str_len]).into_owned())+ }++ /// Angle in radians. Returns -1 on error.+ pub fn angle(&self) -> f32 {+ unsafe { pdfium_sys::FPDFText_GetCharAngle(self.text_page.handle, self.index) }+ }++ pub fn char_box(&self) -> Option<CharBox> {+ let mut left = 0.0;+ let mut right = 0.0;+ let mut bottom = 0.0;+ let mut top = 0.0;+ let ok = unsafe {+ pdfium_sys::FPDFText_GetCharBox(+ self.text_page.handle, self.index,+ &mut left, &mut right, &mut bottom, &mut top,+ )+ };+ if ok != 0 {+ Some(CharBox { left, right, bottom, top })+ } else {+ None+ }+ }++ pub fn loose_char_box(&self) -> Option<RectF> {+ let mut rect: pdfium_sys::FS_RECTF = pdfium_sys::FS_RECTF {+ left: 0.0,+ top: 0.0,+ right: 0.0,+ bottom: 0.0,+ };+ let ok = unsafe {+ pdfium_sys::FPDFText_GetLooseCharBox(self.text_page.handle, self.index, &mut rect)+ };+ if ok != 0 {+ Some(RectF {+ left: rect.left,+ top: rect.top,+ right: rect.right,+ bottom: rect.bottom,+ })+ } else {+ None+ }+ }++ pub fn matrix(&self) -> Option<Matrix> {+ let mut m: pdfium_sys::FS_MATRIX = pdfium_sys::FS_MATRIX {+ a: 0.0,+ b: 0.0,+ c: 0.0,+ d: 0.0,+ e: 0.0,+ f: 0.0,+ };+ let ok = unsafe {+ pdfium_sys::FPDFText_GetMatrix(self.text_page.handle, self.index, &mut m)+ };+ if ok != 0 {+ Some(Matrix {+ a: m.a,+ b: m.b,+ c: m.c,+ d: m.d,+ e: m.e,+ f: m.f,+ })+ } else {+ None+ }+ }++ pub fn is_generated(&self) -> bool {+ unsafe { pdfium_sys::FPDFText_IsGenerated(self.text_page.handle, self.index) == 1 }+ }++ pub fn has_unicode_map_error(&self) -> bool {+ unsafe { pdfium_sys::FPDFText_HasUnicodeMapError(self.text_page.handle, self.index) == 1 }+ }+}++// -- TextCharIter --++pub struct TextCharIter<'tp> {+ text_page: &'tp TextPage<'tp>,+ index: i32,+ count: i32,+}++impl<'tp> Iterator for TextCharIter<'tp> {+ type Item = TextChar<'tp>;++ fn next(&mut self) -> Option<Self::Item> {+ if self.index < self.count {+ let ch = TextChar {+ text_page: self.text_page,+ index: self.index,+ };+ self.index += 1;+ Some(ch)+ } else {+ None+ }+ }++ fn size_hint(&self) -> (usize, Option<usize>) {+ let remaining = (self.count - self.index) as usize;+ (remaining, Some(remaining))+ }+}++impl ExactSizeIterator for TextCharIter<'_> {}diff --git a/packages/rust/crates/pdfium/src/types.rs b/packages/rust/crates/pdfium/src/types.rsnew file mode 100644index 0000000..33a9e3b--- /dev/null+++ b/packages/rust/crates/pdfium/src/types.rs@@ -0,0 +1,41 @@+#[derive(Debug, Clone, Copy, Default)]+pub struct RectF {+ pub left: f32,+ pub top: f32,+ pub right: f32,+ pub bottom: f32,+}++#[derive(Debug, Clone, Copy, Default)]+pub struct CharBox {+ pub left: f64,+ pub right: f64,+ pub bottom: f64,+ pub top: f64,+}++#[derive(Debug, Clone, Copy, Default)]+pub struct Matrix {+ pub a: f32,+ pub b: f32,+ pub c: f32,+ pub d: f32,+ pub e: f32,+ pub f: f32,+}++#[derive(Debug, Clone, Copy, Default)]+pub struct Color {+ pub r: u8,+ pub g: u8,+ pub b: u8,+ pub a: u8,+}++#[derive(Debug, Clone, Copy, Default)]+pub struct TextRect {+ pub left: f64,+ pub top: f64,+ pub right: f64,+ pub bottom: f64,+}diff --git a/packages/rust/src/extract.rs b/packages/rust/src/extract.rsdeleted file mode 100644index 3ab975d..0000000--- a/packages/rust/src/extract.rs+++ /dev/null@@ -1,181 +0,0 @@-use pdfium_render::prelude::*;-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;--// If gap is < fontSize * NEGATIVE_X_GAP_THRESHOLD, consider it a negative gap (e.g. kerning) and start a new word-const 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;--/// 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()- );-- let document = pdfium.load_pdf_from_file(pdf_path, None)?;- let mut pages = Vec::new();-- for (page_index, page) in document.pages().iter().enumerate() {- if let Some(target_page) = page_num {- if page_index as u32 + 1 != target_page {- continue;- }- }-- 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)-}--/// 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 {- 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);- }- } 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),- });- }- } 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);- }- }-- Ok(text_items)-}diff --git a/src/core/parser.ts b/src/core/parser.tsindex 2ed7cf2..39c5f31 100644--- a/src/core/parser.ts+++ b/src/core/parser.ts@@ -2,6 +2,7 @@ import pLimit from "p-limit";import {LiteParseConfig,LiteParseInput,+ ParsedPage,ParseResult,ScreenshotResult,TextItem,@@ -160,24 +161,27 @@ export class LiteParse {log(`Loaded PDF with ${doc.numPages} pages`);+ let processedPages: ParsedPage[] = [];try {- let processedPages: import("./types.js").ParsedPage[];-if (this.config.experimental && this.pdfEngine instanceof LiteParseRsEngine) {// Rust pipeline: extract + grid projection in one shot+ const rustStart = performance.now();processedPages = await this.pdfEngine.parseDocument(doc,this.config.maxPages,this.config.targetPages);+ log(`[rust] extract + project: ${(performance.now() - rustStart).toFixed(1)}ms`);} else {// TypeScript pipeline: extract, OCR, then grid projection+ const extractStart = performance.now();const pages = await this.pdfEngine.extractAllPages(doc,this.config.maxPages,this.config.targetPages,{ extractImages: this.config.ocrEnabled });+ log(`[ts] extract: ${(performance.now() - extractStart).toFixed(1)}ms`);// run BEFORE grid projectionif (this.ocrEngine) {@@ -185,35 +189,38 @@ export class LiteParse {}// Process pages with complete grid projection (after OCR)- const processedPages = await projectPagesToGrid(pages, this.config);-- // Build bounding boxes if enabled- if (this.config.preciseBoundingBox) {- for (const page of processedPages) {- page.boundingBoxes = buildBoundingBoxes(page.textItems);- }- }-- // Build final text- const fullText = processedPages.map((p) => p.text).join("\n\n");-- const result: ParseResult = {- pages: processedPages,- text: fullText,- };-- // Format based on output format- switch (this.config.outputFormat) {- case "json":- result.json = JSON.parse(formatJSON(result));- break;- case "text":- // Already in text format- break;+ const projectStart = performance.now();+ processedPages = await projectPagesToGrid(pages, this.config);+ log(`[ts] project: ${(performance.now() - projectStart).toFixed(1)}ms`);+ log(`[ts] extract + project: ${(performance.now() - extractStart).toFixed(1)}ms`);+ }++ // Build bounding boxes if enabled+ if (this.config.preciseBoundingBox) {+ for (const page of processedPages) {+ page.boundingBoxes = buildBoundingBoxes(page.textItems);}+ }- return result;+ // Build final text+ const fullText = processedPages.map((p) => p.text).join("\n\n");++ const result: ParseResult = {+ pages: processedPages,+ text: fullText,+ };++ // Format based on output format+ switch (this.config.outputFormat) {+ case "json":+ result.json = JSON.parse(formatJSON(result));+ break;+ case "text":+ // Already in text format+ break;}++ return result;} finally {// Always release resources, even if processing throwsawait this.pdfEngine.close(doc);diff --git a/src/engines/pdf/liteparse-rs.ts b/src/engines/pdf/liteparse-rs.tsindex cb14f7b..ad29682 100644--- a/src/engines/pdf/liteparse-rs.ts+++ b/src/engines/pdf/liteparse-rs.ts@@ -238,26 +238,19 @@ export class LiteParseRsEngine implements PdfEngine {/** 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,- };- });+ // Rust extraction already converts to top-left origin+ const textItems: TextItem[] = raw.text_items.map((item) => ({+ str: item.text,+ x: item.x,+ y: item.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,@@ -273,24 +266,19 @@ export class LiteParseRsEngine implements PdfEngine {const pageWidth = raw.page_width;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,- };- });+ // Rust extraction already converts to top-left origin+ const textItems: TextItem[] = raw.text_items.map((item) => ({+ str: item.text,+ x: item.x,+ y: item.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,+ }));const images: Image[] = [];