PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
test: verify conservative review fixes across rendering and automation
FredApps7fe33d1cbd1a
diff --git a/crates/automation/tests/it/automation.rs b/crates/automation/tests/it/automation.rsindex abfd441d3..6a5765b91 100644--- a/crates/automation/tests/it/automation.rs+++ b/crates/automation/tests/it/automation.rs@@ -9780,24 +9780,21 @@ fn added_text_lines_take_their_own_alignments() {assert!(a.call("content_update", &json!({"doc":id,"page":1,"index":1,"aligns":"right"})).is_err());}-/// `aligns` alone on an existing paragraph aligns its rows by index, as the app does: the rows-/// stay rows instead of being rewrapped into one, where only the first entry would count.+/// Existing-row alignment is withheld until its intent survives later editing sessions.#[test]-fn text_edit_aligns_alone_aligns_the_paragraphs_rows() {+fn text_edit_row_alignment_is_refused_without_changing_existing_text() {let dir = workdir("paragraph-row-aligns");let body = "BT /F1 12 Tf 14.4 TL 1 0 0 1 72 700 Tm (The quick brown fox jumps over the lazy dog) Tj T* (and keeps running.) Tj ET";std::fs::write(dir.join("rows.pdf"), win_ansi_paragraph_pdf(body)).unwrap();let mut a = auto(&dir);let doc = ok(&mut a, "doc_open", json!({ "path": "rows.pdf" }))["doc"].as_u64().unwrap();- ok(&mut a, "text_edit", json!({ "doc": doc, "page": 1, "paragraph": 1, "aligns": [null, "right"] }));- let lines = ok(&mut a, "text_lines", json!({ "doc": doc, "page": 1 }));- let rect = |t: &str| -> Vec<f64> {- let line = lines["lines"].as_array().unwrap().iter().find(|l| l["text"] == t).unwrap_or_else(|| panic!("{t:?} is a row: {lines}"));- line["rect"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap()).collect()- };- let (first, second) = (rect("The quick brown fox jumps over the lazy dog"), rect("and keeps running."));- assert!((first[0] - 72.0).abs() < 0.5, "the first row stays at the left: {first:?}");- assert!((second[2] - first[2]).abs() < 1.0 && second[0] > 100.0, "the second row is right-aligned: {second:?} vs {first:?}");+ let before = ok(&mut a, "text_lines", json!({ "doc": doc, "page": 1 }));+ let refused = err(&mut a, "text_edit", json!({ "doc": doc, "page": 1, "paragraph": 1, "aligns": [null, "right"] }));+ assert!(matches!(&refused, ToolError::Failed(m) | ToolError::InvalidArgs(m) if m.contains("selected-row alignment")), "{refused:?}");+ assert_eq!(ok(&mut a, "text_lines", json!({ "doc": doc, "page": 1 })), before);+ ok(&mut a, "doc_save", json!({ "doc": doc, "path": "unchanged.pdf" }));+ let reopened = ok(&mut a, "doc_open", json!({ "path": "unchanged.pdf" }))["doc"].as_u64().unwrap();+ assert_eq!(ok(&mut a, "text_lines", json!({ "doc": reopened, "page": 1 })), before);}/// An editable combo box (ISO 32000-2 Table 233, Edit) takes a value that isn't one of itsdiff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rsindex 5db12eda0..833fa16b9 100644--- a/crates/engine/src/tests.rs+++ b/crates/engine/src/tests.rs@@ -9304,7 +9304,14 @@ fn repeated_backdrop_artwork_survives_background_undo_redo_and_both_save_modes()let mut s = Session::new();let id = s.open("shared-backdrop.pdf", None, Arc::new(bytes), None).unwrap();let black = |s: &Session, id| {- let rgba = pixel(s, id, 0, 30, 70);+ let doc = s.get(id).unwrap();+ let mut renderer = pdfcraft_render::PageRenderer::new(doc.bytes.clone(), doc.config.clone());+ let out = renderer.render(pdfcraft_render::RenderRequest { page: 0, scale: 1.0, ..Default::default() });+ assert!(out.error.is_none(), "{:?}", out.error);+ assert_eq!((out.width, out.height), (100, 100));+ // PDF point (30, 30), inside the later black square, in top-down raster coordinates.+ let i = ((70 * out.width + 30) * 4) as usize;+ let rgba = &out.rgba[i..i + 4];assert!(rgba[0] < 20 && rgba[1] < 20 && rgba[2] < 20, "later artwork survives: {rgba:?}");};black(&s, id);diff --git a/crates/optimize/tests/optimize.rs b/crates/optimize/tests/optimize.rsindex 0f2f1e1f4..00b2dc2db 100644--- a/crates/optimize/tests/optimize.rs+++ b/crates/optimize/tests/optimize.rs@@ -1450,16 +1450,11 @@ fn black_and_white_rgb_in_resources_its_page_tree_and_a_form_share_becomes_grey(}}-/// Black-and-white images above 450 ppi are averaged down to 300 ppi, by default: 1-bit Flate, CCITT-/// Group 4 (alone and behind ASCIIHexDecode), an image mask (its inverting decode array stays), and-/// eight-bit grey and RGB of only black and white (the RGB also the other way round, with a decode-/// array that inverts it, so its ties are 1s), all drawn at 600 ppi, are stored at half the size (as-/// Group 4, or 1-bit Flate when that is smaller, as it is on this clean text), each new pixel the-/// majority of the 2 × 2 it covers (a tie black, or for the mask painted). Left at their size: an image at 400 ppi (stored-/// again as Group 4), a Group 4 one at 400 ppi (not even decoded), one behind ASCIIHexDecode at-/// 400 ppi (stored again as Group 4 alone), and one with a soft mask. The page looks the same at-/// screen size. With `downsample` off nothing is resampled; with Retain, Flate stays Flate, CCITT-/// stays CCITT, and eight-bit grey and RGB are left to the grey and colour settings.+/// Dense local mixed-sample patches are retained conservatively by the monochrome settings.+/// One-bit Flate, CCITT and masks preserve samples; eight-bit bilevel inputs may be packed+/// losslessly. Disable the independent gray/color reduction for the exact-render comparison:+/// those settings can otherwise choose smaller grayscale/JPEG output for eight-bit sources.+/// The Retain case separately checks that gray/color reduction remains available.#[test]fn ambiguous_monochrome_text_is_recompressed_without_lossy_reduction() {let mut doc = Document::new_empty();@@ -1576,7 +1571,7 @@ fn ambiguous_monochrome_text_is_recompressed_without_lossy_reduction() {let untouched = [fax_at_400].map(|r| raw(&doc, r));let hex_len = raw(&doc, hex_at_400).len();- let r = optimize(&mut doc, &Settings::default()).unwrap();+ let r = optimize(&mut doc, &Settings { gray: keep, color: keep, ..Settings::default() }).unwrap();assert_eq!((r.images_resampled, r.images_recompressed), (0, 9), "{r:?}");assert_eq!(stream(&doc, rgb).dict.get(b"ColorSpace"), Some(&Object::name("DeviceGray")));for i in [flate, fax, mask, eight, hex_fax, rgb] {@@ -1607,9 +1602,8 @@ fn ambiguous_monochrome_text_is_recompressed_without_lossy_reduction() {/// Monochrome downsampling leaves a halftone or a dither at its resolution, as most new pixels/// would cover both black and white and taking the sample covering more would turn its tones/// solid: a 50% checkerboard, a 25% ordered dither and an error-diffused one, each at 600 ppi.-/// Text at 600 ppi, which covers both only along its edges, is averaged down. Rows no wider than-/// a fax row (`MAX_CCITT_COLUMNS`) are averaged down too, but not one column wider: the averaging-/// keeps a counter for each column.+/// Dense ragged text is retained too. Rows wider than a fax row (`MAX_CCITT_COLUMNS`)+/// are never averaged: the averaging keeps a counter for each column.#[test]fn monochrome_downsampling_leaves_halftones_and_overwide_rows_at_their_resolution() {let (w, h) = (1200u32, 480u32);@@ -1721,8 +1715,8 @@ fn g4_image(doc: &mut Document, bits: &[u8], (w, h): (u32, u32), hex: bool) -> O/// What Monochrome Images leaves alone and what it may still do. A Group 4 halftone above 450 ppi/// isn't resampled, so it stays byte for byte: alone, by default; behind ASCIIHexDecode, with-/// Retain (by default it is stored again as Group 4 alone). Under Retain a resampled Flate scan-/// stays Flate though Group 4 would be smaller. A busy Group 4 image behind ASCIIHexDecode, whose+/// Retain (by default it is stored again as Group 4 alone). Under Retain an ambiguous Flate scan+/// keeps its bytes and resolution though Group 4 would be smaller. A busy Group 4 image behind ASCIIHexDecode, whose/// code is more than three times its raster, decodes within a fax code's bound and is stored/// again as Group 4 alone. Sixteen-bit grey of only 0x0000 and 0xFFFF isn't eight-bit black and/// white, and keeps its bytes.@@ -1789,7 +1783,8 @@ fn monochrome_images_keep_what_they_should() {let r = optimize(&mut doc, &Settings { mono: RETAIN, ..Settings::default() }).unwrap();assert_eq!(raw(&doc, wrapped), before, "Retain: a wrapped Group 4 halftone stays as it is");let s = stream(&doc, scan);- assert_eq!((s.dict.int(b"Width"), s.dict.name(b"Filter")), (Some(600), Some(&b"FlateDecode"[..])), "Retain: Flate stays Flate: {r:?}");+ assert_eq!((s.dict.int(b"Width"), s.dict.name(b"Filter")), (Some(1200), Some(&b"FlateDecode"[..])), "Retain: {r:?}");+ assert_eq!(doc.decoded_image(&s).unwrap(), ragged, "Retain preserves every sample");}/// `rows` coded as CCITT Group 3, one-dimensional (`/K 0`): each row its runs in turn, white first,