From 8fe8b2a23940f76077aa36eda305febd22e7cadc Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 26 Nov 2024 20:51:22 +0000 Subject: [PATCH] Ignore leading and trailing ignorant fragments in `math.lr` (#5473) --- crates/typst-layout/src/math/lr.rs | 22 +++++++++++++++++----- tests/ref/math-lr-ignore-ignorant.png | Bin 0 -> 970 bytes tests/suite/math/delimited.typ | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 tests/ref/math-lr-ignore-ignorant.png diff --git a/crates/typst-layout/src/math/lr.rs b/crates/typst-layout/src/math/lr.rs index aba9012f2..d195e67dc 100644 --- a/crates/typst-layout/src/math/lr.rs +++ b/crates/typst-layout/src/math/lr.rs @@ -28,8 +28,21 @@ pub fn layout_lr( } let mut fragments = ctx.layout_into_fragments(body, styles)?; + + // Ignore leading and trailing ignorant fragments. + let start_idx = fragments + .iter() + .position(|f| !f.is_ignorant()) + .unwrap_or(fragments.len()); + let end_idx = fragments + .iter() + .skip(start_idx) + .rposition(|f| !f.is_ignorant()) + .map_or(start_idx, |i| start_idx + i + 1); + let inner_fragments = &mut fragments[start_idx..end_idx]; + let axis = scaled!(ctx, styles, axis_height); - let max_extent = fragments + let max_extent = inner_fragments .iter() .map(|fragment| (fragment.ascent() - axis).max(fragment.descent() + axis)) .max() @@ -39,7 +52,7 @@ pub fn layout_lr( let height = elem.size(styles); // Scale up fragments at both ends. - match fragments.as_mut_slice() { + match inner_fragments { [one] => scale(ctx, styles, one, relative_to, height, None), [first, .., last] => { scale(ctx, styles, first, relative_to, height, Some(MathClass::Opening)); @@ -49,7 +62,7 @@ pub fn layout_lr( } // Handle MathFragment::Variant fragments that should be scaled up. - for fragment in &mut fragments { + for fragment in inner_fragments { if let MathFragment::Variant(ref mut variant) = fragment { if variant.mid_stretched == Some(false) { variant.mid_stretched = Some(true); @@ -60,11 +73,10 @@ pub fn layout_lr( // Remove weak SpacingFragment immediately after the opening or immediately // before the closing. - let original_len = fragments.len(); let mut index = 0; fragments.retain(|fragment| { index += 1; - (index != 2 && index + 1 != original_len) + (index != start_idx + 2 && index + 1 != end_idx) || !matches!(fragment, MathFragment::Spacing(_, true)) }); diff --git a/tests/ref/math-lr-ignore-ignorant.png b/tests/ref/math-lr-ignore-ignorant.png new file mode 100644 index 0000000000000000000000000000000000000000..b82a827f59714684078d46e431f454e6a74a7830 GIT binary patch literal 970 zcmV;*12z1KP)1%+s$Yym5TNCB;E zf`A}fDIiT`r;)a_v^~x_cV;?Qc(^edUpf!xyYtN?AGzEiOo9J&O$mep;XpVL{#)S+ zAJoU61AuZ)n*fTAXXA6N^;806tnUS|e%I>W4j4>Z4*)f8;c#^-z8`|C=NkP2e~dfn z(nzn==&SwG2VAvS}N*K~=R9%+>N*K=DRsEz1l(74(Y9Z1A zau_=A2!`Vu{b7$@)q1x-+;T&)V@E&@LyAe!4HRQx2(t-=T|2=ahS+LZ*IfaB71x2$;S#c@gE$*dTX8kZ$2KtyXBlezw1=0- zr^$k69IX*=b0USq169&xnGm+19+(hrK<)fJ>_%J1!hf*UAs-2qdneuM+fO;$zk+c? zJEt=*f%xGv8pnx2Wu+L+RCrekvHVczHYgLIoC5{~#pc?(P(#llKiuXanr zJ`KLjrN3*Zn6 context it + $ (1 / 2) $ +}) +#box({ + show ")": it => context it + $ (1 / 2) $ +}) +#box({ + show "(": it => context it + show ")": it => context it + $ (1 / 2) $ +}) + --- issue-4188-lr-corner-brackets --- // Test positioning of U+231C to U+231F $⌜a⌟⌞b⌝$ = $⌜$$a$$⌟$$⌞$$b$$⌝$