From 0fef85706451331b00d09dc85a77a812c779ce6c Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 12 Mar 2024 12:32:53 +0100 Subject: [PATCH] Fix `Behaviour` of spacing (#3634) --- crates/typst/src/introspection/metadata.rs | 2 ++ crates/typst/src/layout/spacing.rs | 4 ++-- crates/typst/src/realize/behaviour.rs | 6 ++++-- crates/typst/src/realize/mod.rs | 3 +++ tests/ref/bugs/spacing-behaviour.png | Bin 0 -> 638 bytes tests/typ/bugs/spacing-behaviour.typ | 9 +++++++++ 6 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 tests/ref/bugs/spacing-behaviour.png create mode 100644 tests/typ/bugs/spacing-behaviour.typ diff --git a/crates/typst/src/introspection/metadata.rs b/crates/typst/src/introspection/metadata.rs index 287bef678..2d6d8953f 100644 --- a/crates/typst/src/introspection/metadata.rs +++ b/crates/typst/src/introspection/metadata.rs @@ -37,6 +37,8 @@ impl Show for Packed { } } +// TODO: Metadata shows as empty content, so this behaviour is unnecessary. It +// can be removed once 0.11 has landed. impl Behave for Packed { fn behaviour(&self) -> Behaviour { Behaviour::Invisible diff --git a/crates/typst/src/layout/spacing.rs b/crates/typst/src/layout/spacing.rs index 7367c1aa1..f3cb765be 100644 --- a/crates/typst/src/layout/spacing.rs +++ b/crates/typst/src/layout/spacing.rs @@ -68,7 +68,7 @@ impl Behave for Packed { } else if self.weak(StyleChain::default()) { Behaviour::Weak(1) } else { - Behaviour::Invisible + Behaviour::Ignorant } } @@ -166,7 +166,7 @@ impl Behave for Packed { } else if self.weakness(StyleChain::default()) > 0 { Behaviour::Weak(self.weakness(StyleChain::default())) } else { - Behaviour::Invisible + Behaviour::Ignorant } } diff --git a/crates/typst/src/realize/behaviour.rs b/crates/typst/src/realize/behaviour.rs index 533185552..ae44a23e6 100644 --- a/crates/typst/src/realize/behaviour.rs +++ b/crates/typst/src/realize/behaviour.rs @@ -16,9 +16,11 @@ pub enum Behaviour { /// An element that destroys adjacent weak elements. Destructive, /// An element that does not interact at all with other elements, having the - /// same effect as if it didn't exist, but has a visual representation. + /// same effect as if it didn't exist, but has layout extent and/or a visual + /// representation. Ignorant, - /// An element that does not have a visual representation. + /// An element that does not have any layout extent or visual + /// representation. Invisible, } diff --git a/crates/typst/src/realize/mod.rs b/crates/typst/src/realize/mod.rs index 5409cc253..69a961b06 100644 --- a/crates/typst/src/realize/mod.rs +++ b/crates/typst/src/realize/mod.rs @@ -416,6 +416,9 @@ struct ParBuilder<'a>(BehavedBuilder<'a>); impl<'a> ParBuilder<'a> { fn accept(&mut self, content: &'a Content, styles: StyleChain<'a>) -> bool { if content.is::() { + // TODO: This could probably just be `self.0.is_empty()` since no + // weak or invisible elements can be at the start of the builder. It + // can be removed once 0.11 has landed. if self.0.has_strong_elements(false) { self.0.push(content, styles); return true; diff --git a/tests/ref/bugs/spacing-behaviour.png b/tests/ref/bugs/spacing-behaviour.png new file mode 100644 index 0000000000000000000000000000000000000000..08fcfa73946c5612c54ef68d2e74bf4d42fb1874 GIT binary patch literal 638 zcmeAS@N?(olHy`uVBq!ia0y~yU}OQZJvo2`!@o=H7Z@0rTs>VJLn>~)z3uODIZ@{L z$9e5Mi)7r6i)nW(klD6ialqLZ?xmfP?N(D<6}tZ|5d4zfzF@)uCdVU5OB`=1NR~<% zbF2>R4(u#`$;T$Swj}CWTDx!Y?<4PzpON2mxbM@C%J})Wx%b(8Xnk*VOd`3*@R%2` zw~mrPBZrFulVFDf3kow)P?051evZ@qxx2)@85dTT`}S^@{ju}@&f`1td5TMOr_>8v zYms-I9m>V1tbJTz^;oIW*+@t)0=xr@y7AqJ#0?(S0^_fzFj|C zuqZG5{%YCTc8dcxS*@0H+IjiJI^`7e?M%s=j`lo=oz12e9=>>jc8}T8;}7@Ql%FjQ zyEapB+Z&dKy+u)3`ND)D(moiRdC!{+uof<;n%&x*E?~z2Qy!0gHxz z{m#rMA4b@G<8;sx@k!V>=by!?TBhk**kTG8e*r3SdksNxx-sZC`ysnstx+diHiHtD Mr>mdKI;Vst0NIxfW&i*H literal 0 HcmV?d00001 diff --git a/tests/typ/bugs/spacing-behaviour.typ b/tests/typ/bugs/spacing-behaviour.typ new file mode 100644 index 000000000..a2a30b8ad --- /dev/null +++ b/tests/typ/bugs/spacing-behaviour.typ @@ -0,0 +1,9 @@ +// Test that metadata after spacing does not force a new paragraph. + +--- +#{ + h(1em) + counter(heading).update(4) + [Hello ] + counter(heading).display() +}