engine: check a paper's margins against its own axis (#1266)

* engine: check a paper's margins against its own axis

* doc: turn a paper's margins with the sheet when it is stored portrait

* fmt: keep plotters.rs rustfmt-clean

* engine: turn a device paper's margins to the page's orientation

* engine: turn a page's margins when only its orientation changes

PAGESETUP sets `landscape` before `page_device`, but a command that names no paper copies no
device paper, so the retained margins stayed on the old sheet's axes and Sheet::from_page
refused them on the new orientation, falling back to the whole sheet. Turn them with the page,
using the inverse of the rotation `set_paper` applies.

* engine: an orientation-only pagesetup turns the retained margins the way the page turns

---------

Co-authored-by: midasmatterhorn <236787783+midasmatterhorn@users.noreply.github.com>
Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
This commit is contained in:
midasmatterhorn
2026-11-28 16:41:14 +01:00
committed by GitHub
co-authored by midasmatterhorn FredApps
parent 9f553d6764
commit a67a8b5edd
4 changed files with 188 additions and 13 deletions
+126
View File
@@ -117,6 +117,108 @@ fn page_setup_takes_a_device_and_its_papers() {
assert!(report["devices"].as_array().unwrap().iter().any(|d| d == "Plan Printer"));
}
#[test]
/// A paper stored portrait keeps its printable area when the page names it landscape: the margins
/// turn back to the axes the user gave, and PLOT hands them on that way (#1266).
fn a_landscape_page_gets_the_papers_margins_back() {
let mut s = session();
s.execute("plottermanager", &json!({"action": "new", "name": "Banner Printer", "output": "pdf"})).unwrap();
let paper = json!({"name": "Banner", "width": 400, "height": 200, "margins": [150, 0, 150, 0]});
s.execute("plottermanager", &json!({"action": "set", "name": "Banner Printer", "paper": paper})).unwrap();
// Stored portrait, 200 x 400, the side margins on the long axis.
let stored = |s: &mut Session| {
let r = s.execute("plottermanager", &json!({"action": "get", "name": "Banner Printer"})).unwrap();
r["papers"].as_array().unwrap().iter().find(|p| p["name"] == "Banner").cloned().unwrap()
};
let p = stored(&mut s);
assert_eq!((p["width"].as_f64(), p["height"].as_f64()), (Some(200.0), Some(400.0)));
assert_eq!(p["margins"], json!([0.0, 150.0, 0.0, 150.0]));
s.execute("pagesetup", &json!({"layout": "Layout1", "device": "Banner Printer", "paper": "Banner", "landscape": true})).unwrap();
let page = s.doc().unwrap().layout("Layout1").unwrap().page.clone();
assert_eq!((page.width_mm, page.height_mm, page.margins_mm), (200.0, 400.0, [150.0, 0.0, 150.0, 0.0]));
// The 400 x 200 sheet's printable area is 100 x 200 mm, not the whole sheet.
let sheet = cadcraft_render::sheet(s.doc().unwrap(), "Layout1").unwrap();
let mm = |v: f64| v * sheet.unit_mm;
assert_eq!((mm(sheet.size.x), mm(sheet.size.y)), (400.0, 200.0));
assert_eq!((mm(sheet.printable.min.x), mm(sheet.printable.min.y)), (150.0, 0.0));
assert_eq!((mm(sheet.printable.max.x), mm(sheet.printable.max.y)), (250.0, 200.0));
// PLOT hands the margins on in the sheet's orientation.
let o = plotted(s.execute("plot", &json!({"layout": "Layout1", "paper": "Banner"})).unwrap());
assert_eq!((o["width"].as_f64(), o["height"].as_f64()), (Some(200.0), Some(400.0)));
assert_eq!(o["margins"], json!([150.0, 0.0, 150.0, 0.0]));
// Naming the paper landscape again at a new size keeps the margins' axes as well.
s.execute("plottermanager", &json!({"action": "set", "name": "Banner Printer", "paper": {"name": "Banner", "width": 500, "height": 200}}))
.unwrap();
let p = stored(&mut s);
assert_eq!((p["width"].as_f64(), p["height"].as_f64()), (Some(200.0), Some(500.0)));
assert_eq!(p["margins"], json!([0.0, 150.0, 0.0, 150.0]), "the margins keep the short sides");
}
#[test]
/// A page set up on a landscape device paper keeps its printable area when a later PAGESETUP only
/// changes the orientation: no paper is copied, so the retained margins turn to the new sheet
/// instead of being saved against the wrong axes (#1266).
fn an_orientation_only_pagesetup_turns_the_retained_margins() {
let mut s = session();
s.execute("plottermanager", &json!({"action": "new", "name": "Banner Printer", "output": "pdf"})).unwrap();
let paper = json!({"name": "Banner", "width": 400, "height": 200, "margins": [150, 0, 150, 0]});
s.execute("plottermanager", &json!({"action": "set", "name": "Banner Printer", "paper": paper})).unwrap();
// The landscape page carries the margins on its 400 mm sides: 150 mm left and right.
s.execute("pagesetup", &json!({"layout": "Layout1", "device": "Banner Printer", "paper": "Banner", "landscape": true})).unwrap();
let page = s.doc().unwrap().layout("Layout1").unwrap().page.clone();
assert_eq!((page.width_mm, page.height_mm, page.margins_mm), (200.0, 400.0, [150.0, 0.0, 150.0, 0.0]));
// A second command changes only the orientation. The paper is not copied and no margins are
// given, so the retained margins turn to the 200 x 400 portrait sheet: 150 mm top and bottom,
// leaving the same 100 x 200 mm printable area rather than falling back to the whole sheet.
s.execute("pagesetup", &json!({"layout": "Layout1", "landscape": false})).unwrap();
let page = s.doc().unwrap().layout("Layout1").unwrap().page.clone();
assert_eq!((page.paper.as_str(), page.margins_mm), ("Banner", [0.0, 150.0, 0.0, 150.0]));
let sheet = cadcraft_render::sheet(s.doc().unwrap(), "Layout1").unwrap();
let mm = |v: f64| v * sheet.unit_mm;
assert_eq!((mm(sheet.size.x), mm(sheet.size.y)), (200.0, 400.0));
assert_eq!((mm(sheet.printable.min.x), mm(sheet.printable.min.y)), (0.0, 150.0));
assert_eq!((mm(sheet.printable.max.x), mm(sheet.printable.max.y)), (200.0, 250.0));
}
#[test]
/// Asymmetric retained margins turn with an orientation-only PAGESETUP in the direction the page
/// turns — a portrait page turning landscape re-measures them on the landscape axes, and turning
/// back restores the stored margins — so the printable area does not depend on how the
/// orientation was reached (#1266).
fn an_orientation_turn_turns_asymmetric_margins_the_way_the_page_turns() {
let mut s = session();
// A page setup's default orientation is landscape, so the page is named portrait first.
s.execute("pagesetup", &json!({"layout": "Layout1", "paper": "A4", "landscape": false, "margins": [10, 20, 30, 80]})).unwrap();
let page = s.doc().unwrap().layout("Layout1").unwrap().page.clone();
assert_eq!((page.width_mm, page.height_mm, page.margins_mm), (210.0, 297.0, [10.0, 20.0, 30.0, 80.0]));
// Portrait to landscape: the margins turn the way `set_paper` turns a device paper's, so the
// 10 mm left margin becomes the landscape sheet's 80 mm top margin and the printable area is
// the same rectangle on the turned sheet.
s.execute("pagesetup", &json!({"layout": "Layout1", "landscape": true})).unwrap();
let page = s.doc().unwrap().layout("Layout1").unwrap().page.clone();
assert_eq!(page.margins_mm, [80.0, 10.0, 20.0, 30.0], "the margins turn to the landscape axes");
// Landscape back to portrait restores the margins the page was set up with.
s.execute("pagesetup", &json!({"layout": "Layout1", "landscape": false})).unwrap();
let page = s.doc().unwrap().layout("Layout1").unwrap().page.clone();
assert_eq!(page.margins_mm, [10.0, 20.0, 30.0, 80.0], "the round trip keeps the margins");
// A landscape page turns the other way when it goes portrait: the margins are re-measured on
// the portrait sheet's axes, and turning back gives them the landscape reading again.
s.execute("pagesetup", &json!({"layout": "Layout1", "landscape": true, "margins": [1, 2, 3, 4]})).unwrap();
s.execute("pagesetup", &json!({"layout": "Layout1", "landscape": false})).unwrap();
let page = s.doc().unwrap().layout("Layout1").unwrap().page.clone();
assert_eq!(page.margins_mm, [2.0, 3.0, 4.0, 1.0], "the landscape margins turn back to the portrait axes");
s.execute("pagesetup", &json!({"layout": "Layout1", "landscape": true})).unwrap();
let page = s.doc().unwrap().layout("Layout1").unwrap().page.clone();
assert_eq!(page.margins_mm, [1.0, 2.0, 3.0, 4.0], "the second turn restores the landscape margins");
}
#[test]
/// PLOT and EXPORTPDF hand the hook the device's output, resolution, paper and margins, and a
/// layout plots with its own page setup; unknown devices and papers are refused.
@@ -239,3 +341,27 @@ fn plotwindow_picks_both_corners_with_ortho_on() {
let c = hover(&mut s, Vec2::new(10.0, 4.0));
assert!(c.near(Vec2::new(10.0, 0.0), 1e-9), "ortho keeps the line horizontal: {c:?}");
}
#[test]
fn paper_margins_are_checked_against_their_own_axis() {
let mut s = session();
s.execute("plottermanager", &json!({"action": "new", "name": "Banner Printer", "output": "png"})).unwrap();
let paper = |margins: Value| json!({"action": "set", "name": "Banner Printer", "paper": {"name": "Banner", "width": 400, "height": 200, "margins": margins}});
// 250 mm of bottom + top margin leaves no printable height on a 200 mm paper.
assert!(s.execute("plottermanager", &paper(json!([0, 250, 0, 0]))).is_err());
// 300 mm of left + right margin still leaves 100 mm of printable width on a 400 mm paper.
assert!(s.execute("plottermanager", &paper(json!([150, 0, 150, 0]))).is_ok());
// The paper is stored portrait and the margins turn with it: the 100 x 200 printable area
// of the requested sheet stays 200 x 100 on the stored 200 x 400 one.
let stored = s.execute("plottermanager", &json!({"action": "get", "name": "Banner Printer"})).unwrap();
let banner = stored["papers"].as_array().unwrap().iter().find(|p| p["name"] == "Banner").unwrap().clone();
assert_eq!((banner["width"].as_f64().unwrap(), banner["height"].as_f64().unwrap()), (200.0, 400.0));
assert_eq!(banner["margins"], json!([0.0, 150.0, 0.0, 150.0]));
// Setting the same sheet again without margins keeps them: the stored margins belong to the
// portrait sheet and turn back before the per-axis check.
s.execute("plottermanager", &json!({"action": "set", "name": "Banner Printer", "paper": {"name": "Banner", "width": 400, "height": 200}}))
.unwrap();
let stored = s.execute("plottermanager", &json!({"action": "get", "name": "Banner Printer"})).unwrap();
let banner = stored["papers"].as_array().unwrap().iter().find(|p| p["name"] == "Banner").unwrap().clone();
assert_eq!(banner["margins"], json!([0.0, 150.0, 0.0, 150.0]));
}