diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-08-04 11:46:04 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-08-04 11:46:04 +0200 |
| commit | ed4fdcb0ada909f1cc3d7436334e253f0ec14d55 (patch) | |
| tree | 2b9abe7a852a060899ce4aa976391fa8dd9d293b /src/layout | |
| parent | dbfb3d2ced91e56314dfabbb4df9a338926c0a7a (diff) | |
Par nodes 🧳
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/mod.rs | 2 | ||||
| -rw-r--r-- | src/layout/tree.rs | 111 |
2 files changed, 55 insertions, 58 deletions
diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 8a68a6a5..3cfb872f 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -142,8 +142,6 @@ pub enum Command<'a> { /// Start a new line. BreakLine, - /// Start a new paragraph. - BreakParagraph, /// Start a new page, which will be part of the finished layout even if it /// stays empty (since the page break is a _hard_ space break). BreakPage, diff --git a/src/layout/tree.rs b/src/layout/tree.rs index d20fc666..3abdc934 100644 --- a/src/layout/tree.rs +++ b/src/layout/tree.rs @@ -43,26 +43,43 @@ impl<'a> TreeLayouter<'a> { } } - async fn layout_tree(&mut self, tree: &SyntaxTree) { - for node in tree { - self.layout_node(node).await; - } - } - 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 TreeLayouter, deco| { + let decorate = |this: &mut Self, deco| { this.feedback.decorations.push(Spanned::new(deco, node.span)); }; match &node.v { - SyntaxNode::Space => self.layout_space(), - SyntaxNode::Parbreak => self.layout_paragraph(), + SyntaxNode::Spacing => { + self.layouter.add_primary_spacing( + self.style.text.word_spacing(), + SpacingKind::WORD, + ); + }, + SyntaxNode::Linebreak => self.layouter.finish_line(), + 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); @@ -75,16 +92,6 @@ impl<'a> TreeLayouter<'a> { self.layout_text(text).await; } - 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::Raw(lines) => { // TODO: Make this more efficient. let fallback = self.style.text.fallback.clone(); @@ -109,14 +116,25 @@ impl<'a> TreeLayouter<'a> { self.style.text.fallback = fallback; } + SyntaxNode::Par(par) => self.layout_par(par).await, + SyntaxNode::Dyn(dynamic) => { self.layout_dyn(Spanned::new(dynamic.as_ref(), node.span)).await; } } } + async fn layout_par(&mut self, par: &SyntaxTree) { + self.layouter.add_secondary_spacing( + self.style.text.paragraph_spacing(), + SpacingKind::PARAGRAPH, + ); + + self.layout_tree(par).await; + } + async fn layout_dyn(&mut self, dynamic: Spanned<&dyn DynamicNode>) { - // Execute the tree's command-generating layout function. + // Execute the dynamic node's command-generating layout function. let layouted = dynamic.v.layout(LayoutContext { style: &self.style, spaces: self.layouter.remaining(), @@ -131,11 +149,21 @@ impl<'a> TreeLayouter<'a> { } } - fn execute_command<'r>( - &'r mut self, - command: Command<'r>, - tree_span: Span, - ) -> DynFuture<'r, ()> { Box::pin(async move { + 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 execute_command(&mut self, command: Command<'_>, span: Span) { use Command::*; match command { @@ -149,13 +177,12 @@ impl<'a> TreeLayouter<'a> { } BreakLine => self.layouter.finish_line(), - BreakParagraph => self.layout_paragraph(), BreakPage => { if self.ctx.root { self.layouter.finish_space(true) } else { error!( - @self.feedback, tree_span, + @self.feedback, span, "page break cannot only be issued from root context", ); } @@ -183,7 +210,7 @@ impl<'a> TreeLayouter<'a> { ], true); } else { error!( - @self.feedback, tree_span, + @self.feedback, span, "page style cannot only be changed from root context", ); } @@ -195,33 +222,5 @@ impl<'a> TreeLayouter<'a> { self.ctx.axes = axes; } } - }) } - - 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 - ); - } - - fn layout_space(&mut self) { - self.layouter.add_primary_spacing( - self.style.text.word_spacing(), - SpacingKind::WORD, - ); - } - - fn layout_paragraph(&mut self) { - self.layouter.add_secondary_spacing( - self.style.text.paragraph_spacing(), - SpacingKind::PARAGRAPH, - ); } } |
