summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitrij <dimabradima@gmail.com>2023-09-12 16:01:59 +0300
committerGitHub <noreply@github.com>2023-09-12 15:01:59 +0200
commit6275dfd062ca4c519b908ed56feb219879116265 (patch)
tree54dd3356ecf245580ddd494d3d43377a3459bb6c
parent976abdfe7dc08ae42ee87e5c2d4ff46ebe172dd1 (diff)
Add figure separator field (#1791)
-rw-r--r--crates/typst-library/src/meta/figure.rs29
-rw-r--r--tests/ref/meta/figure.pngbin49894 -> 66110 bytes
-rw-r--r--tests/typ/meta/figure.typ9
3 files changed, 31 insertions, 7 deletions
diff --git a/crates/typst-library/src/meta/figure.rs b/crates/typst-library/src/meta/figure.rs
index 4aebdda6..bab7790a 100644
--- a/crates/typst-library/src/meta/figure.rs
+++ b/crates/typst-library/src/meta/figure.rs
@@ -359,12 +359,11 @@ impl Outlinable for FigureElem {
return Ok(None);
}
- let Some(mut caption) =
- self.caption(StyleChain::default()).map(|caption| caption.body())
- else {
+ let Some(caption) = self.caption(StyleChain::default()) else {
return Ok(None);
};
+ let mut realized = caption.body();
if let (
Smart::Custom(Some(Supplement::Content(mut supplement))),
Some(counter),
@@ -381,10 +380,12 @@ impl Outlinable for FigureElem {
supplement += TextElem::packed('\u{a0}');
}
- caption = supplement + numbers + TextElem::packed(": ") + caption;
+ let separator = caption.separator(StyleChain::default());
+
+ realized = supplement + numbers + separator + caption.body();
}
- Ok(Some(caption))
+ Ok(Some(realized))
}
}
@@ -444,6 +445,19 @@ pub struct FigureCaption {
})]
pub position: VAlign,
+ /// The separator which will appear between the number and body.
+ ///
+ /// ```example
+ /// #set figure.caption(separator: [ --- ])
+ ///
+ /// #figure(
+ /// rect[Hello],
+ /// caption: [A rectangle],
+ /// )
+ /// ```
+ #[default(TextElem::packed(": "))]
+ pub separator: Content,
+
/// The caption's body.
///
/// Can be used alongside `kind`, `supplement`, `counter`, `numbering`, and
@@ -487,13 +501,14 @@ pub struct FigureCaption {
impl Synthesize for FigureCaption {
fn synthesize(&mut self, _: &mut Vt, styles: StyleChain) -> SourceResult<()> {
self.push_position(self.position(styles));
+ self.push_separator(self.separator(styles));
Ok(())
}
}
impl Show for FigureCaption {
#[tracing::instrument(name = "FigureCaption::show", skip_all)]
- fn show(&self, vt: &mut Vt, _: StyleChain) -> SourceResult<Content> {
+ fn show(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult<Content> {
let mut realized = self.body();
if let (Some(mut supplement), Some(numbering), Some(counter), Some(location)) =
@@ -503,7 +518,7 @@ impl Show for FigureCaption {
if !supplement.is_empty() {
supplement += TextElem::packed('\u{a0}');
}
- realized = supplement + numbers + TextElem::packed(": ") + realized;
+ realized = supplement + numbers + self.separator(styles) + realized;
}
Ok(realized)
diff --git a/tests/ref/meta/figure.png b/tests/ref/meta/figure.png
index 90f59d14..37386698 100644
--- a/tests/ref/meta/figure.png
+++ b/tests/ref/meta/figure.png
Binary files differ
diff --git a/tests/typ/meta/figure.typ b/tests/typ/meta/figure.typ
index 62d163a9..7d618d06 100644
--- a/tests/typ/meta/figure.typ
+++ b/tests/typ/meta/figure.typ
@@ -100,3 +100,12 @@ We can clearly see that @fig-cylinder and
#show figure: set block(breakable: true)
#figure(table[a][b][c][d][e], caption: [A table])
+
+---
+// Test custom separator for figure caption
+#set figure.caption(separator: [ --- ])
+
+#figure(
+ table(columns: 2)[a][b],
+ caption: [The table with custom separator.],
+)