diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-04-26 13:25:14 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-04-26 13:26:31 +0200 |
| commit | f7c67cde72e6a67f45180856b332bae9863243bd (patch) | |
| tree | 09b1a40e17b2f8aa0c3661ac989a40840cea1f12 /src/library | |
| parent | 09aabc3a21e403e0b09a6d6ba517e34a303b217c (diff) | |
New document & flow building
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/layout/page.rs | 2 | ||||
| -rw-r--r-- | src/library/structure/doc.rs | 29 | ||||
| -rw-r--r-- | src/library/structure/list.rs | 38 | ||||
| -rw-r--r-- | src/library/structure/mod.rs | 2 |
4 files changed, 60 insertions, 11 deletions
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<PageNode>); + +impl DocNode { + /// Layout the document into a sequence of frames, one per page. + pub fn layout( + &self, + ctx: &mut Context, + styles: StyleChain, + ) -> TypResult<Vec<Arc<Frame>>> { + 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<const L: ListKind = UNORDERED> { /// there is list spacing between the items. pub tight: bool, /// The individual bulleted or numbered items. - pub items: Vec<ListItem>, + pub items: StyleVec<ListItem>, } /// 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<usize>, /// The node that produces the item's body. @@ -59,7 +63,11 @@ impl<const L: ListKind> ListNode<L> { 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<const L: ListKind> Show for ListNode<L> { "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<const L: ListKind> Show for ListNode<L> { 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<const L: ListKind> Show for ListNode<L> { } } -impl<const L: ListKind> From<ListItem> for ListNode<L> { - 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::*; |
