From 4b1efc6dfa7d45c46ad2f05675da8fc214953ce1 Mon Sep 17 00:00:00 2001 From: pepen Date: Mon, 9 Nov 2026 19:28:54 +0100 Subject: [PATCH 1/2] Place: a Photoshop file comes in at its own resolution A placed PSD took 72 ppi for its natural size, so a 300 ppi file came in four times larger than in the reference app. `raster_resolution` (#1991) now reads a PSD's ResolutionInfo image resource (1005, 16.16 fixed point), so a PSD is placed at its own ppi like JPEG, PNG, TIFF, WebP and BMP; without a usable value (missing, shorter than its 16 bytes, damaged, not positive) it stays at 72 ppi. The image-resource walk that found the ICC profile (1039) now finds any resource by id, and both use it. Documents already holding a PSD keep their placed size; only new placements (and Data Merge / table cell images) use the new size. Co-Authored-By: Claude Opus 5.5 --- ROADMAP.md | 1 + crates/images/src/psd.rs | 28 ++++++++++++++++---- crates/images/src/resolution.rs | 7 +++-- crates/images/src/tests.rs | 46 +++++++++++++++++++++++++++++++++ docs/file-format-parity.md | 2 +- 5 files changed, 76 insertions(+), 8 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 961d69281..ac73acaac 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -394,6 +394,7 @@ list) · [roadmap.md](docs/roadmap.md) (milestones, plan) · [parity-checklist.m ## Progress log +- **2026-11-13** · A placed Photoshop file comes in at its own resolution (ResolutionInfo), as other rasters do since #1991: a 300 ppi PSD was placed four times too large at 72 ppi. No readiness percentage change. - **2026-11-12** · Properties › Text Frame shows the frame's inset spacing (Top, Bottom, Left and Right with a link that makes all four the same; one Inset for a non-rectangular frame) and four Vertical Justification buttons (Top, Center, Bottom, Justify), and the section also shows with the Type tool in a frame, where it changes the frame that shows the insertion point. No readiness percentage change. - **2026-11-12** · An edit of a long threaded story composes the paragraphs it can change and copies the other lines from the composition before the edit; every typed character composed the whole story. In a 240-paragraph story a typed character composes 3 paragraphs (240 before). An edit that moves lines into other frames composes on until a paragraph starts in the same frame and place as before, or to the story's end, and stories with footnotes, tables, anchored objects, balanced columns or keep options that move lines are composed whole. No readiness percentage change. - **2026-11-12** · Composing a story with endnotes, anchored objects or cross-references counts their marks in one pass over the story, instead of counting again from the story start for every paragraph. No readiness percentage change. diff --git a/crates/images/src/psd.rs b/crates/images/src/psd.rs index 90977d36e..7c2826bb5 100644 --- a/crates/images/src/psd.rs +++ b/crates/images/src/psd.rs @@ -119,7 +119,7 @@ pub(crate) fn decode(bytes: &[u8]) -> Option { } let colour_mode_data = r.block(false)?; - let icc = icc_profile(r.block(false)?); + let icc = image_resource(r.block(false)?, 1039); let layers = r.block(psb)?; let transparency = channels > colours && layer_count(layers, psb).is_some_and(|n| n < 0); let row_bytes = wu.checked_mul(usize::from(depth))?.div_ceil(8); @@ -194,11 +194,29 @@ pub(crate) fn decode(bytes: &[u8]) -> Option { } } -/// The ICC profile in the image resources section (resource 1039), if any. -fn icc_profile(resources: &[u8]) -> Option<&[u8]> { +/// Resolution (horizontal, vertical pixels per inch) from the ResolutionInfo image resource +/// (1005, 16.16 fixed point); `None` without one, when it isn't positive, or for a damaged file +/// (the resource is 16 bytes, so a shorter one is damaged too). +pub(crate) fn resolution(bytes: &[u8]) -> Option<(f64, f64)> { + let mut r = Reader { b: bytes }; + if r.take(4)? != b"8BPS" { + return None; + } + // Version, reserved, channels, height, width, depth, mode; then the colour mode data. + r.take(22)?; + r.block(false)?; + let info = image_resource(r.block(false)?, 1005).filter(|info| info.len() >= 16)?; + let fixed = |at: usize| Some(f64::from(u32::from_be_bytes(info.get(at..at + 4)?.try_into().ok()?)) / 65536.0); + let (h, v) = (fixed(0)?, fixed(8)?); + (h > 0.0 && v > 0.0).then_some((h, v)) +} + +/// The data of image resource `id` in the image resources section (1039 is the ICC profile, +/// 1005 ResolutionInfo), if the file has it. +fn image_resource(resources: &[u8], id: u16) -> Option<&[u8]> { let mut r = Reader { b: resources }; while r.take(4)? == b"8BIM" { - let id = r.u16()?; + let found = r.u16()? == id; // Pascal name, padded to an even length including its length byte. let name_len = usize::from(*r.b.first()?); r.take((name_len + 2) & !1)?; @@ -206,7 +224,7 @@ fn icc_profile(resources: &[u8]) -> Option<&[u8]> { if data.len() % 2 == 1 { r.take(1)?; } - if id == 1039 { + if found { return Some(data); } } diff --git a/crates/images/src/resolution.rs b/crates/images/src/resolution.rs index 3b04d8e73..d797d7cd8 100644 --- a/crates/images/src/resolution.rs +++ b/crates/images/src/resolution.rs @@ -1,8 +1,8 @@ //! Embedded raster resolution: the density a placed graphic was saved with, so placing honors //! it instead of assuming 72 ppi (#1991: a 600 px photo saved at 600 dpi places one inch wide). //! Reads JFIF/EXIF from JPEG (and EXIF from WebP), pHYs from PNG, the resolution -//! tags from TIFF, and the header density from BMP. GIF, PSD and files without (usable) metadata -//! report nothing and keep the pixel-is-a-point size. +//! tags from TIFF, the header density from BMP, and ResolutionInfo from PSD. GIF and files +//! without (usable) metadata report nothing and keep the pixel-is-a-point size. /// Dots per inch in x and y, when the file carries usable resolution metadata. pub fn raster_resolution(bytes: &[u8]) -> Option<(f64, f64)> { @@ -24,6 +24,9 @@ pub fn raster_resolution(bytes: &[u8]) -> Option<(f64, f64)> { if is_classic_tiff(bytes) { return tiff_resolution(bytes); } + if crate::is_psd(bytes) { + return crate::psd::resolution(bytes); + } None } diff --git a/crates/images/src/tests.rs b/crates/images/src/tests.rs index afc0b0791..a2c055b91 100644 --- a/crates/images/src/tests.rs +++ b/crates/images/src/tests.rs @@ -306,6 +306,52 @@ fn tiny_psd() -> Vec { b } +/// `tiny_psd` with a ResolutionInfo resource of `ppi` horizontally and vertically. +fn tiny_psd_at(ppi: u32) -> Vec { + tiny_psd_with_resolution(ppi, 16) +} + +/// `tiny_psd` with a ResolutionInfo resource of `ppi`, its data cut to `len` bytes (16 in full). +fn tiny_psd_with_resolution(ppi: u32, len: usize) -> Vec { + let mut b = tiny_psd(); + let mut data = Vec::new(); + for v in [ppi << 16, 0x0001_0002, ppi << 16, 0x0001_0002] { + data.extend(v.to_be_bytes()); + } + data.truncate(len); + let mut res = Vec::new(); + res.extend(b"8BIM"); + res.extend(1005u16.to_be_bytes()); + res.extend([0, 0]); // empty name, padded + res.extend((data.len() as u32).to_be_bytes()); + res.extend(data); + let at = 26 + 4; // header and empty colour mode data; then the image resources length + b.splice(at..at + 4, (res.len() as u32).to_be_bytes()); + b.splice(at + 4..at + 4, res); + b +} + +/// A 300 ppi PSD places at its resolution (2 px = 0.48 pt), as in the reference app; without +/// ResolutionInfo, or with a nonsense or incomplete one, at 72 ppi. +#[test] +fn psd_natural_size_follows_its_resolution() { + assert_eq!(natural_size(&tiny_psd()), Some((2.0, 1.0))); + let b = tiny_psd_at(300); + let (w, h) = natural_size(&b).unwrap(); + assert!((w - 0.48).abs() < 1e-9 && (h - 0.24).abs() < 1e-9, "{w} {h}"); + assert_eq!(raster_resolution(&b), Some((300.0, 300.0))); + assert_eq!(pixel_size(&b), Some((2, 1))); + assert!(decode_rgba(&b).is_some(), "the resource does not disturb decoding"); + assert_eq!(natural_size(&tiny_psd_at(0)), Some((2.0, 1.0))); + // ResolutionInfo is 16 bytes; a shorter one holding both resolutions is still not trusted. + assert!(psd::resolution(&tiny_psd_with_resolution(300, 16)).is_some()); + assert_eq!(natural_size(&tiny_psd_with_resolution(300, 12)), Some((2.0, 1.0))); + // A resource length past the end of the file is ignored, not trusted. + let mut cut = tiny_psd_at(300); + cut.truncate(40); + assert!(psd::resolution(&cut).is_none()); +} + #[test] fn psd_composite_decodes() { let b = tiny_psd(); diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index bf7ee0141..35eb40042 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -70,7 +70,7 @@ external PDF/X certification. | PDF / AI (PDF-compatible) | `.pdf`, `.ai` | ✓ page boxes, layers, pages | ✓ rendered with hayro, embedded as vector in PDF export, crop to page boxes | layer visibility depth; editable vectors not offered (#227) | 3% | | JPEG, PNG, GIF, BMP, WebP | | ✓ | ✓ embedded ICC profiles applied on screen; CMYK JPEGs (incl. YCCK) through their own profile or the working CMYK space | CMYK JPEG/TIFF keep inks in PDF (#35) | 2% | | TIFF | `.tif` | ✓ incl. CMYK, alpha, indexed colour, paths | ✓ incl. CMYK, embedded ICC profiles on screen | clipping paths from 8BIM resources unverified | 1% | -| PSD | `.psd` | ✓ layers, comps, alpha, paths | ~ composite + Object Layer Options; embedded ICC profile and managed CMYK on screen | layer comps partial | 1% | +| PSD | `.psd` | ✓ layers, comps, alpha, paths | ~ composite + Object Layer Options; embedded ICC profile and managed CMYK on screen; placed at its own resolution | layer comps partial | 1% | | EPS | `.eps` | ✓ (PostScript) | ~ preview only (no interpreter) | | <1% | | SVG | `.svg` | ✓ | ✓ (usvg) | | <1% | | HEIC | `.heic` | ✓ | ✗ | | <1% | From 0d169e1d4763680f50b688f2597dba611984c7e0 Mon Sep 17 00:00:00 2001 From: "autocraft-bot[bot]" <354215646+autocraft-bot[bot]@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:54:42 +0000 Subject: [PATCH 2/2] Apply review fixes - crates/images/src/psd.rs - crates/images/src/tests.rs --- crates/images/src/psd.rs | 6 +++--- crates/images/src/tests.rs | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/images/src/psd.rs b/crates/images/src/psd.rs index 7c2826bb5..a6b3fa9d2 100644 --- a/crates/images/src/psd.rs +++ b/crates/images/src/psd.rs @@ -195,8 +195,8 @@ pub(crate) fn decode(bytes: &[u8]) -> Option { } /// Resolution (horizontal, vertical pixels per inch) from the ResolutionInfo image resource -/// (1005, 16.16 fixed point); `None` without one, when it isn't positive, or for a damaged file -/// (the resource is 16 bytes, so a shorter one is damaged too). +/// (1005, 16.16 fixed point); `None` without one, when either value is outside 1–10 000 ppi, or +/// for a damaged file (the resource is 16 bytes, so a shorter one is damaged too). pub(crate) fn resolution(bytes: &[u8]) -> Option<(f64, f64)> { let mut r = Reader { b: bytes }; if r.take(4)? != b"8BPS" { @@ -208,7 +208,7 @@ pub(crate) fn resolution(bytes: &[u8]) -> Option<(f64, f64)> { let info = image_resource(r.block(false)?, 1005).filter(|info| info.len() >= 16)?; let fixed = |at: usize| Some(f64::from(u32::from_be_bytes(info.get(at..at + 4)?.try_into().ok()?)) / 65536.0); let (h, v) = (fixed(0)?, fixed(8)?); - (h > 0.0 && v > 0.0).then_some((h, v)) + ((1.0..=10_000.0).contains(&h) && (1.0..=10_000.0).contains(&v)).then_some((h, v)) } /// The data of image resource `id` in the image resources section (1039 is the ICC profile, diff --git a/crates/images/src/tests.rs b/crates/images/src/tests.rs index a2c055b91..aec12e9c4 100644 --- a/crates/images/src/tests.rs +++ b/crates/images/src/tests.rs @@ -343,6 +343,7 @@ fn psd_natural_size_follows_its_resolution() { assert_eq!(pixel_size(&b), Some((2, 1))); assert!(decode_rgba(&b).is_some(), "the resource does not disturb decoding"); assert_eq!(natural_size(&tiny_psd_at(0)), Some((2.0, 1.0))); + assert_eq!(natural_size(&tiny_psd_at(20_000)), Some((2.0, 1.0))); // ResolutionInfo is 16 bytes; a shorter one holding both resolutions is still not trusted. assert!(psd::resolution(&tiny_psd_with_resolution(300, 16)).is_some()); assert_eq!(natural_size(&tiny_psd_with_resolution(300, 12)), Some((2.0, 1.0)));