summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMALO <57839069+MDLC01@users.noreply.github.com>2023-10-22 13:33:00 +0200
committerGitHub <noreply@github.com>2023-10-22 13:33:00 +0200
commitaaac1dbd683501e81bef4d961ed90c6c869030e4 (patch)
treee023f78008aa08282e4f1f53ad4b7cc3c8328a09
parent7f185f21e0b93c981ed99bd35ff1e981f48c7be0 (diff)
Adapt default figure separator to the current locale (#2390)
-rw-r--r--crates/typst-library/src/meta/figure.rs43
-rw-r--r--tests/ref/meta/figure.pngbin54197 -> 190166 bytes
-rw-r--r--tests/typ/meta/figure.typ22
3 files changed, 60 insertions, 5 deletions
diff --git a/crates/typst-library/src/meta/figure.rs b/crates/typst-library/src/meta/figure.rs
index bab7790a..0a02df57 100644
--- a/crates/typst-library/src/meta/figure.rs
+++ b/crates/typst-library/src/meta/figure.rs
@@ -1,4 +1,5 @@
use std::str::FromStr;
+use typst::util::option_eq;
use super::{
Count, Counter, CounterKey, CounterUpdate, LocalName, Numbering, NumberingPattern,
@@ -380,7 +381,7 @@ impl Outlinable for FigureElem {
supplement += TextElem::packed('\u{a0}');
}
- let separator = caption.separator(StyleChain::default());
+ let separator = caption.get_separator(StyleChain::default());
realized = supplement + numbers + separator + caption.body();
}
@@ -455,8 +456,10 @@ pub struct FigureCaption {
/// caption: [A rectangle],
/// )
/// ```
- #[default(TextElem::packed(": "))]
- pub separator: Content,
+ ///
+ /// If set to `{auto}`, the separator will be adapted to the current
+ /// [language]($text.lang) and [region]($text.region).
+ pub separator: Smart<Content>,
/// The caption's body.
///
@@ -498,10 +501,40 @@ pub struct FigureCaption {
pub location: Option<Location>,
}
+impl FigureCaption {
+ /// Gets the default separator in the given language and (optionally)
+ /// region.
+ fn local_separator(lang: Lang, region: Option<Region>) -> &'static str {
+ match lang {
+ Lang::CHINESE => ":",
+ Lang::FRENCH if option_eq(region, "CH") => "\u{202f}: ",
+ Lang::FRENCH => "\u{a0}: ",
+ Lang::DANISH
+ | Lang::DUTCH
+ | Lang::ENGLISH
+ | Lang::GERMAN
+ | Lang::ITALIAN
+ | Lang::RUSSIAN
+ | Lang::SPANISH
+ | Lang::SWEDISH
+ | _ => ": ",
+ }
+ }
+
+ fn get_separator(&self, styles: StyleChain) -> Content {
+ self.separator(styles).unwrap_or_else(|| {
+ TextElem::packed(Self::local_separator(
+ TextElem::lang_in(styles),
+ TextElem::region_in(styles),
+ ))
+ })
+ }
+}
+
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));
+ self.push_separator(Smart::Custom(self.get_separator(styles)));
Ok(())
}
}
@@ -518,7 +551,7 @@ impl Show for FigureCaption {
if !supplement.is_empty() {
supplement += TextElem::packed('\u{a0}');
}
- realized = supplement + numbers + self.separator(styles) + realized;
+ realized = supplement + numbers + self.get_separator(styles) + realized;
}
Ok(realized)
diff --git a/tests/ref/meta/figure.png b/tests/ref/meta/figure.png
index 83bd7b7f..1bc3b0dc 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 7d618d06..36e2da47 100644
--- a/tests/typ/meta/figure.typ
+++ b/tests/typ/meta/figure.typ
@@ -109,3 +109,25 @@ We can clearly see that @fig-cylinder and
table(columns: 2)[a][b],
caption: [The table with custom separator.],
)
+
+---
+// Test localized default separator
+#set text(lang: "fr", region: "CH")
+
+#figure(
+ circle(),
+ caption: [Un cercle.],
+)
+#set text(lang: "es")
+
+#figure(
+ polygon.regular(size: 1cm, vertices: 3),
+ caption: [Un triángulo.],
+)
+
+#set text(lang: "fr", region: "CA")
+
+#figure(
+ square(),
+ caption: [Un carré.],
+)