summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYip Coekjan <69834864+Coekjan@users.noreply.github.com>2024-05-06 20:57:16 +0800
committerGitHub <noreply@github.com>2024-05-06 12:57:16 +0000
commit556979c83bd142f812f0e33df1c913724e518743 (patch)
tree8cb965e1c10e9c7903cb66be5f06829ca8c934be
parent061319425b816907f4277c545d679a878b07fe3d (diff)
Change `smallcaps` into an element function (#3981)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
-rw-r--r--crates/typst/src/text/mod.rs2
-rw-r--r--crates/typst/src/text/smallcaps.rs42
-rw-r--r--tests/ref/smallcaps-show-rule.pngbin0 -> 1202 bytes
-rw-r--r--tests/suite/text/smallcaps.typ9
4 files changed, 39 insertions, 14 deletions
diff --git a/crates/typst/src/text/mod.rs b/crates/typst/src/text/mod.rs
index e55a35a6..c641c8a5 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::<OverlineElem>();
global.define_elem::<StrikeElem>();
global.define_elem::<HighlightElem>();
+ global.define_elem::<SmallcapsElem>();
global.define_elem::<RawElem>();
global.define_func::<lower>();
global.define_func::<upper>();
- global.define_func::<smallcaps>();
global.define_func::<lorem>();
}
diff --git a/crates/typst/src/text/smallcaps.rs b/crates/typst/src/text/smallcaps.rs
index b41fdf02..2fb6381e 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<SmallcapsElem> {
+ #[typst_macros::time(name = "smallcaps", span = self.span())]
+ fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult<Content> {
+ 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 00000000..bac72ea7
--- /dev/null
+++ b/tests/ref/smallcaps-show-rule.png
Binary files differ
diff --git a/tests/suite/text/smallcaps.typ b/tests/suite/text/smallcaps.typ
index 6f977244..6f36a028 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]