From 556979c83bd142f812f0e33df1c913724e518743 Mon Sep 17 00:00:00 2001 From: Yip Coekjan <69834864+Coekjan@users.noreply.github.com> Date: Mon, 6 May 2024 20:57:16 +0800 Subject: [PATCH] Change `smallcaps` into an element function (#3981) Co-authored-by: Laurenz --- crates/typst/src/text/mod.rs | 2 +- crates/typst/src/text/smallcaps.rs | 42 ++++++++++++++++++++--------- tests/ref/smallcaps-show-rule.png | Bin 0 -> 1202 bytes tests/suite/text/smallcaps.typ | 9 +++++++ 4 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 tests/ref/smallcaps-show-rule.png 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 0000000000000000000000000000000000000000..bac72ea7f1d6f3b07f0056ba38cd5efd3e0b3670 GIT binary patch literal 1202 zcmV;j1Wo&iP)^{r&&^{Qvv={QUg?`uY3&`~Ug)|M>X&`uhL(_W$gn+C@bB;M z?(XjY=jH9~?f>QD?Ck9Sged`|J>T<=H~y|*8kYnQezq`@V(f_=-|G2cw%*@Nn z%m1~p%F4>g$;tn*t^csB|E;LR#KgkF!vCkDz`(%&rJ%mPzP-J@yu7^spPB!im;alU zwzjtamXH6HkF>P3va+)Ol8pb6i?Ok>|B#BXu(1D*i2sd-|BQwIi-iA*g#U_ztE;R3 zhk*Zvf2pad|Al`4gnj>meE)%Z|ABe`fO(~*rT>3;|9*G>es=$Tb^m*G|9f-)d2yhi zp#OMr|95ZybZnfQod0uc|8i>oacTc>Xa8mQzRVw^=+4nzVvU?Y z4_bym8l-?eJ~ZWccbEX3k^bh*l}=V7k^FBG)eM9<=eg5ndT}an)92>r0e}jb28fF4 z4#z1$76YJ`0izHr_109tRakcLlxsB>$F)U-!`n-X#?LD*wPCdv+)P(j8>IT zw__Cd2ai1iOtOW;sZId6j&&(Zw#+OXif}6maDyAwMa;J1{BU^IE+EUk{|?q1X6(W2 zUVvz0$|K=q7t{9PR8mR06L2TU+@bK=HK3|QsD(~@MOGvnb>(E^^x(?bk{AQ2Kk86BE3taCMhLQQ|O=13