Fix linebreak after inline objects with metadata

This commit is contained in:
Laurenz 2023-04-13 15:52:20 +02:00
parent 9025ecb2ee
commit 46ce9c94e3
8 changed files with 59 additions and 37 deletions

View File

@ -318,7 +318,8 @@ impl Segment<'_> {
Self::Text(len) => len,
Self::Spacing(_) => SPACING_REPLACE.len_utf8(),
Self::Box(_, true) => SPACING_REPLACE.len_utf8(),
Self::Equation(_) | Self::Box(_, _) | Self::Meta => OBJ_REPLACE.len_utf8(),
Self::Equation(_) | Self::Box(_, _) => OBJ_REPLACE.len_utf8(),
Self::Meta => 0,
}
}
}
@ -334,6 +335,8 @@ enum Item<'a> {
Fractional(Fr, Option<(&'a BoxElem, StyleChain<'a>)>),
/// Layouted inline-level content.
Frame(Frame),
/// Metadata.
Meta(Frame),
}
impl<'a> Item<'a> {
@ -351,6 +354,7 @@ impl<'a> Item<'a> {
Self::Text(shaped) => shaped.text.len(),
Self::Absolute(_) | Self::Fractional(_, _) => SPACING_REPLACE.len_utf8(),
Self::Frame(_) => OBJ_REPLACE.len_utf8(),
Self::Meta(_) => 0,
}
}
@ -360,7 +364,7 @@ impl<'a> Item<'a> {
Self::Text(shaped) => shaped.width,
Self::Absolute(v) => *v,
Self::Frame(frame) => frame.width(),
Self::Fractional(_, _) => Abs::zero(),
Self::Fractional(_, _) | Self::Meta(_) => Abs::zero(),
}
}
}
@ -585,7 +589,6 @@ fn collect<'a>(
full.push(if frac { SPACING_REPLACE } else { OBJ_REPLACE });
Segment::Box(elem, frac)
} else if child.is::<MetaElem>() {
full.push(OBJ_REPLACE);
Segment::Meta
} else {
bail!(child.span(), "unexpected paragraph child");
@ -670,7 +673,7 @@ fn prepare<'a>(
Segment::Meta => {
let mut frame = Frame::new(Size::zero());
frame.meta(styles, true);
items.push(Item::Frame(frame));
items.push(Item::Meta(frame));
}
}
@ -1336,7 +1339,7 @@ fn commit(
let frame = shaped.build(vt, justification_ratio, extra_justification);
push(&mut offset, frame);
}
Item::Frame(frame) => {
Item::Frame(frame) | Item::Meta(frame) => {
push(&mut offset, frame.clone());
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 542 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

View File

@ -1,35 +1,4 @@
// Test the query function.
---
#set page(
paper: "a7",
margin: (y: 1cm, x: 0.5cm),
header: {
smallcaps[Typst Academy]
h(1fr)
locate(it => {
let after = query(selector(heading).after(it), it)
let before = query(selector(heading).before(it), it)
let elem = if before.len() != 0 {
before.last()
} else if after.len() != 0 {
after.first()
}
emph(elem.body)
})
}
)
#outline()
= Introduction
#lorem(35)
= Background
#lorem(35)
= Approach
#lorem(60)
// Test a list of figures.
---
#set page(

View File

@ -0,0 +1,32 @@
// Test creating a header with the query function.
---
#set page(
paper: "a7",
margin: (y: 1cm, x: 0.5cm),
header: {
smallcaps[Typst Academy]
h(1fr)
locate(it => {
let after = query(selector(heading).after(it), it)
let before = query(selector(heading).before(it), it)
let elem = if before.len() != 0 {
before.last()
} else if after.len() != 0 {
after.first()
}
emph(elem.body)
})
}
)
#outline()
= Introduction
#lorem(35)
= Background
#lorem(35)
= Approach
#lorem(60)

View File

@ -0,0 +1,18 @@
// Test linebreaks with after inline elements.
---
// Test punctuation after citations.
#set page(width: 162pt)
They can look for the details in @netwok,
which is the authorative source.
#bibliography("/works.bib")
---
// Test punctuation after math equations.
#set page(width: 85pt)
We prove $1 < 2$. \
We prove $1 < 2$! \
We prove $1 < 2$ \