Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

feat(tools): Double-click a frame handle to fit the frame to its content

moppeeer45d803c6a4cc
diff --git a/ROADMAP.md b/ROADMAP.md
index 8a0f82071..a956d2d54 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -153,6 +153,7 @@ Newest first.
- **2026-10-12** · The Effects panel shows the level its settings apply to ("Group: Color Dodge 100%") and, as in InDesign, Isolate Blending and Knockout Group for a selected group. No readiness percentage change.
- **2026-10-11** · Properties gains InDesign's Pathfinder section (Add, Subtract, Intersect, Exclude Overlap, Minus Back) with several objects selected; section titles and icon buttons have accessible names. No readiness percentage change.
- **2026-10-12** · The Eyedropper samples a placed image: clicking the photo with text (or objects) selected fills them with the image's colour under the pointer, as an unnamed colour (`color.sample`), as in InDesign. Images on locked layers can be sampled. No readiness percentage change.
+- **2026-10-12** · Double-clicking a frame handle fits the frame to its content as in InDesign (a corner fits both sides, an edge only its own). Object › Fitting › Fit Frame to Content now fits text frames to their text too, growing them for overset text. No readiness percentage change.
- **2026-10-12** · Words in capitals hyphenate like capitalized words (FERRUM COM-MODI), as in InDesign; Hyphenate Capitalized Words off keeps them whole. No readiness percentage change.
- **2026-10-11** · `transform.move {copy}`, Edit › Duplicate and Step and Repeat return the copies' ids, so a script can go on with them. No readiness percentage change.
- **2026-10-11** · `object.fill` and `object.stroke` refuse a swatch name the document doesn't have (it painted nothing) and suggest the name meant (`Paper` → `[Paper]`). No readiness percentage change.
diff --git a/crates/engine/src/cmd/object.rs b/crates/engine/src/cmd/object.rs
index aa5a2cc01..df8f1eb25 100644
--- a/crates/engine/src/cmd/object.rs
+++ b/crates/engine/src/cmd/object.rs
@@ -586,7 +586,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Fitting",
["Object", "Fitting"],
None,
- "{mode: fillProportionally|fitProportionally|fitContentToFrame|centerContent|fitFrameToContent, ids?}",
+ "{mode: fillProportionally|fitProportionally|fitContentToFrame|centerContent|fitFrameToContent, axis?: both|height|width (text frames: which sides Fit Frame to Content moves; default both), ids?}",
has_selection,
fit
),
@@ -1897,7 +1897,28 @@ fn fitting_options(s: &mut Session, p: &Value) -> Result<Value> {
fn fit(s: &mut Session, p: &Value) -> Result<Value> {
let ids = targets(s, p)?;
let mode = str_param(p, "mode").unwrap_or("fitProportionally").to_string();
+ let (height, width) = match str_param(p, "axis").unwrap_or("both") {
+ "height" => (true, false),
+ "width" => (false, true),
+ "both" => (true, true),
+ other => return Err(bad("object.fit", format!("unknown axis `{other}`"))),
+ };
+ // Text frames: Fit Frame to Content fits the frame to its composed text.
+ let mut text_fits = Vec::new();
+ if mode == "fitFrameToContent" {
+ let d = s.doc()?.doc.clone();
+ for id in &ids {
+ if let Some(r) = fitted_text_frame(&d, &s.cache, *id, height, width) {
+ text_fits.push((*id, r));
+ }
+ }
+ }
s.edit(|d, _| {
+ for (id, r) in &text_fits {
+ if let Some(it) = d.item_mut(*id) {
+ it.path = shapes::rectangle(*r);
+ }
+ }
for id in &ids {
let Some(it) = d.item_mut(*id) else { continue };
let r = it.inner_bounds();
@@ -1919,6 +1940,47 @@ fn fit(s: &mut Session, p: &Value) -> Result<Value> {
})
}
+/// The inner rectangle that fits text frame `id` to its text (Fit Frame to Content): the bottom at
+/// the last line's descent (`height`), the right side at the longest line's end (`width`, one
+/// column only), plus the insets; the top-left stays. Overset text grows the frame first. None for
+/// frames that aren't horizontal text frames, have no text, or are threaded on to another frame.
+fn fitted_text_frame(d: &Document, cache: &designcraft_compose::Cache, id: ItemId, height: bool, width: bool) -> Option<Rect> {
+ let it = d.item(id)?;
+ let tf = it.text_frame()?;
+ if d.frame_vertical(it) || tf.options.path.is_some() {
+ return None;
+ }
+ let story = d.story(tf.story)?;
+ // The last frame of its thread only: an earlier frame's text runs on.
+ if story.frames.last() != Some(&id) {
+ return None;
+ }
+ let r = it.inner_bounds();
+ let inset = tf.options.inset;
+ // Room for everything: a tall frame, composed on a copy.
+ let mut tall = d.clone();
+ if height {
+ tall.item_mut(id)?.path = shapes::rectangle(Rect::new(r.x0, r.y0, r.x1, r.y0 + r.height().max(1.0) + 20_000.0));
+ }
+ let cs = cache.get(&tall, tf.story, None);
+ let ft = cs.frame(id)?;
+ let last = ft.lines.iter().map(|l| l.baseline + l.descent).fold(f64::NEG_INFINITY, f64::max);
+ if !last.is_finite() {
+ return None;
+ }
+ let mut out = r;
+ if height {
+ out.y1 = (last + inset[2]).max(r.y0 + 1.0);
+ }
+ if width && tf.options.columns <= 1 {
+ let right = ft.lines.iter().map(|l| l.end_x).fold(f64::NEG_INFINITY, f64::max);
+ if right.is_finite() {
+ out.x1 = (right + inset[3]).max(r.x0 + 1.0);
+ }
+ }
+ Some(out)
+}
+
pub(crate) trait RemoveKeep {
fn remove_item_keep_story(&mut self, id: ItemId) -> Result<Item>;
}
@@ -3327,3 +3389,52 @@ mod spread_target_tests {
assert!(s.doc().unwrap().doc.spreads[0].items.is_empty());
}
}
+
+#[cfg(test)]
+mod fit_text_frame_tests {
+ use super::*;
+
+ fn frame(s: &mut Session, rect: [f64; 4], text: &str) -> ItemId {
+ let id = ItemId(s.execute("frame.create", &json!({"rect": rect, "content": "text", "text": text})).unwrap()["id"].as_u64().unwrap());
+ s.execute("selection.set", &json!({"ids": [id.0]})).unwrap();
+ s.execute("type.char", &json!({"attrs": {"size": 30}})).unwrap();
+ id
+ }
+
+ fn bounds(s: &Session, id: ItemId) -> Rect {
+ s.doc().unwrap().doc.item(id).unwrap().bounds()
+ }
+
+ #[test]
+ fn fit_frame_to_content_fits_a_text_frame_to_its_text() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let id = frame(&mut s, [100.0, 100.0, 500.0, 300.0], "INDESIGN");
+ // The height only (a top or bottom handle): the width stays.
+ s.execute("object.fit", &json!({"mode": "fitFrameToContent", "axis": "height", "ids": [id.0]})).unwrap();
+ let h = bounds(&s, id);
+ assert!(h.x1 == 500.0 && h.y0 == 100.0 && h.height() > 30.0 && h.height() < 45.0, "{h:?}");
+ // Both (a corner handle, or the menu): around the line.
+ s.execute("object.fit", &json!({"mode": "fitFrameToContent", "ids": [id.0]})).unwrap();
+ let b = bounds(&s, id);
+ assert!(b.x0 == 100.0 && b.width() > 100.0 && b.width() < 250.0, "{b:?}");
+ let d = s.doc().unwrap().doc.clone();
+ let sid = d.item(id).unwrap().text_frame().unwrap().story;
+ assert!(!s.cache.get(&d, sid, None).is_overset(), "still fits");
+ }
+
+ #[test]
+ fn fit_frame_to_content_grows_a_frame_with_overset_text() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let id = frame(&mut s, [100.0, 100.0, 300.0, 140.0], "one two three four five six seven eight nine ten eleven twelve");
+ let d = s.doc().unwrap().doc.clone();
+ let sid = d.item(id).unwrap().text_frame().unwrap().story;
+ assert!(s.cache.get(&d, sid, None).is_overset());
+ s.execute("object.fit", &json!({"mode": "fitFrameToContent", "axis": "height", "ids": [id.0]})).unwrap();
+ let d = s.doc().unwrap().doc.clone();
+ assert!(!s.cache.get(&d, sid, None).is_overset(), "{:?}", bounds(&s, id));
+ assert!(bounds(&s, id).height() > 100.0);
+ assert!(s.execute("object.fit", &json!({"mode": "fitFrameToContent", "axis": "diagonal", "ids": [id.0]})).is_err());
+ }
+}
diff --git a/crates/tools/src/select.rs b/crates/tools/src/select.rs
index fc9a8c316..ac8ecdfa2 100644
--- a/crates/tools/src/select.rs
+++ b/crates/tools/src/select.rs
@@ -2,7 +2,8 @@
//!
//! Click selects the frontmost item (Shift toggles), drag moves (Alt duplicates, Shift constrains),
//! the 8 bounding-box handles resize (Shift keeps proportions, Alt from centre, Cmd scales content),
-//! empty-canvas drags make a marquee, double-clicking a text frame switches to the Type tool.
+//! empty-canvas drags make a marquee, double-clicking a text frame switches to the Type tool and
+//! double-clicking a handle fits the frame to its content (corner: both sides, edge: that one).
use designcraft_doc::{ItemId, SpreadRef};
use designcraft_geom::{Affine, Point, Rect, Vec2};
@@ -773,6 +774,18 @@ impl Tool for SelectionTool {
}
}
PointerKind::DoubleClick => {
+ // A selection handle: Fit Frame to Content, along that handle's side (InDesign).
+ if !self.direct
+ && let Some(h) = handle_at(cx, p)
+ {
+ let axis = match h {
+ 1 | 5 => "height",
+ 3 | 7 => "width",
+ _ => "both",
+ };
+ self.drag = Drag::None;
+ return vec![Action::Exec("object.fit".into(), json!({"mode": "fitFrameToContent", "axis": axis}))];
+ }
if let Some((_, id)) = cx.hit(p)
&& cx.doc.item(id).is_some_and(|i| i.is_text_frame())
{
diff --git a/crates/tools/src/tests.rs b/crates/tools/src/tests.rs
index 6776902aa..ddb15878f 100644
--- a/crates/tools/src/tests.rs
+++ b/crates/tools/src/tests.rs
@@ -876,3 +876,18 @@ fn eyedropper_samples_a_placed_image_and_matches_other_objects() {
let a = t.pointer(&cx, &PointerEvent::new(PointerKind::Down, 120.0 + off.x, 130.0 + off.y));
assert_eq!(a, vec![Action::Exec("object.matchAttributes".into(), serde_json::json!({"from": photo.0}))]);
}
+
+#[test]
+fn double_clicking_a_handle_fits_the_frame_to_its_content_along_that_side() {
+ let mut d = Document::new(&NewDocument::default());
+ let id = add_rect(&mut d, Rect::new(100.0, 100.0, 300.0, 200.0));
+ let s = Selection { items: vec![id], ..Default::default() };
+ let (c, l) = (Cache::new(), CanvasLayout::new(&d, false));
+ let off = l.xf(SpreadRef::Doc(0)).translation();
+ let cx = ctx(&d, &s, &c, &l);
+ let mut t = create("selection");
+ for ((x, y), axis) in [((300.0, 200.0), "both"), ((200.0, 200.0), "height"), ((300.0, 150.0), "width")] {
+ let a = t.pointer(&cx, &PointerEvent::new(PointerKind::DoubleClick, x + off.x, y + off.y));
+ assert_eq!(a, vec![Action::Exec("object.fit".into(), serde_json::json!({"mode": "fitFrameToContent", "axis": axis}))], "{axis}");
+ }
+}