Compare commits

...
Author SHA1 Message Date
autocraft-bot[bot] c0ec0d83e1 Merge main (2f168645) into the pull request
The only conflict was the long "Last reviewed / Change" line in docs/lay

The only conflict was the long "Last reviewed / Change" line in docs/layout-parity.md. Both sides had put a new entry at the front of the change history, and the rest of the line was identical. I kept both entries. The pull request's entry (a section with a different first page that starts continuously) comes first, then main's hidden-mark entry (#3986) as "previously trivial", then the unchanged older history. No conflict markers are left in the repository. I did not build or run tests, because only documentation changed and the dependency fetch had failed.
2026-12-02 21:11:59 +00:00
Mauro MazzieriandClaude Opus 5.5 989fa17544 Merge main into claude/continuous-section-first-page-header (#5543)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-21 18:52:40 +01:00
Mauro MazzieriandClaude Opus 5.5 6eb03bf4ea Merge storytold/wordcraft main
Resolves the docs/layout-parity.md status line and revision history.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JesQG7ckr7g2qSfBwGkTqM
2026-11-17 15:50:22 +00:00
Mauro MazzieriandClaude Opus 5.5 892235af06 Different first page: a continuous section whose first paragraph runs over still began on the page before
Addresses the CodeRabbit review: the run-over case (with a regression test) and the even-page header in docs/layout-parity.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JesQG7ckr7g2qSfBwGkTqM
2026-11-11 16:53:42 +00:00
Mauro MazzieriandClaude Opus 5.5 e14f85661b Merge storytold/wordcraft main
Resolves the docs/layout-parity.md status line and revision history.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JesQG7ckr7g2qSfBwGkTqM
2026-11-11 16:44:02 +00:00
Mauro Mazzieri d3096ed0fb Merge remote-tracking branch 'origin/main' into claude/continuous-section-first-page-header
# Conflicts:
#	crates/layout/src/table_flow.rs
#	docs/layout-parity.md
2026-11-09 15:48:45 +01:00
Mauro MazzieriandClaude Opus 5.5 dc515b6128 Different first page: no first-page header on a continuous section's second page
A page is its section's first page, the one a different first page's header and footer go on,
only if the section begins on it. A page's section is the one at its top, so a section that
starts continuously part-way down a page was taken to begin on the next page, which then showed
the section's first-page header. Word shows the earlier section's header on the page where the
section starts and the section's default header from the next page on: the first-page header of
such a section is never seen (checked in Word for Windows 365 on a synthetic file).

begins_section compares the section's first block with the page's first block, and serves the
drawn headers and footers, header-table obstacles and footer-table wraps alike.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 10:36:33 +01:00
4 changed files with 108 additions and 13 deletions
+26 -9
View File
@@ -36,6 +36,27 @@ pub(crate) fn section_side(start: SectionStart, restart: Option<u32>, last: u32,
}
}
/// Whether `page` (after `prev`) is its section's first page, the one a different first page's
/// header and footer go on: the page where the section begins. A page's section is the one at its
/// top, so a section that starts continuously part-way down `prev` has no first page of its own:
/// Word shows the earlier section's header there and the section's default one from the next page
/// on, also when its first paragraph begins on `prev` and runs over. `sections` as
/// [`Document::sections`] gives them (each with its last block).
pub(crate) fn begins_section(sections: &[(usize, &SectionProps)], prev: Option<&Page>, page: &Page) -> bool {
if prev.is_some_and(|p| p.section == page.section) {
return false;
}
let start = page.section.checked_sub(1).and_then(|k| sections.get(k)).map_or(0, |(end, _)| end.saturating_add(1));
// A first paragraph (or table) that begins part-way down `prev` and runs over still began
// the section there.
let began_before = prev.is_some_and(|p| {
p.items
.iter()
.any(|it| matches!(it, Placed::Lines { story: StoryRef::Body, path, .. } if path.0.first().is_some_and(|&b| b as usize >= start)))
});
start >= page.first_block && !began_before
}
/// The document's sections with their headers and footers (Word's "link to previous"), each
/// section's resolved once for the whole layout in document order rather than by looking back
/// over the sections before it whenever one is needed.
@@ -174,12 +195,12 @@ pub(crate) fn header_table_areas(ctx: &mut Ctx, pb: &PageBuilder) -> Vec<Rect> {
return Vec::new();
}
let Some(page) = pb.pages.last() else { return Vec::new() };
let first = pb.pages.iter().rev().nth(1).is_none_or(|prev| prev.section != page.section);
let hf = Arc::clone(&ctx.hf);
let first = begins_section(&hf.sections, pb.pages.iter().rev().nth(1), page);
let key = (page.section, page.number, first);
if let Some(areas) = ctx.header_tables.get(&key) {
return areas.clone();
}
let hf = Arc::clone(&ctx.hf);
let Some((_, sect)) = hf.sections.get(page.section) else { return Vec::new() };
if sect.text_direction.is_turned() {
return Vec::new();
@@ -405,13 +426,9 @@ pub(crate) fn headers_footers(ctx: &mut Ctx, pages: &mut [Page], sections: &[(us
let hf = Arc::clone(&ctx.hf);
let even_odd = ctx.doc.settings.even_odd_headers;
let styles = styleref::headers_use_styleref(ctx.doc).then(|| ctx.cache.style_index(ctx.doc));
let mut first_of_section: HashSet<usize> = HashSet::new();
let mut seen = HashSet::new();
for (i, p) in pages.iter().enumerate() {
if seen.insert(p.section) {
first_of_section.insert(i);
}
}
let first_of_section: HashSet<usize> = (0..pages.len())
.filter(|&i| pages.get(i).is_some_and(|p| begins_section(sections, i.checked_sub(1).and_then(|k| pages.get(k)), p)))
.collect();
for (i, page) in pages.iter_mut().enumerate() {
let Some((_, sect)) = sections.get(page.section) else { continue };
// Inherit header/footer references from earlier sections (Word's "link to previous").
+3 -3
View File
@@ -2,7 +2,7 @@ use super::boxes::{BoxInput, clear_of_float_tables, layout_box};
use super::ctx::{Ctx, with_pct_base};
use super::floats::{WrapArea, align_in, body_wraps};
use super::flow::again;
use super::headers::{header_items_cap, header_table_areas, placed_cost};
use super::headers::{begins_section, header_items_cap, header_table_areas, placed_cost};
use super::notes::table_notes;
use super::page::{PageBuilder, PageFrame, page_frame};
use super::placed::Placed;
@@ -14,14 +14,14 @@ pub(crate) fn footer_table_wraps(ctx: &mut Ctx, pb: &PageBuilder) -> Vec<WrapAre
return Vec::new();
}
let Some(page) = pb.pages.last() else { return Vec::new() };
let first = pb.pages.iter().rev().nth(1).is_none_or(|prev| prev.section != page.section);
let hf = Arc::clone(&ctx.hf);
let first = begins_section(&hf.sections, pb.pages.iter().rev().nth(1), page);
let key = (page.section, page.number, first);
if let Some(areas) = ctx.footer_wraps.get(&key) {
return areas.clone();
}
ctx.footer_wraps.insert(key, Vec::new());
let doc = ctx.doc;
let hf = Arc::clone(&ctx.hf);
let Some((_, sect)) = hf.sections.get(page.section) else { return Vec::new() };
if sect.text_direction.is_turned() {
return Vec::new();
+77
View File
@@ -17427,6 +17427,83 @@ fn kept_lines_below_a_tall_first_page_header_move_whole_to_page_two() {
assert_eq!((first.page, last.page), (1, 1), "kept paragraph whole on page 2: {first:?} .. {last:?}");
}
/// A section with a different first page that starts continuously part-way down a page has no
/// first page of its own: Word shows the earlier section's header on the page where it starts
/// (the section at the page's top) and the section's default header from the next page on, so
/// its first-page header is never seen. One that starts on a new page shows it there.
#[test]
fn continuous_section_first_page_header_is_not_shown_on_its_second_page() {
use wordcraft_doc::section::SectionStart;
for start in [SectionStart::Continuous, SectionStart::NextPage] {
let text = (0..140).map(|i| format!("Paragraph {i}")).collect::<Vec<_>>().join("\n");
let mut d = Document::from_text(&text);
let part = |d: &mut Document, t: &str| {
d.add_part(wordcraft_doc::PartKind::Header, vec![wordcraft_doc::para_block(Paragraph::with_text(t, Default::default()))])
};
let (one, first, two) = (part(&mut d, "ONE"), part(&mut d, "FIRST"), part(&mut d, "TWO"));
let mut s1 = d.last_section.clone();
s1.headers.default = Some(one);
d.para_mut(StoryRef::Body, &Path::top(19)).unwrap().section = Some(Box::new(s1));
d.last_section.start = start;
d.last_section.title_page = true;
d.last_section.headers.first = Some(first);
d.last_section.headers.default = Some(two);
let l = lay(&d);
let begins = l.caret(&Pos::body(20, 0)).unwrap().page;
let shown: Vec<_> = l.pages.iter().map(|p| p.header_story).collect();
assert!(l.pages.len() > begins + 2, "{start:?}: the second section runs over several pages");
match start {
SectionStart::Continuous => {
assert!(l.caret(&Pos::body(19, 0)).unwrap().page == begins, "the second section starts part-way down a page");
assert_eq!(shown[begins], Some(one), "{start:?}: the page where it starts keeps the first section's header");
assert!(
shown[begins + 1..].iter().all(|h| *h == Some(two)),
"{start:?}: then its default header, never its first-page one: {shown:?}"
);
assert!(l.pages[begins + 1..].iter().all(|p| p.hf_slot == crate::HfSlot::Default), "{start:?}");
}
_ => {
assert_eq!(shown[begins], Some(first), "{start:?}: a section on a new page shows its first-page header there");
assert_eq!(l.pages[begins].hf_slot, crate::HfSlot::First);
assert!(shown[begins + 1..].iter().all(|h| *h == Some(two)), "{start:?}: {shown:?}");
}
}
}
}
/// The same when the continuous section's first paragraph begins part-way down a page and runs
/// over onto the next: the section began on the page before, so the next shows its default
/// header, not its first-page one.
#[test]
fn continuous_section_first_paragraph_running_over_keeps_first_page_header_hidden() {
use wordcraft_doc::section::SectionStart;
let mut paras: Vec<String> = (0..44).map(|i| format!("Paragraph {i}")).collect();
// Section 2's first paragraph: long enough to start low on page 1 and run onto page 2.
paras.push("word ".repeat(1200));
paras.extend((0..60).map(|i| format!("After {i}")));
let mut d = Document::from_text(&paras.join("\n"));
let part = |d: &mut Document, t: &str| {
d.add_part(wordcraft_doc::PartKind::Header, vec![wordcraft_doc::para_block(Paragraph::with_text(t, Default::default()))])
};
let (one, first, two) = (part(&mut d, "ONE"), part(&mut d, "FIRST"), part(&mut d, "TWO"));
let mut s1 = d.last_section.clone();
s1.headers.default = Some(one);
d.para_mut(StoryRef::Body, &Path::top(43)).unwrap().section = Some(Box::new(s1));
d.last_section.start = SectionStart::Continuous;
d.last_section.title_page = true;
d.last_section.headers.first = Some(first);
d.last_section.headers.default = Some(two);
let l = lay(&d);
let begins = l.caret(&Pos::body(44, 0)).unwrap().page;
assert_eq!(l.caret(&Pos::body(43, 0)).unwrap().page, begins, "the second section starts part-way down a page");
let next = begins + 1;
assert!(l.pages.get(next).is_some_and(|p| p.first_block == 44), "its first paragraph runs over onto the next page");
let shown: Vec<_> = l.pages.iter().map(|p| p.header_story).collect();
assert_eq!(shown[begins], Some(one));
assert!(shown[next..].iter().all(|h| *h == Some(two)), "{shown:?}");
assert!(l.pages[next..].iter().all(|p| p.hf_slot == crate::HfSlot::Default));
}
/// Sections starting continuously one after another still go onto new pages: a paragraph that
/// doesn't fit below the text above it moves on rather than running off the page (#3907). A
/// hundred one-line sections lay out like a hundred plain paragraphs.
File diff suppressed because one or more lines are too long