From 56342bd972a13ffe21beaf2b87ab7eb1597704b4 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 2 Nov 2022 14:48:51 +0100 Subject: Move layout traits into library --- src/library/math/mod.rs | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) (limited to 'src/library/math') diff --git a/src/library/math/mod.rs b/src/library/math/mod.rs index 84d4b6ee..5bb5054d 100644 --- a/src/library/math/mod.rs +++ b/src/library/math/mod.rs @@ -36,7 +36,7 @@ pub enum MathNode { Row(Arc>, Span), } -#[node(Show, Layout)] +#[node(Show, LayoutInline)] impl MathNode { /// The math font family. #[property(referenced)] @@ -67,6 +67,15 @@ impl MathNode { self } + + /// Whether the formula is display level. + pub fn display(&self) -> bool { + if let Self::Row(row, _) = self { + matches!(row.as_slice(), [MathNode::Space, .., MathNode::Space]) + } else { + false + } + } } impl Show for MathNode { @@ -79,11 +88,10 @@ impl Show for MathNode { } fn realize(&self, _: Tracked, _: StyleChain) -> SourceResult { - Ok(match self.level() { - Level::Inline => self.clone().pack(), - Level::Block => { - self.clone().pack().aligned(Axes::with_x(Some(Align::Center.into()))) - } + Ok(if self.display() { + self.clone().pack().aligned(Axes::with_x(Some(Align::Center.into()))) + } else { + self.clone().pack() }) } @@ -93,27 +101,22 @@ impl Show for MathNode { styles: StyleChain, realized: Content, ) -> SourceResult { - Ok(match self.level() { - Level::Inline => realized, - Level::Block => { - realized.spaced(styles.get(Self::ABOVE), styles.get(Self::BELOW)) - } + Ok(if self.display() { + realized.spaced(styles.get(Self::ABOVE), styles.get(Self::BELOW)) + } else { + realized }) } } -impl Layout for MathNode { - fn layout( +impl LayoutInline for MathNode { + fn layout_inline( &self, world: Tracked, _: &Regions, styles: StyleChain, ) -> SourceResult> { - let style = match self.level() { - Level::Inline => Style::Text, - Level::Block => Style::Display, - }; - + let style = if self.display() { Style::Display } else { Style::Text }; let span = match self { &Self::Row(_, span) => span, _ => Span::detached(), @@ -121,16 +124,6 @@ impl Layout for MathNode { Ok(vec![layout_tex(world, self, span, style, styles)?]) } - - fn level(&self) -> Level { - if let Self::Row(row, _) = self { - if matches!(row.as_slice(), [MathNode::Space, .., MathNode::Space]) { - return Level::Block; - } - } - - Level::Inline - } } /// Layout a TeX formula into a frame. -- cgit v1.2.3