#![cfg(feature = "hayro")]
#![allow(
clippy::unwrap_used,
clippy::indexing_slicing,
clippy::cast_possible_truncation,
clippy::cast_precision_loss,
reason = "tests: a panic is a test failure; pixel counts are small"
)]
use std::sync::Arc;
use laminapdf::{Document, FlattenIntent, FlattenScope, OpenOptions, SaveOptions};
use laminapdf_render::{ByteSource, CancelToken, HayroBackend, RenderBackend, RenderOpenOptions, RenderRequest};
use laminapdf_test_fixtures::fixture;
fn render(bytes: Vec<u8>, page: u32) -> Vec<u8> {
let source: Arc<dyn ByteSource> = Arc::new(bytes);
let doc = HayroBackend::new().open(source, &RenderOpenOptions::new()).unwrap();
let req = RenderRequest::page(page, 1.0);
let plan = doc.plan(&req).unwrap();
let mut out = vec![0; plan.byte_len()];
doc.render(&req, &mut out, &CancelToken::new()).unwrap();
out
}
fn differing(a: &[u8], b: &[u8]) -> f64 {
let pixels = a.len() / 4;
let changed = a
.as_chunks::<4>()
.0
.iter()
.zip(b.as_chunks::<4>().0)
.filter(|(p, q)| p.iter().zip(q.iter()).any(|(x, y)| x.abs_diff(*y) > 2))
.count();
changed as f64 / pixels as f64
}
fn flattened() -> Vec<u8> {
let mut doc = Document::open_bytes(fixture("annotations/flatten.pdf"), OpenOptions::new()).unwrap();
let ids = ["a11.0", "a12.0", "a19.0", "a22.0", "a60.0"]
.map(|i| i.parse().unwrap())
.to_vec();
doc.flatten_annotations(&FlattenScope::Annotations(ids), FlattenIntent::View)
.unwrap();
doc.save(&SaveOptions::new()).unwrap().bytes
}
#[test]
fn each_flattened_page_looks_as_it_did() {
let after = flattened();
for page in 0..2 {
let share = differing(
&render(fixture("annotations/flatten.pdf"), page),
&render(after.clone(), page),
);
assert!(share < 0.001, "page {page}: {share}");
}
}