summaryrefslogtreecommitdiff
path: root/src/layout/stack.rs
blob: 47dd93eacd1b2d3b5728c0e8e679c48658243f05 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use super::*;

/// A node that stacks and aligns its children.
///
/// # Alignment
/// Individual layouts can be aligned at `Start`, `Center` or  `End` along both
/// axes. These alignments are with processed with respect to the size of the
/// finished layout and not the total usable size. This means that a later
/// layout can have influence on the position of an earlier one. Consider the
/// following example.
/// ```typst
/// [align: right][A word.]
/// [align: left][A sentence with a couple more words.]
/// ```
/// The resulting layout looks like this:
/// ```text
/// |--------------------------------------|
/// |                              A word. |
/// |                                      |
/// | A sentence with a couple more words. |
/// |--------------------------------------|
/// ```
/// The position of the first aligned box thus depends on the length of the
/// sentence in the second box.
#[derive(Debug, Clone, PartialEq)]
pub struct Stack {
    pub dirs: Gen<Dir>,
    pub children: Vec<LayoutNode>,
    pub aligns: Gen<Align>,
    pub expand: Spec<bool>,
}

#[async_trait(?Send)]
impl Layout for Stack {
    async fn layout(
        &self,
        ctx: &mut LayoutContext,
        constraints: LayoutConstraints,
    ) -> Vec<Layouted> {
        let mut items = vec![];

        let size = constraints.spaces[0].size;
        let mut space = StackSpace::new(self.dirs, self.expand, size);
        let mut i = 0;

        for child in &self.children {
            let child_constraints = LayoutConstraints {
                spaces: {
                    let mut remaining = vec![LayoutSpace {
                        base: space.full_size,
                        size: space.usable,
                    }];
                    let next = (i + 1).min(constraints.spaces.len() - 1);
                    remaining.extend(&constraints.spaces[next ..]);
                    remaining
                },
                repeat: constraints.repeat,
            };

            for item in child.layout(ctx, child_constraints).await {
                match item {
                    Layouted::Spacing(spacing) => space.push_spacing(spacing),
                    Layouted::Box(mut boxed, aligns) => {
                        let mut last = false;
                        while let Err(back) = space.push_box(boxed, aligns) {
                            boxed = back;
                            if last {
                                break;
                            }

                            items.push(Layouted::Box(space.finish(), self.aligns));

                            if i + 1 < constraints.spaces.len() {
                                i += 1;
                            } else {
                                last = true;
                            }

                            let size = constraints.spaces[i].size;
                            space = StackSpace::new(self.dirs, self.expand, size);
                        }
                    }
                }
            }
        }

        items.push(Layouted::Box(space.finish(), self.aligns));
        items
    }
}

struct StackSpace {
    dirs: Gen<Dir>,
    expand: Spec<bool>,
    boxes: Vec<(BoxLayout, Gen<Align>)>,
    full_size: Size,
    usable: Size,
    used: Size,
    ruler: Align,
}

impl StackSpace {
    fn new(dirs: Gen<Dir>, expand: Spec<bool>, size: Size) -> Self {
        Self {
            dirs,
            expand,
            boxes: vec![],
            full_size: size,
            usable: size,
            used: Size::ZERO,
            ruler: Align::Start,
        }
    }

    fn push_box(
        &mut self,
        boxed: BoxLayout,
        aligns: Gen<Align>,
    ) -> Result<(), BoxLayout> {
        let main = self.dirs.main.axis();
        let cross = self.dirs.cross.axis();
        if aligns.main < self.ruler || !self.usable.fits(boxed.size) {
            return Err(boxed);
        }

        let size = boxed.size.switch(self.dirs);
        *self.used.get_mut(cross) = self.used.get(cross).max(size.cross);
        *self.used.get_mut(main) += size.main;
        *self.usable.get_mut(main) -= size.main;
        self.boxes.push((boxed, aligns));
        self.ruler = aligns.main;

        Ok(())
    }

    fn push_spacing(&mut self, spacing: Length) {
        let main = self.dirs.main.axis();
        let max = self.usable.get(main);
        let trimmed = spacing.min(max);
        *self.used.get_mut(main) += trimmed;
        *self.usable.get_mut(main) -= trimmed;

        let size = Gen::new(trimmed, Length::ZERO).switch(self.dirs);
        self.boxes.push((BoxLayout::new(size.to_size()), Gen::default()));
    }

    fn finish(mut self) -> BoxLayout {
        let dirs = self.dirs;
        let main = dirs.main.axis();

        if self.expand.horizontal {
            self.used.width = self.full_size.width;
        }

        if self.expand.vertical {
            self.used.height = self.full_size.height;
        }

        let mut sum = Length::ZERO;
        let mut sums = Vec::with_capacity(self.boxes.len() + 1);

        for (boxed, _) in &self.boxes {
            sums.push(sum);
            sum += boxed.size.get(main);
        }

        sums.push(sum);

        let mut layout = BoxLayout::new(self.used);
        let used = self.used.switch(dirs);

        for (i, (boxed, aligns)) in self.boxes.into_iter().enumerate() {
            let size = boxed.size.switch(dirs);

            let before = sums[i];
            let after = sum - sums[i + 1];
            let main_len = used.main - size.main;
            let main_range = if dirs.main.is_positive() {
                before .. main_len - after
            } else {
                main_len - before .. after
            };

            let cross_len = used.cross - size.cross;
            let cross_range = if dirs.cross.is_positive() {
                Length::ZERO .. cross_len
            } else {
                cross_len .. Length::ZERO
            };

            let main = aligns.main.apply(main_range);
            let cross = aligns.cross.apply(cross_range);
            let pos = Gen::new(main, cross).switch(dirs).to_point();

            layout.push_layout(pos, boxed);
        }

        layout
    }
}

impl From<Stack> for LayoutNode {
    fn from(stack: Stack) -> Self {
        Self::dynamic(stack)
    }
}