mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-19 08:51:57 +01:00
Merge pull request #2550 from pepen11/up/psd-resolution
Place: a Photoshop file comes in at its own resolution
This commit is contained in:
5 files changed
+88
-3
No files matched your search
@@ -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-15** · Removed the obsolete gap saying pull requests have no test workflow: [CI](.github/workflows/ci.yml) runs the shared quality gate on every pull request and push to main (#1352). No readiness percentage change.
|
||||
- **2026-11-15** · Accepting tracked deletions or rejecting insertions follows body-text selections through the removed text, including resolving all changes, and preserves independent cell and footnote insertion points. No readiness percentage change.
|
||||
- **2026-11-15** · Format follow-ups to the craft-libs cutover (craft-libs#778; ported from #2734 by Dylan M. Taylor): IDML export keeps CDATA image contents and table spans such as `inf` reach the table edge; DOCX plain-text import leaves out deleted tracked changes and expands inline objects; an XLSX number cell that isn't a number shows as written; unmarked CMYK JPEGs keep the shared decoder's ink polarity; PPTX export writes exact opacity; PDF export repins craft-pdf and craft-prepress to craft-libs main. No readiness percentage change.
|
||||
|
||||
@@ -81,3 +81,19 @@ fn cmyk(file: &craft_psd::PsdFile, pixels: usize) -> Option<CmykRaster> {
|
||||
let alpha = alpha.iter().any(|&a| a < 255).then_some(alpha);
|
||||
Some(CmykRaster { width: header.width, height: header.height, cmyk: inks, alpha })
|
||||
}
|
||||
|
||||
/// ResolutionInfo stores pixels per inch regardless of its display units. Read only the
|
||||
/// borrowed head within the declared resource section, without allocating or decoding pixels.
|
||||
/// Preserve the placement policy from #2550: reject incomplete or out-of-range values.
|
||||
pub(crate) fn resolution(bytes: &[u8]) -> Option<(f64, f64)> {
|
||||
let head = craft_psd::HeadView::from_bytes(bytes).ok()?;
|
||||
for resource in head.resources() {
|
||||
let resource = resource.ok()?;
|
||||
if resource.id == craft_psd::resources::ids::RESOLUTION_INFO {
|
||||
let info = craft_psd::ResolutionInfo::from_bytes(resource.data).ok()?;
|
||||
let (h, v) = (info.h_res(), info.v_res());
|
||||
return ((1.0..=10_000.0).contains(&h) && (1.0..=10_000.0).contains(&v)).then_some((h, v));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -309,6 +309,53 @@ fn tiny_psd() -> Vec<u8> {
|
||||
b
|
||||
}
|
||||
|
||||
/// `tiny_psd` with a ResolutionInfo resource of `ppi` horizontally and vertically.
|
||||
fn tiny_psd_at(ppi: u32) -> Vec<u8> {
|
||||
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<u8> {
|
||||
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)));
|
||||
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)));
|
||||
// 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();
|
||||
@@ -1104,3 +1151,21 @@ fn planar_and_premultiplied_cmyk_tiffs_keep_their_inks() {
|
||||
let straight = &r.cmyk[4..];
|
||||
assert!(straight[0] <= 1 && straight[1] >= 254 && straight[2] >= 254 && straight[3] <= 1, "made straight: {straight:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn psd_resolution_preserves_axes_display_units_and_section_bounds() {
|
||||
let mut bytes = tiny_psd_at(300);
|
||||
// Resource payload begins after header, two section lengths and the resource framing.
|
||||
let payload = 46;
|
||||
bytes[payload + 8..payload + 12].copy_from_slice(&(150u32 << 16).to_be_bytes());
|
||||
bytes[payload + 4..payload + 6].copy_from_slice(&2u16.to_be_bytes());
|
||||
bytes[payload + 12..payload + 14].copy_from_slice(&2u16.to_be_bytes());
|
||||
assert_eq!(raster_resolution(&bytes), Some((300.0, 150.0)));
|
||||
assert_eq!(natural_size(&bytes), Some((0.48, 0.48)));
|
||||
// The same resource framing is used in PSB; display units do not rescale the values.
|
||||
bytes[4..6].copy_from_slice(&2u16.to_be_bytes());
|
||||
assert_eq!(raster_resolution(&bytes), Some((300.0, 150.0)));
|
||||
// Bytes beyond the declared resource section must never complete a short payload.
|
||||
bytes[30..34].copy_from_slice(&24u32.to_be_bytes());
|
||||
assert_eq!(raster_resolution(&bytes), None);
|
||||
}
|
||||
@@ -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% |
|
||||
|
||||
Reference in new issue
Block a user