Skip to content

Commit 46ce9c9

Browse files
committed
Fix linebreak after inline objects with metadata
1 parent 9025ecb commit 46ce9c9

File tree

8 files changed

+59
-37
lines changed

8 files changed

+59
-37
lines changed

library/src/layout/par.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ impl Segment<'_> {
318318
Self::Text(len) => len,
319319
Self::Spacing(_) => SPACING_REPLACE.len_utf8(),
320320
Self::Box(_, true) => SPACING_REPLACE.len_utf8(),
321-
Self::Equation(_) | Self::Box(_, _) | Self::Meta => OBJ_REPLACE.len_utf8(),
321+
Self::Equation(_) | Self::Box(_, _) => OBJ_REPLACE.len_utf8(),
322+
Self::Meta => 0,
322323
}
323324
}
324325
}
@@ -334,6 +335,8 @@ enum Item<'a> {
334335
Fractional(Fr, Option<(&'a BoxElem, StyleChain<'a>)>),
335336
/// Layouted inline-level content.
336337
Frame(Frame),
338+
/// Metadata.
339+
Meta(Frame),
337340
}
338341

339342
impl<'a> Item<'a> {
@@ -351,6 +354,7 @@ impl<'a> Item<'a> {
351354
Self::Text(shaped) => shaped.text.len(),
352355
Self::Absolute(_) | Self::Fractional(_, _) => SPACING_REPLACE.len_utf8(),
353356
Self::Frame(_) => OBJ_REPLACE.len_utf8(),
357+
Self::Meta(_) => 0,
354358
}
355359
}
356360

@@ -360,7 +364,7 @@ impl<'a> Item<'a> {
360364
Self::Text(shaped) => shaped.width,
361365
Self::Absolute(v) => *v,
362366
Self::Frame(frame) => frame.width(),
363-
Self::Fractional(_, _) => Abs::zero(),
367+
Self::Fractional(_, _) | Self::Meta(_) => Abs::zero(),
364368
}
365369
}
366370
}
@@ -585,7 +589,6 @@ fn collect<'a>(
585589
full.push(if frac { SPACING_REPLACE } else { OBJ_REPLACE });
586590
Segment::Box(elem, frac)
587591
} else if child.is::<MetaElem>() {
588-
full.push(OBJ_REPLACE);
589592
Segment::Meta
590593
} else {
591594
bail!(child.span(), "unexpected paragraph child");
@@ -670,7 +673,7 @@ fn prepare<'a>(
670673
Segment::Meta => {
671674
let mut frame = Frame::new(Size::zero());
672675
frame.meta(styles, true);
673-
items.push(Item::Frame(frame));
676+
items.push(Item::Meta(frame));
674677
}
675678
}
676679

@@ -1336,7 +1339,7 @@ fn commit(
13361339
let frame = shaped.build(vt, justification_ratio, extra_justification);
13371340
push(&mut offset, frame);
13381341
}
1339-
Item::Frame(frame) => {
1342+
Item::Frame(frame) | Item::Meta(frame) => {
13401343
push(&mut offset, frame.clone());
13411344
}
13421345
}

tests/ref/meta/query-figure.png

282 KB
Loading

tests/ref/meta/query-header.png

118 KB
Loading

tests/ref/meta/query.png

-541 KB
Binary file not shown.

tests/ref/text/linebreak-obj.png

40.7 KB
Loading

tests/typ/meta/query.typ renamed to tests/typ/meta/query-figure.typ

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,4 @@
1-
// Test the query function.
2-
3-
---
4-
#set page(
5-
paper: "a7",
6-
margin: (y: 1cm, x: 0.5cm),
7-
header: {
8-
smallcaps[Typst Academy]
9-
h(1fr)
10-
locate(it => {
11-
let after = query(selector(heading).after(it), it)
12-
let before = query(selector(heading).before(it), it)
13-
let elem = if before.len() != 0 {
14-
before.last()
15-
} else if after.len() != 0 {
16-
after.first()
17-
}
18-
emph(elem.body)
19-
})
20-
}
21-
)
22-
23-
#outline()
24-
25-
= Introduction
26-
#lorem(35)
27-
28-
= Background
29-
#lorem(35)
30-
31-
= Approach
32-
#lorem(60)
1+
// Test a list of figures.
332

343
---
354
#set page(

tests/typ/meta/query-header.typ

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Test creating a header with the query function.
2+
3+
---
4+
#set page(
5+
paper: "a7",
6+
margin: (y: 1cm, x: 0.5cm),
7+
header: {
8+
smallcaps[Typst Academy]
9+
h(1fr)
10+
locate(it => {
11+
let after = query(selector(heading).after(it), it)
12+
let before = query(selector(heading).before(it), it)
13+
let elem = if before.len() != 0 {
14+
before.last()
15+
} else if after.len() != 0 {
16+
after.first()
17+
}
18+
emph(elem.body)
19+
})
20+
}
21+
)
22+
23+
#outline()
24+
25+
= Introduction
26+
#lorem(35)
27+
28+
= Background
29+
#lorem(35)
30+
31+
= Approach
32+
#lorem(60)

tests/typ/text/linebreak-obj.typ

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Test linebreaks with after inline elements.
2+
3+
---
4+
// Test punctuation after citations.
5+
#set page(width: 162pt)
6+
7+
They can look for the details in @netwok,
8+
which is the authorative source.
9+
10+
#bibliography("/works.bib")
11+
12+
---
13+
// Test punctuation after math equations.
14+
#set page(width: 85pt)
15+
16+
We prove $1 < 2$. \
17+
We prove $1 < 2$! \
18+
We prove $1 < 2$\

0 commit comments

Comments
 (0)