Resolve spacing before comparing (#2235)

This commit is contained in:
Eric Biedert 2023-09-26 11:42:05 +02:00 committed by GitHub
parent c55901e972
commit c8ebcd70d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 42 additions and 20 deletions

View File

@ -1,5 +1,3 @@
use std::cmp::Ordering;
use crate::prelude::*;
/// Inserts horizontal spacing into a paragraph.
@ -64,9 +62,19 @@ impl Behave for HElem {
}
}
fn larger(&self, prev: &Content) -> bool {
let Some(prev) = prev.to::<Self>() else { return false };
self.amount() > prev.amount()
fn larger(
&self,
prev: &(Content, Behaviour, StyleChain),
styles: StyleChain,
) -> bool {
let Some(other) = prev.0.to::<Self>() else { return false };
match (self.amount(), other.amount()) {
(Spacing::Fr(this), Spacing::Fr(other)) => this > other,
(Spacing::Rel(this), Spacing::Rel(other)) => {
this.resolve(styles) > other.resolve(prev.2)
}
_ => false,
}
}
}
@ -156,9 +164,19 @@ impl Behave for VElem {
}
}
fn larger(&self, prev: &Content) -> bool {
let Some(prev) = prev.to::<Self>() else { return false };
self.amount() > prev.amount()
fn larger(
&self,
prev: &(Content, Behaviour, StyleChain),
styles: StyleChain,
) -> bool {
let Some(other) = prev.0.to::<Self>() else { return false };
match (self.amount(), other.amount()) {
(Spacing::Fr(this), Spacing::Fr(other)) => this > other,
(Spacing::Rel(this), Spacing::Rel(other)) => {
this.resolve(styles) > other.resolve(prev.2)
}
_ => false,
}
}
}
@ -216,16 +234,6 @@ impl From<Fr> for Spacing {
}
}
impl PartialOrd for Spacing {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (self, other) {
(Self::Rel(a), Self::Rel(b)) => a.partial_cmp(b),
(Self::Fr(a), Self::Fr(b)) => a.partial_cmp(b),
_ => None,
}
}
}
cast! {
Spacing,
self => match self {

View File

@ -53,7 +53,7 @@ impl<'a> BehavedBuilder<'a> {
let i = self.staged.iter().position(|prev| {
let Behaviour::Weak(prev_level) = prev.1 else { return false };
level < prev_level
|| (level == prev_level && item.larger(&prev.0))
|| (level == prev_level && item.larger(prev, styles))
});
let Some(i) = i else { return };
self.staged.remove(i);

View File

@ -198,7 +198,11 @@ pub trait Behave {
/// Whether this weak element is larger than a previous one and thus picked
/// as the maximum when the levels are the same.
#[allow(unused_variables)]
fn larger(&self, prev: &Content) -> bool {
fn larger(
&self,
prev: &(Content, Behaviour, StyleChain),
styles: StyleChain,
) -> bool {
false
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 77 KiB

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

@ -24,6 +24,16 @@ A #h(0pt) B #h(0pt) \
A B \
A #h(-1fr) B
---
// Test spacing collapsing with different font sizes.
#grid(columns: 2)[
#text(size: 12pt, block(below: 1em)[A])
#text(size: 8pt, block(above: 1em)[B])
][
#text(size: 12pt, block(below: 1em)[A])
#text(size: 8pt, block(above: 1.25em)[B])
]
---
// Test RTL spacing.
#set text(dir: rtl)