Ignore leading and trailing ignorant fragments in math.lr (#5473)

This commit is contained in:
Max 2024-11-26 20:51:22 +00:00 committed by GitHub
parent 22748aaf2e
commit 8fe8b2a239
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 5 deletions

View File

@ -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))
});

Binary file not shown.

After

Width:  |  Height:  |  Size: 970 B

View File

@ -98,6 +98,23 @@ $lr(lr(|, size: #4em), size: #50%)$
$lr(body1, size: #50%)$
$lr(body2, size: #50%)$
--- math-lr-ignore-ignorant ---
// Test ignoring leading and trailing ignorant fragments.
#box($ (1 / 2) $)
#box({
show "(": it => 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
$ab$ = $$$a$$$$$$b$$$