diff options
Diffstat (limited to 'src/library/structure')
| -rw-r--r-- | src/library/structure/heading.rs | 36 | ||||
| -rw-r--r-- | src/library/structure/list.rs | 97 | ||||
| -rw-r--r-- | src/library/structure/table.rs | 11 |
3 files changed, 57 insertions, 87 deletions
diff --git a/src/library/structure/heading.rs b/src/library/structure/heading.rs index a352cc92..a6c87912 100644 --- a/src/library/structure/heading.rs +++ b/src/library/structure/heading.rs @@ -63,11 +63,15 @@ impl Show for HeadingNode { } } - fn show( + fn realize(&self, _: &mut Context, _: StyleChain) -> TypResult<Content> { + Ok(self.body.clone()) + } + + fn finalize( &self, ctx: &mut Context, styles: StyleChain, - realized: Option<Content>, + mut realized: Content, ) -> TypResult<Content> { macro_rules! resolve { ($key:expr) => { @@ -75,8 +79,6 @@ impl Show for HeadingNode { }; } - let mut body = realized.unwrap_or_else(|| self.body.clone()); - let mut map = StyleMap::new(); map.set(TextNode::SIZE, resolve!(Self::SIZE)); @@ -96,30 +98,22 @@ impl Show for HeadingNode { map.set(TextNode::EMPH, Toggle); } - let mut seq = vec![]; if resolve!(Self::UNDERLINE) { - body = body.underlined(); - } - - let above = resolve!(Self::ABOVE); - if !above.is_zero() { - seq.push(Content::Vertical(above.resolve(styles).into())); + realized = realized.underlined(); } - seq.push(body); - - let below = resolve!(Self::BELOW); - if !below.is_zero() { - seq.push(Content::Vertical(below.resolve(styles).into())); - } - - let mut content = Content::sequence(seq).styled_with_map(map); + realized = realized.styled_with_map(map); if resolve!(Self::BLOCK) { - content = Content::block(content); + realized = Content::block(realized); } - Ok(content) + realized = realized.spaced( + resolve!(Self::ABOVE).resolve(styles), + resolve!(Self::BELOW).resolve(styles), + ); + + Ok(realized) } } diff --git a/src/library/structure/list.rs b/src/library/structure/list.rs index c59b443d..6655175f 100644 --- a/src/library/structure/list.rs +++ b/src/library/structure/list.rs @@ -79,66 +79,51 @@ impl<const L: ListKind> Show for ListNode<L> { } } - fn show( - &self, - ctx: &mut Context, - styles: StyleChain, - realized: Option<Content>, - ) -> TypResult<Content> { - let content = if let Some(content) = realized { - content - } else { - let mut cells = vec![]; - let mut number = self.start; - - let label = styles.get(Self::LABEL); - - for item in &self.items { - number = item.number.unwrap_or(number); - cells.push(LayoutNode::default()); - cells.push(label.resolve(ctx, L, number)?.pack()); - cells.push(LayoutNode::default()); - cells.push((*item.body).clone().pack()); - number += 1; - } - - let leading = styles.get(ParNode::LEADING); - let spacing = if self.tight { - styles.get(Self::SPACING) - } else { - styles.get(ParNode::SPACING) - }; - - let gutter = leading + spacing; - let indent = styles.get(Self::INDENT); - let body_indent = styles.get(Self::BODY_INDENT); - - Content::block(GridNode { - tracks: Spec::with_x(vec![ - TrackSizing::Relative(indent.into()), - TrackSizing::Auto, - TrackSizing::Relative(body_indent.into()), - TrackSizing::Auto, - ]), - gutter: Spec::with_y(vec![TrackSizing::Relative(gutter.into())]), - cells, - }) - }; - - let mut seq = vec![]; - let above = styles.get(Self::ABOVE); - if !above.is_zero() { - seq.push(Content::Vertical(above.into())); + fn realize(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { + let mut cells = vec![]; + let mut number = self.start; + + let label = styles.get(Self::LABEL); + + for item in &self.items { + number = item.number.unwrap_or(number); + cells.push(LayoutNode::default()); + cells.push(label.resolve(ctx, L, number)?.pack()); + cells.push(LayoutNode::default()); + cells.push((*item.body).clone().pack()); + number += 1; } - seq.push(content); + let leading = styles.get(ParNode::LEADING); + let spacing = if self.tight { + styles.get(Self::SPACING) + } else { + styles.get(ParNode::SPACING) + }; - let below = styles.get(Self::BELOW); - if !below.is_zero() { - seq.push(Content::Vertical(below.into())); - } + let gutter = leading + spacing; + let indent = styles.get(Self::INDENT); + let body_indent = styles.get(Self::BODY_INDENT); + + Ok(Content::block(GridNode { + tracks: Spec::with_x(vec![ + TrackSizing::Relative(indent.into()), + TrackSizing::Auto, + TrackSizing::Relative(body_indent.into()), + TrackSizing::Auto, + ]), + gutter: Spec::with_y(vec![TrackSizing::Relative(gutter.into())]), + cells, + })) + } - Ok(Content::sequence(seq)) + fn finalize( + &self, + _: &mut Context, + styles: StyleChain, + realized: Content, + ) -> TypResult<Content> { + Ok(realized.spaced(styles.get(Self::ABOVE), styles.get(Self::BELOW))) } } diff --git a/src/library/structure/table.rs b/src/library/structure/table.rs index aefd01b5..191d3dd3 100644 --- a/src/library/structure/table.rs +++ b/src/library/structure/table.rs @@ -64,16 +64,7 @@ impl Show for TableNode { } } - fn show( - &self, - _: &mut Context, - styles: StyleChain, - realized: Option<Content>, - ) -> TypResult<Content> { - if let Some(content) = realized { - return Ok(content); - } - + fn realize(&self, _: &mut Context, styles: StyleChain) -> TypResult<Content> { let primary = styles.get(Self::PRIMARY); let secondary = styles.get(Self::SECONDARY); let stroke = styles.get(Self::STROKE).map(RawStroke::unwrap_or_default); |
