Fix invisibles on final page ()

This commit is contained in:
Laurenz 2023-09-14 13:36:39 +02:00 committed by GitHub
parent f90701e132
commit 3dd12d13f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 10 deletions
crates/typst-library/src
layout
shared
tests

@ -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;
}

@ -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)
})
}

Binary file not shown.

After

(image error) Size: 1.8 KiB

@ -0,0 +1,5 @@
// Test weak pagebreak before bibliography.
---
#pagebreak(weak: true)
#bibliography("/files/works.bib")