summaryrefslogtreecommitdiff
path: root/library/src/math
diff options
context:
space:
mode:
authorPg Biel <9021226+PgBiel@users.noreply.github.com>2023-04-28 13:49:02 -0300
committerGitHub <noreply@github.com>2023-04-28 18:49:02 +0200
commit0d8c3254b75e6493ecf8efc46c37d049c1ae6b63 (patch)
treeff5ba1dcc5112230cbb59c13114c17a51965e0aa /library/src/math
parentd5e68c731c95b5fe366de718a4898e50e5e0525a (diff)
Initial fix for spacing on decorated math operators (#1023)
Diffstat (limited to 'library/src/math')
-rw-r--r--library/src/math/accent.rs8
-rw-r--r--library/src/math/cancel.rs7
-rw-r--r--library/src/math/row.rs14
-rw-r--r--library/src/math/underover.rs3
4 files changed, 28 insertions, 4 deletions
diff --git a/library/src/math/accent.rs b/library/src/math/accent.rs
index 1cd84429..2a650f2e 100644
--- a/library/src/math/accent.rs
+++ b/library/src/math/accent.rs
@@ -58,6 +58,8 @@ impl LayoutMath for AccentElem {
let base = ctx.layout_fragment(&self.base())?;
ctx.unstyle();
+ // Preserve class to preserve automatic spacing.
+ let base_class = base.class().unwrap_or(MathClass::Normal);
let base_attach = match &base {
MathFragment::Glyph(base) => {
attachment(ctx, base.id, base.italics_correction)
@@ -93,7 +95,11 @@ impl LayoutMath for AccentElem {
frame.set_baseline(baseline);
frame.push_frame(accent_pos, accent);
frame.push_frame(base_pos, base.into_frame());
- ctx.push(FrameFragment::new(ctx, frame).with_base_ascent(base_ascent));
+ ctx.push(
+ FrameFragment::new(ctx, frame)
+ .with_class(base_class)
+ .with_base_ascent(base_ascent),
+ );
Ok(())
}
diff --git a/library/src/math/cancel.rs b/library/src/math/cancel.rs
index edc2ba1e..770f3c65 100644
--- a/library/src/math/cancel.rs
+++ b/library/src/math/cancel.rs
@@ -90,7 +90,10 @@ pub struct CancelElem {
impl LayoutMath for CancelElem {
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
- let mut body = ctx.layout_frame(&self.body())?;
+ let body = ctx.layout_fragment(&self.body())?;
+ // Use the same math class as the body, in order to preserve automatic spacing around it.
+ let body_class = body.class().unwrap_or(MathClass::Special);
+ let mut body = body.into_frame();
let styles = ctx.styles();
let body_size = body.size();
@@ -130,7 +133,7 @@ impl LayoutMath for CancelElem {
body.push_frame(center, second_line);
}
- ctx.push(FrameFragment::new(ctx, body));
+ ctx.push(FrameFragment::new(ctx, body).with_class(body_class));
Ok(())
}
diff --git a/library/src/math/row.rs b/library/src/math/row.rs
index 23c9d242..a25178af 100644
--- a/library/src/math/row.rs
+++ b/library/src/math/row.rs
@@ -95,6 +95,20 @@ impl MathRow {
self.iter().map(MathFragment::descent).max().unwrap_or_default()
}
+ pub fn class(&self) -> MathClass {
+ // Predict the class of the output of 'into_fragment'
+ if self.0.len() == 1 {
+ self.0
+ .first()
+ .and_then(|fragment| fragment.class())
+ .unwrap_or(MathClass::Special)
+ } else {
+ // FrameFragment::new() (inside 'into_fragment' in this branch) defaults
+ // to MathClass::Normal for its class.
+ MathClass::Normal
+ }
+ }
+
pub fn into_frame(self, ctx: &MathContext) -> Frame {
let styles = ctx.styles();
let align = AlignElem::alignment_in(styles).x.resolve(styles);
diff --git a/library/src/math/underover.rs b/library/src/math/underover.rs
index 89ec5cff..3ad2538c 100644
--- a/library/src/math/underover.rs
+++ b/library/src/math/underover.rs
@@ -202,6 +202,7 @@ fn layout(
) -> SourceResult<()> {
let gap = gap.scaled(ctx);
let body = ctx.layout_row(body)?;
+ let body_class = body.class();
let glyph = GlyphFragment::new(ctx, c, span);
let stretched = glyph.stretch_horizontal(ctx, body.width(), Abs::zero());
@@ -226,7 +227,7 @@ fn layout(
}
let frame = stack(ctx, rows, Align::Center, gap, baseline);
- ctx.push(FrameFragment::new(ctx, frame));
+ ctx.push(FrameFragment::new(ctx, frame).with_class(body_class));
Ok(())
}