mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-02 22:30:10 +00:00
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0ec0d83e1 | ||
|
|
989fa17544 | ||
|
|
6eb03bf4ea | ||
|
|
892235af06 | ||
|
|
e14f85661b | ||
|
|
d3096ed0fb | ||
|
|
dc515b6128 |
@@ -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").
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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(¶s.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
Reference in New Issue
Block a user