From 53c306e32cd9622f7f2af66e08b6846712e78633 Mon Sep 17 00:00:00 2001 From: Leedehai <18319900+Leedehai@users.noreply.github.com> Date: Wed, 22 May 2024 13:04:46 -0400 Subject: [PATCH] Fix equation resizing when adding the equation number (#4179) --- crates/typst/src/math/equation.rs | 24 ++++--- ...equation-number-align-multiline-expand.png | Bin 518 -> 1250 bytes ...ation-number-align-multiline-no-expand.png | Bin 0 -> 1050 bytes tests/suite/math/equation.typ | 67 +++++++++++++++--- 4 files changed, 72 insertions(+), 19 deletions(-) create mode 100644 tests/ref/math-equation-number-align-multiline-no-expand.png diff --git a/crates/typst/src/math/equation.rs b/crates/typst/src/math/equation.rs index 947f01f2..d6a8d4c7 100644 --- a/crates/typst/src/math/equation.rs +++ b/crates/typst/src/math/equation.rs @@ -466,25 +466,33 @@ fn resize_equation( if matches!(number_align.y, FixedAlignment::Center if is_multiline) { // In this case, the center lines (not baselines) of the number frame // and the equation frame shall be aligned. - let height = equation.height().max(number.height()); return equation.resize( - Size::new(width, height), + Size::new(width, equation.height().max(number.height())), Axes::::new(equation_align, FixedAlignment::Center), ); } let excess_above = Abs::zero().max({ - let (.., baseline) = first; - number.baseline() - baseline + if !is_multiline || matches!(number_align.y, FixedAlignment::Start) { + let (.., baseline) = first; + number.baseline() - baseline + } else { + Abs::zero() + } }); let excess_below = Abs::zero().max({ - let (size, .., baseline) = last; - (number.height() - number.baseline()) - (size.y - baseline) + if !is_multiline || matches!(number_align.y, FixedAlignment::End) { + let (size, .., baseline) = last; + (number.height() - number.baseline()) - (size.y - baseline) + } else { + Abs::zero() + } }); - let height = equation.height() + excess_above + excess_below; + // The vertical expansion is asymmetric on the top and bottom edges, so we + // first align at the top then translate the content downward later. let resizing_offset = equation.resize( - Size::new(width, height), + Size::new(width, equation.height() + excess_above + excess_below), Axes::::new(equation_align, FixedAlignment::Start), ); equation.translate(Point::with_y(excess_above)); diff --git a/tests/ref/math-equation-number-align-multiline-expand.png b/tests/ref/math-equation-number-align-multiline-expand.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2$ zi4Q{Eoz}?2ppn$#V&}HKKIZGOfW6ngIfe`%}F@ zN68l#ykx+ST;atonS=DlH)N~WbClQRaWNT)tEc(GYP0a-3+vg`aJzJFWWjzeu$Kj* z_nN3H@0%@EF?sdWm3ncCzX*gnf#{M_jDQmJy&@+HGLq!-_JxZZ9e|2|C&R;b{IJ*F zb{!ThuCUZkqvHG7)y*h?NqFj5xcIZvrbPQIv*3yuUmBMRq7tLK@e0VT=x*d_K$nlN zlhyzWpEwJ~_W(vdF({_`ekY1=?A& Uhk`aiQ~&?~07*qoM6N<$f&?b^4*&oF literal 0 HcmV?d00001 diff --git a/tests/suite/math/equation.typ b/tests/suite/math/equation.typ index d4fba36b..e7ae677e 100644 --- a/tests/suite/math/equation.typ +++ b/tests/suite/math/equation.typ @@ -194,22 +194,67 @@ $ q &= sum_k ln A \ // numbering's layout box. Note we use pattern "1" here, not "(1)", since // the parenthesis exceeds the numbering's layout box, due to the default // settings of top-edge and bottom-edge of the TextElem that laid it out. +#let equations = [ + #box($ - - - $, fill: silver) + #box( + $ - - - \ + a = b $, + fill: silver) + #box( + $ a = b \ + - - - $, + fill: silver) +] + #set math.equation(numbering: "1", number-align: top) -#box( -$ - &- - \ - a &= b $, -fill: silver) +#equations #set math.equation(numbering: "1", number-align: horizon) -#box( -$ - - - $, -fill: silver) +#equations #set math.equation(numbering: "1", number-align: bottom) -#box( -$ a &= b \ - - &- - $, -fill: silver) +#equations + +--- math-equation-number-align-multiline-no-expand --- +// Tests that if the numbering's layout box doesn't vertically exceed the +// box of the equation frame's boundary, the latter's frame size remains. +// So, in the grid below, frames in each row should have the same height. +#set math.equation(numbering: "1") +#grid( + columns: 4 * (1fr,), + column-gutter: 3 * (2pt,), + row-gutter: 2pt, + align: horizon, + [ + #set math.equation(number-align: horizon) + #box($ - - \ a \ sum $, fill: silver) + ], + [ + #set math.equation(number-align: bottom) + #box($ - - \ a \ sum $, fill: silver) + ], + [ + #set math.equation(number-align: horizon) + #box($ sum \ a \ - - $, fill: silver) + ], + [ + #set math.equation(number-align: top) + #box($ sum \ a \ - - $, fill: silver) + ], + + [ + #set math.equation(number-align: horizon) + #box($ - - $, fill: silver) + ], + [ + #set math.equation(number-align: top) + #box($ - - $, fill: silver) + ], + [ + #set math.equation(number-align: bottom) + #box($ - - $, fill: silver) + ], +) --- issue-numbering-hint --- // In this bug, the hint and error messages for an equation