summaryrefslogtreecommitdiff
path: root/src/library/math
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-11-02 14:48:51 +0100
committerLaurenz <laurmaedje@gmail.com>2022-11-02 14:48:51 +0100
commit56342bd972a13ffe21beaf2b87ab7eb1597704b4 (patch)
tree78f9549141e753dde4a938670c54f3fe8695a058 /src/library/math
parent37ac5d966ebaf97ac79c507028cd5b742b510b89 (diff)
Move layout traits into library
Diffstat (limited to 'src/library/math')
-rw-r--r--src/library/math/mod.rs49
1 files changed, 21 insertions, 28 deletions
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<Vec<MathNode>>, 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<dyn World>, _: StyleChain) -> SourceResult<Content> {
- 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<Content> {
- 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<dyn World>,
_: &Regions,
styles: StyleChain,
) -> SourceResult<Vec<Frame>> {
- 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.