diff --git a/crates/idml/src/import/item.rs b/crates/idml/src/import/item.rs index 590f53467..af13c2157 100644 --- a/crates/idml/src/import/item.rs +++ b/crates/idml/src/import/item.rs @@ -184,6 +184,32 @@ impl<'r> Importer<'r> { } } self.read_content(e, &model_item, 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); + } + // 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) } @@ -422,6 +448,71 @@ 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)) +} + +/// 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 e9d5fa923..2c06368d6 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -660,6 +660,102 @@ 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}"); + } + // 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(); + 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"); +} + +/// #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.