diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-11-22 20:12:37 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-11-22 20:12:37 +0100 |
| commit | 5992f11b4c33d82fa245205980094accb57a8c76 (patch) | |
| tree | b0043ba4b4cabab2fa8f63ec80f37c9d247e134a /library/src/layout | |
| parent | b476de87b7cea1405bf3c051ff8e0ac7c473dbae (diff) | |
Reorganize content type
Diffstat (limited to 'library/src/layout')
| -rw-r--r-- | library/src/layout/flow.rs | 6 | ||||
| -rw-r--r-- | library/src/layout/mod.rs | 33 | ||||
| -rw-r--r-- | library/src/layout/place.rs | 4 | ||||
| -rw-r--r-- | library/src/layout/spacing.rs | 4 | ||||
| -rw-r--r-- | library/src/layout/stack.rs | 4 |
5 files changed, 24 insertions, 27 deletions
diff --git a/library/src/layout/flow.rs b/library/src/layout/flow.rs index b69f4f4e..4508023a 100644 --- a/library/src/layout/flow.rs +++ b/library/src/layout/flow.rs @@ -25,7 +25,7 @@ impl LayoutBlock for FlowNode { for (child, map) in self.0.iter() { let styles = map.chain(&styles); - if let Some(&node) = child.downcast::<VNode>() { + if let Some(&node) = child.to::<VNode>() { layouter.layout_spacing(node.amount, styles); } else if child.has::<dyn LayoutBlock>() { layouter.layout_block(world, child, styles)?; @@ -134,7 +134,7 @@ impl FlowLayouter { // Placed nodes that are out of flow produce placed items which aren't // aligned later. - if let Some(placed) = block.downcast::<PlaceNode>() { + if let Some(placed) = block.to::<PlaceNode>() { if placed.out_of_flow() { let frame = block.layout_block(world, &self.regions, styles)?.remove(0); self.items.push(FlowItem::Placed(frame)); @@ -149,7 +149,7 @@ impl FlowLayouter { styles.get(ParNode::ALIGN), // Vertical align node alignment is respected by the flow. block - .downcast::<AlignNode>() + .to::<AlignNode>() .and_then(|aligned| aligned.aligns.y) .map(|align| align.resolve(styles)) .unwrap_or(Align::Top), diff --git a/library/src/layout/mod.rs b/library/src/layout/mod.rs index 7b11177b..b05da901 100644 --- a/library/src/layout/mod.rs +++ b/library/src/layout/mod.rs @@ -32,8 +32,7 @@ use typst::diag::SourceResult; use typst::frame::Frame; use typst::geom::*; use typst::model::{ - capability, Content, Node, SequenceNode, Style, StyleChain, StyleVecBuilder, - StyledNode, + capability, Content, SequenceNode, Style, StyleChain, StyleVecBuilder, StyledNode, }; use typst::World; @@ -48,7 +47,7 @@ use crate::text::{ /// Root-level layout. #[capability] -pub trait LayoutRoot: 'static + Sync + Send { +pub trait LayoutRoot { /// Layout into one frame per page. fn layout_root(&self, world: Tracked<dyn World>) -> SourceResult<Vec<Frame>>; } @@ -69,7 +68,7 @@ impl LayoutRoot for Content { /// Block-level layout. #[capability] -pub trait LayoutBlock: 'static + Sync + Send { +pub trait LayoutBlock { /// Layout into one frame per region. fn layout_block( &self, @@ -88,7 +87,7 @@ impl LayoutBlock for Content { styles: StyleChain, ) -> SourceResult<Vec<Frame>> { if !styles.applicable(self) { - if let Some(node) = self.to::<dyn LayoutBlock>() { + if let Some(node) = self.with::<dyn LayoutBlock>() { let barrier = Style::Barrier(self.id()); let styles = barrier.chain(&styles); return node.layout_block(world, regions, styles); @@ -105,7 +104,7 @@ impl LayoutBlock for Content { /// Inline-level layout. #[capability] -pub trait LayoutInline: 'static + Sync + Send { +pub trait LayoutInline { /// Layout into a single frame. fn layout_inline( &self, @@ -127,13 +126,13 @@ impl LayoutInline for Content { assert!(regions.last.is_none()); if !styles.applicable(self) { - if let Some(node) = self.to::<dyn LayoutInline>() { + if let Some(node) = self.with::<dyn LayoutInline>() { let barrier = Style::Barrier(self.id()); let styles = barrier.chain(&styles); return node.layout_inline(world, regions, styles); } - if let Some(node) = self.to::<dyn LayoutBlock>() { + if let Some(node) = self.with::<dyn LayoutBlock>() { let barrier = Style::Barrier(self.id()); let styles = barrier.chain(&styles); return Ok(node.layout_block(world, regions, styles)?.remove(0)); @@ -312,11 +311,11 @@ impl<'a> Builder<'a> { content: &'a Content, styles: StyleChain<'a>, ) -> SourceResult<()> { - if let Some(styled) = content.downcast::<StyledNode>() { + if let Some(styled) = content.to::<StyledNode>() { return self.styled(styled, styles); } - if let Some(seq) = content.downcast::<SequenceNode>() { + if let Some(seq) = content.to::<SequenceNode>() { return self.sequence(seq, styles); } @@ -347,7 +346,7 @@ impl<'a> Builder<'a> { } let keep = content - .downcast::<PagebreakNode>() + .to::<PagebreakNode>() .map_or(false, |pagebreak| !pagebreak.weak); self.interrupt(Interruption::Page, styles, keep)?; @@ -457,12 +456,12 @@ struct DocBuilder<'a> { impl<'a> DocBuilder<'a> { fn accept(&mut self, content: &Content, styles: StyleChain<'a>) -> bool { - if let Some(pagebreak) = content.downcast::<PagebreakNode>() { + if let Some(pagebreak) = content.to::<PagebreakNode>() { self.keep_next = !pagebreak.weak; return true; } - if let Some(page) = content.downcast::<PageNode>() { + if let Some(page) = content.to::<PageNode>() { self.pages.push(page.clone(), styles); self.keep_next = false; return true; @@ -498,11 +497,11 @@ impl<'a> FlowBuilder<'a> { } if content.has::<dyn LayoutBlock>() { - let is_tight_list = if let Some(node) = content.downcast::<ListNode>() { + let is_tight_list = if let Some(node) = content.to::<ListNode>() { node.tight - } else if let Some(node) = content.downcast::<EnumNode>() { + } else if let Some(node) = content.to::<EnumNode>() { node.tight - } else if let Some(node) = content.downcast::<DescNode>() { + } else if let Some(node) = content.to::<DescNode>() { node.tight } else { false @@ -576,7 +575,7 @@ impl<'a> ListBuilder<'a> { return true; } - if let Some(item) = content.downcast::<ListItem>() { + if let Some(item) = content.to::<ListItem>() { if self .items .items() diff --git a/library/src/layout/place.rs b/library/src/layout/place.rs index 42f7ff7d..a4f05a6c 100644 --- a/library/src/layout/place.rs +++ b/library/src/layout/place.rs @@ -49,9 +49,7 @@ impl PlaceNode { /// origin. Instead of relative to the parent's current flow/cursor /// position. pub fn out_of_flow(&self) -> bool { - self.0 - .downcast::<AlignNode>() - .map_or(false, |node| node.aligns.y.is_some()) + self.0.to::<AlignNode>().map_or(false, |node| node.aligns.y.is_some()) } } diff --git a/library/src/layout/spacing.rs b/library/src/layout/spacing.rs index 74bd2f0f..b42f90ef 100644 --- a/library/src/layout/spacing.rs +++ b/library/src/layout/spacing.rs @@ -44,7 +44,7 @@ impl Behave for HNode { } fn larger(&self, prev: &Content) -> bool { - let Some(prev) = prev.downcast::<Self>() else { return false }; + let Some(prev) = prev.to::<Self>() else { return false }; self.amount > prev.amount } } @@ -110,7 +110,7 @@ impl Behave for VNode { } fn larger(&self, prev: &Content) -> bool { - let Some(prev) = prev.downcast::<Self>() else { return false }; + let Some(prev) = prev.to::<Self>() else { return false }; self.amount > prev.amount } } diff --git a/library/src/layout/stack.rs b/library/src/layout/stack.rs index 02129e1f..12a1c384 100644 --- a/library/src/layout/stack.rs +++ b/library/src/layout/stack.rs @@ -182,11 +182,11 @@ impl<'a> StackLayouter<'a> { // Block-axis alignment of the `AlignNode` is respected // by the stack node. let align = block - .downcast::<AlignNode>() + .to::<AlignNode>() .and_then(|node| node.aligns.get(self.axis)) .map(|align| align.resolve(styles)) .unwrap_or_else(|| { - if let Some(styled) = block.downcast::<StyledNode>() { + if let Some(styled) = block.to::<StyledNode>() { let map = &styled.map; if map.contains(ParNode::ALIGN) { return StyleChain::with_root(map).get(ParNode::ALIGN); |
