use std::path::{Path, PathBuf};
use pdf2::Document;
use pdf2_render::{encode_png, render_page_number, RenderOptions, RenderedPage};
fn repo(relative: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../..")
.join(relative)
}
fn render(page: usize) -> RenderedPage {
let bytes = std::fs::read(repo("tests/fixtures/calibration.pdf")).expect("read fixture");
let document = Document::open(bytes).expect("open calibration.pdf");
render_page_number(&document, page, &RenderOptions::default()).expect("render page")
}
fn pixel(rendered: &RenderedPage, x: u32, y: u32) -> [u8; 3] {
let [r, g, b, a] = rendered.canvas.pixel(x, y).expect("inside the canvas");
assert_eq!(a, 255, "the canvas is opaque");
[r, g, b]
}
const WHITE: [u8; 3] = [255, 255, 255];
const BLACK: [u8; 3] = [0, 0, 0];
const RED: [u8; 3] = [255, 0, 0];
const GREEN: [u8; 3] = [0, 255, 0];
const BLUE: [u8; 3] = [0, 0, 255];
const CYAN: [u8; 3] = [0, 255, 255];
const MAGENTA: [u8; 3] = [255, 0, 255];
const YELLOW: [u8; 3] = [255, 255, 0];
#[track_caller]
fn expect_at(rendered: &RenderedPage, x: u32, y: u32, colour: [u8; 3], what: &str) {
assert_eq!(pixel(rendered, x, y), colour, "{what} at ({x}, {y})");
}
fn ink(rendered: &RenderedPage, x0: u32, y0: u32, x1: u32, y1: u32) -> usize {
let mut count = 0;
for y in y0..y1 {
for x in x0..x1 {
if pixel(rendered, x, y) != WHITE {
count += 1;
}
}
}
count
}
fn ink_runs(rendered: &RenderedPage, x0: u32, y0: u32, x1: u32, y1: u32) -> Vec<(u32, u32)> {
let mut runs: Vec<(u32, u32)> = Vec::new();
for x in x0..x1 {
let inked = (y0..y1).any(|y| pixel(rendered, x, y) != WHITE);
match (inked, runs.last_mut()) {
(true, Some(last)) if last.1 == x => last.1 = x + 1,
(true, _) => runs.push((x, x + 1)),
(false, _) => {}
}
}
runs
}
fn right_edge(rendered: &RenderedPage, x0: u32, y0: u32, x1: u32, y1: u32) -> Option<u32> {
let mut edge = None;
for x in x0..x1 {
for y in y0..y1 {
if pixel(rendered, x, y) != WHITE {
edge = Some(x);
break;
}
}
}
edge
}
#[test]
fn colour_operators_produce_the_arithmetic_byte_values() {
let page = render(0);
assert_eq!((page.canvas.width(), page.canvas.height()), (200, 200));
expect_at(&page, 30, 30, BLACK, "gray 0");
expect_at(&page, 76, 30, [128, 128, 128], "gray 0.5");
expect_at(&page, 122, 30, [64, 64, 64], "gray 0.25");
expect_at(&page, 168, 30, [191, 191, 191], "gray 0.75");
expect_at(&page, 30, 76, RED, "rgb 1 0 0");
expect_at(&page, 76, 76, GREEN, "rgb 0 1 0");
expect_at(&page, 122, 76, BLUE, "rgb 0 0 1");
expect_at(&page, 168, 76, [51, 102, 255], "rgb 0.2 0.4 1");
expect_at(&page, 30, 122, CYAN, "cmyk 1 0 0 0");
expect_at(&page, 76, 122, [128, 128, 128], "cmyk 0 0 0 0.5");
expect_at(&page, 122, 122, RED, "cmyk 0 1 1 0");
expect_at(&page, 168, 122, BLACK, "cmyk 1 1 0 1");
expect_at(&page, 50, 30, WHITE, "gutter between cells");
assert_eq!(page.report.unsupported_total(), 0, "{}", page.report);
}
#[test]
fn cap_styles_extend_by_half_the_line_width_in_their_own_way() {
let page = render(1);
assert_eq!((page.canvas.width(), page.canvas.height()), (300, 300));
expect_at(&page, 139, 40, BLACK, "butt: inside the end");
expect_at(
&page,
140,
40,
WHITE,
"butt: the pixel past x = 140 is empty",
);
expect_at(&page, 143, 70, BLACK, "round: past the end is painted");
expect_at(
&page,
145,
73,
WHITE,
"round: outside its radius, 6.5 px out",
);
expect_at(
&page,
145,
104,
BLACK,
"square: fills the corner at (5.5, 3.5)",
);
expect_at(&page, 146, 100, WHITE, "square: stops at half the width");
}
#[test]
fn joins_are_mitered_or_bevelled() {
let page = render(1);
expect_at(&page, 83, 175, BLACK, "miter fills the corner");
expect_at(&page, 183, 175, WHITE, "bevel cuts the corner");
expect_at(&page, 75, 177, BLACK, "both paint the stroke itself");
expect_at(&page, 175, 177, BLACK, "both paint the stroke itself");
expect_at(&page, 83, 185, BLACK, "the inside of the turn is covered");
}
#[test]
fn the_fill_rule_decides_whether_the_inner_subpath_is_a_hole() {
let page = render(1);
expect_at(&page, 50, 250, RED, "nonzero keeps the inner rectangle");
expect_at(&page, 30, 250, RED, "and the ring around it");
expect_at(
&page,
170,
250,
WHITE,
"even-odd punches the inner rectangle",
);
expect_at(&page, 150, 250, BLUE, "the ring is still painted");
}
#[test]
fn a_dash_phase_starts_the_pattern_partway_through_a_dash() {
let page = render(1);
expect_at(&page, 125, 140, MAGENTA, "inside the first, shortened dash");
expect_at(&page, 140, 140, WHITE, "inside the first gap");
expect_at(&page, 150, 140, MAGENTA, "inside the second, full dash");
expect_at(&page, 170, 140, WHITE, "inside the second gap");
}
#[test]
fn a_bezier_circle_covers_about_pi_r_squared() {
let page = render(1);
expect_at(&page, 250, 70, [0, 153, 0], "the centre is filled");
expect_at(&page, 250, 34, WHITE, "outside the top of the circle");
let area = ink(&page, 215, 35, 285, 105);
let expected = std::f64::consts::PI * 30.0 * 30.0;
let ratio = area as f64 / expected;
assert!(
(0.96..=1.04).contains(&ratio),
"ink {area} vs pi r^2 {expected:.0} (ratio {ratio:.3})"
);
}
#[test]
fn an_image_at_integer_scale_keeps_exact_block_edges() {
let page = render(2);
assert_eq!((page.canvas.width(), page.canvas.height()), (400, 300));
expect_at(&page, 50, 180, RED, "top-left block");
expect_at(&page, 110, 180, GREEN, "top-right block");
expect_at(&page, 50, 260, BLUE, "bottom-left block");
expect_at(&page, 110, 260, WHITE, "bottom-right block");
expect_at(&page, 79, 180, RED, "last pixel before the vertical edge");
expect_at(&page, 81, 180, GREEN, "first pixel after the vertical edge");
expect_at(&page, 50, 219, RED, "last pixel before the horizontal edge");
expect_at(
&page,
50,
221,
BLUE,
"first pixel after the horizontal edge",
);
}
#[test]
fn decode_inverts_and_indexed_picks_palette_entries() {
let page = render(2);
expect_at(&page, 50, 50, CYAN, "inverted red");
expect_at(&page, 110, 50, MAGENTA, "inverted green");
expect_at(&page, 50, 110, YELLOW, "inverted blue");
expect_at(&page, 110, 110, BLACK, "inverted white");
expect_at(&page, 175, 250, BLACK, "gray image, first sample");
expect_at(&page, 205, 250, WHITE, "gray image, second sample");
expect_at(&page, 255, 250, RED, "indexed 0 is red");
expect_at(&page, 285, 250, BLUE, "indexed 1 is blue");
expect_at(&page, 235, 250, WHITE, "outside the indexed image");
}
#[test]
fn a_quarter_turn_matrix_rotates_the_image_without_mirroring_it() {
let page = render(2);
expect_at(&page, 190, 110, RED, "red after a quarter turn");
expect_at(&page, 190, 50, GREEN, "green after a quarter turn");
expect_at(&page, 250, 110, BLUE, "blue after a quarter turn");
expect_at(&page, 250, 50, WHITE, "white after a quarter turn");
expect_at(&page, 161, 110, RED, "the drawn square starts at x = 160");
expect_at(&page, 158, 110, WHITE, "and nothing is painted to its left");
}
#[test]
fn a_media_box_origin_that_is_not_zero_shifts_the_content() {
let page = render(3);
assert_eq!((page.canvas.width(), page.canvas.height()), (200, 200));
expect_at(&page, 10, 190, RED, "the bottom-left rectangle");
expect_at(&page, 190, 10, BLUE, "the top-right rectangle");
expect_at(&page, 100, 100, WHITE, "the space between them");
expect_at(&page, 0, 0, WHITE, "nothing leaks into the corner");
}
#[test]
fn rotate_90_swaps_the_canvas_and_turns_the_page_clockwise() {
let page = render(4);
assert_eq!((page.canvas.width(), page.canvas.height()), (100, 300));
expect_at(&page, 50, 50, RED, "the rectangle at the user origin");
expect_at(&page, 50, 250, BLUE, "the rectangle at user x = 200");
expect_at(&page, 50, 150, WHITE, "the gap between them");
expect_at(&page, 5, 5, RED, "the turn reaches the corner");
expect_at(&page, 95, 295, BLUE, "and the far corner");
}
#[test]
fn horizontal_scaling_and_word_spacing_change_the_run_by_the_factor() {
let page = render(5);
assert_eq!((page.canvas.width(), page.canvas.height()), (300, 200));
let full = right_edge(&page, 0, 12, 300, 55).expect("the 100% run");
let half = right_edge(&page, 0, 86, 300, 122).expect("the 50% run");
let full_width = f64::from(full) - 20.0;
let half_width = f64::from(half) - 20.0;
assert!(
(0.47..=0.53).contains(&(half_width / full_width)),
"50% run is {half_width} wide against {full_width} at 100%"
);
let tight = right_edge(&page, 0, 122, 300, 145).expect("the run without word spacing");
let spaced = right_edge(&page, 0, 162, 300, 185).expect("the run with word spacing");
let growth = f64::from(spaced) - f64::from(tight);
assert!(
(76.0..=84.0).contains(&growth),
"word spacing moved the right edge by {growth}, expected 2 x 40"
);
}
#[test]
fn widths_from_the_font_dictionary_set_the_advance() {
let page = render(6);
assert_eq!((page.canvas.width(), page.canvas.height()), (300, 200));
for (band, expected) in [
((40, 55), (333.0 + 250.0) / 1000.0 * 20.0),
((90, 105), (333.0 + 750.0) / 1000.0 * 20.0),
((140, 155), (900.0 + 900.0) / 1000.0 * 20.0),
] {
let runs = ink_runs(&page, 0, band.0, 300, band.1);
assert_eq!(runs.len(), 2, "two glyphs in band {band:?}: {runs:?}");
let advance = f64::from(runs[1].0) - f64::from(runs[0].0);
assert!(
(advance - expected).abs() <= 1.5,
"band {band:?}: the second glyph starts {advance} px after the first, expected {expected}"
);
}
}
#[test]
fn a_type_0_font_is_reported_instead_of_painted_with_fallback_glyphs() {
let page = render(7);
assert_eq!(
page.report.unsupported.get("font:Type0"),
Some(&1),
"{:?}",
page.report
);
assert_eq!(
ink(&page, 0, 0, 300, 200),
0,
"nothing is painted for a font the renderer cannot draw"
);
}
#[test]
fn every_calibration_page_uses_only_supported_operators() {
for page in 0..6 {
let rendered = render(page);
assert_eq!(
rendered.report.unsupported_total(),
0,
"page {}: {}",
page + 1,
rendered.report
);
}
}
#[test]
fn the_committed_goldens_match_the_renderer() {
for page in 0..8 {
let rendered = render(page);
let png = encode_png(&rendered.canvas);
let golden_path = repo(&format!(
"tests/fixtures/golden/calibration-{}.png",
page + 1
));
if std::env::var_os("BLESS").is_some() {
std::fs::write(&golden_path, &png).expect("bless golden");
continue;
}
let golden = std::fs::read(&golden_path).expect("read golden");
assert!(
golden == png,
"page {} differs from its golden (rendered {} bytes, golden {} bytes); \
re-run with BLESS=1 if the change is intended",
page + 1,
png.len(),
golden.len()
);
}
}