Stack u3: #1866: a model plot on an inch sheet is in inches whatever MEASUREMENT says

apply_model_paper_units could only clear paper_mm, and an unset unit falls back to
MEASUREMENT, so in a drawing with MEASUREMENT 1 a model plot on ANSI A at 1:1 came out in
millimetres, 25.4 times too small (the case CodeRabbit raised and the review confirmed). With
PageSetup::paper_mm now an Option (#3257), the model plot's units are set explicitly.
This commit is contained in:
Tanish Manku
2026-11-19 15:23:13 -07:00
parent 7001f74839
commit 8b19ba1ff9
2 changed files with 21 additions and 8 deletions
+12 -1
View File
@@ -93,6 +93,17 @@ fn model_plot_takes_units_from_the_paper() {
assert_pt(line_pt(&pdf), TEN_IN_PT);
}
#[test]
fn model_plot_on_an_inch_sheet_is_in_inches_whatever_measurement_says() {
// MEASUREMENT 1 (an ISO-template drawing) must not turn an inch sheet's units into
// millimetres: a 10-unit line on ANSI A at 1:1 is 10 inches for every drawing unit.
for (insunits, measurement) in [(4, 1), (0, 1), (1, 1), (4, 0)] {
let pdf = plot_on(&drawing(insunits, measurement), "ANSI A");
assert_media(&pdf, ANSI_A_PT);
assert_pt(line_pt(&pdf), TEN_IN_PT);
}
}
#[test]
fn layout_plot_keeps_its_page_setup_units() {
// The @echelon regression: a metric drawing whose layout has a page setup in inches. The
@@ -106,7 +117,7 @@ fn layout_plot_keeps_its_page_setup_units() {
height_mm: 297.0,
margins_mm: [0.0; 4],
landscape: false,
paper_mm: false,
paper_mm: Some(false),
..PageSetup::default()
};
d.layouts.push(l);
+9 -7
View File
@@ -131,9 +131,11 @@ pub fn model_page(d: &Drawing) -> PageSetup {
/// Set a model-space plot's paper units once its paper is known: the paper's own units decide
/// (ISO and user millimetre sizes are millimetres, ANSI and ARCH sheets inches), as a model-space
/// page setup's "plot paper units" would. Layouts keep their stored page setup and go through
/// [`page_unit_mm`]; only model-space callers use this.
/// [`page_unit_mm`]; only model-space callers use this. The units are set explicitly either
/// way: an inch sheet left to follow `MEASUREMENT` would plot in millimetres in a drawing whose
/// `MEASUREMENT` is 1.
pub fn apply_model_paper_units(page: &mut PageSetup) {
page.paper_mm = model_unit_mm(page) == 1.0;
page.paper_mm = Some(model_unit_mm(page) == 1.0);
}
/// Millimetres per paper-space unit on a layout with page setup `page`: `1.0` when the page
@@ -258,7 +260,7 @@ mod tests {
let page = model_page(&d);
assert_eq!(page.paper, "ISO A4 (210.00 x 297.00 MM)");
assert_eq!((page.width_mm, page.height_mm), (210.0, 297.0));
assert!(page.paper_mm, "A4 in a metric drawing is in millimetres");
assert_eq!(page.paper_mm, Some(true), "A4 in a metric drawing is in millimetres");
// The length unit decides, not MEASUREMENT.
d.header.set_i64("MEASUREMENT", 0);
assert_eq!(model_page(&d).paper, "ISO A4 (210.00 x 297.00 MM)");
@@ -266,7 +268,7 @@ mod tests {
let page = model_page(&Drawing::new_imperial());
assert_eq!(page.paper, "ANSI A (8.50 x 11.00 Inches)");
assert!((page.width_mm - 215.9).abs() < 1e-9 && (page.height_mm - 279.4).abs() < 1e-9);
assert!(!page.paper_mm, "ANSI A in an imperial drawing is in inches");
assert_eq!(page.paper_mm, Some(false), "ANSI A in an imperial drawing is in inches");
}
#[test]
@@ -274,14 +276,14 @@ mod tests {
// The sheet decides, whatever the drawing's length unit is.
let mut a4 = PageSetup { paper: "ISO A4 (210.00 x 297.00 MM)".into(), ..PageSetup::default() };
apply_model_paper_units(&mut a4);
assert!(a4.paper_mm, "ISO A4 is a millimetre sheet");
assert_eq!(a4.paper_mm, Some(true), "ISO A4 is a millimetre sheet");
let mut ansi = PageSetup { paper: "ANSI A (8.50 x 11.00 Inches)".into(), ..PageSetup::default() };
apply_model_paper_units(&mut ansi);
assert!(!ansi.paper_mm, "ANSI A is an inch sheet");
assert_eq!(ansi.paper_mm, Some(false), "ANSI A is an inch sheet");
// A user size in millimetres is one to one.
let mut user = PageSetup { paper: "User (300.00 x 400.00 MM)".into(), ..PageSetup::default() };
apply_model_paper_units(&mut user);
assert!(user.paper_mm);
assert_eq!(user.paper_mm, Some(true));
}
#[test]