diff --git a/crates/doc/src/text/object.rs b/crates/doc/src/text/object.rs index 5bd31f7699..c556f2f5b0 100644 --- a/crates/doc/src/text/object.rs +++ b/crates/doc/src/text/object.rs @@ -27,7 +27,11 @@ impl TextObject { /// Character attributes as presented by controls (stored runs stay local for layout /// and rich-text clipboard operations). Explicit vertical character scale stays separate. pub fn effective_char_style(&self, style: &CharStyle) -> Option { - let scale = self.style_scale()?; + Self::scaled_char_style(style, self.style_scale()?) + } + + /// `style` with `scale` taken into its attributes ([`Self::effective_char_style`]). + fn scaled_char_style(style: &CharStyle, scale: TextStyleScale) -> Option { let mut style = style.clone(); style.size *= scale.points; style.leading = style.leading.map(|v| v * scale.points); @@ -57,19 +61,27 @@ impl TextObject { /// scale from `xf`, so panels show document-point sizes without a separate transform factor. /// Returns `false` when there is nothing to bake (identity scale or an unrepresentable /// transform). + /// + /// Type on a path bakes only the uniform part of the scale (the vertical one) and keeps any + /// stretch in `xf`: its brackets and glyphs sit at fractions of the path's length, which a + /// non-uniform scale of the path would move, and a uniform one keeps. pub fn scale_text_attributes(&mut self) -> bool { - let Some(scale) = self.style_scale() else { return false }; - if (scale.points - 1.0).abs() <= 1e-12 && (scale.horizontal - 1.0).abs() <= 1e-12 { + let Some(mut scale) = self.style_scale() else { return false }; + let [a, b, c, d, _, _] = self.xf.as_coeffs(); + let (mut sx, sy) = (a.hypot(b), c.hypot(d)); + if sx <= 1e-12 || sy <= 1e-12 { return false; } - let [a, b, c, d, _, _] = self.xf.as_coeffs(); - let (sx, sy) = (a.hypot(b), c.hypot(d)); - if sx <= 1e-12 || sy <= 1e-12 { + if matches!(self.kind, TextKind::OnPath { .. }) { + scale.horizontal = 1.0; + sx = sy; + } + if (scale.points - 1.0).abs() <= 1e-12 && (scale.horizontal - 1.0).abs() <= 1e-12 { return false; } let mut baked = Vec::with_capacity(self.runs.len()); for run in &self.runs { - let Some(eff) = self.effective_char_style(&run.style) else { return false }; + let Some(eff) = Self::scaled_char_style(&run.style, scale) else { return false }; baked.push(eff); } for (run, style) in self.runs.iter_mut().zip(baked) { @@ -78,6 +90,14 @@ impl TextObject { // Character strokes are drawn in text space: widen them by the vertical scale we remove. self.scale_char_strokes(1.0 / sy); self.xf *= Affine::scale_non_uniform(1.0 / sx, 1.0 / sy); + // An area's frame and a path type's path are drawn through `xf` too: give them the scale + // `xf` loses, so the frame (and so the line breaks) and the path stay where they were. + let grow = Affine::scale_non_uniform(sx, sy); + match &mut self.kind { + TextKind::Area { frame } => frame.transform(grow), + TextKind::OnPath { path, .. } => path.transform(grow), + TextKind::Point => {} + } self.cached_bounds = None; self.cached_baselines.clear(); self.cached_ink = None; diff --git a/crates/engine/src/tests/typescale.rs b/crates/engine/src/tests/typescale.rs index f96193a6c4..9aa287f601 100644 --- a/crates/engine/src/tests/typescale.rs +++ b/crates/engine/src/tests/typescale.rs @@ -369,3 +369,129 @@ fn scale_text_attributes_keeps_tracked_type_where_it_was() { assert!((*a - *b).hypot() < 1e-6, "{before:?} {after:?}"); } } + +/// What Scale Text Attributes must leave as it was: the document-space bounds of an area's frame +/// or a path type's path, each line's text, whether the text overflows, and where each glyph +/// starts on the page. +struct Placed { + outline: Option, + lines: Vec<(usize, usize)>, + overflow: bool, + pens: Vec, +} + +fn placed(s: &Session, id: NodeId) -> Placed { + let t = text(s, id); + let lay = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), &t); + let outline = match &t.kind { + vectorcraft_doc::TextKind::Area { frame } => Some(frame), + vectorcraft_doc::TextKind::OnPath { path, .. } => Some(path), + vectorcraft_doc::TextKind::Point => None, + }; + Placed { + outline: outline.and_then(|p| p.transformed(t.xf).bounds()), + lines: lay.lines.iter().map(|l| (l.start, l.end)).collect(), + overflow: lay.overflow, + pens: lay.glyphs.iter().map(|g| t.xf * (g.xf * Point::ZERO)).collect(), + } +} + +fn same_place(before: &Placed, after: &Placed) { + let (Some(b), Some(a)) = (before.outline, after.outline) else { panic!("no frame or path: {:?} {:?}", before.outline, after.outline) }; + for (x, y) in [(b.x0, a.x0), (b.y0, a.y0), (b.x1, a.x1), (b.y1, a.y1)] { + assert!((x - y).abs() < 1e-6, "frame {b:?} became {a:?}"); + } + assert_eq!(after.lines, before.lines, "line breaks"); + assert_eq!(after.overflow, before.overflow, "overflow"); + assert_eq!(before.pens.len(), after.pens.len()); + for (b, a) in before.pens.iter().zip(&after.pens) { + assert!((*a - *b).hypot() < 1e-6, "{:?} {:?}", before.pens, after.pens); + } +} + +/// #8297: Scale Text Attributes on scaled area type keeps the frame the scale gave it, so the +/// lines break where they did and no text becomes overset. It used to leave the frame at its +/// unscaled size, which then held two lines of the larger type and hid the rest. +#[test] +fn scale_text_attributes_keeps_an_area_frame_and_its_lines() { + for (sx, sy, size) in [(200, 200, 24.0), (200, 100, 12.0), (50, 50, 6.0)] { + let mut s = session(); + let id = NodeId( + s.execute( + "text.create", + &json!({"x": 40, "y": 60, "size": 12, "area": {"width": 120, "height": 80}, "text": "Bright river stones mark a gentle path past old trees."}), + ) + .unwrap()["id"] + .as_u64() + .unwrap(), + ); + s.execute("object.scale", &json!({"sx": sx, "sy": sy, "origin": [0, 0]})).unwrap(); + let before = placed(&s, id); + assert!(before.lines.len() >= 3 && !before.overflow, "{sx}x{sy}: {:?}", before.lines); + s.execute("type.scaleTextAttributes", &json!({})).unwrap(); + near(text(&s, id).runs[0].style.size, size); + same_place(&before, &placed(&s, id)); + if (sx, sy) == (200, 200) { + let f = before.outline.unwrap(); + assert_eq!((f.x0.round(), f.y0.round(), f.x1.round(), f.y1.round()), (80.0, 120.0, 320.0, 280.0)); + } + s.execute("edit.undo", &json!({})).unwrap(); + near(text(&s, id).runs[0].style.size, 12.0); + same_place(&before, &placed(&s, id)); + s.execute("edit.redo", &json!({})).unwrap(); + same_place(&before, &placed(&s, id)); + } +} + +/// Type on a path keeps its path: it used to shrink with the scale removed, so the type ran past +/// its end. +#[test] +fn scale_text_attributes_keeps_a_path_types_path() { + let mut s = session(); + let pid = s.execute("path.create", &json!({ "d": "M100 300 L300 300" })).unwrap()["id"].as_u64().unwrap(); + let id = NodeId( + s.execute("text.createInPath", &json!({"path": pid, "mode": "onPath", "text": "Along the way", "size": 12, "at": [100, 300]})).unwrap()["id"] + .as_u64() + .unwrap(), + ); + s.execute("select.set", &json!({"ids": [id.0]})).unwrap(); + s.execute("object.scale", &json!({"sx": 200, "origin": [0, 0]})).unwrap(); + let before = placed(&s, id); + assert!(!before.overflow); + s.execute("type.scaleTextAttributes", &json!({})).unwrap(); + near(text(&s, id).runs[0].style.size, 24.0); + same_place(&before, &placed(&s, id)); +} + +/// A non-uniform scale on type on a path: only the uniform part is baked and the stretch stays in +/// the transform, so the brackets, which sit at fractions of the path's length, stay where they +/// were. Baking the stretch into the path would move them: on this path, scaled 3 × 2, the length +/// grows from 200 to 500 and not in proportion, so the same fraction lands elsewhere. +#[test] +fn scale_text_attributes_keeps_path_type_on_a_stretched_path_where_it_was() { + let mut s = session(); + let pid = s.execute("path.create", &json!({ "d": "M0 0 L100 0 L100 100" })).unwrap()["id"].as_u64().unwrap(); + let id = NodeId( + s.execute("text.createInPath", &json!({"path": pid, "mode": "onPath", "text": "Corner", "size": 12, "at": [60, 0]})).unwrap()["id"] + .as_u64() + .unwrap(), + ); + s.execute("select.set", &json!({"ids": [id.0]})).unwrap(); + s.execute("object.scale", &json!({"sx": 300, "sy": 200, "origin": [0, 0]})).unwrap(); + let before = placed(&s, id); + let brackets = |s: &Session| match text(s, id).kind { + vectorcraft_doc::TextKind::OnPath { start, end, .. } => (start, end), + k => panic!("not path type: {k:?}"), + }; + let before_brackets = brackets(&s); + s.execute("type.scaleTextAttributes", &json!({})).unwrap(); + near(text(&s, id).runs[0].style.size, 24.0); + near(text(&s, id).runs[0].style.h_scale, 100.0); + let left = text(&s, id).style_scale().unwrap(); + near(left.points, 1.0); + near(left.horizontal, 1.5); + assert_eq!(brackets(&s), before_brackets); + same_place(&before, &placed(&s, id)); + // Only the stretch is left: there is nothing more to bake. + assert!(s.execute("type.scaleTextAttributes", &json!({})).is_err()); +}