mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pr/1560
This commit is contained in:
3 files changed
+91
-2
No files matched your search
@@ -231,10 +231,57 @@ pub fn text_layer_from_tysh(data: &[u8], dpi: f32) -> Option<TextLayer> {
|
||||
} else {
|
||||
layer.text = txt.unwrap_or_default().replace('\r', "\n");
|
||||
}
|
||||
// Some legacy PSDs store point text at a large local origin even though PointBase is zero.
|
||||
// Their transform maps that origin onto the canvas, but maps our zero-based layout far away.
|
||||
// Move the imported layout to the descriptor's ink origin so editing, engine commands, and
|
||||
// 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];
|
||||
}
|
||||
layer.sync_summary();
|
||||
Some(layer)
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// use a zero PointBase plus a very large `boundingBox` origin, with an equally remote transform
|
||||
/// translation that cancels it out. Do not apply this to ordinary point text: its transform origin
|
||||
/// is already the layout origin, and shifting it by the ink bounds would move the layer.
|
||||
fn legacy_point_origin(t: &TySh) -> Option<[f64; 2]> {
|
||||
let e = engine_data(&t.text)?;
|
||||
let point_base = e
|
||||
.path(&["EngineDict", "Rendered", "Shapes", "Children"])
|
||||
.and_then(E::as_array)
|
||||
.and_then(|children| children.first())
|
||||
.and_then(|shape| shape.path(&["Cookie", "Photoshop", "PointBase"]))
|
||||
.and_then(E::as_array)?;
|
||||
let [px, py] = point_base else { return None };
|
||||
if px.as_f64() != Some(0.0) || py.as_f64() != Some(0.0) {
|
||||
return None;
|
||||
}
|
||||
let bounds = descriptor_rect(&t.text, "boundingBox")?;
|
||||
let [a, b, c, d, e, f] = t.transform.m;
|
||||
let values = [a, b, c, d, e, f, bounds[0], bounds[1]];
|
||||
if !values.iter().all(|v| v.is_finite()) {
|
||||
return None;
|
||||
}
|
||||
let shifted = [a * bounds[0] + c * bounds[1] + e, b * bounds[0] + d * bounds[1] + f];
|
||||
let origin_distance = e.abs().max(f.abs());
|
||||
let shifted_distance = shifted[0].abs().max(shifted[1].abs());
|
||||
// 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]])
|
||||
}
|
||||
|
||||
fn descriptor_rect(text: &Descriptor, key: &str) -> Option<[f64; 4]> {
|
||||
let D::Descriptor(rect) = text.get(key)? else { return None };
|
||||
let bounds = [dnum(rect.get("Left"))?, dnum(rect.get("Top "))?, dnum(rect.get("Rght"))?, dnum(rect.get("Btom"))?];
|
||||
(bounds.iter().all(|v| v.is_finite()) && bounds[2] > bounds[0] && bounds[3] > bounds[1]).then_some(bounds)
|
||||
}
|
||||
|
||||
pub(crate) fn arr_f(v: Option<&E>) -> Vec<f64> {
|
||||
v.and_then(E::as_array).map(|a| a.iter().filter_map(E::as_f64).collect()).unwrap_or_default()
|
||||
}
|
||||
|
||||
@@ -669,6 +669,48 @@ 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.
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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]
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//! typing session is one "Edit Type" history step and automation sees exactly what the user does.
|
||||
//! Offsets in [`TextEdit`] are character indices (the engine's unit); the layout works in bytes.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::{Arc, PoisonError};
|
||||
|
||||
use egui::{Color32, Pos2, Stroke};
|
||||
use photocraft_doc::{Document, LayerContent, LayerId, TextLayer};
|
||||
@@ -76,7 +76,7 @@ fn hit_layer(app: &mut PhotocraftApp, x: f64, y: f64) -> Option<LayerId> {
|
||||
/// they sit somewhere else than the glyphs the caret and selection are placed on.
|
||||
fn shows_own_layout(doc: &Document, t: &TextLayer) -> bool {
|
||||
let Some(cache) = &t.cache else { return false };
|
||||
let Ok(mut eng) = photocraft_text::shared().lock() else { return true };
|
||||
let mut eng = photocraft_text::shared().lock().unwrap_or_else(PoisonError::into_inner);
|
||||
let ours = eng.render(t, doc.resolution_dpi, doc.pixel_format()).1.surface.content_bounds();
|
||||
let have = cache.content_bounds();
|
||||
[(ours.x0, have.x0), (ours.y0, have.y0), (ours.x1, have.x1), (ours.y1, have.y1)].iter().all(|(a, b)| (a - b).abs() <= 1)
|
||||
|
||||
Reference in new issue
Block a user