diff --git a/src/library/hidden.rs b/src/library/hidden.rs new file mode 100644 index 00000000..7287bf3d --- /dev/null +++ b/src/library/hidden.rs @@ -0,0 +1,32 @@ +//! Hiding of nodes without affecting layout. + +use super::prelude::*; + +/// A node that hides its child without affecting layout. +#[derive(Debug, Hash)] +pub struct HideNode(pub PackedNode); + +#[class] +impl HideNode { + fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult { + Ok(Node::inline(Self(args.expect("body")?))) + } +} + +impl Layout for HideNode { + fn layout( + &self, + ctx: &mut LayoutContext, + regions: &Regions, + styles: StyleChain, + ) -> Vec>> { + let mut frames = self.0.layout(ctx, regions, styles); + + // Clear the frames. + for Constrained { item: frame, .. } in &mut frames { + *frame = Rc::new(Frame { elements: vec![], ..**frame }); + } + + frames + } +} diff --git a/src/library/mod.rs b/src/library/mod.rs index 51d57c3e..81c8fcdc 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -10,6 +10,7 @@ pub mod deco; pub mod flow; pub mod grid; pub mod heading; +pub mod hidden; pub mod image; pub mod link; pub mod list; @@ -33,6 +34,7 @@ pub use deco::*; pub use flow::*; pub use grid::*; pub use heading::*; +pub use hidden::*; pub use link::*; pub use list::*; pub use pad::*; @@ -116,6 +118,7 @@ pub fn new() -> Scope { std.def_class::>("move"); std.def_class::>("scale"); std.def_class::>("rotate"); + std.def_class::("hide"); std.def_class::("stack"); std.def_class::("grid"); std.def_class::("columns"); diff --git a/tests/ref/layout/hide.png b/tests/ref/layout/hide.png new file mode 100644 index 00000000..8a60cf90 Binary files /dev/null and b/tests/ref/layout/hide.png differ diff --git a/tests/typ/layout/hide.typ b/tests/typ/layout/hide.typ new file mode 100644 index 00000000..a979b24f --- /dev/null +++ b/tests/typ/layout/hide.typ @@ -0,0 +1,5 @@ +// Test the `hide` function. + +--- +AB #h(1fr) CD \ +#hide[A]B #h(1fr) C#hide[D]