Fixing the styling issues of the empty pages caused by pagebreak (#2182)

This commit is contained in:
LU Jialin 2023-09-25 20:13:54 +08:00 committed by GitHub
parent 98e5d97509
commit 079ccd5e5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 8 deletions

View File

@ -310,7 +310,6 @@ pub struct PageElem {
pub body: Content,
/// Whether the page should be aligned to an even or odd page.
/// Not part of the public API for now.
#[internal]
pub clear_to: Option<Parity>,
}
@ -328,6 +327,7 @@ impl PageElem {
vt: &mut Vt,
styles: StyleChain,
page_counter: &mut ManualPageCounter,
extend_to: Option<Parity>,
) -> SourceResult<Fragment> {
tracing::info!("Page layout");
@ -378,12 +378,10 @@ impl PageElem {
let mut frames = child.layout(vt, styles, regions)?.into_frames();
// Align the child to the pagebreak's parity.
if self
.clear_to(styles)
.is_some_and(|p| !p.matches(page_counter.physical().get()))
{
if extend_to.is_some_and(|p| p.matches(page_counter.physical().get())) {
// Insert empty page after the current pages.
let size = area.map(Abs::is_finite).select(area, Size::zero());
frames.insert(0, Frame::new(size));
frames.push(Frame::new(size));
}
let fill = self.fill(styles);

View File

@ -48,7 +48,10 @@ impl LayoutRoot for DocumentElem {
let mut pages = vec![];
let mut page_counter = ManualPageCounter::new();
for mut child in &self.children() {
let children = self.children();
let mut iter = children.iter().peekable();
while let Some(mut child) = iter.next() {
let outer = styles;
let mut styles = styles;
if let Some((elem, local)) = child.to_styled() {
@ -57,7 +60,13 @@ impl LayoutRoot for DocumentElem {
}
if let Some(page) = child.to::<PageElem>() {
let fragment = page.layout(vt, styles, &mut page_counter)?;
let extend_to = iter.peek().and_then(|&next| {
next.to_styled()
.map_or(next, |(elem, _)| elem)
.to::<PageElem>()?
.clear_to(styles)
});
let fragment = page.layout(vt, styles, &mut page_counter, extend_to)?;
pages.extend(fragment);
} else {
bail!(child.span(), "unexpected document child");

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

View File

@ -0,0 +1,13 @@
// https://github.com/typst/typst/issues/2095
// The empty page 2 should not have a page number
#set page(numbering: none)
This and next page should not be numbered
#pagebreak(weak: true, to: "odd")
#set page(numbering: "1")
#counter(page).update(1)
This page should

View File

@ -0,0 +1,12 @@
// https://github.com/typst/typst/issues/2162
// The styles should not be applied to the pagebreak empty page,
// it should only be applied after that.
#pagebreak(to: "even") // We should now skip to page 2
Some text on page 2
#pagebreak(to: "even") // We should now skip to page 4
#set page(fill: orange) // This sets the color of the page starting from page 4
Some text on page 4