From b9d157f7e81f28c5ec8fb1fe21b797fa4a416792 Mon Sep 17 00:00:00 2001 From: Fangdun Tsai Date: Fri, 8 Nov 2024 13:20:38 +0800 Subject: [PATCH] feat: add Symbol.dispose method --- Cargo.lock | 456 ---------------------------- Cargo.toml | 14 - crates/pdf-viewer/Cargo.toml | 19 -- crates/pdf-viewer/README.md | 1 - crates/pdf-viewer/src/document.rs | 46 --- crates/pdf-viewer/src/lib.rs | 5 - crates/pdf-viewer/src/viewer.rs | 47 --- packages/pdf-viewer/src/bitmap.ts | 4 + packages/pdf-viewer/src/document.ts | 4 + packages/pdf-viewer/src/page.ts | 4 + packages/pdf-viewer/src/runtime.ts | 1 + packages/pdf-viewer/src/viewer.ts | 4 + packages/pdfium/dist/index.d.ts | 1 + 13 files changed, 18 insertions(+), 588 deletions(-) delete mode 100644 Cargo.lock delete mode 100644 Cargo.toml delete mode 100644 crates/pdf-viewer/Cargo.toml delete mode 100644 crates/pdf-viewer/README.md delete mode 100644 crates/pdf-viewer/src/document.rs delete mode 100644 crates/pdf-viewer/src/lib.rs delete mode 100644 crates/pdf-viewer/src/viewer.rs diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index 064471f..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,456 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - -[[package]] -name = "autocfg" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" - -[[package]] -name = "bitflags" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" - -[[package]] -name = "bumpalo" -version = "3.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" - -[[package]] -name = "bytemuck" -version = "1.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8334215b81e418a0a7bdb8ef0849474f40bb10c8b71f1c4ed315cff49f32494d" - -[[package]] -name = "byteorder" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" - -[[package]] -name = "bytes" -version = "1.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" - -[[package]] -name = "cc" -version = "1.1.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2e7962b54006dcfcc61cb72735f4d89bb97061dd6a7ed882ec6b8ee53714c6f" -dependencies = [ - "shlex", -] - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "chrono" -version = "0.4.38" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" -dependencies = [ - "android-tzdata", - "iana-time-zone", - "js-sys", - "num-traits", - "wasm-bindgen", - "windows-targets", -] - -[[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.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" - -[[package]] -name = "iana-time-zone" -version = "0.1.61" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "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 = "itertools" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" -dependencies = [ - "either", -] - -[[package]] -name = "js-sys" -version = "0.3.72" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" -dependencies = [ - "wasm-bindgen", -] - -[[package]] -name = "libc" -version = "0.2.161" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" - -[[package]] -name = "libloading" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" -dependencies = [ - "cfg-if", - "windows-targets", -] - -[[package]] -name = "log" -version = "0.4.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" - -[[package]] -name = "maybe-owned" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4facc753ae494aeb6e3c22f839b158aebd4f9270f55cd3c79906c45476c47ab4" - -[[package]] -name = "num-traits" -version = "0.2.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" -dependencies = [ - "autocfg", -] - -[[package]] -name = "once_cell" -version = "1.20.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" - -[[package]] -name = "pdf-viewer" -version = "0.1.0" -dependencies = [ - "js-sys", - "pdfium-render", - "wasm-bindgen", -] - -[[package]] -name = "pdfium-render" -version = "0.8.25" -source = "git+https://github.com/fundon/pdfium-render?branch=turn-off-automatic-document-closing#a7808c8163beff07ae9ae09c98ab3012e8605d4f" -dependencies = [ - "bitflags", - "bytemuck", - "bytes", - "chrono", - "console_error_panic_hook", - "console_log", - "itertools", - "js-sys", - "libloading", - "log", - "maybe-owned", - "once_cell", - "utf16string", - "vecmath", - "wasm-bindgen", - "wasm-bindgen-futures", - "web-sys", -] - -[[package]] -name = "piston-float" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad78bf43dcf80e8f950c92b84f938a0fc7590b7f6866fbcbeca781609c115590" - -[[package]] -name = "proc-macro2" -version = "1.0.88" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "sample-wasm-bindgen" -version = "0.1.0" -dependencies = [ - "js-sys", - "pdf-viewer", - "wasm-bindgen", -] - -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "syn" -version = "2.0.79" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "unicode-ident" -version = "1.0.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" - -[[package]] -name = "utf16string" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b62a1e85e12d5d712bf47a85f426b73d303e2d00a90de5f3004df3596e9d216" -dependencies = [ - "byteorder", -] - -[[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.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" -dependencies = [ - "cfg-if", - "once_cell", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" -dependencies = [ - "bumpalo", - "log", - "once_cell", - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-futures" -version = "0.4.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc7ec4f8827a71586374db3e87abdb5a2bb3a15afed140221307c3ec06b1f63b" -dependencies = [ - "cfg-if", - "js-sys", - "wasm-bindgen", - "web-sys", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" - -[[package]] -name = "web-sys" -version = "0.3.72" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112" -dependencies = [ - "js-sys", - "wasm-bindgen", -] - -[[package]] -name = "windows-core" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index cf30a46..0000000 --- a/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[workspace] -members = ["crates/pdf-viewer", "examples/sample-wasm-bindgen"] -resolver = "2" - -[workspace.dependencies] -pdf-viewer = { path = "crates/pdf-viewer" } - -js-sys = "0.3.72" -wasm-bindgen = "0.2.95" - -[profile.release] -codegen-units = 1 -lto = true -opt-level = 3 diff --git a/crates/pdf-viewer/Cargo.toml b/crates/pdf-viewer/Cargo.toml deleted file mode 100644 index 4bdecbe..0000000 --- a/crates/pdf-viewer/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -edition = "2021" -name = "pdf-viewer" -publish = false -version = "0.1.0" - -[lib] -crate-type = ["cdylib", "rlib"] - -[dependencies] - -js-sys = { workspace = true } -wasm-bindgen = { workspace = true } - -pdfium-render = { git = "https://github.com/fundon/pdfium-render", branch = "turn-off-automatic-document-closing", default-features = false, features = [ - "pdfium_latest", - "sync", - "thread_safe", -] } diff --git a/crates/pdf-viewer/README.md b/crates/pdf-viewer/README.md deleted file mode 100644 index 344abf4..0000000 --- a/crates/pdf-viewer/README.md +++ /dev/null @@ -1 +0,0 @@ -We need to maintain the Document Handle instead of being automatically closed after the Rust life cycle. diff --git a/crates/pdf-viewer/src/document.rs b/crates/pdf-viewer/src/document.rs deleted file mode 100644 index 572cf96..0000000 --- a/crates/pdf-viewer/src/document.rs +++ /dev/null @@ -1,46 +0,0 @@ -use std::sync::Arc; - -use pdfium_render::{ - bindgen::FPDF_DOCUMENT, - prelude::{PdfDocument, Pdfium}, -}; -use wasm_bindgen::prelude::wasm_bindgen; - -#[wasm_bindgen] -#[derive(Clone, Debug)] -pub struct Document { - engine: Arc, - inner: FPDF_DOCUMENT, -} - -impl Document { - pub fn new(engine: Arc, inner: FPDF_DOCUMENT) -> Self { - Self { engine, inner } - } -} - -#[wasm_bindgen] -impl Document { - #[wasm_bindgen] - pub fn page_count(&self) -> u16 { - PdfDocument::from_pdfium(self.inner, self.engine.bindings()) - .pages() - .len() - } - - #[wasm_bindgen] - pub fn page( - &self, - width: i32, - height: i32, - // rotation: Option, - ) -> js_sys::Uint8ClampedArray { - PdfDocument::from_pdfium(self.inner, self.engine.bindings()) - .pages() - .first() - .unwrap() - .render(width, height, None) - .map(|bitmap| unsafe { js_sys::Uint8ClampedArray::view(&bitmap.as_rgba_bytes()) }) - .unwrap() - } -} diff --git a/crates/pdf-viewer/src/lib.rs b/crates/pdf-viewer/src/lib.rs deleted file mode 100644 index 4279b7f..0000000 --- a/crates/pdf-viewer/src/lib.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod document; -mod viewer; - -pub use document::*; -pub use viewer::*; diff --git a/crates/pdf-viewer/src/viewer.rs b/crates/pdf-viewer/src/viewer.rs deleted file mode 100644 index 5c587d8..0000000 --- a/crates/pdf-viewer/src/viewer.rs +++ /dev/null @@ -1,47 +0,0 @@ -use js_sys::Uint8Array; -use pdfium_render::prelude::Pdfium; -use std::collections::HashMap; -use std::sync::{Arc, RwLock}; -use wasm_bindgen::prelude::*; - -use crate::Document; - -#[wasm_bindgen] -#[derive(Debug)] -pub struct Viewer { - engine: Arc, - docs: Arc>>, -} - -#[wasm_bindgen] -impl Viewer { - pub fn new() -> Self { - Self { - engine: Arc::new(Pdfium::default()), - docs: Default::default(), - } - } - - pub fn close(&self) {} - - pub fn open( - &self, - id: String, - bytes: Uint8Array, - // password: Option, - ) -> Result { - let doc_ptr = self - .engine - .load_pdf_from_byte_vec(bytes.to_vec(), None) - .map(|doc| doc.handle()) - .map_err(|e| e.to_string())?; - - let mut docs = self.docs.write().map_err(|e| e.to_string())?; - - let doc = Document::new(self.engine.clone(), doc_ptr); - - docs.insert(id, doc.clone()); - - Ok(doc) - } -} diff --git a/packages/pdf-viewer/src/bitmap.ts b/packages/pdf-viewer/src/bitmap.ts index 16cc8f6..bf702f3 100644 --- a/packages/pdf-viewer/src/bitmap.ts +++ b/packages/pdf-viewer/src/bitmap.ts @@ -42,4 +42,8 @@ export class Bitmap { this.runtime.free(bufferPtr); return buffer; } + + [Symbol.dispose]() { + this.close(); + } } diff --git a/packages/pdf-viewer/src/document.ts b/packages/pdf-viewer/src/document.ts index 4104d65..4cb6760 100644 --- a/packages/pdf-viewer/src/document.ts +++ b/packages/pdf-viewer/src/document.ts @@ -124,4 +124,8 @@ export class Document { return new Page(this, at, pagePtr); } + + [Symbol.dispose]() { + this.close(); + } } diff --git a/packages/pdf-viewer/src/page.ts b/packages/pdf-viewer/src/page.ts index 5b72c26..2883ad9 100644 --- a/packages/pdf-viewer/src/page.ts +++ b/packages/pdf-viewer/src/page.ts @@ -186,4 +186,8 @@ export class Page { return [deviceX, deviceY]; } + + [Symbol.dispose]() { + this.close(); + } } diff --git a/packages/pdf-viewer/src/runtime.ts b/packages/pdf-viewer/src/runtime.ts index 68d55cc..02a75f6 100644 --- a/packages/pdf-viewer/src/runtime.ts +++ b/packages/pdf-viewer/src/runtime.ts @@ -77,6 +77,7 @@ export class Runtime { * Initialize the PDFium library and allocate global resources for it. */ init = this.wasm.FPDF_InitLibraryWithConfig; + deinit = this.wasm.FPDF_DestroyLibrary; /** * Gets last error code when a function fails. diff --git a/packages/pdf-viewer/src/viewer.ts b/packages/pdf-viewer/src/viewer.ts index 4dbf16f..c624f7b 100644 --- a/packages/pdf-viewer/src/viewer.ts +++ b/packages/pdf-viewer/src/viewer.ts @@ -72,4 +72,8 @@ export class Viewer { ); return new Bitmap(this.runtime, ptr, format); } + + [Symbol.dispose]() { + this.runtime.deinit(); + } } diff --git a/packages/pdfium/dist/index.d.ts b/packages/pdfium/dist/index.d.ts index d3a64ec..07072f2 100644 --- a/packages/pdfium/dist/index.d.ts +++ b/packages/pdfium/dist/index.d.ts @@ -14,6 +14,7 @@ export interface FPDF_Config { */ export interface FPDF_Bindings { FPDF_InitLibraryWithConfig(config: FPDF_Config): void; + FPDF_DestroyLibrary(): void; FPDF_GetLastError(): T;