mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-04 22:49:54 +00:00
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12ff59a960 | ||
|
|
48fca374b3 | ||
|
|
5833546550 | ||
|
|
5d43bb5414 | ||
|
|
63f5623990 | ||
|
|
a830b61074 | ||
|
|
b6a4fd1266 |
@@ -773,6 +773,10 @@ pub struct SectionProps {
|
||||
pub page_w: f32,
|
||||
pub page_h: f32,
|
||||
pub landscape: bool,
|
||||
/// Top and bottom margins, points. Negative is Word's exact margin (`w:pgMar` `w:top` /
|
||||
/// `w:bottom` below zero, ECMA-376 §17.6.11; DOC `dyaTop` / `dyaBottom`): the text starts
|
||||
/// (ends) at the absolute value from the page edge even when the header (footer) is taller,
|
||||
/// and overlaps it.
|
||||
pub margin_top: f32,
|
||||
pub margin_bottom: f32,
|
||||
pub margin_left: f32,
|
||||
@@ -912,17 +916,21 @@ impl SectionProps {
|
||||
pub fn text_width(&self) -> f32 {
|
||||
(self.page_w - self.margin_left - self.margin_right - self.gutter).max(18.0)
|
||||
}
|
||||
/// Height of the text area between the margins, points (a negative, exact margin counts at
|
||||
/// its size).
|
||||
pub fn text_height(&self) -> f32 {
|
||||
(self.page_h - self.margin_top - self.margin_bottom).max(18.0)
|
||||
(self.page_h - self.margin_top.abs() - self.margin_bottom.abs()).max(18.0)
|
||||
}
|
||||
/// Swap to landscape/portrait (keeps margins relative to the paper as Word does: rotates them).
|
||||
/// An exact (negative) top or bottom turns into a side at its size, as in Word: only the top
|
||||
/// and bottom can be exact.
|
||||
pub fn set_landscape(&mut self, on: bool) {
|
||||
if on == self.landscape {
|
||||
return;
|
||||
}
|
||||
self.landscape = on;
|
||||
std::mem::swap(&mut self.page_w, &mut self.page_h);
|
||||
let (t, b, l, r) = (self.margin_top, self.margin_bottom, self.margin_left, self.margin_right);
|
||||
let (t, b, l, r) = (self.margin_top.abs(), self.margin_bottom.abs(), self.margin_left, self.margin_right);
|
||||
if on {
|
||||
self.margin_top = l;
|
||||
self.margin_bottom = r;
|
||||
|
||||
@@ -520,20 +520,22 @@ fn margins(s: &mut Session, v: &Value) -> CmdResult {
|
||||
None => return Err(CmdError::Params(format!("unknown preset `{x}`"))),
|
||||
},
|
||||
None => {
|
||||
// A negative margin given counts as zero; one left out keeps its value, so an exact
|
||||
// (negative) top or bottom from the file stays exact when another side changes, as
|
||||
// in Word.
|
||||
let c = sect(s);
|
||||
let given = |k: &str| p::f32(v, k).map(|m| m.max(0.0));
|
||||
(
|
||||
p::f32(v, "top").unwrap_or(c.margin_top),
|
||||
p::f32(v, "bottom").unwrap_or(c.margin_bottom),
|
||||
p::f32(v, "left").unwrap_or(c.margin_left),
|
||||
p::f32(v, "right").unwrap_or(c.margin_right),
|
||||
given("top").unwrap_or(c.margin_top),
|
||||
given("bottom").unwrap_or(c.margin_bottom),
|
||||
given("left").unwrap_or(c.margin_left.max(0.0)),
|
||||
given("right").unwrap_or(c.margin_right.max(0.0)),
|
||||
)
|
||||
}
|
||||
};
|
||||
// Negative margins count as zero before the fit check, so one can't offset another.
|
||||
let (t, b, l, r) = (t.max(0.0), b.max(0.0), l.max(0.0), r.max(0.0));
|
||||
let gutter = p::f32(v, "gutter").map(|g| g.max(0.0));
|
||||
let cur = sect(s);
|
||||
if !margins_fit((cur.page_w, cur.page_h), (t, b, l, r), gutter.unwrap_or(cur.gutter)) {
|
||||
if !margins_fit((cur.page_w, cur.page_h), (t.abs(), b.abs(), l, r), gutter.unwrap_or(cur.gutter)) {
|
||||
return Err(CmdError::Params("margins leave no room for text".into()));
|
||||
}
|
||||
// A preset picks mirrored or not, as does `mirror` (Page Setup's Multiple pages); plain numbers
|
||||
@@ -588,7 +590,7 @@ fn size(s: &mut Session, v: &Value) -> CmdResult {
|
||||
sections
|
||||
.iter()
|
||||
.find(|(e, _)| *e == block)
|
||||
.is_none_or(|(_, x)| margins_fit(oriented(x), (x.margin_top, x.margin_bottom, x.margin_left, x.margin_right), x.gutter))
|
||||
.is_none_or(|(_, x)| margins_fit(oriented(x), (x.margin_top.abs(), x.margin_bottom.abs(), x.margin_left, x.margin_right), x.gutter))
|
||||
};
|
||||
if !selected_section_ends(s).into_iter().all(fits) {
|
||||
return Err(CmdError::Params(MARGINS_DONT_FIT.into()));
|
||||
|
||||
@@ -433,3 +433,26 @@ fn page_number_format_keeps_or_clears_the_start() {
|
||||
assert_eq!(back["pageNumFormat"], "decimal");
|
||||
assert_eq!(back["pageNumStart"], 1);
|
||||
}
|
||||
|
||||
/// An exact (negative) top or bottom margin survives Margins and Orientation: changing another
|
||||
/// margin keeps it exact, and turning the page moves its size, as a plain margin, to the side it
|
||||
/// goes to.
|
||||
#[test]
|
||||
fn exact_margins_survive_margins_and_orientation() {
|
||||
let mut s = doc();
|
||||
s.doc.last_section.margin_top = -60.0;
|
||||
s.doc.last_section.margin_bottom = -40.0;
|
||||
s.run("layout.margins", &json!({"left": 80.0})).unwrap();
|
||||
let x = &s.doc.last_section;
|
||||
assert_eq!((x.margin_top, x.margin_bottom, x.margin_left), (-60.0, -40.0, 80.0));
|
||||
// A negative margin given still counts as zero.
|
||||
s.run("layout.margins", &json!({"top": -30.0})).unwrap();
|
||||
assert_eq!((s.doc.last_section.margin_top, s.doc.last_section.margin_bottom), (0.0, -40.0));
|
||||
s.doc.last_section.margin_top = -60.0;
|
||||
s.run("layout.orientation", &json!({"value": "landscape"})).unwrap();
|
||||
let x = &s.doc.last_section;
|
||||
assert!(x.margin_left >= 0.0 && x.margin_right >= 0.0, "{x:?}");
|
||||
let mut sides = [x.margin_left, x.margin_right];
|
||||
sides.sort_by(f32::total_cmp);
|
||||
assert_eq!(sides, [40.0, 60.0], "the exact margins' sizes go to the sides");
|
||||
}
|
||||
|
||||
@@ -563,7 +563,7 @@ fn box_float_table_rect(
|
||||
Anchor::Page => Some((0.0, s.page_h)),
|
||||
Anchor::Margin => {
|
||||
let mut reference = s.clone();
|
||||
reference.margin_top = fr.margin_top.unwrap_or(s.margin_top);
|
||||
reference.margin_top = fr.margin_top.unwrap_or(s.margin_top.abs());
|
||||
Some((reference.margin_top, reference.text_height()))
|
||||
}
|
||||
_ => None,
|
||||
|
||||
@@ -82,7 +82,7 @@ pub(crate) fn float_rect(frame: Option<PageFrame>, col: (f32, f32), y0: f32, w:
|
||||
};
|
||||
let page_y = |f: &PageFrame| {
|
||||
let s = f.sect;
|
||||
let top = f.margin_top.unwrap_or(s.margin_top);
|
||||
let top = f.margin_top.unwrap_or(s.margin_top.abs());
|
||||
match float.v_rel {
|
||||
Anchor::Page => Some((0.0, s.page_h)),
|
||||
Anchor::Margin => {
|
||||
@@ -90,8 +90,8 @@ pub(crate) fn float_rect(frame: Option<PageFrame>, col: (f32, f32), y0: f32, w:
|
||||
reference.margin_top = top;
|
||||
Some((top, reference.text_height()))
|
||||
}
|
||||
Anchor::TopMargin | Anchor::InsideMargin => Some((0.0, s.margin_top)),
|
||||
Anchor::BottomMargin | Anchor::OutsideMargin => Some((s.page_h - s.margin_bottom, s.margin_bottom)),
|
||||
Anchor::TopMargin | Anchor::InsideMargin => Some((0.0, s.margin_top.abs())),
|
||||
Anchor::BottomMargin | Anchor::OutsideMargin => Some((s.page_h - s.margin_bottom.abs(), s.margin_bottom.abs())),
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
|
||||
@@ -386,7 +386,7 @@ impl PageBuilder<'_> {
|
||||
self.number = self.restart_at.take().unwrap_or_else(|| self.number.saturating_add(1));
|
||||
let (w, h) = if self.web { (s.page_w, f32::MAX / 4.0) } else { (s.page_w, s.page_h) };
|
||||
let bottom = self.body_bottom(body_top);
|
||||
self.leading_floor = (s.page_h - s.margin_bottom).min(self.foot_top);
|
||||
self.leading_floor = (s.page_h - s.margin_bottom.abs()).min(self.foot_top);
|
||||
let body = Rect::new(s.margin_left + s.gutter, body_top, s.text_width(), (bottom - body_top).max(36.0));
|
||||
let mut decor = Vec::new();
|
||||
if !self.web {
|
||||
@@ -473,8 +473,12 @@ impl PageBuilder<'_> {
|
||||
self.bottom >= self.leading_floor - 0.01
|
||||
}
|
||||
/// The bottom of the body on a page started with its body at `body_top`: a footer taller than
|
||||
/// the bottom margin pushes it up (Word: no gap), leaving it at least a little room.
|
||||
/// the bottom margin pushes it up (Word: no gap), leaving it at least a little room. A
|
||||
/// negative bottom margin is exact: the body ends at its size and the footer overlaps it.
|
||||
fn body_bottom(&self, body_top: f32) -> f32 {
|
||||
if self.sect.margin_bottom < 0.0 {
|
||||
return self.sect.page_h + self.sect.margin_bottom;
|
||||
}
|
||||
// ODF keeps the footer's spacing above a footer too tall for the bottom margin.
|
||||
let gap = if self.sect.footer_gap.is_finite() { self.sect.footer_gap.clamp(0.0, 1584.0) } else { 0.0 };
|
||||
(self.sect.page_h - self.sect.margin_bottom).min((self.foot_top - gap).max(body_top + 36.0))
|
||||
@@ -569,8 +573,12 @@ pub(crate) fn item_bottom(y: f32, para: &ParaLayout, l0: usize, l1: usize) -> Op
|
||||
Some(y + l.top + l.height - f.top)
|
||||
}
|
||||
|
||||
/// Where body text starts: the top margin, or below the header if it's taller.
|
||||
/// Where body text starts: the top margin, or below the header if it's taller. A negative top
|
||||
/// margin is exact (ECMA-376 §17.6.11): the text starts at its size and the header overlaps it.
|
||||
pub(crate) fn body_top_for(ctx: &mut Ctx, sect: &SectionProps, header: Option<u32>) -> f32 {
|
||||
if sect.margin_top < 0.0 {
|
||||
return -sect.margin_top;
|
||||
}
|
||||
let Some(id) = header else { return sect.margin_top };
|
||||
let Some(part) = ctx.doc.parts.get(&id) else { return sect.margin_top };
|
||||
let blocks = part.blocks.clone();
|
||||
|
||||
@@ -291,7 +291,7 @@ fn section_tops(ctx: &mut Ctx, hf: &HfLinks, (si, sect): (usize, &SectionProps),
|
||||
let (heads, feet) = (hf.heads.set(si), hf.feet.set(si));
|
||||
let hb = |i: Option<u32>| hf.defining_width(false, si, i);
|
||||
let fb = |i: Option<u32>| hf.defining_width(true, si, i);
|
||||
let body = if fixed { sect.margin_top } else { with_pct_base(ctx, hb(heads.default), |c| body_top_for(c, sect, heads.default)) };
|
||||
let body = if fixed { sect.margin_top.abs() } else { with_pct_base(ctx, hb(heads.default), |c| body_top_for(c, sect, heads.default)) };
|
||||
let first = if fixed || !sect.title_page { body } else { with_pct_base(ctx, hb(heads.first), |c| body_top_for(c, sect, heads.first)) };
|
||||
let foot = if fixed { f32::MAX } else { with_pct_base(ctx, fb(feet.default), |c| foot_top_for(c, sect, feet.default)) };
|
||||
let first_foot = if fixed || !sect.title_page { foot } else { with_pct_base(ctx, fb(feet.first), |c| foot_top_for(c, sect, feet.first)) };
|
||||
@@ -625,7 +625,7 @@ pub(crate) fn layout_pass(
|
||||
section_endnotes(&mut ctx, &mut pb, &mut endnotes, (sect, si, block));
|
||||
}
|
||||
if pb.pages.is_empty() {
|
||||
pb.new_page(0, sect_ref.margin_top);
|
||||
pb.new_page(0, sect_ref.margin_top.abs());
|
||||
}
|
||||
// The rest of the endnotes after the last paragraph.
|
||||
let rest: Vec<u32> = endnotes.into_iter().map(|(id, _)| id).collect();
|
||||
|
||||
@@ -395,7 +395,7 @@ fn place_floating_table(ctx: &mut Ctx, pb: &mut PageBuilder, tl: &table::TableLa
|
||||
// The text a table anchored to it stands in: where the next paragraph starts.
|
||||
let v_area = match f.v_rel {
|
||||
Anchor::Page => (0.0, s.page_h),
|
||||
Anchor::Margin => (s.margin_top, s.text_height()),
|
||||
Anchor::Margin => (s.margin_top.abs(), s.text_height()),
|
||||
_ => (pb.y, 0.0),
|
||||
};
|
||||
let x = float_table_x(pb, w, f, legacy, ctx.mirror);
|
||||
|
||||
@@ -1167,3 +1167,40 @@ fn a_page_reference_into_nested_text_boxes_stops_where_layout_does() {
|
||||
let output = layout(&d, &mut LayoutCache::new(), &LayoutOptions { output_page_refs: true, ..Default::default() });
|
||||
assert_eq!(shown_in(&output, 0, StoryRef::Body), ["2", "2", "9"]);
|
||||
}
|
||||
|
||||
/// Word's exact margins: a negative top (bottom) margin starts (ends) the body at its size from
|
||||
/// the page edge even when the header (footer) is taller, and the two overlap (ECMA-376
|
||||
/// §17.6.11, `w:pgMar` below zero; DOC `dyaTop` / `dyaBottom`), so the same text fits on fewer
|
||||
/// pages. Positive margins stay "at least": a taller header or footer pushes the body.
|
||||
#[test]
|
||||
fn negative_margins_are_exact_and_let_the_header_and_footer_overlap_the_body() {
|
||||
crate::test_fonts();
|
||||
let make = |top: f32, bottom: f32| {
|
||||
let mut d = Document::from_text(&"Body line\n".repeat(100));
|
||||
d.last_section.page_w = 595.3;
|
||||
d.last_section.page_h = 841.9;
|
||||
d.last_section.margin_top = top;
|
||||
d.last_section.margin_bottom = bottom;
|
||||
d.last_section.header = 36.0;
|
||||
d.last_section.footer = 36.0;
|
||||
let head = d.add_part(PartKind::Header, paras("Header line", 6));
|
||||
let foot = d.add_part(PartKind::Footer, paras("Footer line", 6));
|
||||
d.last_section.headers.default = Some(head);
|
||||
d.last_section.footers.default = Some(foot);
|
||||
let l = lay(&d);
|
||||
let body = extent(&l.pages[0].items, StoryRef::Body).unwrap();
|
||||
let header = extent(&l.pages[0].header, StoryRef::Part(head)).unwrap();
|
||||
let footer = extent(&l.pages[0].footer, StoryRef::Part(foot)).unwrap();
|
||||
(l.pages.len(), body, header, footer)
|
||||
};
|
||||
let (pages, body, header, footer) = make(-60.0, -60.0);
|
||||
assert!(header.1 > 60.0 && footer.0 < 841.9 - 60.0, "the bands reach past the margins: {header:?} {footer:?}");
|
||||
assert!((body.0 - 60.0).abs() < 0.5, "the body starts at the exact top margin, over the header: {body:?}");
|
||||
assert!(
|
||||
body.1 <= 841.9 - 60.0 + 0.01 && body.1 > footer.0,
|
||||
"the body runs down to the exact bottom margin, over the footer: {body:?} {footer:?}"
|
||||
);
|
||||
let (at_least_pages, at_least_body, at_least_header, at_least_footer) = make(60.0, 60.0);
|
||||
assert!(at_least_body.0 >= at_least_header.1 - 0.01 && at_least_body.1 <= at_least_footer.0 + 0.01, "{at_least_body:?}");
|
||||
assert!(pages < at_least_pages, "exact margins: {pages} pages, at least: {at_least_pages}");
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ pub(crate) fn unturned_sect(s: &SectionProps, body_top: f32) -> SectionProps {
|
||||
v.page_w = s.page_h;
|
||||
v.page_h = s.page_w;
|
||||
v.gutter = 0.0;
|
||||
let (top, right, bottom, left) = (body_top, s.margin_right, s.margin_bottom, s.margin_left + s.gutter);
|
||||
let (top, right, bottom, left) = (body_top, s.margin_right, s.margin_bottom.abs(), s.margin_left + s.gutter);
|
||||
// The unturned frame's (u, v) lands at (W - v, u) for `Down` and (v, H - u) for `Up`: its
|
||||
// top edge is the real right edge (left for `Up`), its left edge the real top (bottom).
|
||||
match s.text_direction {
|
||||
|
||||
@@ -69,12 +69,7 @@ pub(crate) fn page_rulers(app: &mut WordApp, ui: &mut Ui, out: &craft::pages::ui
|
||||
// A section whose columns `column_drag` refuses shows its gaps without grips.
|
||||
let gaps_draggable = cg.as_ref().is_none_or(|cg| super::column_ruler::draggable(&cg.sect));
|
||||
|
||||
let mut margins = Margins::uniform(0.0);
|
||||
margins.top = f64::from(sect.margin_top);
|
||||
margins.bottom = f64::from(sect.margin_bottom);
|
||||
margins.left = f64::from(sect.margin_left);
|
||||
margins.right = f64::from(sect.margin_right);
|
||||
let edges = PageEdges::new(margins);
|
||||
let edges = page_edges(§);
|
||||
let mirrored = app.session.doc.settings.mirror_margins && page.number.is_multiple_of(2);
|
||||
let unit = DocUnit::new(unit_kind(app.session.units()), 96.0);
|
||||
let kind = ruler_align(app.canvas.tab_kind).unwrap_or_default();
|
||||
@@ -260,8 +255,21 @@ fn indent_params(shown: &RulerValues, v: &RulerValues, col_x: f32, w: f32) -> Op
|
||||
(!p.is_empty()).then_some(Value::Object(p))
|
||||
}
|
||||
|
||||
/// The page's margins as the ruler shows and clamps them. An exact (negative) top or bottom margin (#7107) counts at
|
||||
/// its size, where the layout puts the body's edge: it is shown there, and a drag of the opposite edge keeps an inch
|
||||
/// of text beside it, as `layout.margins` checks it. Only a moved edge is sent back ([`margin_params`]), so the exact
|
||||
/// margin stays exact unless it is the one dragged.
|
||||
pub(super) fn page_edges(sect: &wordcraft_doc::section::SectionProps) -> PageEdges {
|
||||
let mut margins = Margins::uniform(0.0);
|
||||
margins.top = f64::from(sect.margin_top.abs());
|
||||
margins.bottom = f64::from(sect.margin_bottom.abs());
|
||||
margins.left = f64::from(sect.margin_left);
|
||||
margins.right = f64::from(sect.margin_right);
|
||||
PageEdges::new(margins)
|
||||
}
|
||||
|
||||
/// `layout.margins` params for the margins `v` moved from `shown`.
|
||||
fn margin_params(shown: &PageEdges, v: &PageEdges) -> Option<Value> {
|
||||
pub(super) fn margin_params(shown: &PageEdges, v: &PageEdges) -> Option<Value> {
|
||||
let (a, b) = (&shown.margins, &v.margins);
|
||||
let mut p = Map::new();
|
||||
for (key, was, now) in [("left", a.left, b.left), ("right", a.right, b.right), ("top", a.top, b.top), ("bottom", a.bottom, b.bottom)] {
|
||||
|
||||
@@ -137,6 +137,42 @@ fn dragging_the_bottom_margin_on_the_ruler_keeps_the_view_and_follows_the_pointe
|
||||
assert_eq!(h.state().session.undo_depth(), depth + 1, "one undo step for the drag");
|
||||
}
|
||||
|
||||
/// #7107: an exact (negative) top or bottom margin counts at its size on the vertical ruler, so a
|
||||
/// drag of the opposite edge keeps an inch of text beside it and `layout.margins` accepts it, and
|
||||
/// the exact margin, not dragged, stays exact.
|
||||
#[test]
|
||||
fn ruler_margin_drags_clamp_against_an_exact_opposite_margin() {
|
||||
use crate::canvas::rulers::{margin_params, page_edges};
|
||||
use craft::pages::ui::{MarginEdge, MarginRules, dragged_margin};
|
||||
|
||||
let s = wordcraft_doc::section::SectionProps {
|
||||
page_w: 612.0,
|
||||
page_h: 792.0,
|
||||
margin_left: 72.0,
|
||||
margin_right: 72.0,
|
||||
margin_top: -72.0,
|
||||
margin_bottom: -72.0,
|
||||
gutter: 0.0,
|
||||
..Default::default()
|
||||
};
|
||||
let rules = MarginRules::new((612.0, 792.0), page_edges(&s));
|
||||
assert_eq!(dragged_margin(MarginEdge::Bottom, 72.0, &rules).points, 792.0 - 72.0 - 72.0);
|
||||
assert_eq!(dragged_margin(MarginEdge::Top, 720.0, &rules).points, 792.0 - 72.0 - 72.0);
|
||||
|
||||
// Through the command: the clamped value fits beside the exact top margin, which stays exact.
|
||||
let mut app = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), Default::default());
|
||||
app.session.doc.last_section.margin_top = -72.0;
|
||||
let sect = wordcraft_engine::cmd::page::sect(&app.session);
|
||||
let shown = page_edges(§);
|
||||
let moved = shown.with(dragged_margin(MarginEdge::Bottom, 72.0, &MarginRules::new((f64::from(sect.page_w), f64::from(sect.page_h)), shown)));
|
||||
let p = margin_params(&shown, &moved).expect("the bottom margin moved");
|
||||
assert!(p.get("top").is_none(), "the exact top margin is not sent: {p}");
|
||||
app.run("layout.margins", p).unwrap();
|
||||
let after = wordcraft_engine::cmd::page::sect(&app.session);
|
||||
assert_eq!(after.margin_top, -72.0);
|
||||
assert!(after.page_h - after.margin_top.abs() - after.margin_bottom >= 72.0 - 0.01);
|
||||
}
|
||||
|
||||
/// #1878: in Multiple Pages both rulers belong to the caret's page: the margin splitters of the
|
||||
/// horizontal ruler lie over that page's width and the vertical ruler's over its height, and
|
||||
/// they follow the caret to another page of the row and to the next row.
|
||||
|
||||
@@ -25,6 +25,10 @@ pub(crate) struct PaperLabels {
|
||||
/// `layout.size` keeps the section's orientation; `layout.orientation` then turns the page and
|
||||
/// its margins to what the dialog shows. A size equal to the opened one (either way round,
|
||||
/// within 0.01 pt) doesn't run `layout.size`.
|
||||
/// An exact (negative) top or bottom the dialog still shows as the section has it, on a page
|
||||
/// not turned, is left out of `layout.margins`, which keeps a side left out (a negative given
|
||||
/// counts as zero): changing another side keeps it exact, as the ruler does. The fit checks
|
||||
/// count an exact margin at its size.
|
||||
pub(super) fn page_layout_commands(
|
||||
app: &WordApp,
|
||||
(paper, was): ((f32, f32), (f32, f32)),
|
||||
@@ -33,6 +37,20 @@ pub(super) fn page_layout_commands(
|
||||
) -> Result<Vec<(String, Value)>, &'static str> {
|
||||
use wordcraft_engine::cmd::page::{MARGINS_DONT_FIT, margins_fit, sect};
|
||||
let cur = sect(&app.session);
|
||||
let kept = |shown: f32, now: f32| landscape == cur.landscape && now < 0.0 && shown == now;
|
||||
let (keep_top, keep_bottom) = (kept(m.0, cur.margin_top), kept(m.1, cur.margin_bottom));
|
||||
let sizes = |(t, b, l, r): (f32, f32, f32, f32)| (t.abs(), b.abs(), l, r);
|
||||
// `layout.margins` with the sides it sets: all but the exact ones kept.
|
||||
let set_margins = |(t, b, l, r): (f32, f32, f32, f32)| {
|
||||
let mut v = json!({"top": t, "bottom": b, "left": l, "right": r});
|
||||
for (keep, k) in [(keep_top, "top"), (keep_bottom, "bottom")] {
|
||||
if keep && let Some(o) = v.as_object_mut() {
|
||||
o.remove(k);
|
||||
}
|
||||
}
|
||||
("layout.margins".to_string(), v)
|
||||
};
|
||||
let m = sizes(m);
|
||||
if !margins_fit(paper, m, cur.gutter) {
|
||||
return Err(MARGINS_DONT_FIT);
|
||||
}
|
||||
@@ -41,8 +59,8 @@ pub(super) fn page_layout_commands(
|
||||
let new_size = (w0 - w1).abs() >= 0.01 || (h0 - h1).abs() >= 0.01;
|
||||
let size = ("layout.size".to_string(), json!({"width": paper.0, "height": paper.1}));
|
||||
let orientation = ("layout.orientation".to_string(), json!({"value": if landscape { "landscape" } else { "portrait" }}));
|
||||
let margins = ("layout.margins".to_string(), json!({"top": m.0, "bottom": m.1, "left": m.2, "right": m.3}));
|
||||
let old_margins = (cur.margin_top, cur.margin_bottom, cur.margin_left, cur.margin_right);
|
||||
let margins = set_margins(m);
|
||||
let old_margins = sizes((cur.margin_top, cur.margin_bottom, cur.margin_left, cur.margin_right));
|
||||
Ok(if !new_size || margins_fit(turned(cur.landscape, paper), old_margins, cur.gutter) {
|
||||
let mut v = if new_size { vec![size] } else { Vec::new() };
|
||||
v.extend([orientation, margins]);
|
||||
@@ -50,7 +68,7 @@ pub(super) fn page_layout_commands(
|
||||
} else if margins_fit(turned(landscape, (cur.page_w, cur.page_h)), m, cur.gutter) {
|
||||
vec![orientation, margins, size]
|
||||
} else {
|
||||
vec![orientation, ("layout.margins".to_string(), json!({"top": 0.0, "bottom": 0.0, "left": 0.0, "right": 0.0})), size, margins]
|
||||
vec![orientation, set_margins((0.0, 0.0, 0.0, 0.0)), size, margins]
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1140,6 +1140,27 @@ fn page_setup_refuses_margins_that_dont_fit_and_applies_smaller_ones_first() {
|
||||
assert_eq!((s.page_w.round(), s.page_h.round(), s.margin_left.round(), s.margin_top.round()), (100.0, 500.0, 18.0, 18.0));
|
||||
}
|
||||
|
||||
/// #7108: an exact (negative) top or bottom margin Page Setup's OK didn't change stays exact
|
||||
/// when another side changes, as with the ruler and `layout.margins`; one changed in the
|
||||
/// dialog takes the value shown.
|
||||
#[test]
|
||||
fn page_setup_keeps_an_exact_margin_it_didnt_change() {
|
||||
let mut h = app_harness(app_with("Page"));
|
||||
h.run_steps(2);
|
||||
let sect = |h: &egui_kittest::Harness<'_, WordApp>| wordcraft_engine::cmd::page::sect(&h.state().session);
|
||||
{
|
||||
let x = &mut h.state_mut().session.doc.last_section;
|
||||
(x.margin_top, x.margin_bottom) = (-60.0, -40.0);
|
||||
}
|
||||
page_setup(&mut h, |f| f.margins.left = 80.0);
|
||||
assert!(!is_open(h.state(), "pageSetup"));
|
||||
let x = sect(&h);
|
||||
assert_eq!((x.margin_top, x.margin_bottom, x.margin_left), (-60.0, -40.0, 80.0), "the exact margins kept");
|
||||
page_setup(&mut h, |f| f.margins.top = 50.0);
|
||||
let x = sect(&h);
|
||||
assert_eq!((x.margin_top, x.margin_bottom, x.margin_left), (50.0, -40.0, 80.0), "the changed one set, the other kept");
|
||||
}
|
||||
|
||||
/// #1433: one OK in Page Setup is one undo step, whichever order its commands run in (the
|
||||
/// size first; the margins first; margins of 0 in between), and Undo restores the section.
|
||||
#[test]
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user