Figure body forces paragraph

This commit is contained in:
Laurenz 2025-01-23 17:53:57 +01:00
parent 1f53ff26b9
commit 3c42efcfe8
3 changed files with 32 additions and 12 deletions

View File

@ -19,7 +19,9 @@ use crate::layout::{
AlignElem, Alignment, BlockBody, BlockElem, Em, HAlignment, Length, OuterVAlignment, AlignElem, Alignment, BlockBody, BlockElem, Em, HAlignment, Length, OuterVAlignment,
PlaceElem, PlacementScope, VAlignment, VElem, PlaceElem, PlacementScope, VAlignment, VElem,
}; };
use crate::model::{Numbering, NumberingPattern, Outlinable, Refable, Supplement}; use crate::model::{
Numbering, NumberingPattern, Outlinable, ParbreakElem, Refable, Supplement,
};
use crate::text::{Lang, Region, TextElem}; use crate::text::{Lang, Region, TextElem};
use crate::visualize::ImageElem; use crate::visualize::ImageElem;
@ -328,6 +330,7 @@ impl Synthesize for Packed<FigureElem> {
impl Show for Packed<FigureElem> { impl Show for Packed<FigureElem> {
#[typst_macros::time(name = "figure", span = self.span())] #[typst_macros::time(name = "figure", span = self.span())]
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> { fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let span = self.span();
let target = TargetElem::target_in(styles); let target = TargetElem::target_in(styles);
let mut realized = self.body.clone(); let mut realized = self.body.clone();
@ -341,24 +344,27 @@ impl Show for Packed<FigureElem> {
seq.push(first); seq.push(first);
if !target.is_html() { if !target.is_html() {
let v = VElem::new(self.gap(styles).into()).with_weak(true); let v = VElem::new(self.gap(styles).into()).with_weak(true);
seq.push(v.pack().spanned(self.span())) seq.push(v.pack().spanned(span))
} }
seq.push(second); seq.push(second);
realized = Content::sequence(seq) realized = Content::sequence(seq)
} }
// Ensure that the body is considered a paragraph.
realized += ParbreakElem::shared().clone().spanned(span);
if target.is_html() { if target.is_html() {
return Ok(HtmlElem::new(tag::figure) return Ok(HtmlElem::new(tag::figure)
.with_body(Some(realized)) .with_body(Some(realized))
.pack() .pack()
.spanned(self.span())); .spanned(span));
} }
// Wrap the contents in a block. // Wrap the contents in a block.
realized = BlockElem::new() realized = BlockElem::new()
.with_body(Some(BlockBody::Content(realized))) .with_body(Some(BlockBody::Content(realized)))
.pack() .pack()
.spanned(self.span()); .spanned(span);
// Wrap in a float. // Wrap in a float.
if let Some(align) = self.placement(styles) { if let Some(align) = self.placement(styles) {
@ -367,10 +373,10 @@ impl Show for Packed<FigureElem> {
.with_scope(self.scope(styles)) .with_scope(self.scope(styles))
.with_float(true) .with_float(true)
.pack() .pack()
.spanned(self.span()); .spanned(span);
} else if self.scope(styles) == PlacementScope::Parent { } else if self.scope(styles) == PlacementScope::Parent {
bail!( bail!(
self.span(), span,
"parent-scoped placement is only available for floating figures"; "parent-scoped placement is only available for floating figures";
hint: "you can enable floating placement with `figure(placement: auto, ..)`" hint: "you can enable floating placement with `figure(placement: auto, ..)`"
); );
@ -604,14 +610,17 @@ impl Show for Packed<FigureCaption> {
realized = supplement + numbers + self.get_separator(styles) + realized; realized = supplement + numbers + self.get_separator(styles) + realized;
} }
if TargetElem::target_in(styles).is_html() { Ok(if TargetElem::target_in(styles).is_html() {
return Ok(HtmlElem::new(tag::figcaption) HtmlElem::new(tag::figcaption)
.with_body(Some(realized)) .with_body(Some(realized))
.pack() .pack()
.spanned(self.span())); .spanned(self.span())
} } else {
BlockElem::new()
Ok(realized) .with_body(Some(BlockBody::Content(realized)))
.pack()
.spanned(self.span())
})
} }
} }

BIN
tests/ref/figure-par.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@ -180,6 +180,17 @@ We can clearly see that @fig-cylinder and
caption: [Underlined], caption: [Underlined],
) )
--- figure-par ---
// Ensure that a figure body is considered a paragraph.
#show par: highlight
#figure[Text]
#figure(
[Text],
caption: [A caption]
)
--- figure-and-caption-show --- --- figure-and-caption-show ---
// Test creating custom figure and custom caption // Test creating custom figure and custom caption