use std::path::{Path, PathBuf};
use pdf2::Document;
use pdf2_render::{RenderOptions, RenderedPage, render_page_number};
fn corpus(name: &str) -> Vec<u8> {
let path: PathBuf = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../corpus/tier-a")
.join(name);
std::fs::read(&path).unwrap_or_else(|error| {
panic!(
"{}: {error} — restore the corpus with `cargo run -q -p xtask -- corpus`",
path.display()
)
})
}
fn render(name: &str) -> RenderedPage {
let document = Document::open(corpus(name)).expect("open corpus file");
render_page_number(&document, 0, &RenderOptions::default()).expect("render")
}
fn ink(rendered: &RenderedPage) -> usize {
let mut painted = 0;
for y in 0..rendered.canvas.height() {
for x in 0..rendered.canvas.width() {
if rendered.canvas.pixel(x, y) != Some([255, 255, 255, 255]) {
painted += 1;
}
}
}
painted
}
#[test]
fn the_page_that_fills_with_a_tile_paints_it_through_its_key() {
let rendered = render("colorkeymask.pdf");
assert_eq!(
rendered.report.to_string(),
"all operators supported",
"the tile paints: {}",
rendered.report
);
let at = |x: u32, y: u32| rendered.canvas.pixel(x, y).expect("in bounds");
assert_eq!(
at(40, 100),
[255, 255, 255, 255],
"the keyed red band is the page"
);
assert_eq!(at(100, 100), [0, 255, 0, 255], "the green band paints");
assert_eq!(at(170, 100), [0, 0, 255, 255], "the blue band paints");
assert_eq!(
at(5, 400),
[255, 255, 255, 255],
"and nothing leaks past the fill"
);
}
#[test]
fn a_char_procedures_tile_paints_too() {
let rendered = render("ContentStreamCycleType3insideType3.pdf");
assert_eq!(
rendered.report.to_string(),
"all operators supported",
"the char procedure's tile paints: {}",
rendered.report
);
assert!(ink(&rendered) > 5_000, "and the glyph has ink");
}