typst_library/text/smallcaps.rs
1use crate::diag::SourceResult;
2use crate::engine::Engine;
3use crate::foundations::{elem, Content, Packed, Show, StyleChain};
4use crate::text::TextElem;
5
6/// Displays text in small capitals.
7///
8/// # Example
9/// ```example
10/// Hello \
11/// #smallcaps[Hello]
12/// ```
13///
14/// # Smallcaps fonts
15/// By default, this uses the `smcp` and `c2sc` OpenType features on the font.
16/// Not all fonts support these features. Sometimes, smallcaps are part of a
17/// dedicated font. This is, for example, the case for the _Latin Modern_ family
18/// of fonts. In those cases, you can use a show-set rule to customize the
19/// appearance of the text in smallcaps:
20///
21/// ```typ
22/// #show smallcaps: set text(font: "Latin Modern Roman Caps")
23/// ```
24///
25/// In the future, this function will support synthesizing smallcaps from normal
26/// letters, but this is not yet implemented.
27///
28/// # Smallcaps headings
29/// You can use a [show rule]($styling/#show-rules) to apply smallcaps
30/// formatting to all your headings. In the example below, we also center-align
31/// our headings and disable the standard bold font.
32///
33/// ```example
34/// #set par(justify: true)
35/// #set heading(numbering: "I.")
36///
37/// #show heading: smallcaps
38/// #show heading: set align(center)
39/// #show heading: set text(
40/// weight: "regular"
41/// )
42///
43/// = Introduction
44/// #lorem(40)
45/// ```
46#[elem(title = "Small Capitals", Show)]
47pub struct SmallcapsElem {
48 /// Whether to turn uppercase letters into small capitals as well.
49 ///
50 /// Unless overridden by a show rule, this enables the `c2sc` OpenType
51 /// feature.
52 ///
53 /// ```example
54 /// #smallcaps(all: true)[UNICEF] is an
55 /// agency of #smallcaps(all: true)[UN].
56 /// ```
57 #[default(false)]
58 pub all: bool,
59 /// The content to display in small capitals.
60 #[required]
61 pub body: Content,
62}
63
64impl Show for Packed<SmallcapsElem> {
65 #[typst_macros::time(name = "smallcaps", span = self.span())]
66 fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
67 let sc = if self.all(styles) { Smallcaps::All } else { Smallcaps::Minuscules };
68 Ok(self.body.clone().styled(TextElem::set_smallcaps(Some(sc))))
69 }
70}
71
72/// What becomes small capitals.
73#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
74pub enum Smallcaps {
75 /// Minuscules become small capitals.
76 Minuscules,
77 /// All letters become small capitals.
78 All,
79}