typst_library/layout/
repeat.rs

1use crate::diag::SourceResult;
2use crate::engine::Engine;
3use crate::foundations::{elem, Content, NativeElement, Packed, Show, StyleChain};
4use crate::layout::{BlockElem, Length};
5
6/// Repeats content to the available space.
7///
8/// This can be useful when implementing a custom index, reference, or outline.
9///
10/// Space may be inserted between the instances of the body parameter, so be
11/// sure to adjust the [`justify`]($repeat.justify) parameter accordingly.
12///
13/// Errors if there are no bounds on the available space, as it would create
14/// infinite content.
15///
16/// # Example
17/// ```example
18/// Sign on the dotted line:
19/// #box(width: 1fr, repeat[.])
20///
21/// #set text(10pt)
22/// #v(8pt, weak: true)
23/// #align(right)[
24///   Berlin, the 22nd of December, 2022
25/// ]
26/// ```
27#[elem(Show)]
28pub struct RepeatElem {
29    /// The content to repeat.
30    #[required]
31    pub body: Content,
32
33    /// The gap between each instance of the body.
34    #[default]
35    pub gap: Length,
36
37    /// Whether to increase the gap between instances to completely fill the
38    /// available space.
39    #[default(true)]
40    pub justify: bool,
41}
42
43impl Show for Packed<RepeatElem> {
44    fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
45        Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_repeat)
46            .pack()
47            .spanned(self.span()))
48    }
49}