diff --git a/crates/typst/src/text/mod.rs b/crates/typst/src/text/mod.rs index e55a35a68..c641c8a5a 100644 --- a/crates/typst/src/text/mod.rs +++ b/crates/typst/src/text/mod.rs @@ -66,10 +66,10 @@ pub(super) fn define(global: &mut Scope) { global.define_elem::(); global.define_elem::(); global.define_elem::(); + global.define_elem::(); global.define_elem::(); global.define_func::(); global.define_func::(); - global.define_func::(); global.define_func::(); } diff --git a/crates/typst/src/text/smallcaps.rs b/crates/typst/src/text/smallcaps.rs index b41fdf02c..2fb6381e0 100644 --- a/crates/typst/src/text/smallcaps.rs +++ b/crates/typst/src/text/smallcaps.rs @@ -1,14 +1,10 @@ -use crate::foundations::{func, Content}; +use crate::diag::SourceResult; +use crate::engine::Engine; +use crate::foundations::{elem, Content, Packed, Show, StyleChain}; use crate::text::TextElem; /// Displays text in small capitals. /// -/// _Note:_ This enables the OpenType `smcp` feature for the font. Not all fonts -/// support this feature. Sometimes smallcaps are part of a dedicated font and -/// sometimes they are not available at all. In the future, this function will -/// support selecting a dedicated smallcaps font as well as synthesizing -/// smallcaps from normal letters, but this is not yet implemented. -/// /// # Example /// ```example /// #set par(justify: true) @@ -23,10 +19,30 @@ use crate::text::TextElem; /// = Introduction /// #lorem(40) /// ``` -#[func(title = "Small Capitals")] -pub fn smallcaps( - /// The text to display to small capitals. - body: Content, -) -> Content { - body.styled(TextElem::set_smallcaps(true)) +/// +/// # Smallcaps fonts +/// By default, this enables the OpenType `smcp` feature for the font. Not all +/// fonts support this feature. Sometimes smallcaps are part of a dedicated +/// font. This is, for example, the case for the _Latin Modern_ family of fonts. +/// In those cases, you can use a show-set rule to customize the appearance of +/// the text in smallcaps: +/// +/// ```typ +/// #show smallcaps: set text(font: "Latin Modern Roman Caps") +/// ``` +/// +/// In the future, this function will support synthesizing smallcaps from normal +/// letters, but this is not yet implemented. +#[elem(title = "Small Capitals", Show)] +pub struct SmallcapsElem { + /// The content to display in small capitals. + #[required] + pub body: Content, +} + +impl Show for Packed { + #[typst_macros::time(name = "smallcaps", span = self.span())] + fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult { + Ok(self.body().clone().styled(TextElem::set_smallcaps(true))) + } } diff --git a/tests/ref/smallcaps-show-rule.png b/tests/ref/smallcaps-show-rule.png new file mode 100644 index 000000000..bac72ea7f Binary files /dev/null and b/tests/ref/smallcaps-show-rule.png differ diff --git a/tests/suite/text/smallcaps.typ b/tests/suite/text/smallcaps.typ index 6f977244e..6f36a028b 100644 --- a/tests/suite/text/smallcaps.typ +++ b/tests/suite/text/smallcaps.typ @@ -1,3 +1,12 @@ --- smallcaps --- // Test smallcaps. #smallcaps[Smallcaps] + +--- smallcaps-show-rule --- +// There is no dedicated smallcaps font in typst-dev-assets, so we just use some +// other font to test this show rule. +#show smallcaps: set text(font: "PT Sans") +#smallcaps[Smallcaps] + +#show smallcaps: set text(fill: red) +#smallcaps[Smallcaps]