ajrcarey / pdfium-render

A high-level idiomatic Rust wrapper around Pdfium, the C++ PDF library used by the Google Chromium project.
https://crates.io/crates/pdfium-render
Other
364 stars 59 forks source link

Add image page object support. #30

Closed ajrcarey closed 2 years ago

ajrcarey commented 2 years ago

Fleshed out image object functions in PdfPageImageObject.

The FPDFBitmap_GetBuffer() function is problematic. It returns a pointer to a mutable buffer of pixel data. Mutating that buffer changes the image linked to an image object. When compiling to WASM, the buffer lives in a separate WASM module, so the buffer returned by the WasmPdfiumBindings::FPDFBitmap_GetBuffer() is necessarily a copy (since it must reside in pdfium-render's WASM module, not Pdfium's). Mutating this buffer does nothing since Pdfium never sees it, and there is no way for pdfium-render to know when the caller has finished mutating data, so there is no reliable way for pdfium-render to copy any changes made to the buffer back across to Pdfium's WASM module.

Adjusted definition of FPDFBitmap_GetBuffer() so that it returns *const c_void rather than *mut c_void when compiling to WASM, in order to make the problem more obvious to the caller. (Obviously the caller can recast the pointer to *mut if they really want to, but this forces them to do so.) Added doc comments to the binding to explain the problem. Added new FPDFBitmap_SetBuffer() function that makes it easy for a caller to provide an updated pixel data buffer for a bitmap in a way that works for all target platforms including WASM.

ajrcarey commented 2 years ago

Added bindings for FPDF_VIEWERREF_*() functions. Added example of image object creation in examples/image.rs. Updated documentation. Need to complete WASM bindings before publishing crate version 0.7.8.

ajrcarey commented 2 years ago

Completed WASM bindings. Added tests to confirm setting an image object's image and then retrieving that image object's image returns the same image :) Published crate version 0.7.8.