From ad4683c5ffe1969b23fbbbc1c3fcb6a6dc250262 Mon Sep 17 00:00:00 2001 From: ".CooCoo" Date: Sat, 14 Nov 2026 19:11:52 -0800 Subject: [PATCH] Paste: params text gives the pasted image's size as its stored resolution (#2783) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit File › Place now sizes a raster by the resolution stored in the file, and one point per pixel only when the file stores none. edit.pasteImage uses the same size, so its params text and comment say so, and a test pastes a 144 ppi PNG and checks that it comes in at half its pixel size. --- crates/engine/src/cmd/paste_image.rs | 39 +++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/paste_image.rs b/crates/engine/src/cmd/paste_image.rs index f9eef8a25..2869e0bc5 100644 --- a/crates/engine/src/cmd/paste_image.rs +++ b/crates/engine/src/cmd/paste_image.rs @@ -28,7 +28,7 @@ pub fn specs() -> Vec { "Paste Image", [], None, - "{base64: an image file (PNG/JPEG/GIF/WebP/TIFF/BMP) as base64, name?: its name in the Links panel ending in an image extension (default \"Pasted image.png\"), spread?, at?: [x, y] (spread coords of the image's center; default the first page's center)} → {id, asset} — embedded; a selected empty frame takes the image, otherwise a new frame holds it at one point per pixel, as File › Place sizes an image, and at most 60% of the page width", + "{base64: an image file (PNG/JPEG/GIF/WebP/TIFF/BMP) as base64, name?: its name in the Links panel ending in an image extension (default \"Pasted image.png\"), spread?, at?: [x, y] (spread coords of the image's center; default the first page's center)} → {id, asset} — embedded; a selected empty frame takes the image, otherwise a new frame holds it at the size File › Place gives it (the resolution stored in the file, or one point per pixel when the file stores none) and at most 60% of the page width", has_doc, paste_image )] @@ -140,8 +140,8 @@ fn paste_image(s: &mut Session, p: &Value) -> Result { Some(at) => at, None => sp.pages.first().map_or(Point::ZERO, |pg| pg.bounds().center()), }; - // The size File › Place gives it: one point per pixel (`designcraft_images::natural_size`), at - // most 60% of the page width. + // The size File › Place gives it (`designcraft_images::natural_size`): the resolution stored in + // the file, or one point per pixel when it stores none, at most 60% of the page width. let (nw, nh) = designcraft_render::natural_size(&bytes, 0).unwrap_or((f64::from(pw), f64::from(ph))); let w = nw.min(d.settings.page_width * 0.6); let h = nh * w / nw; @@ -224,6 +224,39 @@ mod tests { assert!((b.center().x - page.center().x).abs() < 1e-9 && (b.center().y - page.center().y).abs() < 1e-9, "{b:?}"); } + /// `png` with a pHYs chunk after IHDR giving `ppm` pixels per meter on both axes. + fn png_with_phys(png: &[u8], ppm: u32) -> Vec { + let mut data = ppm.to_be_bytes().repeat(2); + data.push(1); + let mut kind_and_data = b"pHYs".to_vec(); + kind_and_data.extend_from_slice(&data); + let mut crc = 0xFFFF_FFFFu32; + for &b in &kind_and_data { + crc ^= u32::from(b); + for _ in 0..8 { + crc = if crc & 1 == 1 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 }; + } + } + // Signature (8) + IHDR chunk (length 4 + type 4 + data 13 + CRC 4). + let mut out = png[..33].to_vec(); + out.extend_from_slice(&(data.len() as u32).to_be_bytes()); + out.extend_from_slice(&kind_and_data); + out.extend_from_slice(&(!crc).to_be_bytes()); + out.extend_from_slice(&png[33..]); + out + } + + #[test] + fn a_pasted_image_file_comes_in_at_its_stored_resolution() { + let mut s = doc(); + // 5669 pixels per meter is 144 ppi: 40 × 30 pixels place 20 × 15 points. + let png = png_with_phys(&png(40, 30), 5669); + let r = s.execute(ID, &json!({"base64": base64_encode(&png), "at": [300, 400]})).unwrap(); + let b = s.doc().unwrap().doc.item(ItemId(r["id"].as_u64().unwrap())).unwrap().bounds(); + assert!((b.width() - 20.0).abs() < 0.01 && (b.height() - 15.0).abs() < 0.01, "{b:?}"); + assert!((b.center().x - 300.0).abs() < 1e-9 && (b.center().y - 400.0).abs() < 1e-9, "{b:?}"); + } + #[test] fn a_selected_empty_frame_takes_the_image() { let mut s = doc();