summaryrefslogtreecommitdiff
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
parentc55901e972f6671ecfe05358a96f85f34c703cc0 (diff)
Resolve spacing before comparing (#2235)
-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
-rw-r--r--tests/ref/compiler/array.pngbin8385 -> 8306 bytes
-rw-r--r--tests/ref/compiler/repr.pngbin51799 -> 44479 bytes
-rw-r--r--tests/ref/compiler/show-selector.pngbin17066 -> 16871 bytes
-rw-r--r--tests/ref/layout/spacing.pngbin3348 -> 4158 bytes
-rw-r--r--tests/ref/meta/outline-entry.pngbin78782 -> 62248 bytes
-rw-r--r--tests/ref/text/raw-align.pngbin32879 -> 32817 bytes
-rw-r--r--tests/ref/text/raw.pngbin21607 -> 21650 bytes
-rw-r--r--tests/typ/layout/spacing.typ10
11 files changed, 42 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
}
}
diff --git a/tests/ref/compiler/array.png b/tests/ref/compiler/array.png
index a96dfe64..9b6bf8b3 100644
--- a/tests/ref/compiler/array.png
+++ b/tests/ref/compiler/array.png
Binary files differ
diff --git a/tests/ref/compiler/repr.png b/tests/ref/compiler/repr.png
index 82ece777..aa519dd0 100644
--- a/tests/ref/compiler/repr.png
+++ b/tests/ref/compiler/repr.png
Binary files differ
diff --git a/tests/ref/compiler/show-selector.png b/tests/ref/compiler/show-selector.png
index f8259698..52e99c9a 100644
--- a/tests/ref/compiler/show-selector.png
+++ b/tests/ref/compiler/show-selector.png
Binary files differ
diff --git a/tests/ref/layout/spacing.png b/tests/ref/layout/spacing.png
index c733fb02..9bab536a 100644
--- a/tests/ref/layout/spacing.png
+++ b/tests/ref/layout/spacing.png
Binary files differ
diff --git a/tests/ref/meta/outline-entry.png b/tests/ref/meta/outline-entry.png
index 3a277397..f8f5412f 100644
--- a/tests/ref/meta/outline-entry.png
+++ b/tests/ref/meta/outline-entry.png
Binary files differ
diff --git a/tests/ref/text/raw-align.png b/tests/ref/text/raw-align.png
index 734008e1..6d1044f7 100644
--- a/tests/ref/text/raw-align.png
+++ b/tests/ref/text/raw-align.png
Binary files differ
diff --git a/tests/ref/text/raw.png b/tests/ref/text/raw.png
index 09912afc..27120d74 100644
--- a/tests/ref/text/raw.png
+++ b/tests/ref/text/raw.png
Binary files differ
diff --git a/tests/typ/layout/spacing.typ b/tests/typ/layout/spacing.typ
index fbc5b0d8..f4e59590 100644
--- a/tests/typ/layout/spacing.typ
+++ b/tests/typ/layout/spacing.typ
@@ -25,6 +25,16 @@ 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)
A #h(10pt) B \