PDF support but no open yet cause test fails although there's no apparent issue

This commit is contained in:
Breval Ferrari 2025-03-20 02:17:05 -04:00
parent f9b54bb505
commit c4fcb7bb25
No known key found for this signature in database
GPG key ID: F71E304D6400AB8E
6 changed files with 108 additions and 0 deletions

View file

@ -19,6 +19,22 @@ derive-new = "0"
strum = { version = "0.26", features = ["derive"] }
derive_wrapper = "0.1"
symphonia = { version = "0.5", features = ["all"] }
printpdf = { version = "0.8.1", features = [
"bmp",
"dds",
"gif",
"hdr",
"ico",
"jpeg",
"png",
"pnm",
"rayon",
"tga",
"tiff",
"js-sys",
"webp",
] }
[dev-dependencies]
project-root = "0"
pretty_assertions = "*"

2
bingus/src/doc.rs Normal file
View file

@ -0,0 +1,2 @@
pub use printpdf::PdfDocument;
mod pdf;

87
bingus/src/doc/pdf.rs Normal file
View file

@ -0,0 +1,87 @@
use printpdf::{Op, PdfDocument, PdfPage, PdfParseOptions, PdfSaveOptions};
use crate::{Bendable, IntoDataBytes, TryFromDataBytes};
impl TryFromDataBytes for PdfDocument {
type Error = String;
type Format = ();
fn try_from_data_bytes(
bytes: crate::Bytes,
_format: Self::Format,
_crop: crate::Crop,
) -> Result<Self, Self::Error>
where
Self: Sized,
{
PdfDocument::parse(
&bytes,
&PdfParseOptions {
fail_on_error: false,
},
&mut Vec::new(),
)
}
}
impl IntoDataBytes for PdfDocument {
fn into_data_bytes(self) -> crate::Bytes {
self.save(
&PdfSaveOptions {
optimize: false,
subset_fonts: false,
secure: false,
image_optimization: None,
},
&mut Vec::new(),
)
}
}
impl Bendable for PdfDocument {
type Unit = Op;
fn map<F: Fn(&Self::Unit) -> Self::Unit + Sync>(self, f: F) -> Self {
let PdfDocument {
metadata,
resources,
bookmarks,
pages,
} = self;
PdfDocument {
pages: pages
.into_iter()
.map(|page| PdfPage {
ops: page.ops.into_iter().map(|op| f(&op)).collect::<Vec<Op>>(),
..page
})
.collect(),
metadata,
resources,
bookmarks,
}
}
fn format() -> crate::Format {
crate::Format::Doc
}
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn map_integrity() {
let original = PdfDocument::parse(
include_bytes!("../../../testing material/doc/pdf/basic-text.pdf"),
&PdfParseOptions {
fail_on_error: true,
},
&mut Vec::new(),
)
.unwrap();
assert_eq!(original, original.clone().map(|u| u.clone()))
}
}

View file

@ -1,4 +1,5 @@
pub mod bin;
pub mod doc;
pub mod img;
pub mod snd;
pub mod txt;
@ -20,6 +21,7 @@ mod dynamic {
};
use infer::MatcherType;
use printpdf::PdfDocument;
use strum::EnumDiscriminants;
use thiserror::Error;
@ -30,6 +32,7 @@ mod dynamic {
Binary(Bytes),
Sound(Audio),
Text,
Doc(PdfDocument),
}
#[derive(Debug, Error)]

Binary file not shown.

Binary file not shown.