mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Fix "set align" for block equations (#2157)
This commit is contained in:
parent
e55348dbc0
commit
b10f9ae7b7
@ -43,7 +43,7 @@ use self::ctx::*;
|
|||||||
use self::fragment::*;
|
use self::fragment::*;
|
||||||
use self::row::*;
|
use self::row::*;
|
||||||
use self::spacing::*;
|
use self::spacing::*;
|
||||||
use crate::layout::{BoxElem, HElem, ParElem, Spacing};
|
use crate::layout::{AlignElem, BoxElem, HElem, ParElem, Spacing};
|
||||||
use crate::meta::Supplement;
|
use crate::meta::Supplement;
|
||||||
use crate::meta::{
|
use crate::meta::{
|
||||||
Count, Counter, CounterUpdate, LocalName, Numbering, Outlinable, Refable,
|
Count, Counter, CounterUpdate, LocalName, Numbering, Outlinable, Refable,
|
||||||
@ -200,14 +200,18 @@ impl Show for EquationElem {
|
|||||||
fn show(&self, _: &mut Vt, styles: StyleChain) -> SourceResult<Content> {
|
fn show(&self, _: &mut Vt, styles: StyleChain) -> SourceResult<Content> {
|
||||||
let mut realized = self.clone().pack().guarded(Guard::Base(Self::elem()));
|
let mut realized = self.clone().pack().guarded(Guard::Base(Self::elem()));
|
||||||
if self.block(styles) {
|
if self.block(styles) {
|
||||||
realized = realized.aligned(Align::CENTER);
|
realized = AlignElem::new(realized).pack();
|
||||||
}
|
}
|
||||||
Ok(realized)
|
Ok(realized)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Finalize for EquationElem {
|
impl Finalize for EquationElem {
|
||||||
fn finalize(&self, realized: Content, _: StyleChain) -> Content {
|
fn finalize(&self, realized: Content, style: StyleChain) -> Content {
|
||||||
|
let mut realized = realized;
|
||||||
|
if self.block(style) {
|
||||||
|
realized = realized.styled(AlignElem::set_alignment(Align::CENTER));
|
||||||
|
}
|
||||||
realized
|
realized
|
||||||
.styled(TextElem::set_weight(FontWeight::from_number(450)))
|
.styled(TextElem::set_weight(FontWeight::from_number(450)))
|
||||||
.styled(TextElem::set_font(FontList(vec![FontFamily::new(
|
.styled(TextElem::set_font(FontList(vec![FontFamily::new(
|
||||||
@ -251,17 +255,26 @@ impl Layout for EquationElem {
|
|||||||
.layout(vt, styles, pod)?
|
.layout(vt, styles, pod)?
|
||||||
.into_frame();
|
.into_frame();
|
||||||
|
|
||||||
|
let full_counter_width = counter.width() + NUMBER_GUTTER.resolve(styles);
|
||||||
let width = if regions.size.x.is_finite() {
|
let width = if regions.size.x.is_finite() {
|
||||||
regions.size.x
|
regions.size.x
|
||||||
} else {
|
} else {
|
||||||
frame.width()
|
frame.width() + 2.0 * full_counter_width
|
||||||
+ 2.0 * (counter.width() + NUMBER_GUTTER.resolve(styles))
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let height = frame.height().max(counter.height());
|
let height = frame.height().max(counter.height());
|
||||||
frame.resize(Size::new(width, height), Axes::splat(FixedAlign::Center));
|
let align = AlignElem::alignment_in(styles).resolve(styles).x;
|
||||||
|
frame.resize(Size::new(width, height), Axes::splat(align));
|
||||||
|
|
||||||
let x = if TextElem::dir_in(styles).is_positive() {
|
let dir = TextElem::dir_in(styles);
|
||||||
|
let offset = match (align, dir) {
|
||||||
|
(FixedAlign::Start, Dir::RTL) => full_counter_width,
|
||||||
|
(FixedAlign::End, Dir::LTR) => -full_counter_width,
|
||||||
|
_ => Abs::zero(),
|
||||||
|
};
|
||||||
|
frame.translate(Point::with_x(offset));
|
||||||
|
|
||||||
|
let x = if dir.is_positive() {
|
||||||
frame.width() - counter.width()
|
frame.width() - counter.width()
|
||||||
} else {
|
} else {
|
||||||
Abs::zero()
|
Abs::zero()
|
||||||
|
BIN
tests/ref/math/block-alignment.png
Normal file
BIN
tests/ref/math/block-alignment.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.0 KiB |
33
tests/typ/math/block-alignment.typ
Normal file
33
tests/typ/math/block-alignment.typ
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
// Test alignment of block equations.
|
||||||
|
|
||||||
|
---
|
||||||
|
// Test unnumbered
|
||||||
|
#let eq(alignment) = {
|
||||||
|
show math.equation: set align(alignment)
|
||||||
|
$ a + b = c $
|
||||||
|
}
|
||||||
|
|
||||||
|
#eq(center)
|
||||||
|
#eq(left)
|
||||||
|
#eq(right)
|
||||||
|
|
||||||
|
#set text(dir: rtl)
|
||||||
|
#eq(start)
|
||||||
|
#eq(end)
|
||||||
|
|
||||||
|
---
|
||||||
|
// Test numbered
|
||||||
|
#let eq(alignment) = {
|
||||||
|
show math.equation: set align(alignment)
|
||||||
|
$ a + b = c $
|
||||||
|
}
|
||||||
|
|
||||||
|
#set math.equation(numbering: "(1)")
|
||||||
|
|
||||||
|
#eq(center)
|
||||||
|
#eq(left)
|
||||||
|
#eq(right)
|
||||||
|
|
||||||
|
#set text(dir: rtl)
|
||||||
|
#eq(start)
|
||||||
|
#eq(end)
|
Loading…
x
Reference in New Issue
Block a user