diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-10-07 17:07:44 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-10-07 17:07:44 +0200 |
| commit | 537545e7f8351d7677c396456e46568f5a5e2a7a (patch) | |
| tree | f4c7614293246db06c7fa7496458da01b15c3b84 /src/layout/nodes/document.rs | |
| parent | ca1256c924f3672feb76dbc2bc2e309eb4fc4cf5 (diff) | |
Evaluation and node-based layouting 🚀
Diffstat (limited to 'src/layout/nodes/document.rs')
| -rw-r--r-- | src/layout/nodes/document.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/layout/nodes/document.rs b/src/layout/nodes/document.rs new file mode 100644 index 00000000..af7a31e6 --- /dev/null +++ b/src/layout/nodes/document.rs @@ -0,0 +1,52 @@ +use super::*; + +/// The top-level layouting node. +#[derive(Debug, Clone, PartialEq)] +pub struct Document { + pub runs: Vec<Pages>, +} + +impl Document { + /// Create a new document. + pub fn new() -> Self { + Self { runs: vec![] } + } + + /// Layout the document. + pub async fn layout(&self, ctx: &mut LayoutContext) -> Vec<BoxLayout> { + let mut layouts = vec![]; + for run in &self.runs { + layouts.extend(run.layout(ctx).await); + } + layouts + } +} + +/// A variable-length run of pages that all have the same properties. +#[derive(Debug, Clone, PartialEq)] +pub struct Pages { + /// The size of the pages. + pub size: Size, + /// The layout node that produces the actual pages. + pub child: LayoutNode, +} + +impl Pages { + /// Layout the page run. + pub async fn layout(&self, ctx: &mut LayoutContext) -> Vec<BoxLayout> { + let constraints = LayoutConstraints { + spaces: vec![LayoutSpace { base: self.size, size: self.size }], + repeat: true, + }; + + self.child + .layout(ctx, constraints) + .await + .into_iter() + .filter_map(|item| match item { + LayoutItem::Spacing(_) => None, + LayoutItem::Box(layout, _) => Some(layout), + }) + .collect() + } +} |
