diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-09-14 13:36:39 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-14 13:36:39 +0200 |
| commit | 3dd12d13f81ec5920e6d23248933a11c54ddb590 (patch) | |
| tree | 81893d12ad105e621c347cc1fa1dfa5bd425261b | |
| parent | f90701e132679f8372e8a32171dd013646c7f276 (diff) | |
Fix invisibles on final page (#2141)
| -rw-r--r-- | crates/typst-library/src/layout/mod.rs | 16 | ||||
| -rw-r--r-- | crates/typst-library/src/shared/behave.rs | 9 | ||||
| -rw-r--r-- | tests/ref/bugs/pagebreak-bibliography.png | bin | 0 -> 1860 bytes | |||
| -rw-r--r-- | tests/typ/bugs/pagebreak-bibliography.typ | 5 |
4 files changed, 20 insertions, 10 deletions
diff --git a/crates/typst-library/src/layout/mod.rs b/crates/typst-library/src/layout/mod.rs index d9e8ec9a..018cf1a6 100644 --- a/crates/typst-library/src/layout/mod.rs +++ b/crates/typst-library/src/layout/mod.rs @@ -250,7 +250,7 @@ fn realize_root<'a>( let mut builder = Builder::new(vt, scratch, true); builder.accept(content, styles)?; - builder.interrupt_page(Some(styles))?; + builder.interrupt_page(Some(styles), true)?; let (pages, shared) = builder.doc.unwrap().pages.finish(); Ok((DocumentElem::new(pages.to_vec()).pack(), shared)) } @@ -375,7 +375,7 @@ impl<'a, 'v, 't> Builder<'a, 'v, 't> { .to::<PagebreakElem>() .map_or(false, |pagebreak| !pagebreak.weak(styles)); - self.interrupt_page(keep.then_some(styles))?; + self.interrupt_page(keep.then_some(styles), false)?; if let Some(doc) = &mut self.doc { if doc.accept(content, styles) { @@ -424,7 +424,7 @@ impl<'a, 'v, 't> Builder<'a, 'v, 't> { if self.doc.is_none() { bail!(span, "page configuration is not allowed inside of containers"); } - self.interrupt_page(outer)?; + self.interrupt_page(outer, false)?; } else if local.interruption::<ParElem>().is_some() || local.interruption::<AlignElem>().is_some() { @@ -462,10 +462,14 @@ impl<'a, 'v, 't> Builder<'a, 'v, 't> { Ok(()) } - fn interrupt_page(&mut self, styles: Option<StyleChain<'a>>) -> SourceResult<()> { + fn interrupt_page( + &mut self, + styles: Option<StyleChain<'a>>, + last: bool, + ) -> SourceResult<()> { self.interrupt_par()?; let Some(doc) = &mut self.doc else { return Ok(()) }; - if !self.flow.0.is_basically_empty() || (doc.keep_next && styles.is_some()) { + if (doc.keep_next && styles.is_some()) || self.flow.0.has_strong_elements(last) { let (flow, shared) = mem::take(&mut self.flow).0.finish(); let styles = if shared == StyleChain::default() { styles.unwrap_or_default() @@ -588,7 +592,7 @@ struct ParBuilder<'a>(BehavedBuilder<'a>); impl<'a> ParBuilder<'a> { fn accept(&mut self, content: &'a Content, styles: StyleChain<'a>) -> bool { if content.is::<MetaElem>() { - if !self.0.is_basically_empty() { + if self.0.has_strong_elements(false) { self.0.push(content.clone(), styles); return true; } diff --git a/crates/typst-library/src/shared/behave.rs b/crates/typst-library/src/shared/behave.rs index 471daa18..ed7a2593 100644 --- a/crates/typst-library/src/shared/behave.rs +++ b/crates/typst-library/src/shared/behave.rs @@ -32,10 +32,11 @@ impl<'a> BehavedBuilder<'a> { /// Whether the builder is empty except for some weak elements that will /// probably collapse. - pub fn is_basically_empty(&self) -> bool { - self.builder.is_empty() - && self.staged.iter().all(|(_, behaviour, _)| { - matches!(behaviour, Behaviour::Weak(_) | Behaviour::Invisible) + pub fn has_strong_elements(&self, last: bool) -> bool { + !self.builder.is_empty() + || self.staged.iter().any(|(_, behaviour, _)| { + !matches!(behaviour, Behaviour::Weak(_) | Behaviour::Invisible) + || (last && *behaviour == Behaviour::Invisible) }) } diff --git a/tests/ref/bugs/pagebreak-bibliography.png b/tests/ref/bugs/pagebreak-bibliography.png Binary files differnew file mode 100644 index 00000000..43de1574 --- /dev/null +++ b/tests/ref/bugs/pagebreak-bibliography.png diff --git a/tests/typ/bugs/pagebreak-bibliography.typ b/tests/typ/bugs/pagebreak-bibliography.typ new file mode 100644 index 00000000..bfc78250 --- /dev/null +++ b/tests/typ/bugs/pagebreak-bibliography.typ @@ -0,0 +1,5 @@ +// Test weak pagebreak before bibliography. + +--- +#pagebreak(weak: true) +#bibliography("/files/works.bib") |
