133 lines
5.6 KiB
Rust
133 lines
5.6 KiB
Rust
//! Day 3 mandatory test 3 (the PDF half): a PDF export keeps its text extractable in reading order and every page at
|
|
//! its exact size; pdfium draws it like the core's own render. Also PNG at a scale, JPG and SVG.
|
|
use pictor_doc::{Document, FontBook, export, fixtures, render};
|
|
use std::path::PathBuf;
|
|
use std::sync::OnceLock;
|
|
|
|
fn book() -> &'static FontBook {
|
|
static B: OnceLock<FontBook> = OnceLock::new();
|
|
B.get_or_init(FontBook::system)
|
|
}
|
|
|
|
fn engine() -> &'static pictor_pdf::Engine {
|
|
let dir = std::env::var_os("PDFIUM_DYNAMIC_LIB_PATH")
|
|
.map(PathBuf::from)
|
|
.unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../third_party/pdfium/bin"));
|
|
pictor_pdf::Engine::load(Some(&dir)).expect("pdfium: run scripts/fetch-pdfium.ps1 first")
|
|
}
|
|
|
|
/// An A4 page with the text fixture's boxes on it, plus a second page of another size.
|
|
fn a4_with_text() -> Document {
|
|
let mut d = fixtures::text();
|
|
d.pages[0].width = pictor_doc::A4.0;
|
|
d.pages[0].height = pictor_doc::A4.1;
|
|
let mut second = fixtures::shapes().pages.remove(0);
|
|
second.id = d.new_id();
|
|
for l in &mut second.layers {
|
|
l.id = d.new_id();
|
|
if let pictor_doc::LayerKind::Vector { objects } = &mut l.kind {
|
|
for o in objects {
|
|
o.id = d.new_id();
|
|
}
|
|
}
|
|
}
|
|
d.pages.push(second);
|
|
d
|
|
}
|
|
|
|
fn squash(s: &str) -> String {
|
|
s.split_whitespace().collect::<Vec<_>>().join(" ")
|
|
}
|
|
|
|
#[test]
|
|
fn pdf_text_is_extractable_in_order_and_pages_are_exact() {
|
|
let doc = a4_with_text();
|
|
let pdf = export::pdf(&doc, book()).unwrap();
|
|
std::fs::write(format!("{}/export-text.pdf", env!("CARGO_TARGET_TMPDIR")), &pdf).unwrap();
|
|
let sizes = pictor_pdf::page_sizes(engine(), &pdf, None).unwrap();
|
|
assert_eq!(sizes.len(), 2);
|
|
assert!((sizes[0].0 - 595.276).abs() < 0.01 && (sizes[0].1 - 841.89).abs() < 0.01, "A4 is {sizes:?}");
|
|
assert!((sizes[1].0 - 400.0).abs() < 0.01 && (sizes[1].1 - 300.0).abs() < 0.01, "{sizes:?}");
|
|
let text = pictor_pdf::extract_text(engine(), &pdf, None).unwrap();
|
|
let t = squash(&text[0]);
|
|
let mut at = 0;
|
|
for w in [
|
|
"Monika Nováková",
|
|
"Pictor lays text out in the core: shaping,",
|
|
"line breaks and alignment, so the screen",
|
|
"Příliš žluťoučký kůň",
|
|
"úpěl ďábelské ódy.",
|
|
"Centred",
|
|
"in two lines",
|
|
"Right, italic",
|
|
] {
|
|
match t[at..].find(w) {
|
|
Some(i) => at += i + w.len(),
|
|
None => panic!("{w:?} missing or out of order in:\n{t}"),
|
|
}
|
|
}
|
|
// Letter-spaced capitals: pdfium may read the tracking as spaces; the letters are all there, in order.
|
|
assert!(t.replace(' ', "").contains("PROJEKTOVÁMANAŽERKA"), "{t}");
|
|
// The fonts are embedded subsets.
|
|
let raw = String::from_utf8_lossy(&pdf);
|
|
assert!(raw.contains("/FontFile2") || raw.contains("/FontFile3"), "no embedded font program");
|
|
assert!(raw.contains("/ToUnicode"));
|
|
}
|
|
|
|
fn diff(a: &image::RgbaImage, b: &pictor_doc::Raster) -> f64 {
|
|
assert_eq!((a.width(), a.height()), (b.width, b.height));
|
|
let mut sum = 0u64;
|
|
for (p, q) in a.as_raw().as_chunks::<4>().0.iter().zip(b.rgba.as_chunks::<4>().0) {
|
|
// The core's pixels over white paper, as pdfium draws them.
|
|
let a = q[3] as i32;
|
|
for i in 0..3 {
|
|
let over_white = (q[i] as i32 * a + 255 * (255 - a)) / 255;
|
|
sum += (p[i] as i32 - over_white).unsigned_abs() as u64;
|
|
}
|
|
}
|
|
sum as f64 / (a.width() as f64 * a.height() as f64 * 3.0)
|
|
}
|
|
|
|
#[test]
|
|
fn pdfium_draws_the_pdf_like_the_core_does() {
|
|
for (name, doc) in fixtures::all() {
|
|
let pdf = export::pdf(&doc, book()).unwrap();
|
|
std::fs::write(format!("{}/export-{name}.pdf", env!("CARGO_TARGET_TMPDIR")), &pdf).unwrap();
|
|
let page = &doc.pages[0];
|
|
let scale = 2.0;
|
|
let core = render::page(&doc, page, book(), scale).unwrap();
|
|
let theirs = pictor_pdf::render_page(engine(), &pdf, None, 0, core.width).unwrap().into_rgba8();
|
|
let theirs = image::imageops::resize(&theirs, core.width, core.height, image::imageops::FilterType::Triangle);
|
|
let d = diff(&theirs, &core);
|
|
theirs.save(format!("{}/export-{name}-pdfium.png", env!("CARGO_TARGET_TMPDIR"))).unwrap();
|
|
// Anti-aliasing and image filtering differ; a misplaced shape or a missing glyph run costs far more.
|
|
assert!(d < 3.0, "{name}: mean difference {d:.2} per channel");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn png_jpg_and_svg() {
|
|
let doc = fixtures::shapes();
|
|
let page = &doc.pages[0];
|
|
let png = export::png(&doc, page, book(), 3.0).unwrap();
|
|
let img = image::load_from_memory(&png).unwrap();
|
|
assert_eq!((img.width(), img.height()), (1200, 900));
|
|
let jpg = export::jpg(&doc, page, book(), 1.0, 90).unwrap();
|
|
let img = image::load_from_memory(&jpg).unwrap();
|
|
assert_eq!((img.width(), img.height()), (400, 300));
|
|
for (name, doc) in fixtures::all() {
|
|
let svg = export::svg(&doc, &doc.pages[0], book());
|
|
std::fs::write(format!("{}/export-{name}.svg", env!("CARGO_TARGET_TMPDIR")), &svg).unwrap();
|
|
let tree = usvg::Tree::from_str(&svg, &usvg::Options::default()).unwrap_or_else(|e| panic!("{name}: {e}"));
|
|
assert!(tree.root().has_children(), "{name}: empty SVG");
|
|
}
|
|
let svg = export::svg(&fixtures::text(), &fixtures::text().pages[0], book());
|
|
assert!(svg.contains(">Monika Nováková</text>"), "text is not text in the SVG");
|
|
}
|
|
|
|
#[test]
|
|
fn a_huge_png_is_refused() {
|
|
let doc = fixtures::shapes();
|
|
assert!(matches!(export::png(&doc, &doc.pages[0], book(), 100.0), Err(export::ExportError::TooLarge)));
|
|
}
|