typst_library/layout/
pad.rs

1use crate::diag::SourceResult;
2use crate::engine::Engine;
3use crate::foundations::{elem, Content, NativeElement, Packed, Show, StyleChain};
4use crate::layout::{BlockElem, Length, Rel};
5
6/// Adds spacing around content.
7///
8/// The spacing can be specified for each side individually, or for all sides at
9/// once by specifying a positional argument.
10///
11/// # Example
12/// ```example
13/// #set align(center)
14///
15/// #pad(x: 16pt, image("typing.jpg"))
16/// _Typing speeds can be
17///  measured in words per minute._
18/// ```
19#[elem(title = "Padding", Show)]
20pub struct PadElem {
21    /// The padding at the left side.
22    #[parse(
23        let all = args.named("rest")?.or(args.find()?);
24        let x = args.named("x")?.or(all);
25        let y = args.named("y")?.or(all);
26        args.named("left")?.or(x)
27    )]
28    pub left: Rel<Length>,
29
30    /// The padding at the top side.
31    #[parse(args.named("top")?.or(y))]
32    pub top: Rel<Length>,
33
34    /// The padding at the right side.
35    #[parse(args.named("right")?.or(x))]
36    pub right: Rel<Length>,
37
38    /// The padding at the bottom side.
39    #[parse(args.named("bottom")?.or(y))]
40    pub bottom: Rel<Length>,
41
42    /// A shorthand to set `left` and `right` to the same value.
43    #[external]
44    pub x: Rel<Length>,
45
46    /// A shorthand to set `top` and `bottom` to the same value.
47    #[external]
48    pub y: Rel<Length>,
49
50    /// A shorthand to set all four sides to the same value.
51    #[external]
52    pub rest: Rel<Length>,
53
54    /// The content to pad at the sides.
55    #[required]
56    pub body: Content,
57}
58
59impl Show for Packed<PadElem> {
60    fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
61        Ok(BlockElem::multi_layouter(self.clone(), engine.routines.layout_pad)
62            .pack()
63            .spanned(self.span()))
64    }
65}