Files
Pictor-public/crates/doc/tests/render.rs
T

62 lines
2.4 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! The core's painter: fixture documents render to what they say (spot colours at known places), and every fixture
//! is written to `target/tmp/render-<name>.png` for a look.
use pictor_doc::{FontBook, fixtures, render};
use std::sync::OnceLock;
pub fn book() -> &'static FontBook {
static B: OnceLock<FontBook> = OnceLock::new();
B.get_or_init(FontBook::system)
}
fn near(a: [u8; 4], b: [u8; 4], tol: i32) -> bool {
a.iter().zip(b).all(|(x, y)| (*x as i32 - y as i32).abs() <= tol)
}
#[test]
fn fixtures_render_and_are_written_for_a_look() {
for (name, doc) in fixtures::all() {
let r = render::page(&doc, &doc.pages[0], book(), 2.0).expect("renders");
assert_eq!(
(r.width, r.height),
((doc.pages[0].width * 2.0).round() as u32, (doc.pages[0].height * 2.0).round() as u32)
);
let png = pictor_doc::format::encode_png(&r).unwrap();
std::fs::write(format!("{}/render-{name}.png", env!("CARGO_TARGET_TMPDIR")), png).unwrap();
}
}
#[test]
fn shapes_land_where_the_document_says() {
let doc = fixtures::shapes();
let r = render::page(&doc, &doc.pages[0], book(), 1.0).unwrap();
// Inside the navy rounded rectangle (20,20 140×90), away from its brass stroke.
assert!(near(r.pixel(90, 65), [0x1f, 0x2a, 0x44, 255], 2), "{:?}", r.pixel(90, 65));
// On its brass stroke (top edge at y = 20).
assert!(near(r.pixel(90, 20), [0xb0, 0x8d, 0x57, 255], 30), "{:?}", r.pixel(90, 20));
// The white paper outside everything.
assert_eq!(r.pixel(390, 5), [255, 255, 255, 255]);
// The group at 50 %: green over white is a light green.
let g = r.pixel(225, 155);
assert!(near(g, [0x9c, 0xe7, 0xbc, 255], 6), "{g:?}");
}
#[test]
fn a_bitmap_layer_at_scale_one_keeps_its_pixels() {
let mut doc = fixtures::bitmap();
doc.pages[0].layers[0].opacity = 1.0;
doc.pages[0].layers.truncate(1);
let r = render::page(&doc, &doc.pages[0], book(), 1.0).unwrap();
assert_eq!(r, doc.assets.values().next().unwrap().clone());
}
#[test]
fn text_is_drawn_in_its_colour() {
let doc = fixtures::text();
let r = render::page(&doc, &doc.pages[0], book(), 2.0).unwrap();
// Somewhere in the title's band there is navy ink.
let navy = (40..140)
.flat_map(|x| (36..90).map(move |y| (x, y)))
.any(|(x, y)| near(r.pixel(x, y), [0x1f, 0x2a, 0x44, 255], 20));
assert!(navy, "no navy title ink");
}