Introduce second variant of Ignorant Behaviour and prevent weak page breaks at basically empty pages (#1929)

This commit is contained in:
Beiri22 2023-08-30 12:58:16 +02:00 committed by GitHub
parent 3c508737d9
commit e1558268f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 46 additions and 12 deletions

View File

@ -467,7 +467,7 @@ impl<'a, 'v, 't> Builder<'a, 'v, 't> {
fn interrupt_page(&mut self, styles: Option<StyleChain<'a>>) -> SourceResult<()> { fn interrupt_page(&mut self, styles: Option<StyleChain<'a>>) -> SourceResult<()> {
self.interrupt_par()?; self.interrupt_par()?;
let Some(doc) = &mut self.doc else { return Ok(()) }; let Some(doc) = &mut self.doc else { return Ok(()) };
if !self.flow.0.is_empty() || (doc.keep_next && styles.is_some()) { if !self.flow.0.is_basically_empty() || (doc.keep_next && styles.is_some()) {
let (flow, shared) = mem::take(&mut self.flow).0.finish(); let (flow, shared) = mem::take(&mut self.flow).0.finish();
let styles = if shared == StyleChain::default() { let styles = if shared == StyleChain::default() {
styles.unwrap_or_default() styles.unwrap_or_default()

View File

@ -63,7 +63,7 @@ impl Behave for HElem {
} else if self.weak(StyleChain::default()) { } else if self.weak(StyleChain::default()) {
Behaviour::Weak(1) Behaviour::Weak(1)
} else { } else {
Behaviour::Ignorant Behaviour::Invisible
} }
} }
@ -158,7 +158,7 @@ impl Behave for VElem {
} else if self.weakness(StyleChain::default()) > 0 { } else if self.weakness(StyleChain::default()) > 0 {
Behaviour::Weak(self.weakness(StyleChain::default())) Behaviour::Weak(self.weakness(StyleChain::default()))
} else { } else {
Behaviour::Ignorant Behaviour::Invisible
} }
} }

View File

@ -38,6 +38,6 @@ impl Show for MetadataElem {
impl Behave for MetadataElem { impl Behave for MetadataElem {
fn behaviour(&self) -> Behaviour { fn behaviour(&self) -> Behaviour {
Behaviour::Ignorant Behaviour::Invisible
} }
} }

View File

@ -34,10 +34,9 @@ impl<'a> BehavedBuilder<'a> {
/// probably collapse. /// probably collapse.
pub fn is_basically_empty(&self) -> bool { pub fn is_basically_empty(&self) -> bool {
self.builder.is_empty() self.builder.is_empty()
&& self && self.staged.iter().all(|(_, behaviour, _)| {
.staged matches!(behaviour, Behaviour::Weak(_) | Behaviour::Invisible)
.iter() })
.all(|(_, behaviour, _)| matches!(behaviour, Behaviour::Weak(_)))
} }
/// Push an item into the sequence. /// Push an item into the sequence.
@ -74,7 +73,7 @@ impl<'a> BehavedBuilder<'a> {
self.builder.push(elem, styles); self.builder.push(elem, styles);
self.last = interaction; self.last = interaction;
} }
Behaviour::Ignorant => { Behaviour::Ignorant | Behaviour::Invisible => {
self.staged.push((elem, interaction, styles)); self.staged.push((elem, interaction, styles));
} }
} }
@ -95,7 +94,10 @@ impl<'a> BehavedBuilder<'a> {
/// false. /// false.
fn flush(&mut self, supportive: bool) { fn flush(&mut self, supportive: bool) {
for (item, interaction, styles) in self.staged.drain(..) { for (item, interaction, styles) in self.staged.drain(..) {
if supportive || interaction == Behaviour::Ignorant { if supportive
|| interaction == Behaviour::Ignorant
|| interaction == Behaviour::Invisible
{
self.builder.push(item, styles); self.builder.push(item, styles);
} }
} }

View File

@ -610,7 +610,7 @@ pub struct MetaElem {
impl Behave for MetaElem { impl Behave for MetaElem {
fn behaviour(&self) -> Behaviour { fn behaviour(&self) -> Behaviour {
Behaviour::Ignorant Behaviour::Invisible
} }
} }

View File

@ -214,8 +214,10 @@ pub enum Behaviour {
/// An element that destroys adjacent weak elements. /// An element that destroys adjacent weak elements.
Destructive, Destructive,
/// An element that does not interact at all with other elements, having the /// An element that does not interact at all with other elements, having the
/// same effect as if it didn't exist. /// same effect as if it didn't exist, but has a visual representation.
Ignorant, Ignorant,
/// An element that does not have a visual representation.
Invisible,
} }
/// Guards content against being affected by the same show rule multiple times. /// Guards content against being affected by the same show rule multiple times.

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -0,0 +1,30 @@
// Test page breaks on basically empty pages.
---
// After place
// Should result in three pages.
First
#pagebreak(weak: true)
#place(right)[placed A]
#pagebreak(weak: true)
Third
---
// After only ignorables & invisibles
// Should result in two pages.
First
#pagebreak(weak: true)
#counter(page).update(1)
#metadata("Some")
#pagebreak(weak: true)
Second
---
// After only ignorables, but regular break
// Should result in three pages.
First
#pagebreak()
#counter(page).update(1)
#metadata("Some")
#pagebreak()
Third