VectorCraft: An open-source, vector art tool written by scratch in pure Rust
Merge #629: Fix pasted type losing its layout bounds cache
bflatastic9e2fb37f3ab5
diff --cc crates/engine/src/cmd/edit.rsindex d52008e22,8f2b5cec8..8ebe35dca--- a/crates/engine/src/cmd/edit.rs+++ b/crates/engine/src/cmd/edit.rs@@@ -381,15 -364,13 +381,18 @@@ fn paste_clip(s: &mut Session, p: &Valu}}sel.set(new_ids.iter().copied());+ // Pasted type arrives without the layout bounds cache: fill it so boxes and alignment are+ // right away (opening the file later would, but paste must not depend on a reopen).+ super::typecmd::refresh_bounds_of(d, &new_ids);- Ok((new_ids, imported))+ Ok((new_ids, imported, artboard))})?;+ // The Artboard tool takes the pasted artboard.+ if let Some(i) = artboard.filter(|_| s.tool_id() == "artboard") {+ s.set_tool_option("active", &json!(i));+ }Ok(json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>(),+ "artboard": artboard,"added": imported.added,"merged": imported.merged,"renamed": imported.renamed,diff --cc crates/engine/src/cmd/typecmd.rsindex e083021d0,8935b3253..97dd421b1--- a/crates/engine/src/cmd/typecmd.rs+++ b/crates/engine/src/cmd/typecmd.rs@@@ -97,96 -97,26 +97,110 @@@ fn orientation(s: &mut Session, p: &ValOk(json!({"vertical": vertical, "ids": ids.iter().map(|id| id.0).collect::<Vec<_>>()}))}-/// Recompute the layout bounds cache after a text edit.+/// Recompute the layout caches (bounds and baselines) after a text edit. Every engine text edit ends here (typing+/// through `text.editRange` too), inside its `Session::edit`, so this is where Auto Size area type+/// ([`vectorcraft_doc::AreaFit::AutoHeight`]) fits its frame to the text: part of the same undo+/// step as the edit.pub(crate) fn refresh_bounds(t: &mut TextObject) {+ // Edits that changed the paragraph count keep one paragraph style per paragraph.+ t.normalize_paras();+ auto_height(t);let lay = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t);t.cached_bounds = Some(lay.bounds);+ t.cached_baselines = lay.baselines();+}++/// The text-space bounds of area type's frame, when Auto Size can fit it: a rectangle (one+/// subpath, as large as its bounds) of horizontal type in one row, with some height.+fn auto_height_frame(t: &TextObject) -> Option<vectorcraft_geom::Rect> {+ use vectorcraft_geom::Shape;+ let vectorcraft_doc::TextKind::Area { frame } = &t.kind else { return None };+ if t.area.fit != vectorcraft_doc::AreaFit::AutoHeight || t.vertical || t.area.rows > 1 || frame.subpaths.len() != 1 {+ return None;+ }+ let b = frame.bounds()?;+ let area = frame.to_bezpath().area().abs();+ let finite = [b.x0, b.y0, b.x1, b.y1].iter().all(|v| v.is_finite());+ (finite && b.height() > 1e-6 && b.width() > 1e-6 && (area - b.area()).abs() <= b.area() * 1e-6).then_some(b)+}++/// Auto Size: move the bottom of a rectangular area type frame to just below its last line (plus+/// the inset), so the frame's height follows the text. Text in several columns gets the least+/// height (to 0.01 pt) at which it fits. True when the frame changed.+fn auto_height(t: &mut TextObject) -> bool {+ use vectorcraft_geom::Shape;+ let Some(b) = auto_height_frame(t) else { return false };+ let db = vectorcraft_text::FontDb::global();+ // Lay out in a frame as tall as the canvas allows: everything the width lets through flows.+ let tall = (crate::MAX_COORD - b.y0).min(1.0e5);+ if tall <= 1.0 {+ return false;+ }+ let mut probe = t.clone();+ let with_size = |probe: &mut TextObject, w: f64, h: f64| {+ if let vectorcraft_doc::TextKind::Area { frame } = &mut probe.kind {+ *frame = PathData::from_bezpath(&vectorcraft_geom::Rect::new(b.x0, b.y0, b.x0 + w, b.y0 + h).to_path(0.1));+ }+ };+ // Columns: all the text in one column as wide as each of them is surely tall enough.+ let cols = t.area.columns.max(1);+ let col_w = if cols > 1 { ((b.width() - t.area.gutter.max(0.0) * (cols - 1) as f64) / cols as f64).max(1.0) } else { b.width() };+ with_size(&mut probe, col_w, tall);+ probe.area.columns = 1;+ let lay = vectorcraft_text::layout(db, &probe);+ let inset = t.area.inset.max(0.0);+ let Some(bottom) = lay.lines.iter().map(|l| l.baseline + l.descent).filter(|y| y.is_finite()).reduce(f64::max) else { return false };+ // The layout's fit test is `baseline + descent <= bottom + 0.01`: the last line fits exactly.+ let mut h = (bottom + inset - b.y0).clamp(1.0, tall);+ if cols > 1 {+ // The least height (to 0.01 pt) at which the columns hold the text.+ probe.area.columns = cols;+ let fits = |probe: &mut TextObject, h: f64| {+ with_size(probe, b.width(), h);+ !vectorcraft_text::layout(db, probe).overflow+ };+ if !fits(&mut probe, h) {+ return false;+ }+ let (mut lo, mut hi) = (1.0, h);+ for _ in 0..24 {+ if hi - lo <= 0.01 {+ break;+ }+ let mid = (lo + hi) * 0.5;+ if fits(&mut probe, mid) { hi = mid } else { lo = mid }+ }+ h = hi;+ }+ if (h - b.height()).abs() <= 1e-6 {+ return false;+ }+ // Scale the frame about its top edge, keeping its anchors (and their order) as they are.+ let a = Affine::translate((0.0, b.y0)) * Affine::scale_non_uniform(1.0, h / b.height()) * Affine::translate((0.0, -b.y0));+ let vectorcraft_doc::TextKind::Area { frame } = &mut t.kind else { return false };+ let mut next = frame.clone();+ next.transform(a);+ if next.bounds().is_none_or(|nb| ![nb.x0, nb.y0, nb.x1, nb.y1].iter().all(|v| v.is_finite() && v.abs() <= crate::MAX_COORD)) {+ return false;+ }+ *frame = next;+ true}+ /// Recompute the layout bounds cache for the text among `ids` (and their descendants) that lacks+ /// it. Pasted or imported type arrives without the cache (not serialized), so selection boxes, the+ /// Transform panel and hit testing would fall back to [`TextObject::estimate_bounds`] (a rough+ /// 0.55 em per character) until the text is edited — the box comes up short and alignment looks off.+ pub(crate) fn refresh_bounds_of(d: &mut Document, ids: &[NodeId]) {+ for id in text_ids(d, ids) {+ if let Some(NodeKind::Text(t)) = d.node_mut(id).map(|n| &mut n.kind)+ && t.cached_bounds.is_none()+ {+ refresh_bounds(t);+ }+ }+ }+/// Text objects among `ids` and their descendants.fn text_ids(d: &Document, ids: &[NodeId]) -> Vec<NodeId> {let mut out = vec![];