typst_library/model/
strong.rs

1use crate::diag::SourceResult;
2use crate::engine::Engine;
3use crate::foundations::{
4    elem, Content, NativeElement, Packed, Show, StyleChain, TargetElem,
5};
6use crate::html::{tag, HtmlElem};
7use crate::text::{TextElem, WeightDelta};
8
9/// Strongly emphasizes content by increasing the font weight.
10///
11/// Increases the current font weight by a given `delta`.
12///
13/// # Example
14/// ```example
15/// This is *strong.* \
16/// This is #strong[too.] \
17///
18/// #show strong: set text(red)
19/// And this is *evermore.*
20/// ```
21///
22/// # Syntax
23/// This function also has dedicated syntax: To strongly emphasize content,
24/// simply enclose it in stars/asterisks (`*`). Note that this only works at
25/// word boundaries. To strongly emphasize part of a word, you have to use the
26/// function.
27#[elem(title = "Strong Emphasis", keywords = ["bold", "weight"], Show)]
28pub struct StrongElem {
29    /// The delta to apply on the font weight.
30    ///
31    /// ```example
32    /// #set strong(delta: 0)
33    /// No *effect!*
34    /// ```
35    #[default(300)]
36    pub delta: i64,
37
38    /// The content to strongly emphasize.
39    #[required]
40    pub body: Content,
41}
42
43impl Show for Packed<StrongElem> {
44    #[typst_macros::time(name = "strong", span = self.span())]
45    fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
46        let body = self.body.clone();
47        Ok(if TargetElem::target_in(styles).is_html() {
48            HtmlElem::new(tag::strong)
49                .with_body(Some(body))
50                .pack()
51                .spanned(self.span())
52        } else {
53            body.styled(TextElem::set_delta(WeightDelta(self.delta(styles))))
54        })
55    }
56}