mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-02 22:30:10 +00:00
Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dcd16d463d | ||
|
|
8654ed1319 | ||
|
|
4afcb2812f | ||
|
|
834c8054cf | ||
|
|
540bd57931 | ||
|
|
11790205d1 | ||
|
|
6997afcc34 | ||
|
|
0074cb6ac6 | ||
|
|
3c44fc6443 |
@@ -210,10 +210,31 @@ impl NoteBox {
|
||||
cuts
|
||||
}
|
||||
|
||||
/// The least of the note that must stay with its reference: its first line (all of it if it
|
||||
/// can't be split).
|
||||
/// Whether widow/orphan control in the note's paragraphs forbids splitting at `cut`: a
|
||||
/// paragraph's first line alone above it (an orphan) or its last line alone below it (a
|
||||
/// widow), as in Word for Mac 16.114.4.
|
||||
pub(crate) fn breaks_widow_control(&self, cut: f32) -> bool {
|
||||
self.items.iter().filter(|it| self.splittable(it)).any(|it| {
|
||||
let Placed::Lines { para, l0, l1, y, .. } = it else { return false };
|
||||
let Some(first) = para.lines.get(*l0) else { return false };
|
||||
let ends_at = |k: usize| para.lines.get(k).is_some_and(|l| (y + l.top - first.top + l.height - cut).abs() < 0.01);
|
||||
let n = para.lines.len();
|
||||
para.rp.widow_control && n >= 2 && ((*l0 == 0 && *l1 >= 2 && ends_at(0)) || (*l1 == n && ends_at(n - 2)))
|
||||
})
|
||||
}
|
||||
|
||||
/// Where the note may be split with its paragraphs' widow/orphan control kept, ascending.
|
||||
pub(crate) fn kept_cuts(&self) -> Vec<f32> {
|
||||
let mut cuts = self.cuts();
|
||||
cuts.retain(|&c| !self.breaks_widow_control(c));
|
||||
cuts
|
||||
}
|
||||
|
||||
/// The least of the note that must stay with its reference: its first line, or more under
|
||||
/// widow/orphan control (all of it if it can't be split).
|
||||
pub(crate) fn first_piece(&self) -> f32 {
|
||||
self.cuts().first().copied().unwrap_or(self.h).min(self.h)
|
||||
// Widow/orphan control may keep more lines together (two, or a short paragraph whole).
|
||||
self.kept_cuts().first().copied().unwrap_or(self.h).min(self.h)
|
||||
}
|
||||
|
||||
/// Split the note at line bottom `cut`: the part above (`cut` tall) and the rest, moved up to
|
||||
|
||||
@@ -160,7 +160,7 @@ impl PageBuilder<'_> {
|
||||
// The note area starts below the text (a page of continued notes alone: at its top).
|
||||
let area = self.bottom.max(self.body_floor());
|
||||
let fits = total <= self.orig_bottom - area + 0.01;
|
||||
let top = match (fits, self.beneath) {
|
||||
let mut top = match (fits, self.beneath) {
|
||||
(false, _) => area,
|
||||
// Below the page's last line of text.
|
||||
(true, true) => self.body_floor().min(self.orig_bottom - total),
|
||||
@@ -173,12 +173,15 @@ impl PageBuilder<'_> {
|
||||
let mut placed = Vec::new();
|
||||
// Where the continuation notice goes, below a note that continues on the next page.
|
||||
let mut notice_y = None;
|
||||
// The bottom of what is placed (with the notice).
|
||||
let mut end = y;
|
||||
let mut rest = notes.into_iter();
|
||||
while let Some(n) = rest.next() {
|
||||
let room = self.orig_bottom - y;
|
||||
if fits || n.h <= room + 0.01 {
|
||||
let ny = y;
|
||||
y += n.h;
|
||||
end = y;
|
||||
placed.push((n, ny));
|
||||
continue;
|
||||
}
|
||||
@@ -186,21 +189,38 @@ impl PageBuilder<'_> {
|
||||
// always leaves something here (its first line, or all of it if it can't be split),
|
||||
// so continuing ends.
|
||||
let first = placed.is_empty();
|
||||
let cuts = n.cuts();
|
||||
// Widow/orphan control in the note keeps two of a paragraph's lines on either side;
|
||||
// the page's first note gives that up before it gives up the page's room.
|
||||
let room = room - self.seps.notice_h();
|
||||
match cuts.iter().rev().find(|&&c| c <= room + 0.01).or(cuts.first().filter(|_| first)).copied() {
|
||||
let fit = |cuts: Vec<f32>| cuts.into_iter().rev().find(|&c| c <= room + 0.01);
|
||||
let cut = fit(n.kept_cuts()).or_else(|| if first { fit(n.cuts()).or(n.cuts().first().copied()) } else { None });
|
||||
match cut {
|
||||
Some(c) => {
|
||||
let (head, tail) = n.split(c);
|
||||
notice_y = Some(y + head.h);
|
||||
end = y + head.h + self.seps.notice_h();
|
||||
placed.push((head, y));
|
||||
self.carry.push(tail);
|
||||
}
|
||||
None if first => placed.push((n, y)),
|
||||
None if first => {
|
||||
end = y + n.h;
|
||||
placed.push((n, y));
|
||||
}
|
||||
None => self.carry.push(n),
|
||||
}
|
||||
self.carry.extend(rest.by_ref());
|
||||
break;
|
||||
}
|
||||
// Notes that continue on the next page still end on the bottom margin, the room left
|
||||
// going above the separator (Word for Mac 16.114.4), unless they go beneath the text.
|
||||
if !fits && !self.beneath {
|
||||
let shift = (self.orig_bottom - end).max(0.0);
|
||||
top += shift;
|
||||
notice_y = notice_y.map(|ny| ny + shift);
|
||||
for (_, ny) in &mut placed {
|
||||
*ny += shift;
|
||||
}
|
||||
}
|
||||
if let Some(pg) = self.pages.last_mut() {
|
||||
match if cont { &self.seps.cont } else { &self.seps.sep } {
|
||||
Some((items, _)) => pg.items.extend(items.iter().cloned().map(|mut it| {
|
||||
|
||||
@@ -445,9 +445,10 @@ fn section_endnotes(ctx: &mut Ctx, pb: &mut PageBuilder, endnotes: &mut VecDeque
|
||||
/// the turned pages of vertical sections.
|
||||
fn finish_pages(pb: PageBuilder, doc: &Document, sections: &[(usize, &SectionProps)], web: bool) -> Vec<Page> {
|
||||
let mut pb = pb;
|
||||
pb.close_column_lines();
|
||||
pb.apply_valign();
|
||||
pb.flush_notes();
|
||||
// Each page is aligned once, before its notes go on it: `finish_page` leaves the page
|
||||
// top-aligned after, so a page of continued notes isn't moved down again past the bottom
|
||||
// margin when the next one starts.
|
||||
pb.finish_page();
|
||||
let last_block = doc.body.len().saturating_sub(1);
|
||||
for _ in 0..MAX_NOTE_PAGES {
|
||||
if pb.carry.is_empty() {
|
||||
@@ -455,7 +456,7 @@ fn finish_pages(pb: PageBuilder, doc: &Document, sections: &[(usize, &SectionPro
|
||||
}
|
||||
let top = pb.top;
|
||||
pb.new_page(last_block, top);
|
||||
pb.flush_notes();
|
||||
pb.finish_page();
|
||||
}
|
||||
pb.carry.clear();
|
||||
pb.raise_front();
|
||||
|
||||
@@ -11306,6 +11306,156 @@ fn long_footnotes_continue_on_the_next_page() {
|
||||
assert!(own_note.top > p1.3, "own note after the continued one: {own_note:?} {p1:?}");
|
||||
}
|
||||
|
||||
/// A document of `body` one-line paragraphs, the last of them referencing a footnote of `note`
|
||||
/// sentences, with widow control `widow` in the note; the note's id.
|
||||
fn long_note_after(body: usize, note: usize, widow: bool) -> (Document, u32) {
|
||||
let mut d = Document::from_text(&"Body text line.\n".repeat(body));
|
||||
let mut p = wordcraft_doc::Paragraph::with_text(&"A long footnote that runs on. ".repeat(note), Default::default());
|
||||
p.props.widow_control = Some(widow);
|
||||
p.props.space_after = Some(0.0);
|
||||
let id = d.add_part(wordcraft_doc::PartKind::Footnote, vec![wordcraft_doc::para_block(p)]);
|
||||
d.insert_object(
|
||||
&Pos::body(body.saturating_sub(1), 4),
|
||||
InlineObject::NoteRef { kind: wordcraft_doc::para::NoteKind::Footnote, id, custom: String::new() },
|
||||
&Default::default(),
|
||||
)
|
||||
.unwrap();
|
||||
(d, id)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_footnote_continuing_on_the_next_page_ends_on_the_bottom_margin() {
|
||||
crate::test_fonts();
|
||||
// Observed in Word for Mac 16.114.4: whatever room the split leaves goes above the
|
||||
// separator, the note's last line on the page sitting on the bottom margin.
|
||||
let bottom = Document::default().last_section.page_h - Document::default().last_section.margin_bottom;
|
||||
let mut slack = Vec::new();
|
||||
// With widow/orphan control too, which can leave more room than a plain cut.
|
||||
for (body, widow) in (11..24).flat_map(|b| [(b, false), (b, true)]) {
|
||||
let (d, id) = long_note_after(body, 60, widow);
|
||||
let l = lay(&d);
|
||||
let pieces = note_pieces(&l, id);
|
||||
assert!(pieces.len() >= 2 && pieces[0].0 == 0 && pieces[1].0 == 1, "{body} {widow}: {pieces:?}");
|
||||
// Every line of the note is placed once, in order.
|
||||
let lines = l.pages[0]
|
||||
.items
|
||||
.iter()
|
||||
.find_map(|it| if let Placed::Lines { story: StoryRef::Part(s), para, .. } = it { (*s == id).then(|| para.lines.len()) } else { None })
|
||||
.unwrap();
|
||||
assert_eq!(pieces.windows(2).find(|w| w[0].2 != w[1].1), None, "{body} {widow}: pieces follow on: {pieces:?}");
|
||||
assert_eq!((pieces[0].1, pieces.last().map(|p| p.2)), (0, Some(lines)), "{body} {widow}: all {lines} lines: {pieces:?}");
|
||||
let (para, l0, l1, y) = l.pages[0]
|
||||
.items
|
||||
.iter()
|
||||
.find_map(|it| match it {
|
||||
Placed::Lines { story: StoryRef::Part(s), para, l0, l1, y, .. } if *s == id => Some((para.clone(), *l0, *l1, *y)),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap();
|
||||
let end = item_bottom(y, ¶, l0, l1).unwrap();
|
||||
slack.push(bottom - end);
|
||||
}
|
||||
assert!(slack.iter().all(|s| s.abs() < 0.05), "room left under the note: {slack:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_footnote_beneath_the_text_stays_under_it_when_it_continues() {
|
||||
crate::test_fonts();
|
||||
// Footnotes placed beneath the text (`w:pos w:val="beneathText"`) start right under the page's
|
||||
// last line even when one continues on the next page: the room left stays below them, not
|
||||
// above the separator as for notes at the page bottom.
|
||||
use wordcraft_doc::section::{NotePos, NoteProps};
|
||||
let mut gaps = Vec::new();
|
||||
for body in 11..24 {
|
||||
let (mut d, id) = long_note_after(body, 60, false);
|
||||
d.settings.footnote_pr = NoteProps { pos: Some(NotePos::BeneathText), ..Default::default() };
|
||||
let l = lay(&d);
|
||||
let pieces = note_pieces(&l, id);
|
||||
assert!(pieces.len() >= 2 && pieces[0].0 == 0, "{body}: {pieces:?}");
|
||||
let body_bottom = l.pages[0]
|
||||
.items
|
||||
.iter()
|
||||
.filter_map(
|
||||
|it| if let Placed::Lines { story: StoryRef::Body, y, para, l0, l1, .. } = it { item_bottom(*y, para, *l0, *l1) } else { None },
|
||||
)
|
||||
.fold(0.0f32, f32::max);
|
||||
gaps.push(pieces[0].3 - body_bottom);
|
||||
}
|
||||
// Under the last line by the default separator's height, whatever the body's length.
|
||||
assert!(gaps.iter().all(|g| (g - crate::notes::NOTE_SEP).abs() < 0.05), "the note starts the separator's height under the text: {gaps:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_footnote_continued_onto_pages_of_notes_stays_above_the_bottom_margin_when_centred() {
|
||||
crate::test_fonts();
|
||||
// A centred (or bottom-aligned) section whose last page's note runs on over pages of notes
|
||||
// alone: each page is aligned once, before its notes are placed, so no note line is moved
|
||||
// past the bottom margin by aligning a page again after its notes went on it.
|
||||
use wordcraft_doc::section::PageVAlign;
|
||||
let bottom = Document::default().last_section.page_h - Document::default().last_section.margin_bottom;
|
||||
for va in [PageVAlign::Center, PageVAlign::Bottom] {
|
||||
let (mut d, id) = long_note_after(3, 400, false);
|
||||
d.last_section.valign = va;
|
||||
let l = lay(&d);
|
||||
let pieces = note_pieces(&l, id);
|
||||
assert!(pieces.iter().map(|p| p.0).max().unwrap_or(0) >= 2, "{va:?}: the note runs over pages of notes alone: {pieces:?}");
|
||||
for (pi, p) in l.pages.iter().enumerate() {
|
||||
for it in &p.items {
|
||||
if let Placed::Lines { story: StoryRef::Part(s), para, l0, l1, y, .. } = it
|
||||
&& *s == id
|
||||
{
|
||||
let end = item_bottom(*y, para, *l0, *l1).unwrap();
|
||||
assert!(end <= bottom + 0.05, "{va:?}: page {pi} note lines end at {end}, below the bottom margin {bottom}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn widow_control_leaves_two_lines_of_a_split_footnote_on_the_next_page() {
|
||||
crate::test_fonts();
|
||||
// Observed in Word for Mac 16.114.4: a note that would leave its last line alone on the next
|
||||
// page takes one more line along when its paragraph has widow control.
|
||||
let lines_after = |body: usize, widow: bool| {
|
||||
let (d, id) = long_note_after(body, 60, widow);
|
||||
let pieces = note_pieces(&lay(&d), id);
|
||||
pieces.get(1).filter(|p| p.0 == 1).map(|p| p.2 - p.1)
|
||||
};
|
||||
let widows: Vec<usize> = (9..26).filter(|&b| lines_after(b, false) == Some(1)).collect();
|
||||
assert!(!widows.is_empty(), "no body length leaves one line of the note on page 2");
|
||||
for b in widows {
|
||||
assert_eq!(lines_after(b, true), Some(2), "body of {b} lines");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn orphan_control_keeps_two_lines_of_a_split_footnote_with_its_reference() {
|
||||
crate::test_fonts();
|
||||
// Observed in Word for Mac 16.114.4: rather than leave a note paragraph's first line alone at
|
||||
// the bottom of the page, the line with the reference goes on to the next page with it.
|
||||
let first_piece = |body: usize, widow: bool| {
|
||||
let (mut d, id) = long_note_after(body, 60, widow);
|
||||
// Body lines as tall as the note's, so the room under the text shrinks a note line at a time.
|
||||
for block in &mut d.body {
|
||||
if let Block::Para(para) = Arc::make_mut(block) {
|
||||
para.props.space_after = Some(0.0);
|
||||
}
|
||||
}
|
||||
let l = lay(&d);
|
||||
let refs = l.caret(&Pos::body(body - 1, 4)).unwrap().page;
|
||||
let pieces = note_pieces(&l, id);
|
||||
(refs, pieces.first().map(|p| (p.0, p.2 - p.1)))
|
||||
};
|
||||
let orphans: Vec<usize> = (20..60).filter(|&b| first_piece(b, false).1.is_some_and(|p| p.1 == 1)).collect();
|
||||
assert!(!orphans.is_empty(), "no body length leaves one line of the note with its reference");
|
||||
for b in orphans {
|
||||
let (refs, piece) = first_piece(b, true);
|
||||
assert_eq!(piece.map(|p| p.0), Some(refs), "body of {b} lines: the note starts on its reference's page");
|
||||
assert!(piece.is_some_and(|p| p.1 >= 2), "body of {b} lines: {piece:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn short_footnotes_stay_whole() {
|
||||
crate::test_fonts();
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user