Adapt default figure separator to the current locale (#2390)

This commit is contained in:
MALO 2023-10-22 13:33:00 +02:00 committed by GitHub
parent 7f185f21e0
commit aaac1dbd68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 5 deletions

View File

@ -1,4 +1,5 @@
use std::str::FromStr; use std::str::FromStr;
use typst::util::option_eq;
use super::{ use super::{
Count, Counter, CounterKey, CounterUpdate, LocalName, Numbering, NumberingPattern, Count, Counter, CounterKey, CounterUpdate, LocalName, Numbering, NumberingPattern,
@ -380,7 +381,7 @@ impl Outlinable for FigureElem {
supplement += TextElem::packed('\u{a0}'); supplement += TextElem::packed('\u{a0}');
} }
let separator = caption.separator(StyleChain::default()); let separator = caption.get_separator(StyleChain::default());
realized = supplement + numbers + separator + caption.body(); realized = supplement + numbers + separator + caption.body();
} }
@ -455,8 +456,10 @@ pub struct FigureCaption {
/// caption: [A rectangle], /// 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. /// The caption's body.
/// ///
@ -498,10 +501,40 @@ pub struct FigureCaption {
pub location: Option<Location>, 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 { impl Synthesize for FigureCaption {
fn synthesize(&mut self, _: &mut Vt, styles: StyleChain) -> SourceResult<()> { fn synthesize(&mut self, _: &mut Vt, styles: StyleChain) -> SourceResult<()> {
self.push_position(self.position(styles)); self.push_position(self.position(styles));
self.push_separator(self.separator(styles)); self.push_separator(Smart::Custom(self.get_separator(styles)));
Ok(()) Ok(())
} }
} }
@ -518,7 +551,7 @@ impl Show for FigureCaption {
if !supplement.is_empty() { if !supplement.is_empty() {
supplement += TextElem::packed('\u{a0}'); supplement += TextElem::packed('\u{a0}');
} }
realized = supplement + numbers + self.separator(styles) + realized; realized = supplement + numbers + self.get_separator(styles) + realized;
} }
Ok(realized) Ok(realized)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 53 KiB

After

Width:  |  Height:  |  Size: 186 KiB

View File

@ -109,3 +109,25 @@ We can clearly see that @fig-cylinder and
table(columns: 2)[a][b], table(columns: 2)[a][b],
caption: [The table with custom separator.], 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é.],
)