mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 21:27:00 +00:00
Scale Text Attributes: an area's frame and a path type's path keep their size
Baking an object's scale into the character attributes removed that scale from the type's transform, but the area frame (and a path type's path), which is drawn through that transform too, kept its unscaled size. A 120 x 80 frame scaled 200% shrank back to 120 x 80 on the page, so the 24 pt type broke into two lines and the rest became overset. The frame and the path now take the scale the transform loses, so they, the line breaks and every glyph stay where they were. Fixes #8297
This commit is contained in:
@@ -78,6 +78,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;
|
||||
|
||||
@@ -369,3 +369,96 @@ 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<vectorcraft_geom::Rect>,
|
||||
lines: Vec<(usize, usize)>,
|
||||
overflow: bool,
|
||||
pens: Vec<Point>,
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user