summaryrefslogtreecommitdiff
path: root/library/src/layout/place.rs
blob: c64766ccc927cf86fe6433a9dea95d96c1b506a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use crate::prelude::*;

/// # Place
/// Place content at an absolute position.
///
/// Placed content will not affect the position of other content. Place is
/// always relative to its parent container and will be in the foreground of all
/// other content in the container. Page margins will be respected.
///
///
/// ## Example
/// ```example
/// #set page(height: 60pt)
/// Hello, world!
///
/// #place(
///   top + right,
///   square(
///     width: 10pt,
///     stroke: 1pt + blue
///   ),
/// )
/// ```
///
/// ## Parameters
/// - alignment: `Axes<Option<GenAlign>>` (positional)
///   Relative to which position in the parent container to place the content.
///
///   When an axis of the page is `{auto}` sized, all alignments relative to that
///   axis will be ignored, instead, the item will be placed in the origin of the
///   axis.
///
/// - body: `Content` (positional, required)
///   The content to place.
///
/// - dx: `Rel<Length>` (named)
///   The horizontal displacement of the placed content.
///
///   ```example
///   #set page(height: 100pt)
///   #for i in range(16) {
///     let amount = i * 4pt
///     place(center, dx: amount - 32pt, dy: amount)[A]
///   }
///   ```
///
/// - dy: `Rel<Length>` (named)
///   The vertical displacement of the placed content.
///
/// ## Category
/// layout
#[func]
#[capable(Layout, Behave)]
#[derive(Debug, Hash)]
pub struct PlaceNode(pub Content, bool);

#[node]
impl PlaceNode {
    fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> {
        let aligns = args.find()?.unwrap_or(Axes::with_x(Some(GenAlign::Start)));
        let dx = args.named("dx")?.unwrap_or_default();
        let dy = args.named("dy")?.unwrap_or_default();
        let body = args.expect::<Content>("body")?;
        let out_of_flow = aligns.y.is_some();
        Ok(Self(body.moved(Axes::new(dx, dy)).aligned(aligns), out_of_flow).pack())
    }
}

impl Layout for PlaceNode {
    fn layout(
        &self,
        vt: &mut Vt,
        styles: StyleChain,
        regions: Regions,
    ) -> SourceResult<Fragment> {
        let out_of_flow = self.out_of_flow();

        // The pod is the base area of the region because for absolute
        // placement we don't really care about the already used area.
        let pod = {
            let finite = regions.base().map(Abs::is_finite);
            let expand = finite & (regions.expand | out_of_flow);
            Regions::one(regions.base(), expand)
        };

        let mut frame = self.0.layout(vt, styles, pod)?.into_frame();

        // If expansion is off, zero all sizes so that we don't take up any
        // space in our parent. Otherwise, respect the expand settings.
        let target = regions.expand.select(regions.size, Size::zero());
        frame.resize(target, Align::LEFT_TOP);

        Ok(Fragment::frame(frame))
    }
}

impl PlaceNode {
    /// Whether this node wants to be placed relative to its its parent's base
    /// origin. Instead of relative to the parent's current flow/cursor
    /// position.
    pub fn out_of_flow(&self) -> bool {
        self.1
    }
}

impl Behave for PlaceNode {
    fn behaviour(&self) -> Behaviour {
        Behaviour::Ignorant
    }
}