From 7a5c4262892b8a7a67566415e18d9f979dd1a949 Mon Sep 17 00:00:00 2001 From: jayzed80 Date: Fri, 13 Nov 2026 22:56:53 +0000 Subject: [PATCH 1/3] IDML import: a slightly slanted text frame keeps its slant in its transform, so its text starts at the column edge (#2615) A text frame whose path is a parallelogram within 1 in 20 of axis-aligned is imported as its rectangle with the slant in the item transform. Before, the composer treated it as a shaped frame and the sloped top edge cut the first lines: the first line started far right of the column edge and one-line headings wrapped. --- crates/idml/src/import/item.rs | 58 ++++++++++++++++++++++++++++++++++ crates/idml/src/tests.rs | 37 ++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/crates/idml/src/import/item.rs b/crates/idml/src/import/item.rs index ac27a833e..758d99d8a 100644 --- a/crates/idml/src/import/item.rs +++ b/crates/idml/src/import/item.rs @@ -181,6 +181,19 @@ impl<'r> Importer<'r> { } } self.read_content(e, tag, id, &mut it); + // A text frame drawn as a slightly slanted rectangle carries the slant in its transform: + // its text is set in the rectangle and slants with the frame, instead of being cut by + // the sloped edges as in a shaped frame (#2615). + if tag == "TextFrame" + && let Some((rect, slant)) = unslant_text_frame_path(&it.path) + { + let back = slant.inverse(); + it.path = rect; + it.xf *= slant; + if let Some(p) = &mut it.wrap.path { + p.transform(back); + } + } Some(it) } @@ -419,6 +432,51 @@ fn clipping_path(g: &El, to_graphic: Affine) -> Option Option<(PathData, Affine)> { + let [sp] = path.subpaths.as_slice() else { return None }; + let [p0, p1, p2, p3] = sp.anchors.as_slice() else { return None }; + let pts = [p0, p1, p2, p3]; + if !sp.closed || pts.iter().any(|a| a.h_in != a.p || a.h_out != a.p || !(a.p.x.is_finite() && a.p.y.is_finite())) { + return None; + } + let (e1, e2) = (p1.p - p0.p, p3.p - p0.p); + // A parallelogram: the fourth corner is where the two edges from the first one lead. + let size = e1.hypot().max(e2.hypot()); + if !size.is_finite() || ((p0.p + e1 + e2) - p2.p).hypot() > size * 1e-9 { + return None; + } + let (h, v) = if e1.x.abs() >= e1.y.abs() { (e1, e2) } else { (e2, e1) }; + // Edge directions with positive width and height. + let h = if h.x < 0.0 { -h } else { h }; + let v = if v.y < 0.0 { -v } else { v }; + let (w, ht) = (h.x, v.y); + let (sy, sx) = (h.y / w, v.x / ht); + if !(w > 0.0 && ht > 0.0 && sy.abs() <= MAX_FRAME_SLANT && sx.abs() <= MAX_FRAME_SLANT) || (sy == 0.0 && sx == 0.0) { + return None; + } + let c = p0.p.midpoint(p2.p).to_vec2(); + let slant = Affine::translate(c) * Affine::new([1.0, sy, sx, 1.0, 0.0, 0.0]) * Affine::translate(-c); + // Unslanted corners, snapped to the rectangle's edges so it is exactly axis-aligned. + let mut rect = path.transformed(slant.inverse()); + for sp in &mut rect.subpaths { + for a in &mut sp.anchors { + let x = if a.p.x < c.x { c.x - w / 2.0 } else { c.x + w / 2.0 }; + let y = if a.p.y < c.y { c.y - ht / 2.0 } else { c.y + ht / 2.0 }; + a.p = Point::new(x, y); + (a.h_in, a.h_out) = (a.p, a.p); + } + } + Some((rect, slant)) +} + fn path_of(pg: &El) -> PathData { path_capped(pg, usize::MAX) } diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index 1b09e6ec1..01bb25f92 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -660,6 +660,43 @@ fn absent_item_and_document_attributes_take_indesign_defaults() { } } +/// #2615: a text frame whose path is a slightly slanted rectangle (here its top and bottom drop +/// 2.09 pt across 207.75 pt) is imported as a rectangle with the slant in its transform. Its text +/// starts at the column edge and a heading that fits stays on one line, instead of the sloped top +/// edge cutting the first lines as in a shaped frame. +#[test] +fn slightly_slanted_text_frame_sets_its_text_in_the_rectangle() { + let corners = [(-136.5, -74.69600535168195), (-136.5, 93.6875), (71.24999999999999, 91.6022553516819), (71.24999999999999, -76.78125000000001)]; + let pts: String = + corners.iter().map(|(x, y)| format!(r#""#)).collect(); + let body = "Body text follows the heading and fills a few more lines of the frame so that the story has more than one paragraph."; + let designmap = format!( + r#" + +Results at a glance
{body}
+ +{pts} + +
"# + ); + let d = import_idml_with(&zip_files(&[("designmap.xml", &designmap)]), &|_| None).unwrap(); + let it = &d.spreads[0].items[0]; + // The frame is where InDesign drew it: its corners on the spread are unchanged. + let anchors = &it.path.subpaths[0].anchors; + for (a, &(x, y)) in anchors.iter().zip(&corners) { + let p = it.xf * a.p; + assert!((p.x - (x + 300.0)).abs() < 1e-9 && (p.y - (y + 300.0)).abs() < 1e-9, "{p:?} vs {x} {y}"); + } + let sid = it.text_frame().unwrap().story; + let cs = designcraft_compose::compose_story(&d, sid, &Default::default()); + let lines: Vec<_> = cs.frames[0].lines.iter().collect(); + assert!(lines.len() >= 3, "{} lines", lines.len()); + let area = it.text_area(); + assert!((lines[0].x0 - area.x0).abs() < 1e-9 && (lines[0].x1 - area.x1).abs() < 1e-9, "first line {}..{} in {area:?}", lines[0].x0, lines[0].x1); + assert_eq!(lines.iter().filter(|l| l.para == 0).count(), 1, "the heading fits on one line"); + assert!(lines.iter().all(|l| (l.x0 - area.x0).abs() < 1e-9), "every line starts at the column edge"); +} + /// InDesign: a break character ends its paragraph (`
` with a ParagraphBreakType closes the /// range); the text after it is a paragraph with its own style, set on the next page of the /// break's parity. From 391886dd173f497114dd4e4f77d4b8e3b4abb8bc Mon Sep 17 00:00:00 2001 From: jayzed80 Date: Sat, 14 Nov 2026 00:59:44 +0000 Subject: [PATCH 2/3] Address review: map explicit gradient vectors with the unslanted text frame path (#2615) --- crates/idml/src/import/item.rs | 7 +++++++ crates/idml/src/tests.rs | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/idml/src/import/item.rs b/crates/idml/src/import/item.rs index 758d99d8a..ed2306c2b 100644 --- a/crates/idml/src/import/item.rs +++ b/crates/idml/src/import/item.rs @@ -193,6 +193,13 @@ impl<'r> Importer<'r> { if let Some(p) = &mut it.wrap.path { p.transform(back); } + // Explicit gradient vectors are in the inner space too, so they move with the path. + for v in [&mut it.fill.gradient_vector, &mut it.stroke.gradient_vector] { + if let Some([x0, y0, x1, y1]) = *v { + let (a, b) = (back * Point::new(x0, y0), back * Point::new(x1, y1)); + *v = Some([a.x, a.y, b.x, b.y]); + } + } } Some(it) } diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index 01bb25f92..b0fd5230c 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -675,7 +675,7 @@ fn slightly_slanted_text_frame_sets_its_text_in_the_rectangle() { Results at a glance
{body}
-{pts} +{pts}
"# ); @@ -687,6 +687,12 @@ fn slightly_slanted_text_frame_sets_its_text_in_the_rectangle() { let p = it.xf * a.p; assert!((p.x - (x + 300.0)).abs() < 1e-9 && (p.y - (y + 300.0)).abs() < 1e-9, "{p:?} vs {x} {y}"); } + // An explicit gradient vector moves with the path: it is still where InDesign put it. + let [x0, y0, x1, y1] = it.fill.gradient_vector.expect("gradient vector"); + for ((x, y), (ex, ey)) in [(x0, y0), (x1, y1)].into_iter().zip([(-100.0, 0.0), (0.0, 0.0)]) { + let p = it.xf * designcraft_geom::Point::new(x, y); + assert!((p.x - (ex + 300.0)).abs() < 1e-9 && (p.y - (ey + 300.0)).abs() < 1e-9, "{p:?} vs {ex} {ey}"); + } let sid = it.text_frame().unwrap().story; let cs = designcraft_compose::compose_story(&d, sid, &Default::default()); let lines: Vec<_> = cs.frames[0].lines.iter().collect(); From d28f479e7e1c8d4d2d8bdb92f934ecc46ac0928f Mon Sep 17 00:00:00 2001 From: jayzed80 Date: Thu, 19 Nov 2026 18:15:23 +0000 Subject: [PATCH 3/3] IDML import: keep a slanted frame's gradient parameter (#2615) A linear gradient's colour is its parameter along the axis, and the slant moved into the item transform changes that parameter for points off the axis, so mapping only the vector's two endpoints back to the unslanted inner space moved the colours. The axis now takes the shear's inverse transpose, scaled to span the same parameter range, for linear fills and strokes; a radial vector is a centre and a radius point, so it is still only mapped back. The new test imports a frame whose sides lean 8 pt over its height with a linear gradient fill and stroke, and checks the parameter at off-axis spread points: 0.21 and 0.46 without the compensation, 0.25 and 0.5 with it. --- crates/idml/src/import/item.rs | 38 ++++++++++++++++++++---- crates/idml/src/tests.rs | 53 ++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 6 deletions(-) diff --git a/crates/idml/src/import/item.rs b/crates/idml/src/import/item.rs index eb9300ff6..6d194c7fc 100644 --- a/crates/idml/src/import/item.rs +++ b/crates/idml/src/import/item.rs @@ -196,12 +196,18 @@ impl<'r> Importer<'r> { if let Some(p) = &mut it.wrap.path { p.transform(back); } - // Explicit gradient vectors are in the inner space too, so they move with the path. - for v in [&mut it.fill.gradient_vector, &mut it.stroke.gradient_vector] { - if let Some([x0, y0, x1, y1]) = *v { - let (a, b) = (back * Point::new(x0, y0), back * Point::new(x1, y1)); - *v = Some([a.x, a.y, b.x, b.y]); - } + // Explicit gradient vectors are in the inner space too, so they move with the path. A + // linear gradient's colour is its parameter along the axis, which the slant changes + // for points off the axis, so its axis is compensated; a radial vector is a centre + // and a radius point, so both just move back. + let linear = |name: &str| { + designcraft_color::swatch::resolve_gradient(&self.swatches, name).is_some_and(|g| g.kind == designcraft_color::GradientKind::Linear) + }; + if let Some(v) = it.fill.gradient_vector { + it.fill.gradient_vector = Some(unslant_gradient_vector(v, back, linear(&it.fill.swatch))); + } + if let Some(v) = it.stroke.gradient_vector { + it.stroke.gradient_vector = Some(unslant_gradient_vector(v, back, linear(&it.stroke.swatch))); } } Some(it) @@ -481,6 +487,26 @@ fn unslant_text_frame_path(path: &PathData) -> Option<(PathData, Affine)> { Some((rect, slant)) } +/// A gradient vector in a text frame's inner space, moved into the unslanted space that `back` +/// maps the path back to. A radial vector is a centre and a radius point, so both move back. A +/// linear gradient's colour is its parameter along the axis, and the slant changes that parameter +/// for points off the axis, so its axis takes the shear's inverse transpose, scaled to span the +/// same parameter range: every point then keeps the colour it had in the original space. +fn unslant_gradient_vector([x0, y0, x1, y1]: [f64; 4], back: Affine, linear: bool) -> [f64; 4] { + let (a, b) = (back * Point::new(x0, y0), back * Point::new(x1, y1)); + let [m11, m12, m21, m22, _, _] = back.as_coeffs(); + let det = m11 * m22 - m12 * m21; + let (dx, dy) = (x1 - x0, y1 - y0); + // The axis's image under the shear's inverse transpose, and the factor that keeps the + // parameter the one it had in the original space. + let (ex, ey) = ((m22 * dx - m12 * dy) / det, (m11 * dy - m21 * dx) / det); + let k = (dx * dx + dy * dy) / (ex * ex + ey * ey); + if !linear || !(k.is_finite() && k > 0.0) { + return [a.x, a.y, b.x, b.y]; + } + [a.x, a.y, a.x + k * ex, a.y + k * ey] +} + fn path_from_model(geometry: craft_idml::model::PathGeometry) -> PathData { let subpaths = geometry .paths diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index 0325d492e..909e9a593 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -703,6 +703,59 @@ fn slightly_slanted_text_frame_sets_its_text_in_the_rectangle() { assert!(lines.iter().all(|l| (l.x0 - area.x0).abs() < 1e-9), "every line starts at the column edge"); } +/// #2615: a linear gradient (fill and stroke) on a slanted text frame keeps the colour it had in +/// the frame's original space at points off its axis. Mapping only the vector's endpoints back +/// would tilt the parameter field: here the frame's sides lean 8 pt over its 300 pt height, and +/// the spread point (300, 300) is half-way along the gradient. +#[test] +fn a_slanted_text_frames_gradient_keeps_its_parameter_off_the_axis() { + let corners = [(246.0, 0.0), (254.0, 300.0), (354.0, 300.0), (346.0, 0.0)]; + let pts: String = + corners.iter().map(|(x, y)| format!(r#""#)).collect(); + let graphic = r#" + + + + + + + +"#; + let designmap = format!( + r#" + + + +{pts} + +"# + ); + let d = import_idml_with(&zip_files(&[("designmap.xml", &designmap), ("Resources/Graphic.xml", graphic)]), &|_| None).unwrap(); + let it = &d.spreads[0].items[0]; + // The frame is unslanted: its path is the rectangle, the slant is in its transform, and its + // corners on the spread are where InDesign drew them. + let rect = [(250.0, 0.0), (250.0, 300.0), (350.0, 300.0), (350.0, 0.0)]; + for (a, (&(x, y), &(ex, ey))) in it.path.subpaths[0].anchors.iter().zip(rect.iter().zip(&corners)) { + assert!((a.p.x - x).abs() < 1e-9 && (a.p.y - y).abs() < 1e-9, "path {:?} vs {x} {y}", a.p); + let p = it.xf * a.p; + assert!((p.x - ex).abs() < 1e-9 && (p.y - ey).abs() < 1e-9, "corner {p:?} vs {ex} {ey}"); + } + // The parameter along each gradient at an off-axis spread point is the one it had before the + // frame was unslanted: (250, 150) to (350, 150), so x = 275 is a quarter and x = 300 a half. + let param = |v: [f64; 4], p: designcraft_geom::Point| { + let d = designcraft_geom::Point::new(v[2], v[3]) - designcraft_geom::Point::new(v[0], v[1]); + (p - designcraft_geom::Point::new(v[0], v[1])).dot(d) / d.hypot2() + }; + for (x, want) in [(275.0, 0.25), (300.0, 0.5)] { + let p = it.xf.inverse() * designcraft_geom::Point::new(x, 300.0); + for (what, v) in [("fill", it.fill.gradient_vector), ("stroke", it.stroke.gradient_vector)] { + let v = v.unwrap_or_else(|| panic!("{what} gradient vector")); + let got = param(v, p); + assert!((got - want).abs() < 1e-9, "{what} at x = {x}: {got} instead of {want}"); + } + } +} + /// InDesign: a break character ends its paragraph (`
` with a ParagraphBreakType closes the /// range); the text after it is a paragraph with its own style, set on the next page of the /// break's parity.