PDF support but no open yet cause test fails although there's no apparent issue
This commit is contained in:
parent
f9b54bb505
commit
c4fcb7bb25
6 changed files with 108 additions and 0 deletions
2
bingus/src/doc.rs
Normal file
2
bingus/src/doc.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub use printpdf::PdfDocument;
|
||||
mod pdf;
|
87
bingus/src/doc/pdf.rs
Normal file
87
bingus/src/doc/pdf.rs
Normal 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()))
|
||||
}
|
||||
}
|
|
@ -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)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue