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 | |
| parent | b476de87b7cea1405bf3c051ff8e0ac7c473dbae (diff) | |
Reorganize content type
Diffstat (limited to 'library')
| -rw-r--r-- | library/src/core/behave.rs | 6 | ||||
| -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 | ||||
| -rw-r--r-- | library/src/lib.rs | 2 | ||||
| -rw-r--r-- | library/src/math/mod.rs | 2 | ||||
| -rw-r--r-- | library/src/math/tex.rs | 4 | ||||
| -rw-r--r-- | library/src/text/par.rs | 16 | ||||
| -rw-r--r-- | library/src/text/shift.rs | 8 |
11 files changed, 42 insertions, 47 deletions
diff --git a/library/src/core/behave.rs b/library/src/core/behave.rs index 8558faca..ec8fade9 100644 --- a/library/src/core/behave.rs +++ b/library/src/core/behave.rs @@ -4,7 +4,7 @@ use typst::model::{capability, Content, StyleChain, StyleVec, StyleVecBuilder}; /// How a node interacts with other nodes. #[capability] -pub trait Behave: 'static + Send + Sync { +pub trait Behave { /// The node's interaction behaviour. fn behaviour(&self) -> Behaviour; @@ -62,13 +62,13 @@ impl<'a> BehavedBuilder<'a> { /// Push an item into the sequence. pub fn push(&mut self, item: Content, styles: StyleChain<'a>) { let interaction = item - .to::<dyn Behave>() + .with::<dyn Behave>() .map_or(Behaviour::Supportive, Behave::behaviour); match interaction { Behaviour::Weak(level) => { if matches!(self.last, Behaviour::Weak(_)) { - let item = item.to::<dyn Behave>().unwrap(); + let item = item.with::<dyn Behave>().unwrap(); let i = self.staged.iter().position(|prev| { let Behaviour::Weak(prev_level) = prev.1 else { return false }; level < prev_level 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); diff --git a/library/src/lib.rs b/library/src/lib.rs index ca10837f..41c841f2 100644 --- a/library/src/lib.rs +++ b/library/src/lib.rs @@ -161,7 +161,7 @@ pub fn items() -> LangItems { linebreak: |justify| text::LinebreakNode { justify }.pack(), text: |text| text::TextNode(text).pack(), text_id: NodeId::of::<text::TextNode>(), - text_str: |content| Some(&content.downcast::<text::TextNode>()?.0), + text_str: |content| Some(&content.to::<text::TextNode>()?.0), smart_quote: |double| text::SmartQuoteNode { double }.pack(), parbreak: || text::ParbreakNode.pack(), strong: |body| text::StrongNode(body).pack(), diff --git a/library/src/math/mod.rs b/library/src/math/mod.rs index fb53b52d..1ea03d70 100644 --- a/library/src/math/mod.rs +++ b/library/src/math/mod.rs @@ -35,7 +35,7 @@ impl Show for MathNode { let mut realized = self .clone() .pack() - .guard(RecipeId::Base(NodeId::of::<Self>())) + .guarded(RecipeId::Base(NodeId::of::<Self>())) .styled_with_map(map); if self.display { diff --git a/library/src/math/tex.rs b/library/src/math/tex.rs index d7c5493b..ffde719b 100644 --- a/library/src/math/tex.rs +++ b/library/src/math/tex.rs @@ -10,7 +10,7 @@ use crate::text::{families, variant, LinebreakNode, SpaceNode, TextNode}; /// Turn a math node into TeX math code. #[capability] -pub trait Texify: 'static + Sync + Send { +pub trait Texify { /// Perform the conversion. fn texify(&self) -> EcoString; } @@ -25,7 +25,7 @@ impl Texify for Content { return r"\\".into(); } - if let Some(node) = self.to::<dyn Texify>() { + if let Some(node) = self.with::<dyn Texify>() { return node.texify(); } diff --git a/library/src/text/par.rs b/library/src/text/par.rs index 37d5e396..196878f8 100644 --- a/library/src/text/par.rs +++ b/library/src/text/par.rs @@ -420,7 +420,7 @@ fn collect<'a>( let segment = if child.is::<SpaceNode>() { full.push(' '); Segment::Text(1) - } else if let Some(node) = child.downcast::<TextNode>() { + } else if let Some(node) = child.to::<TextNode>() { let prev = full.len(); if let Some(case) = styles.get(TextNode::CASE) { full.push_str(&case.apply(&node.0)); @@ -428,18 +428,18 @@ fn collect<'a>( full.push_str(&node.0); } Segment::Text(full.len() - prev) - } else if let Some(node) = child.downcast::<LinebreakNode>() { + } else if let Some(node) = child.to::<LinebreakNode>() { let c = if node.justify { '\u{2028}' } else { '\n' }; full.push(c); Segment::Text(c.len_utf8()) - } else if let Some(node) = child.downcast::<SmartQuoteNode>() { + } else if let Some(node) = child.to::<SmartQuoteNode>() { let prev = full.len(); if styles.get(TextNode::SMART_QUOTES) { let lang = styles.get(TextNode::LANG); let region = styles.get(TextNode::REGION); let quotes = Quotes::from_lang(lang, region); let peeked = iter.peek().and_then(|(child, _)| { - if let Some(node) = child.downcast::<TextNode>() { + if let Some(node) = child.to::<TextNode>() { node.0.chars().next() } else if child.is::<SmartQuoteNode>() { Some('"') @@ -455,7 +455,7 @@ fn collect<'a>( full.push(if node.double { '"' } else { '\'' }); } Segment::Text(full.len() - prev) - } else if let Some(&node) = child.downcast::<HNode>() { + } else if let Some(&node) = child.to::<HNode>() { full.push(SPACING_REPLACE); Segment::Spacing(node.amount) } else if child.has::<dyn LayoutInline>() { @@ -523,7 +523,7 @@ fn prepare<'a>( } }, Segment::Inline(inline) => { - if let Some(repeat) = inline.downcast::<RepeatNode>() { + if let Some(repeat) = inline.to::<RepeatNode>() { items.push(Item::Repeat(repeat, styles)); } else { let size = Size::new(regions.first.x, regions.base.y); @@ -606,11 +606,11 @@ fn is_compatible(a: Script, b: Script) -> bool { /// Get a style property, but only if it is the same for all children of the /// paragraph. -fn shared_get<'a, K: Key<'a>>( +fn shared_get<'a, K: Key>( styles: StyleChain<'a>, children: &StyleVec<Content>, key: K, -) -> Option<K::Output> { +) -> Option<K::Output<'a>> { children .styles() .all(|map| !map.contains(key)) diff --git a/library/src/text/shift.rs b/library/src/text/shift.rs index 32f110c6..e63211c6 100644 --- a/library/src/text/shift.rs +++ b/library/src/text/shift.rs @@ -69,13 +69,11 @@ impl<const S: ShiftKind> Show for ShiftNode<S> { /// Find and transform the text contained in `content` to the given script kind /// if and only if it only consists of `Text`, `Space`, and `Empty` leaf nodes. fn search_text(content: &Content, mode: ShiftKind) -> Option<EcoString> { - if content.is_empty() { - Some(EcoString::new()) - } else if content.is::<SpaceNode>() { + if content.is::<SpaceNode>() { Some(' '.into()) - } else if let Some(text) = content.downcast::<TextNode>() { + } else if let Some(text) = content.to::<TextNode>() { convert_script(&text.0, mode) - } else if let Some(seq) = content.downcast::<SequenceNode>() { + } else if let Some(seq) = content.to::<SequenceNode>() { let mut full = EcoString::new(); for item in seq.0.iter() { match search_text(item, mode) { |
