From 9a6396c09096b479fc9e1d6d8f84abc851ca5542 Mon Sep 17 00:00:00 2001 From: austin62jake <340374338+austin62jake@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:15:37 -0600 Subject: [PATCH 1/2] fix(engine): Page Setup's paper sets the layout's paper units (#2836) A layout's paper units were millimetres only when a DXF said so; otherwise they followed the drawing's MEASUREMENT. A new drawing is imperial, so ISO A0 picked in Page Setup was 46.81 x 33.11 units, the inch equivalent of 1189 x 841 mm. PAGESETUP's `paper` now also sets the units from the paper: millimetres for a metric paper, the drawing's MEASUREMENT for an inch paper. A one-off plot's `paper` keeps the layout's units, so its objects print at the size drawn. --- crates/engine/src/cmd/layout.rs | 5 +++++ crates/engine/src/cmd/layout/tests.rs | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index e4b497efe..0e2b902c4 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -681,6 +681,11 @@ fn run_pagesetup(s: &mut Session, p: &Value) -> Result { page.paper = ps.name.into(); page.width_mm = ps.width_mm; page.height_mm = ps.height_mm; + // The paper's own units become the layout's paper units: millimetres for a metric paper, + // else the drawing's MEASUREMENT. Otherwise ISO A0 in a new (imperial) drawing's layout + // is 33.11 x 46.81 units (#2836). Only a saved page setup changes units; a one-off plot's + // `paper` keeps the layout's, so its objects stay the size they were drawn. + page.paper_mm = !ps.imperial; } let mm = |k: &str| p.get(k).and_then(Value::as_f64).filter(|v| v.is_finite() && *v > 0.0 && *v <= 5080.0); match (mm("width"), mm("height")) { diff --git a/crates/engine/src/cmd/layout/tests.rs b/crates/engine/src/cmd/layout/tests.rs index 57d52a7ef..fd07c155a 100644 --- a/crates/engine/src/cmd/layout/tests.rs +++ b/crates/engine/src/cmd/layout/tests.rs @@ -165,14 +165,32 @@ fn viewport_scale_lock_and_layers() { } } +/// #2836: picking a paper sets the layout's paper units from it. In a new drawing (imperial, +/// MEASUREMENT 0) ISO A0 was 46.81 x 33.11 units, the inch equivalent; now it is 1189 x 841. +/// An inch paper gives the layout back the drawing's units. +#[test] +fn pagesetup_paper_sets_the_layout_paper_units() { + let mut s = Session::new(); + assert_eq!(cadcraft_render::paper::paper_unit_mm(s.doc().unwrap()), 25.4, "a new drawing is imperial"); + let size = |s: &Session| { + let sheet = cadcraft_render::sheet(s.doc().unwrap(), "Layout1").unwrap(); + ((sheet.size.x * 100.0).round() / 100.0, (sheet.size.y * 100.0).round() / 100.0, sheet.unit_mm) + }; + let r = s.execute("pagesetup", &json!({"layout": "Layout1", "paper": "A0", "landscape": true})).unwrap(); + assert_eq!(r["page"]["paperMm"], true); + assert_eq!(size(&s), (1189.0, 841.0, 1.0), "ISO A0 in millimetres"); + s.execute("pagesetup", &json!({"layout": "Layout1", "paper": "ANSI D"})).unwrap(); + assert_eq!(size(&s), (34.0, 22.0, 25.4), "ANSI D in the drawing's inches"); +} + #[test] fn pagesetup_paper_table() { let mut s = Session::new(); let r = s.execute("pagesetup", &json!({"layout": "Layout1", "paper": "A3", "landscape": true, "margins": [5, 5, 5, 5]})).unwrap(); assert_eq!(r["page"]["widthMm"], 297.0); let sheet = cadcraft_render::sheet(s.doc().unwrap(), "Layout1").unwrap(); - // Imperial drawing: paper units are inches. - assert!((sheet.size.x - 420.0 / 25.4).abs() < 1e-9 && (sheet.size.y - 297.0 / 25.4).abs() < 1e-9); + // An imperial drawing, but a metric paper: the layout's paper units are millimetres (#2836). + assert!((sheet.size.x - 420.0).abs() < 1e-9 && (sheet.size.y - 297.0).abs() < 1e-9); assert!(s.execute("pagesetup", &json!({"layout": "Layout1", "paper": "Z12"})).is_err()); assert!(s.execute("pagesetup", &json!({"layout": "Layout1", "margins": [1, 2]})).is_err()); assert!(s.execute("pagesetup", &json!({"layout": "Layout1", "width": 1e308, "height": 5})).is_err()); From 272e812e315b30f357ae09f28941428d4f1c7d6b Mon Sep 17 00:00:00 2001 From: austin62jake <340374338+austin62jake@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:32:36 -0600 Subject: [PATCH 2/2] fix(engine): a custom size sent with `paper` keeps the layout's units PAGESETUP took the units from a named paper even when the same call's `width` and `height` replaced it with a custom size, so the result depended on whether `paper` was sent too. A custom size keeps the layout's units either way. --- crates/engine/src/cmd/layout.rs | 8 ++++++-- crates/engine/src/cmd/layout/tests.rs | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 0e2b902c4..e50f54ba8 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -684,8 +684,12 @@ fn run_pagesetup(s: &mut Session, p: &Value) -> Result { // The paper's own units become the layout's paper units: millimetres for a metric paper, // else the drawing's MEASUREMENT. Otherwise ISO A0 in a new (imperial) drawing's layout // is 33.11 x 46.81 units (#2836). Only a saved page setup changes units; a one-off plot's - // `paper` keeps the layout's, so its objects stay the size they were drawn. - page.paper_mm = !ps.imperial; + // `paper` keeps the layout's, so its objects stay the size they were drawn. A custom + // `width` and `height` in the same call replace this paper below, and a custom size keeps + // the layout's units, with `paper` or without it. + if p.get("width").is_none() && p.get("height").is_none() { + page.paper_mm = !ps.imperial; + } } let mm = |k: &str| p.get(k).and_then(Value::as_f64).filter(|v| v.is_finite() && *v > 0.0 && *v <= 5080.0); match (mm("width"), mm("height")) { diff --git a/crates/engine/src/cmd/layout/tests.rs b/crates/engine/src/cmd/layout/tests.rs index fd07c155a..4068286d5 100644 --- a/crates/engine/src/cmd/layout/tests.rs +++ b/crates/engine/src/cmd/layout/tests.rs @@ -181,6 +181,10 @@ fn pagesetup_paper_sets_the_layout_paper_units() { assert_eq!(size(&s), (1189.0, 841.0, 1.0), "ISO A0 in millimetres"); s.execute("pagesetup", &json!({"layout": "Layout1", "paper": "ANSI D"})).unwrap(); assert_eq!(size(&s), (34.0, 22.0, 25.4), "ANSI D in the drawing's inches"); + // A custom size replaces the named paper in the same call and keeps the layout's units. + let r = s.execute("pagesetup", &json!({"layout": "Layout1", "paper": "A4", "width": 254, "height": 508})).unwrap(); + assert_eq!(r["page"]["paperMm"], false); + assert_eq!(size(&s), (20.0, 10.0, 25.4), "the custom size in the layout's inches"); } #[test]