Compare commits

...
Author SHA1 Message Date
FredApps b92b7a40ad Merge remote-tracking branch 'origin/main' into fix/fit-one-page-freebsd
Signed-off-by: FredApps <FredApps@users.noreply.github.com>

# Conflicts:
#	crates/engine/src/tests/fit_one_page.rs
2026-11-18 09:51:35 +07:00
FredApps 37caf16978 Tests: two Fit One Page tests find where the document spills
A plain document asks for Aptos, which no test machine has, so its
sans stand-in differs between machines: DejaVu Sans on FreeBSD is
larger than the Linux runners' font. With fixed line counts the step
and budget tests overflowed by more than the fit may recover there,
and have failed every FreeBSD night since they were added. They now
take the most lines that fit and move the last paragraph to page 2
with space before it, so the overflow is under half a point anywhere.
2026-11-15 23:07:00 +01:00
+52 -14
View File
@@ -41,6 +41,41 @@ fn assert_fixture_font(s: &mut Session) {
assert!(runs > 0, "the font check must inspect laid-out body text");
}
/// A document that just spills onto a second page: the most lines `make` can put on one page,
/// then space before the last paragraph until it moves to page 2 ([`spill_last_paragraph`]).
/// The fixtures are set in the bundled [`FIXTURE_FONT`], but a fixed line count still depends on
/// that font's exact metrics and the page setup, so the tests find the spill point instead.
fn just_spilling(make: impl Fn(usize) -> Session) -> Session {
let mut lines = 10;
while make(lines + 1).layout().pages.len() < 2 {
lines += 1;
assert!(lines < 120, "{lines} lines still fit on one page");
}
let mut s = make(lines);
assert_eq!(s.layout().pages.len(), 1, "{lines} lines must fit on one page");
spill_last_paragraph(&mut s);
s
}
/// Adds space before the last paragraph, half a point at a time, until it moves to page 2. The
/// document then overflows by less than half a point, which any font reduction it is fitted with
/// brings back.
fn spill_last_paragraph(s: &mut Session) {
let mut before = match s.doc.body.last().map(|b| &**b) {
Some(wordcraft_doc::Block::Para(p)) => p.props.space_before.unwrap_or(0.0),
_ => 0.0,
};
while s.layout().pages.len() < 2 {
before += 0.5;
assert!(before <= 200.0, "space before the last paragraph never moved it to page 2");
if let Some(wordcraft_doc::Block::Para(p)) = s.doc.body.last_mut().map(std::sync::Arc::make_mut) {
p.props.space_before = Some(before);
}
s.touch();
s.relayout();
}
}
#[test]
fn fit_one_page_fails_on_single_page() {
let mut s = create_multiline_doc(5);
@@ -144,15 +179,20 @@ fn fit_one_page_respects_max_delta() {
#[test]
fn fit_one_page_respects_step_pt() {
let mut s = create_multiline_doc(27);
for b in &mut s.doc.body {
if let wordcraft_doc::Block::Para(p) = std::sync::Arc::make_mut(b) {
p.mark.size = Some(11.4);
for r in &mut p.runs {
r.props.size = Some(11.4);
let at_11_4 = |lines: usize| {
let mut s = create_multiline_doc(lines);
for b in &mut s.doc.body {
if let wordcraft_doc::Block::Para(p) = std::sync::Arc::make_mut(b) {
p.mark.size = Some(11.4);
for r in &mut p.runs {
r.props.size = Some(11.4);
}
}
}
}
s.touch();
s
};
let mut s = just_spilling(at_11_4);
assert_eq!(s.layout().pages.len(), 2, "must start as 2-page document at 11.4 pt");
assert_fixture_font(&mut s);
@@ -162,7 +202,7 @@ fn fit_one_page_respects_step_pt() {
let default_res = s.run("layout.fitOnePage", &json!({"maxDeltaPt": 0.4, "adjustSpacing": false}));
assert!(default_res.is_err(), "default step 0.5 must skip all candidates when maxDelta is 0.4");
// With stepPt = 0.1 under the same maxDeltaPt = 0.4, fine-grained steps (0.1..0.4) succeed (11.4 -> 11.0 pt).
// With stepPt = 0.1 under the same maxDeltaPt = 0.4, a fine-grained step (0.1 to 0.4 pt) succeeds.
s.doc = doc_backup;
let res = s.run("layout.fitOnePage", &json!({"maxDeltaPt": 0.4, "stepPt": 0.1, "adjustSpacing": false}));
assert!(res.is_ok(), "stepPt=0.1 must generate fine-grained candidates under maxDelta 0.4: {res:?}");
@@ -877,7 +917,7 @@ fn fit_one_page_tracks_reduced_complex_script_font_sizes() {
#[test]
fn fit_one_page_preserves_budget_across_document_serialization() {
let mut s = create_multiline_doc(26);
let mut s = just_spilling(create_multiline_doc);
assert_eq!(s.layout().pages.len(), 2);
assert_fixture_font(&mut s);
@@ -894,12 +934,10 @@ fn fit_one_page_preserves_budget_across_document_serialization() {
assert_eq!(reopened_doc.fit_state.as_ref().map(|f| f.cum_delta), Some(0.5));
assert!(reopened_doc.custom_props.is_empty(), "custom_props must remain empty");
// Reopen into a new session with additional spill
// Reopen into a new session with additional spill: the last paragraph moves to page 2 again
let mut s2 = Session::new(reopened_doc);
let extra_p = wordcraft_doc::Paragraph::with_text("Extra overflow paragraph.", CharProps::default());
s2.doc.body.push(wordcraft_doc::para_block(extra_p));
s2.touch();
s2.relayout();
assert_eq!(s2.layout().pages.len(), 1);
spill_last_paragraph(&mut s2);
// Second fit on reopened document preserves previous 0.5 delta, allowing only remaining 0.5 pt up to 1.0 cap
let res2 = s2.run("layout.fitOnePage", &json!({"maxDeltaPt": 0.5, "adjustSpacing": false}));