typst_library/layout/
pad.rs

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