mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 10:47:31 -06:00
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:
co-authored by
midasmatterhorn
FredApps
parent
9f553d6764
commit
a67a8b5edd
@@ -683,8 +683,21 @@ fn run_pagesetup(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// The standard size the layout had before this call, so that resubmitting it (the Plot
|
||||
// dialog's Apply to Layout always sends `paper`) keeps the layout's paper units.
|
||||
let old_paper = cadcraft_render::paper_size(&page.paper).map(|ps| ps.name);
|
||||
let turned = p.get("landscape").and_then(Value::as_bool).filter(|l| *l != page.landscape);
|
||||
// The orientation first: a paper taken from a device turns its margins to the sheet the page
|
||||
// names when `page_device` copies them.
|
||||
if let Some(l) = turned {
|
||||
page.landscape = l;
|
||||
}
|
||||
// A plotter's own papers (with their printable areas) come first.
|
||||
let device_paper = super::plotters::page_device(cmd, p, &mut page)?;
|
||||
// A page that keeps its paper keeps its margins, and the margins are measured against the
|
||||
// sheet's axes: turning the orientation turns them too (#1266). Explicit margins are already
|
||||
// on the new sheet's axes.
|
||||
if turned.is_some() && !device_paper && p.get("margins").is_none() {
|
||||
page.margins_mm =
|
||||
if page.landscape { super::plotters::landscape_margins(page.margins_mm) } else { super::plotters::portrait_margins(page.margins_mm) };
|
||||
}
|
||||
if let Some(paper) = p.get("paper").filter(|_| !device_paper) {
|
||||
let n = paper.as_str().ok_or_else(|| bad(cmd, "`paper` must be a paper name"))?;
|
||||
let ps = cadcraft_render::paper_size(n).ok_or_else(|| {
|
||||
@@ -723,9 +736,6 @@ fn run_pagesetup(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
(None, None) if p.get("width").is_none() && p.get("height").is_none() => {}
|
||||
_ => return Err(bad(cmd, "`width` and `height` (mm, 0-5080) are both required for a custom size")),
|
||||
}
|
||||
if let Some(l) = p.get("landscape").and_then(Value::as_bool) {
|
||||
page.landscape = l;
|
||||
}
|
||||
// Paper units: what one paper-space unit is on this layout, whatever the paper's own family
|
||||
// (an ARCH D sheet can be drawn in millimetres, #3249).
|
||||
if let Some(v) = p.get("paperUnits") {
|
||||
|
||||
@@ -215,11 +215,25 @@ fn device_paper<'a>(dev: &'a PlotterConfig, name: &str) -> Option<&'a DevicePape
|
||||
dev.paper(name).or_else(|| dev.paper(cadcraft_render::paper_size(name)?.name))
|
||||
}
|
||||
|
||||
/// A paper's stored portrait margins on the landscape sheet: the sheet's axes swap, so left and
|
||||
/// right become bottom and top (#1266).
|
||||
pub(crate) fn landscape_margins([l, b, r, t]: [f64; 4]) -> [f64; 4] {
|
||||
[t, l, b, r]
|
||||
}
|
||||
|
||||
/// The inverse: a landscape page's margins on the stored portrait sheet, for a page that keeps
|
||||
/// its paper when its orientation turns (#1266).
|
||||
pub(crate) fn portrait_margins([l, b, r, t]: [f64; 4]) -> [f64; 4] {
|
||||
[b, r, t, l]
|
||||
}
|
||||
|
||||
fn set_paper(page: &mut PageSetup, p: &DevicePaper) {
|
||||
page.paper = p.name.clone();
|
||||
page.width_mm = p.width;
|
||||
page.height_mm = p.height;
|
||||
page.margins_mm = p.margins;
|
||||
// The paper keeps its margins for its stored portrait sheet; the page measures them against
|
||||
// the sheet in the orientation it names, so they turn with it (#1266).
|
||||
page.margins_mm = if page.landscape { landscape_margins(p.margins) } else { p.margins };
|
||||
}
|
||||
|
||||
fn unknown_device(cmd: &str, name: &str) -> EngineError {
|
||||
@@ -354,9 +368,16 @@ pub(crate) fn plot_options(s: &Session, p: &Value, cmd: &str) -> Result<Value> {
|
||||
o.remove("paper");
|
||||
o.insert("width".into(), json!(pp.width));
|
||||
o.insert("height".into(), json!(pp.height));
|
||||
o.insert("margins".into(), json!(pp.margins));
|
||||
// The paper's margins belong to its stored portrait sheet; the plot draws on the sheet in
|
||||
// the orientation it names, so they turn to it (#1266).
|
||||
let landscape = p.get("landscape").and_then(Value::as_bool).unwrap_or_else(|| match &layout {
|
||||
Some(pg) => pg.landscape,
|
||||
None => dev.orientation == Orientation::Landscape,
|
||||
});
|
||||
let [l, b, r, t] = pp.margins;
|
||||
o.insert("margins".into(), json!(if landscape { [t, l, b, r] } else { [l, b, r, t] }));
|
||||
if p.get("landscape").is_none() && layout.is_none() {
|
||||
o.insert("landscape".into(), json!(dev.orientation == Orientation::Landscape));
|
||||
o.insert("landscape".into(), json!(landscape));
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
@@ -527,13 +548,22 @@ fn edit_device(cmd: &str, c: &mut PlotterConfig, p: &Value) -> Result<()> {
|
||||
_ => return Err(bad(cmd, "`paper.width` and `paper.height` (mm) are both required")),
|
||||
};
|
||||
let margins = match pv.get("margins") {
|
||||
None => existing.as_ref().map(|e| e.margins).unwrap_or([0.0; 4]),
|
||||
None => match existing.as_ref() {
|
||||
// Stored margins belong to the stored portrait sheet; naming the same sheet
|
||||
// landscape turns them back, so re-saving a paper does not refuse its own
|
||||
// margins.
|
||||
// Naming the sheet landscape turns the stored portrait margins to it, whatever
|
||||
// its new sides: sanitization turns them back for the stored (portrait) sheet.
|
||||
Some(e) if w > h => {
|
||||
let [l, b, r, t] = e.margins;
|
||||
[t, l, b, r]
|
||||
}
|
||||
_ => existing.as_ref().map(|e| e.margins).unwrap_or([0.0; 4]),
|
||||
},
|
||||
Some(m) => {
|
||||
let a: Vec<f64> = m.as_array().map(|a| a.iter().filter_map(Value::as_f64).collect()).unwrap_or_default();
|
||||
match a[..] {
|
||||
[l, b, r, t] if a.iter().all(|v| v.is_finite() && (0.0..=1000.0).contains(v)) && l + r < w.min(h) && b + t < w.max(h) => {
|
||||
[l, b, r, t]
|
||||
}
|
||||
[l, b, r, t] if a.iter().all(|v| v.is_finite() && (0.0..=1000.0).contains(v)) && l + r < w && b + t < h => [l, b, r, t],
|
||||
_ => return Err(bad(cmd, "`paper.margins` must be [left, bottom, right, top] in mm, leaving a printable area")),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user