Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: number-align should align line/number baselines #4024

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 65 additions & 39 deletions crates/typst/src/math/equation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,42 +389,30 @@ fn add_equation_number(
region_size_x: Abs,
full_number_width: Abs,
) -> Frame {
let first = equation_builder
.frames
.first()
.map_or((equation_builder.size, Point::zero()), |(frame, point)| {
(frame.size(), *point)
});
let last = equation_builder
.frames
.last()
.map_or((equation_builder.size, Point::zero()), |(frame, point)| {
(frame.size(), *point)
});
let first = equation_builder.frames.first().map_or(
(equation_builder.size, Point::zero(), Abs::zero()),
|(baseline, frame, point)| (frame.size(), *point, *baseline),
);
let last = equation_builder.frames.last().map_or(
(equation_builder.size, Point::zero(), Abs::zero()),
|(baseline, frame, point)| (frame.size(), *point, *baseline),
);
let line_count = equation_builder.frames.len();
let mut equation = equation_builder.build();

let width = if region_size_x.is_finite() {
region_size_x
} else {
equation.width() + 2.0 * full_number_width
};
let height = match number_align.y {
FixedAlignment::Start => {
let (size, point) = first;
let excess_above = (number.height() - size.y) / 2.0 - point.y;
equation.height() + Abs::zero().max(excess_above)
}
FixedAlignment::Center => equation.height().max(number.height()),
FixedAlignment::End => {
let (size, point) = last;
let excess_below =
(number.height() + size.y) / 2.0 - equation.height() + point.y;
equation.height() + Abs::zero().max(excess_below)
}
};
let resizing_offset = equation.resize(
Size::new(width, height),
Axes::<FixedAlignment>::new(equation_align, number_align.y.inv()),
let resizing_offset = resize_equation(
&mut equation,
&number,
number_align,
equation_align,
width,
line_count,
[first, last],
);
equation.translate(Point::with_x(match (equation_align, number_align.x) {
(FixedAlignment::Start, FixedAlignment::Start) => full_number_width,
Expand All @@ -437,19 +425,57 @@ fn add_equation_number(
FixedAlignment::End => equation.width() - number.width(),
_ => unreachable!(),
};
let dh = |h1: Abs, h2: Abs| (h1 - h2) / 2.0;
let align_baseline = |(_, point, baseline): (_, Point, Abs), number: &Frame| {
resizing_offset.y + point.y + baseline - number.baseline()
};
let y = match number_align.y {
FixedAlignment::Start => {
let (size, point) = first;
resizing_offset.y + point.y + dh(size.y, number.height())
}
FixedAlignment::Center => dh(equation.height(), number.height()),
FixedAlignment::End => {
let (size, point) = last;
resizing_offset.y + point.y + dh(size.y, number.height())
}
FixedAlignment::Start => align_baseline(first, &number),
FixedAlignment::Center if line_count < 2 => align_baseline(first, &number),
FixedAlignment::Center => (equation.height() - number.height()) / 2.0,
FixedAlignment::End => align_baseline(last, &number),
};

equation.push_frame(Point::new(x, y), number);
equation
}

fn resize_equation(
equation: &mut Frame,
number: &Frame,
number_align: Axes<FixedAlignment>,
equation_align: FixedAlignment,
width: Abs,
line_count: usize,
[first, last]: [(Axes<Abs>, Point, Abs); 2],
) -> Point {
let excess_above = Abs::zero().max({
let (.., baseline) = first;
number.baseline() - baseline
});
let excess_below = Abs::zero().max({
let (size, .., baseline) = last;
(number.height() - number.baseline()) - (size.y - baseline)
});
let height = match number_align.y {
FixedAlignment::Start => equation.height() + excess_above,
FixedAlignment::Center if line_count < 2 => {
equation.height() + excess_above + excess_below
}
FixedAlignment::Center => equation.height().max(number.height()),
FixedAlignment::End => equation.height() + excess_below,
};

if matches!(number_align.y, FixedAlignment::Center if line_count < 2) {
let resizing_offset = equation.resize(
Size::new(width, height),
Axes::<FixedAlignment>::new(equation_align, FixedAlignment::Start),
);
equation.translate(Point::with_y(excess_above));
resizing_offset + Point::with_y(excess_above)
} else {
equation.resize(
Size::new(width, height),
Axes::<FixedAlignment>::new(equation_align, number_align.y.inv()),
)
}
}
12 changes: 6 additions & 6 deletions crates/typst/src/math/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl MathRun {
};

let align = AlignElem::alignment_in(styles).resolve(styles).x;
let mut frames: Vec<(Frame, Point)> = vec![];
let mut frames: Vec<(Abs, Frame, Point)> = vec![];
let mut size = Size::zero();
for (i, row) in rows.into_iter().enumerate() {
if i == row_count - 1 && row.0.is_empty() {
Expand All @@ -192,7 +192,7 @@ impl MathRun {
}
size.x.set_max(sub.width());
size.y += sub.height();
frames.push((sub, pos));
frames.push((sub.baseline(), sub, pos));
}

MathRunFrameBuilder { size, frames }
Expand Down Expand Up @@ -375,16 +375,16 @@ impl Iterator for LeftRightAlternator {
pub struct MathRunFrameBuilder {
/// The size of the resulting frame.
pub size: Size,
/// Sub frames for each row, and the positions where they should be pushed into
/// the resulting frame.
pub frames: Vec<(Frame, Point)>,
/// Each row: the baseline, the frame, and the positions where they should
/// be pushed into the resulting frame.
pub frames: Vec<(Abs, Frame, Point)>,
Leedehai marked this conversation as resolved.
Show resolved Hide resolved
}

impl MathRunFrameBuilder {
/// Consumes the builder and returns a [`Frame`].
pub fn build(self) -> Frame {
let mut frame = Frame::soft(self.size);
for (sub, pos) in self.frames.into_iter() {
for (_, sub, pos) in self.frames.into_iter() {
frame.push_frame(pos, sub);
}
frame
Expand Down
Binary file modified tests/ref/math-equation-align-numbered.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/ref/math-equation-number-align-end.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/ref/math-equation-number-align-left.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/ref/math-equation-number-align-monoline.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/ref/math-equation-number-align-multiline-bottom.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/ref/math-equation-number-align-multiline-expand.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/ref/math-equation-number-align-multiline-top-start.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/ref/math-equation-number-align-right.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/ref/math-equation-number-align-start.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/ref/math-equation-number-align.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/ref/math-equation-numbering.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/ref/outline.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/ref/ref-supplements.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 16 additions & 2 deletions tests/suite/math/equation.typ
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ $ a + b = c $
// Error: 52-67 expected `start`, `left`, `right`, or `end`, found center
#set math.equation(numbering: "(1)", number-align: center + bottom)

--- math-equation-number-align-monoline ---
#set math.equation(numbering: "(1)")
$ p = sum_k k ln a $

#set math.equation(numbering: "(1)", number-align: top)
$ p = sum_k k ln a $

#set math.equation(numbering: "(1)", number-align: bottom)
$ p = sum_k k ln a $

--- math-equation-number-align-multiline ---
#set math.equation(numbering: "(1)")

Expand All @@ -163,13 +173,17 @@ $ p &= ln a b \

$ p &= ln a b \
&= ln a + ln b $
$ q &= sum_k k ln a \
&= sum_k ln A $

--- math-equation-number-align-multiline-bottom ---
#show math.equation: set align(left)
#set math.equation(numbering: "(1)", number-align: bottom)

$ q &= ln sqrt(a b) \
&= 1/2 (ln a + ln b) $
$ p &= ln a b \
&= ln a + ln b $
$ q &= sum_k ln A \
&= sum_k k ln a $

--- math-equation-number-align-multiline-expand ---
// Tests that if the numbering's layout box vertically exceeds the box of
Expand Down