Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(psd): refine legacy point anchors after font resolution

adrdsouzac71e3f94ad85
diff --git a/crates/io/src/affinity.rs b/crates/io/src/affinity.rs
index efba868be..9c4fd72b9 100644
--- a/crates/io/src/affinity.rs
+++ b/crates/io/src/affinity.rs
@@ -43,7 +43,7 @@ pub(crate) fn import(name: &str, bytes: &[u8]) -> Result<ImportResult, IoError>
// Type layers draw from their model, within the import's raster bound: the sizes and
// frames come from an untrusted file.
let mut document = b.doc;
- crate::text_import::prepare(&mut document);
+ crate::text_import::prepare(&mut document, None);
// Save never writes back over the Affinity file.
Ok(ImportResult { document, warnings, source_read_only: true, preview_only: false })
}
diff --git a/crates/io/src/lib.rs b/crates/io/src/lib.rs
index a620ddcf3..5dee7d88b 100644
--- a/crates/io/src/lib.rs
+++ b/crates/io/src/lib.rs
@@ -283,7 +283,8 @@ fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, m
ctl.progress(0.05);
let (mut document, warnings) = psd_import::psd_to_document_decoded(&file, ctl, merged).ok_or(IoError::Cancelled)?;
document.name = name.to_string();
- text_import::prepare(&mut document);
+ let txt2 = file.global_blocks.iter().find(|b| &b.key == b"Txt2").and_then(|b| photocraft_text::psd::parse_txt2(&b.data));
+ text_import::prepare(&mut document, txt2.as_ref());
return Ok(ImportResult { document, warnings, source_read_only: false, preview_only: false });
}
if affinity::is_affinity(bytes) || affinity::has_extension(name) {
diff --git a/crates/io/src/text_import.rs b/crates/io/src/text_import.rs
index 9272f0b5b..0522f39e9 100644
--- a/crates/io/src/text_import.rs
+++ b/crates/io/src/text_import.rs
@@ -28,8 +28,14 @@ fn render_bounded(eng: &mut photocraft_text::TextEngine, t: &mut TextLayer, dpi:
/// - a type layer without pixels (ag-psd, GIMP and other writers leave the image data out and
/// let Photoshop re-render on open) is rendered from its model, instead of staying empty
/// until it is edited.
-pub(crate) fn prepare(doc: &mut Document) {
- fn visit(layers: &mut [Layer], eng: &mut photocraft_text::TextEngine, dpi: f32, format: photocraft_color::PixelFormat) {
+pub(crate) fn prepare(doc: &mut Document, txt2: Option<&photocraft_text::engine_data::Value>) {
+ fn visit(
+ layers: &mut [Layer],
+ eng: &mut photocraft_text::TextEngine,
+ dpi: f32,
+ format: photocraft_color::PixelFormat,
+ txt2: Option<&photocraft_text::engine_data::Value>,
+ ) {
for l in layers {
match &mut l.content {
LayerContent::Text(t) => {
@@ -56,11 +62,12 @@ pub(crate) fn prepare(doc: &mut Document) {
if changed {
t.sync_summary();
}
+ photocraft_text::psd::refine_legacy_point_origin(t, dpi, eng, txt2);
if t.cache.is_none() {
render_bounded(eng, t, dpi, format);
}
}
- LayerContent::Group(g) => visit(&mut g.children, eng, dpi, format),
+ LayerContent::Group(g) => visit(&mut g.children, eng, dpi, format, txt2),
_ => {}
}
}
@@ -68,5 +75,5 @@ pub(crate) fn prepare(doc: &mut Document) {
let dpi = doc.resolution_dpi;
let format = doc.pixel_format();
let mut eng = photocraft_text::shared().lock().unwrap_or_else(|e| e.into_inner());
- visit(&mut doc.layers, &mut eng, dpi, format);
+ visit(&mut doc.layers, &mut eng, dpi, format, txt2);
}
diff --git a/crates/text/src/psd.rs b/crates/text/src/psd.rs
index 0092fcf92..1c9867eba 100644
--- a/crates/text/src/psd.rs
+++ b/crates/text/src/psd.rs
@@ -376,12 +376,43 @@ pub fn text_layer_from_tysh(data: &[u8], dpi: f32) -> Option<TextLayer> {
// writing the TySh all agree on the visible position (#1469).
if let Some([x, y]) = legacy_point_origin(&t) {
let [a, b, c, d, e, f] = layer.transform.m;
- layer.transform.m = [a, b, c, d, a * x + c * y + e, b * x + d * y + f];
+ let corrected = [a, b, c, d, a * x + c * y + e, b * x + d * y + f];
+ if corrected.iter().all(|v| v.is_finite()) {
+ layer.transform.m = corrected;
+ }
}
layer.sync_summary();
Some(layer)
}
+/// Refines a legacy point-text estimate after imported fonts and styles have been resolved.
+/// Uses the rendering engine's font context; an accepted `Txt2` frame stays authoritative.
+pub fn refine_legacy_point_origin(layer: &mut TextLayer, dpi: f32, engine: &mut crate::TextEngine, txt2: Option<&E>) {
+ let Some(raw) = layer.psd_raw.clone() else { return };
+ let Some(t) = parse_tysh(&raw) else { return };
+ let Some([x, y]) = legacy_point_origin(&t) else { return };
+ if !matches!(layer.shape, TextShape::Point) {
+ return;
+ }
+ let guessed = t.transform.mul(&Affine::translate(x, y));
+ if layer.transform != guessed {
+ return;
+ }
+ let Some(bounds) = engine.layout(layer, dpi).bounds() else { return };
+ if !bounds.iter().all(|v| v.is_finite()) {
+ return;
+ }
+ let corrected = t.transform.mul(&Affine::translate(x - f64::from(bounds[0]), y - f64::from(bounds[1])));
+ if corrected.m.iter().all(|v| v.is_finite()) {
+ layer.transform = corrected;
+ // A valid frame can coincide with the initial estimate; numeric equality cannot tell
+ // whether it was used. Reapply it after the fallback so it always wins.
+ if let Some(txt2) = txt2 {
+ apply_txt2_frame(layer, &raw, txt2);
+ }
+ }
+}
+
/// The local origin to fold into a legacy point-text transform, if present.
///
/// Photoshop normally keeps point text close to its PointBase. The affected legacy files instead
@@ -412,7 +443,11 @@ fn legacy_point_origin(t: &TySh) -> Option<[f64; 2]> {
// A local bounding-box origin in the thousands and a transform origin beyond a normal canvas
// are the signature of this old writer. Requiring that the bounding-box origin is closer to
// the document prevents applying the correction again after export/import.
- (bounds[0].abs().max(bounds[1].abs()) > 1024.0 && origin_distance > 4096.0 && shifted_distance < origin_distance).then_some([bounds[0], bounds[1]])
+ if !(bounds[0].abs().max(bounds[1].abs()) > 1024.0 && origin_distance > 4096.0 && shifted_distance < origin_distance) {
+ return None;
+ }
+
+ Some([bounds[0], bounds[1]])
}
fn descriptor_rect(text: &Descriptor, key: &str) -> Option<[f64; 4]> {
diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs
index 13de665cc..9eaedb6f8 100644
--- a/crates/text/src/tests.rs
+++ b/crates/text/src/tests.rs
@@ -1222,47 +1222,106 @@ fn psd_round_trips_antialias_opentype_and_warp() {
}
/// #1469: Some older PSD writers combine a zero PointBase with an enormous local ink origin.
-/// The TySh transform cancels that origin, so importing it as a zero-based layout puts text far
-/// off-canvas. Fold the descriptor origin into the imported transform and preserve it on a TySh
-/// round trip so every editing and export path uses the same position.
+/// Recover the saved ink origin relative to style-aware layout bounds, so point alignment anchors
+/// stay fixed for left, centered and right text through import and a TySh round trip.
#[test]
fn legacy_tysh_point_origin_imports_and_round_trips_on_canvas() {
use photocraft_psd::descriptor::{Descriptor, Id, Value as D};
- let source = styled("Name", CharStyle { font_family: "Inter".into(), size_pt: 120.0, ..Default::default() });
- let mut tysh = crate::psd::parse_tysh(&crate::psd::build_tysh(&source, 72.0, None)).unwrap();
- tysh.transform = Affine { m: [4.1667, 0.0, 0.0, 4.1667, -32375.0, -32887.5] };
- let ink = [8050.0, 8160.0, 8220.0, 8300.0];
- let rect = |class| {
- D::Descriptor(
- Descriptor::new(class)
- .with("Left", D::UnitFloat { unit: *b"#Pnt", value: ink[0] })
- .with("Top ", D::UnitFloat { unit: *b"#Pnt", value: ink[1] })
- .with("Rght", D::UnitFloat { unit: *b"#Pnt", value: ink[2] })
- .with("Btom", D::UnitFloat { unit: *b"#Pnt", value: ink[3] }),
- )
- };
- tysh.text.items.retain(|(key, _)| !key.is("bounds") && !key.is("boundingBox"));
- tysh.text.items.push((Id::new("bounds"), rect("bounds")));
- tysh.text.items.push((Id::new("boundingBox"), rect("boundingBox")));
- let data = crate::psd::write_tysh(&tysh);
-
- let imported = crate::psd::text_layer_from_tysh(&data, 72.0).unwrap();
- let expected = [4.1667, 0.0, 0.0, 4.1667, 1166.935, 1112.772];
- for (got, want) in imported.transform.m.iter().zip(expected) {
- assert!((got - want).abs() < 0.001, "{:?}", imported.transform.m);
- }
- let (_, rendered) = TextEngine::new().render(&imported, 72.0, PixelFormat::RGBA8);
- let placed = rendered.surface.content_bounds();
- assert!(!placed.is_empty(), "the normalized layer renders");
- assert!((1000..3000).contains(&placed.x0) && (500..2000).contains(&placed.y0), "{placed:?}");
-
- let round_trip = crate::psd::text_layer_from_tysh(&crate::psd::build_tysh(&imported, 72.0, Some(ink.map(|v| v as f32))), 72.0).unwrap();
- for (got, want) in round_trip.transform.m.iter().zip(expected) {
- assert!((got - want).abs() < 0.001, "{:?}", round_trip.transform.m);
+ let original = Affine { m: [4.1667, 0.0, 0.0, 4.1667, -32375.0, -32887.5] };
+ let saved_ink_origin = [8050.0, 8160.0];
+ for align in [TextAlign::Left, TextAlign::Center, TextAlign::Right] {
+ let mut source = styled("Name", CharStyle { font_family: "Inter".into(), size_pt: 120.0, ..Default::default() });
+ source.paragraphs = vec![ParagraphRun { len: source.text.len(), style: ParagraphStyle { align, ..Default::default() } }];
+ let native = TextEngine::new().layout(&source, 72.0).bounds().unwrap();
+ let saved_ink = [
+ saved_ink_origin[0],
+ saved_ink_origin[1],
+ saved_ink_origin[0] + f64::from(native[2] - native[0]),
+ saved_ink_origin[1] + f64::from(native[3] - native[1]),
+ ];
+ let rect = |class| {
+ D::Descriptor(
+ Descriptor::new(class)
+ .with("Left", D::UnitFloat { unit: *b"#Pnt", value: saved_ink[0] })
+ .with("Top ", D::UnitFloat { unit: *b"#Pnt", value: saved_ink[1] })
+ .with("Rght", D::UnitFloat { unit: *b"#Pnt", value: saved_ink[2] })
+ .with("Btom", D::UnitFloat { unit: *b"#Pnt", value: saved_ink[3] }),
+ )
+ };
+ let mut tysh = crate::psd::parse_tysh(&crate::psd::build_tysh(&source, 72.0, None)).unwrap();
+ tysh.transform = original;
+ tysh.text.items.retain(|(key, _)| !key.is("bounds") && !key.is("boundingBox"));
+ tysh.text.items.push((Id::new("bounds"), rect("bounds")));
+ tysh.text.items.push((Id::new("boundingBox"), rect("boundingBox")));
+ let data = crate::psd::write_tysh(&tysh);
+
+ let mut imported = crate::psd::text_layer_from_tysh(&data, 72.0).unwrap();
+ imported.psd_raw = Some(std::sync::Arc::new(data.clone()));
+ let mut engine = TextEngine::new();
+ crate::psd::refine_legacy_point_origin(&mut imported, 72.0, &mut engine, None);
+ assert_eq!(imported.paragraph_runs()[0].style.align, align);
+ let expected_anchor = original.apply(photocraft_geom::Point::new(saved_ink_origin[0], saved_ink_origin[1]));
+ let recovered_ink = imported.transform.apply(photocraft_geom::Point::new(f64::from(native[0]), f64::from(native[1])));
+ assert!((recovered_ink.x - expected_anchor.x).abs() < 0.001, "{align:?}: {recovered_ink:?} vs {expected_anchor:?}");
+ assert!((recovered_ink.y - expected_anchor.y).abs() < 0.001, "{align:?}: {recovered_ink:?} vs {expected_anchor:?}");
+
+ let (_, rendered) = engine.render(&imported, 72.0, PixelFormat::RGBA8);
+ let placed = rendered.surface.content_bounds();
+ assert!(!placed.is_empty(), "{align:?}: no rendered pixels");
+ let mut expected = source.clone();
+ expected.transform = original.mul(&Affine::translate(saved_ink_origin[0] - f64::from(native[0]), saved_ink_origin[1] - f64::from(native[1])));
+ let (_, expected_pixels) = engine.render(&expected, 72.0, PixelFormat::RGBA8);
+ assert_eq!(placed, expected_pixels.surface.content_bounds(), "{align:?}: raster placement");
+
+ let round_trip_data = crate::psd::build_tysh(&imported, 72.0, Some(native));
+ let round_trip = crate::psd::text_layer_from_tysh(&round_trip_data, 72.0).unwrap();
+ let round_trip_ink = TextEngine::new().layout(&round_trip, 72.0).bounds().unwrap();
+ let round_trip_origin = round_trip.transform.apply(photocraft_geom::Point::new(f64::from(round_trip_ink[0]), f64::from(round_trip_ink[1])));
+ assert!((round_trip_origin.x - expected_anchor.x).abs() < 1.0, "{align:?}: {round_trip_origin:?}");
+ assert!((round_trip_origin.y - expected_anchor.y).abs() < 1.0, "{align:?}: {round_trip_origin:?}");
+
+ // A valid frame may equal the old ink-corner estimate. It must still win over refinement.
+ let framed = with_text_index(&data, 0);
+ let txt2 = crate::psd::parse_txt2(&txt2_with_frame("Name", Some(saved_ink_origin), [1.0, 0.0, 0.0, 1.0, 0.0, 0.0])).unwrap();
+ let mut with_frame = crate::psd::text_layer_from_tysh(&framed, 72.0).unwrap();
+ with_frame.psd_raw = Some(std::sync::Arc::new(framed.clone()));
+ crate::psd::apply_txt2_frame(&mut with_frame, &framed, &txt2);
+ let authoritative = with_frame.transform;
+ crate::psd::refine_legacy_point_origin(&mut with_frame, 72.0, &mut engine, Some(&txt2));
+ assert_eq!(with_frame.transform, authoritative, "{align:?}: exact frame wins");
}
}
+#[test]
+fn legacy_origin_uses_the_rendering_engines_registered_face() {
+ let mut engine = TextEngine::new();
+ let mut style = CharStyle { font_family: "JetBrains Mono".into(), font_style: "Regular".into(), size_pt: 100.0, ..Default::default() };
+ // This exact-face alias exists only in this engine, like a user-registered font.
+ engine.fonts.select_named_face(&mut style);
+ assert!(style.font_family.starts_with(".PhotoCraft-face-"));
+ let source = with_para(styled("Name", style), ParagraphStyle { align: TextAlign::Right, ..Default::default() });
+ let native = engine.layout(&source, 72.0).bounds().unwrap();
+ let mut t = crate::psd::parse_tysh(&crate::psd::build_tysh(&source, 72.0, None)).unwrap();
+ t.transform = Affine::translate(-8000.0, -8000.0);
+ let saved = [8100.0, 8200.0, 8100.0 + f64::from(native[2] - native[0]), 8200.0 + f64::from(native[3] - native[1])];
+ let raw = with_bounds(&crate::psd::write_tysh(&t), saved);
+ let mut t = crate::psd::parse_tysh(&raw).unwrap();
+ let bounds = t.text.get("bounds").unwrap().clone();
+ t.text.items.retain(|(k, _)| !k.is("boundingBox"));
+ t.text.items.push((photocraft_psd::descriptor::Id::new("boundingBox"), bounds));
+ let raw = crate::psd::write_tysh(&t);
+ let mut imported = crate::psd::text_layer_from_tysh(&raw, 72.0).unwrap();
+ // Import preparation has resolved the face before refining placement.
+ imported.runs = source.runs.clone();
+ imported.sync_summary();
+ imported.psd_raw = Some(std::sync::Arc::new(raw));
+ crate::psd::refine_legacy_point_origin(&mut imported, 72.0, &mut engine, None);
+ let bounds = engine.layout(&imported, 72.0).bounds().unwrap();
+ let placed = imported.transform.apply(photocraft_geom::Point::new(f64::from(bounds[0]), f64::from(bounds[1])));
+ assert!((placed.x - 100.0).abs() < 0.001 && (placed.y - 200.0).abs() < 0.001, "{placed:?}");
+}
+
/// Regression: a PSD whose text engine data has no `EngineDict` (or isn't a dictionary)
/// panicked with `expect("EngineDict")` when the layer was written back (PSD export).
#[test]
diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md
index ba7462370..28cf1e337 100644
--- a/docs/file-format-parity.md
+++ b/docs/file-format-parity.md
@@ -1,6 +1,6 @@
# File-format parity
-> **Last reviewed:** 2026-10-12 · **Last updated:** 2026-10-13 · **Change:** minor (earlier: PDF write one flattened page via Save As / Save a Copy / CLI, #3401; CMYK Invert, Posterize and Threshold on the inks; smart filter masks on PSD open and save; Photoshop-oracle floor 188/258; lossy DNG, compression 34892, decodes with its levels and LinearizationTable applied to the stored 8-bit samples, FILE-215-11; CMYK Hue/Saturation and Color Balance keep the black ink; CMYK Posterize on the stored values, psd-tools oracle floor 242/309; CMYK adjustment layers composite on the document's inks; Photoshop-oracle floor 165/258; CMYK Brightness/Contrast on the inks, CMYK Threshold's luma; modern Brightness/Contrast's brightness curve; Photoshop-oracle floor 151/258; Lab documents composite on their stored L, a, b; Photoshop-oracle floor 152/258; Levels on whole levels at every step with Photoshop's shadow toe; Photoshop-oracle floor 146/258, psd-tools 238/309; 32-bit adjustments unclipped; Photoshop-oracle floor 149/258; mask feathers blur with their exact variance; Threshold splits on Photoshop's luminosity; a pass-through clipping base with an adjustment renders isolated; psd-tools oracle floor 240/309; 16-bit Posterize as Photoshop bins it; psd-tools oracle floor 241/309; Gradient Map luminosity; Photoshop-oracle floor 147/258; PSD: `Mt16`/`Mt32` merged transparency read; palette PNGs open in Indexed Color mode, #4804; earlier: CMYK Invert, Posterize and Threshold on the inks; 16-bit Lab Invert; Photoshop-oracle floor 145/258; `.atn` import maps 112 events; DDS textures open, #1327; DPX read and write, #1049; #2465 fixed: CS2 placed layers with only a `PlLd` block open their contents; Photoshop Actions `.atn` read and write; `.shc` contour files read and write; earlier the same day: indexed BMP palette mode; Save keeps a JPEG's quality and a WebP's encoding) · **Target:** Adobe Photoshop 2026
+> **Last reviewed:** 2026-10-12 · **Last updated:** 2026-10-15 · **Change:** minor (earlier: PDF write one flattened page via Save As / Save a Copy / CLI, #3401; CMYK Invert, Posterize and Threshold on the inks; smart filter masks on PSD open and save; Photoshop-oracle floor 188/258; lossy DNG, compression 34892, decodes with its levels and LinearizationTable applied to the stored 8-bit samples, FILE-215-11; CMYK Hue/Saturation and Color Balance keep the black ink; CMYK Posterize on the stored values, psd-tools oracle floor 242/309; CMYK adjustment layers composite on the document's inks; Photoshop-oracle floor 165/258; CMYK Brightness/Contrast on the inks, CMYK Threshold's luma; modern Brightness/Contrast's brightness curve; Photoshop-oracle floor 151/258; Lab documents composite on their stored L, a, b; Photoshop-oracle floor 152/258; Levels on whole levels at every step with Photoshop's shadow toe; Photoshop-oracle floor 146/258, psd-tools 238/309; 32-bit adjustments unclipped; Photoshop-oracle floor 149/258; mask feathers blur with their exact variance; Threshold splits on Photoshop's luminosity; a pass-through clipping base with an adjustment renders isolated; psd-tools oracle floor 240/309; 16-bit Posterize as Photoshop bins it; psd-tools oracle floor 241/309; Gradient Map luminosity; Photoshop-oracle floor 147/258; PSD: `Mt16`/`Mt32` merged transparency read; palette PNGs open in Indexed Color mode, #4804; earlier: CMYK Invert, Posterize and Threshold on the inks; 16-bit Lab Invert; Photoshop-oracle floor 145/258; `.atn` import maps 112 events; DDS textures open, #1327; DPX read and write, #1049; #2465 fixed: CS2 placed layers with only a `PlLd` block open their contents; Photoshop Actions `.atn` read and write; `.shc` contour files read and write; earlier the same day: indexed BMP palette mode; Save keeps a JPEG's quality and a WebP's encoding) · **Target:** Adobe Photoshop 2026
Every format Photoshop 2026 (27.11.0) reads or writes, with PhotoCraft's support, fidelity and
tests. Photoshop's list comes from its `Info.plist` `CFBundleDocumentTypes`, the PiPL read/write
@@ -142,6 +142,7 @@ remaining gaps in #1606 stay tracked with per-sample corpus ceilings.
| Date | Change | Summary |
|---|---|---|
+| 2026-10-15 | minor | Legacy PSD point-text placement is refined after font resolution using the rendering engine; centered/right alignment keeps its layout offset, while valid `Txt2` frames remain authoritative. Synthetic alignment, font-context and round-trip regressions cover this fallback. |
| 2026-10-14 | minor | PSD smart filter masks: Photoshop stores a smart object's filtered pixels without its filter mask and mixes the unfiltered ones (`FEid`) back in; import now mixes them and export writes them that way, so sf-filter-mask-hard, -gradient and sf-stack-blend-and-mask pass; Photoshop-oracle floor 185 → 188 of 258 |
| 2026-10-13 | minor | Lossy DNG (compression 34892) decodes: 8-bit baseline JPEG tiles and strips, CFA and LinearRaw, partial edge tiles; the stored values take the normal levels and LinearizationTable (FILE-215-11, lossy DNG part) |
| 2026-10-14 | minor | Corpus floors raised to the measured results of the CMYK, Lab, 32-bit, Levels, Threshold, Posterize, feather and pass-through fixes landed together: Photoshop-authored 165 → 185 of 258, io 146 → 148 of 170, psd-tools 242 → 249 of 309 |