From f7c67cde72e6a67f45180856b332bae9863243bd Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 26 Apr 2022 13:25:14 +0200 Subject: New document & flow building --- src/library/layout/page.rs | 2 +- src/library/structure/doc.rs | 29 +++++++++++++++++++++++++++++ src/library/structure/list.rs | 38 ++++++++++++++++++++++++++++---------- src/library/structure/mod.rs | 2 ++ 4 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 src/library/structure/doc.rs (limited to 'src/library') diff --git a/src/library/layout/page.rs b/src/library/layout/page.rs index 38ba53e9..8e5801ea 100644 --- a/src/library/layout/page.rs +++ b/src/library/layout/page.rs @@ -4,7 +4,7 @@ use super::ColumnsNode; use crate::library::prelude::*; /// Layouts its child onto one or multiple pages. -#[derive(Clone, PartialEq, Hash)] +#[derive(PartialEq, Clone, Hash)] pub struct PageNode(pub LayoutNode); #[node] diff --git a/src/library/structure/doc.rs b/src/library/structure/doc.rs new file mode 100644 index 00000000..d3fc0b39 --- /dev/null +++ b/src/library/structure/doc.rs @@ -0,0 +1,29 @@ +use crate::library::layout::PageNode; +use crate::library::prelude::*; + +/// A sequence of page runs. +#[derive(Hash)] +pub struct DocNode(pub StyleVec); + +impl DocNode { + /// Layout the document into a sequence of frames, one per page. + pub fn layout( + &self, + ctx: &mut Context, + styles: StyleChain, + ) -> TypResult>> { + let mut frames = vec![]; + for (page, map) in self.0.iter() { + let number = 1 + frames.len(); + frames.extend(page.layout(ctx, number, map.chain(&styles))?); + } + Ok(frames) + } +} + +impl Debug for DocNode { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + f.write_str("Doc ")?; + self.0.fmt(f) + } +} diff --git a/src/library/structure/list.rs b/src/library/structure/list.rs index 6655175f..ac705156 100644 --- a/src/library/structure/list.rs +++ b/src/library/structure/list.rs @@ -1,3 +1,5 @@ +use std::fmt::Write; + use unscanny::Scanner; use crate::library::layout::{GridNode, TrackSizing}; @@ -14,12 +16,14 @@ pub struct ListNode { /// there is list spacing between the items. pub tight: bool, /// The individual bulleted or numbered items. - pub items: Vec, + pub items: StyleVec, } /// An item in a list. -#[derive(Debug, Clone, PartialEq, Hash)] +#[derive(Clone, PartialEq, Hash)] pub struct ListItem { + /// The kind of item. + pub kind: ListKind, /// The number of the item. pub number: Option, /// The node that produces the item's body. @@ -59,7 +63,11 @@ impl ListNode { items: args .all()? .into_iter() - .map(|body| ListItem { number: None, body: Box::new(body) }) + .map(|body| ListItem { + kind: L, + number: None, + body: Box::new(body), + }) .collect(), })) } @@ -72,7 +80,7 @@ impl Show for ListNode { "tight" => Value::Bool(self.tight), "items" => Value::Array( self.items - .iter() + .items() .map(|item| Value::Content((*item.body).clone())) .collect() ), @@ -85,12 +93,13 @@ impl Show for ListNode { let label = styles.get(Self::LABEL); - for item in &self.items { + for (item, map) in self.items.iter() { number = item.number.unwrap_or(number); cells.push(LayoutNode::default()); - cells.push(label.resolve(ctx, L, number)?.pack()); + cells + .push(label.resolve(ctx, L, number)?.styled_with_map(map.clone()).pack()); cells.push(LayoutNode::default()); - cells.push((*item.body).clone().pack()); + cells.push((*item.body).clone().styled_with_map(map.clone()).pack()); number += 1; } @@ -127,9 +136,18 @@ impl Show for ListNode { } } -impl From for ListNode { - fn from(item: ListItem) -> Self { - Self { items: vec![item], tight: true, start: 1 } +impl Debug for ListItem { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + if self.kind == UNORDERED { + f.write_char('-')?; + } else { + if let Some(number) = self.number { + write!(f, "{}", number)?; + } + f.write_char('.')?; + } + f.write_char(' ')?; + self.body.fmt(f) } } diff --git a/src/library/structure/mod.rs b/src/library/structure/mod.rs index 073d52e9..a597211e 100644 --- a/src/library/structure/mod.rs +++ b/src/library/structure/mod.rs @@ -1,9 +1,11 @@ //! Document structuring. +mod doc; mod heading; mod list; mod table; +pub use doc::*; pub use heading::*; pub use list::*; pub use table::*; -- cgit v1.2.3