summaryrefslogtreecommitdiff
path: root/src/layout/tree.rs
blob: fe12cc3375e27d2496ed17994690e3868a159081 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! Layouting of syntax trees.

use super::line::{LineContext, LineLayouter};
use super::text::{layout_text, TextContext};
use super::*;
use crate::style::LayoutStyle;
use crate::syntax::decoration::Decoration;
use crate::syntax::span::{Span, Spanned};
use crate::syntax::tree::{CallExpr, Code, Heading, SyntaxNode, SyntaxTree};
use crate::{DynFuture, Feedback, Pass};

/// Layout a syntax tree into a collection of boxes.
pub async fn layout_tree(tree: &SyntaxTree, ctx: LayoutContext<'_>) -> Pass<MultiLayout> {
    let mut layouter = TreeLayouter::new(ctx);
    layouter.layout_tree(tree).await;
    layouter.finish()
}

/// Performs the tree layouting.
struct TreeLayouter<'a> {
    ctx: LayoutContext<'a>,
    layouter: LineLayouter,
    style: LayoutStyle,
    feedback: Feedback,
}

impl<'a> TreeLayouter<'a> {
    fn new(ctx: LayoutContext<'a>) -> Self {
        Self {
            layouter: LineLayouter::new(LineContext {
                spaces: ctx.spaces.clone(),
                axes: ctx.axes,
                align: ctx.align,
                repeat: ctx.repeat,
                line_spacing: ctx.style.text.line_spacing(),
            }),
            style: ctx.style.clone(),
            ctx,
            feedback: Feedback::new(),
        }
    }

    fn finish(self) -> Pass<MultiLayout> {
        Pass::new(self.layouter.finish(), self.feedback)
    }

    fn layout_tree<'t>(&'t mut self, tree: &'t SyntaxTree) -> DynFuture<'t, ()> {
        Box::pin(async move {
            for node in tree {
                self.layout_node(node).await;
            }
        })
    }

    async fn layout_node(&mut self, node: &Spanned<SyntaxNode>) {
        let decorate = |this: &mut Self, deco| {
            this.feedback.decorations.push(Spanned::new(deco, node.span));
        };

        match &node.v {
            SyntaxNode::Spacing => self.layout_space(),
            SyntaxNode::Linebreak => self.layouter.finish_line(),
            SyntaxNode::Parbreak => self.layout_parbreak(),

            SyntaxNode::ToggleItalic => {
                self.style.text.italic = !self.style.text.italic;
                decorate(self, Decoration::Italic);
            }
            SyntaxNode::ToggleBolder => {
                self.style.text.bolder = !self.style.text.bolder;
                decorate(self, Decoration::Bold);
            }

            SyntaxNode::Text(text) => {
                if self.style.text.italic {
                    decorate(self, Decoration::Italic);
                }
                if self.style.text.bolder {
                    decorate(self, Decoration::Bold);
                }
                self.layout_text(text).await;
            }

            SyntaxNode::Heading(heading) => self.layout_heading(heading).await,

            SyntaxNode::Raw(lines) => self.layout_raw(lines).await,
            SyntaxNode::Code(block) => self.layout_code(block).await,

            SyntaxNode::Call(call) => {
                self.layout_call(Spanned::new(call, node.span)).await;
            }
        }
    }

    fn layout_space(&mut self) {
        self.layouter
            .add_primary_spacing(self.style.text.word_spacing(), SpacingKind::WORD);
    }

    fn layout_parbreak(&mut self) {
        self.layouter.add_secondary_spacing(
            self.style.text.paragraph_spacing(),
            SpacingKind::PARAGRAPH,
        );
    }

    async fn layout_text(&mut self, text: &str) {
        self.layouter.add(
            layout_text(text, TextContext {
                loader: &self.ctx.loader,
                style: &self.style.text,
                dir: self.ctx.axes.primary,
                align: self.ctx.align,
            })
            .await,
        );
    }

    async fn layout_heading(&mut self, heading: &Heading) {
        let style = self.style.text.clone();
        self.style.text.font_scale *= 1.5 - 0.1 * heading.level.v.min(5) as f64;
        self.style.text.bolder = true;

        self.layout_parbreak();
        self.layout_tree(&heading.tree).await;
        self.layout_parbreak();

        self.style.text = style;
    }

    async fn layout_raw(&mut self, lines: &[String]) {
        // TODO: Make this more efficient.
        let fallback = self.style.text.fallback.clone();
        self.style.text.fallback.list.insert(0, "monospace".to_string());
        self.style.text.fallback.flatten();

        let mut first = true;
        for line in lines {
            if !first {
                self.layouter.finish_line();
            }
            first = false;
            self.layout_text(line).await;
        }

        self.style.text.fallback = fallback;
    }

    async fn layout_code(&mut self, code: &Code) {
        if code.block {
            self.layout_parbreak();
        }

        self.layout_raw(&code.lines).await;

        if code.block {
            self.layout_parbreak()
        }
    }

    async fn layout_call(&mut self, call: Spanned<&CallExpr>) {
        let ctx = LayoutContext {
            style: &self.style,
            spaces: self.layouter.remaining(),
            root: false,
            ..self.ctx
        };

        let val = call.v.eval(&ctx, &mut self.feedback).await;
        let commands = Spanned::new(val, call.span).into_commands();

        for command in commands {
            self.execute_command(command, call.span).await;
        }
    }

    async fn execute_command(&mut self, command: Command, span: Span) {
        use Command::*;

        match command {
            LayoutSyntaxTree(tree) => self.layout_tree(&tree).await,

            Add(layout) => self.layouter.add(layout),
            AddMultiple(layouts) => self.layouter.add_multiple(layouts),
            AddSpacing(space, kind, axis) => match axis {
                Primary => self.layouter.add_primary_spacing(space, kind),
                Secondary => self.layouter.add_secondary_spacing(space, kind),
            },

            BreakLine => self.layouter.finish_line(),
            BreakPage => {
                if self.ctx.root {
                    self.layouter.finish_space(true)
                } else {
                    error!(
                        @self.feedback, span,
                        "page break cannot only be issued from root context",
                    );
                }
            }

            SetTextStyle(style) => {
                self.layouter.set_line_spacing(style.line_spacing());
                self.style.text = style;
            }
            SetPageStyle(style) => {
                if self.ctx.root {
                    self.style.page = style;

                    // The line layouter has no idea of page styles and thus we
                    // need to recompute the layouting space resulting of the
                    // new page style and update it within the layouter.
                    let margins = style.margins();
                    self.ctx.base = style.size.unpadded(margins);
                    self.layouter.set_spaces(
                        vec![LayoutSpace {
                            size: style.size,
                            padding: margins,
                            expansion: LayoutExpansion::new(true, true),
                        }],
                        true,
                    );
                } else {
                    error!(
                        @self.feedback, span,
                        "page style cannot only be changed from root context",
                    );
                }
            }

            SetAlignment(align) => self.ctx.align = align,
            SetAxes(axes) => {
                self.layouter.set_axes(axes);
                self.ctx.axes = axes;
            }
        }
    }
}