summaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorEric Biedert <github@ericbiedert.de>2023-09-26 11:42:05 +0200
committerGitHub <noreply@github.com>2023-09-26 11:42:05 +0200
commitc8ebcd70d6b2b9b3ad5142411305d766cc2d0c2e (patch)
treef3167f3d685606f3eea5bfe3ebb2ba8f986abc2f /crates
parentc55901e972f6671ecfe05358a96f85f34c703cc0 (diff)
Resolve spacing before comparing (#2235)
Diffstat (limited to 'crates')
-rw-r--r--crates/typst-library/src/layout/spacing.rs44
-rw-r--r--crates/typst-library/src/shared/behave.rs2
-rw-r--r--crates/typst/src/model/realize.rs6
3 files changed, 32 insertions, 20 deletions
diff --git a/crates/typst-library/src/layout/spacing.rs b/crates/typst-library/src/layout/spacing.rs
index 50f66a8e..cf69f6d9 100644
--- a/crates/typst-library/src/layout/spacing.rs
+++ b/crates/typst-library/src/layout/spacing.rs
@@ -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 {
diff --git a/crates/typst-library/src/shared/behave.rs b/crates/typst-library/src/shared/behave.rs
index ed7a2593..f97e3fbc 100644
--- a/crates/typst-library/src/shared/behave.rs
+++ b/crates/typst-library/src/shared/behave.rs
@@ -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);
diff --git a/crates/typst/src/model/realize.rs b/crates/typst/src/model/realize.rs
index 745d7f43..8edeb0e7 100644
--- a/crates/typst/src/model/realize.rs
+++ b/crates/typst/src/model/realize.rs
@@ -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
}
}