From aeb036f4dc2ede271d6c0710e91fae605ecdac84 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 29 Jan 2022 22:32:30 +0100 Subject: Fix that templates don't create a scope --- Cargo.lock | 14 +++++----- src/eval/mod.rs | 9 ++++--- src/library/mod.rs | 4 +-- src/library/place.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++ src/library/placed.rs | 67 ----------------------------------------------- tests/ref/style/wrap.png | Bin 10612 -> 24733 bytes tests/typ/code/block.typ | 7 +++++ tests/typ/style/wrap.typ | 6 +++++ 8 files changed, 95 insertions(+), 79 deletions(-) create mode 100644 src/library/place.rs delete mode 100644 src/library/placed.rs diff --git a/Cargo.lock b/Cargo.lock index 74035c53..ebda1f7d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -399,9 +399,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.113" +version = "0.2.116" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eef78b64d87775463c549fbd80e19249ef436ea3bf1de2a1eb7e717ec7fab1e9" +checksum = "565dbd88872dbe4cc8a46e527f26483c1d1f7afa6b884a3bd6cd893d4f98da74" [[package]] name = "line-wrap" @@ -537,7 +537,7 @@ checksum = "db8bcd96cb740d03149cbad5518db9fd87126a10ab519c011893b1754134c468" [[package]] name = "pixglyph" version = "0.1.0" -source = "git+https://github.com/typst/pixglyph#b63319b30eb34bcd7f3f3bb4c253a0731bae4234" +source = "git+https://github.com/typst/pixglyph#da648abb60d0e0f4353cd7602652c832132e6a39" dependencies = [ "ttf-parser", ] @@ -769,18 +769,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.135" +version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cf9235533494ea2ddcdb794665461814781c53f19d87b76e571a1c35acbad2b" +checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.135" +version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8dcde03d87d4c973c04be249e7d8f0b35db1c848c487bd43032808e59dd8328d" +checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" dependencies = [ "proc-macro2", "quote", diff --git a/src/eval/mod.rs b/src/eval/mod.rs index 05fa7eba..2fa07d49 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -187,7 +187,7 @@ fn process_nodes( ctx: &mut EvalContext, nodes: &mut impl Iterator, ) -> TypResult { - let prev = mem::take(&mut ctx.styles); + let prev_styles = mem::take(&mut ctx.styles); let mut seq = Vec::with_capacity(nodes.size_hint().1.unwrap_or_default()); while let Some(piece) = nodes.next() { // Need to deal with wrap here. @@ -201,7 +201,7 @@ fn process_nodes( seq.push(Styled::new(node, ctx.styles.clone())); } - ctx.styles = prev; + ctx.styles = prev_styles; Ok(Node::Sequence(seq)) } @@ -461,7 +461,10 @@ impl Eval for TemplateExpr { type Output = Node; fn eval(&self, ctx: &mut EvalContext) -> TypResult { - self.body().eval(ctx) + ctx.scopes.enter(); + let node = self.body().eval(ctx)?; + ctx.scopes.exit(); + Ok(node) } } diff --git a/src/library/mod.rs b/src/library/mod.rs index 81c8fcdc..44f8f947 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -17,7 +17,7 @@ pub mod list; pub mod pad; pub mod page; pub mod par; -pub mod placed; +pub mod place; pub mod shape; pub mod spacing; pub mod stack; @@ -40,7 +40,7 @@ pub use list::*; pub use pad::*; pub use page::*; pub use par::*; -pub use placed::*; +pub use place::*; pub use shape::*; pub use spacing::*; pub use stack::*; diff --git a/src/library/place.rs b/src/library/place.rs new file mode 100644 index 00000000..cee687fa --- /dev/null +++ b/src/library/place.rs @@ -0,0 +1,67 @@ +//! Absolute placement of nodes. + +use super::prelude::*; +use super::AlignNode; + +/// Place content at an absolute position. +#[derive(Debug, Hash)] +pub struct PlaceNode(pub PackedNode); + +#[class] +impl PlaceNode { + fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult { + let aligns = args.find().unwrap_or(Spec::with_x(Some(Align::Left))); + let tx = args.named("dx")?.unwrap_or_default(); + let ty = args.named("dy")?.unwrap_or_default(); + let body: PackedNode = args.expect("body")?; + Ok(Node::block(Self( + body.moved(Point::new(tx, ty)).aligned(aligns), + ))) + } +} + +impl Layout for PlaceNode { + fn layout( + &self, + ctx: &mut LayoutContext, + regions: &Regions, + styles: StyleChain, + ) -> Vec>> { + let out_of_flow = self.out_of_flow(); + + // The pod is the base area of the region because for absolute + // placement we don't really care about the already used area (current). + let pod = { + let finite = regions.base.map(Length::is_finite); + let expand = finite & (regions.expand | out_of_flow); + Regions::one(regions.base, regions.base, expand) + }; + + let mut frames = self.0.layout(ctx, &pod, styles); + let Constrained { item: frame, cts } = &mut frames[0]; + + // If expansion is off, zero all sizes so that we don't take up any + // space in our parent. Otherwise, respect the expand settings. + let target = regions.expand.select(regions.current, Size::zero()); + Rc::make_mut(frame).resize(target, Align::LEFT_TOP); + + // Set base constraint because our pod size is base and exact + // constraints if we needed to expand or offset. + *cts = Constraints::new(regions.expand); + cts.base = regions.base.map(Some); + cts.exact = regions.current.filter(regions.expand | out_of_flow); + + frames + } +} + +impl PlaceNode { + /// Whether this node wants to be placed relative to its its parent's base + /// origin. instead of relative to the parent's current flow/cursor + /// position. + pub fn out_of_flow(&self) -> bool { + self.0 + .downcast::() + .map_or(false, |node| node.aligns.y.is_some()) + } +} diff --git a/src/library/placed.rs b/src/library/placed.rs deleted file mode 100644 index cee687fa..00000000 --- a/src/library/placed.rs +++ /dev/null @@ -1,67 +0,0 @@ -//! Absolute placement of nodes. - -use super::prelude::*; -use super::AlignNode; - -/// Place content at an absolute position. -#[derive(Debug, Hash)] -pub struct PlaceNode(pub PackedNode); - -#[class] -impl PlaceNode { - fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult { - let aligns = args.find().unwrap_or(Spec::with_x(Some(Align::Left))); - let tx = args.named("dx")?.unwrap_or_default(); - let ty = args.named("dy")?.unwrap_or_default(); - let body: PackedNode = args.expect("body")?; - Ok(Node::block(Self( - body.moved(Point::new(tx, ty)).aligned(aligns), - ))) - } -} - -impl Layout for PlaceNode { - fn layout( - &self, - ctx: &mut LayoutContext, - regions: &Regions, - styles: StyleChain, - ) -> Vec>> { - let out_of_flow = self.out_of_flow(); - - // The pod is the base area of the region because for absolute - // placement we don't really care about the already used area (current). - let pod = { - let finite = regions.base.map(Length::is_finite); - let expand = finite & (regions.expand | out_of_flow); - Regions::one(regions.base, regions.base, expand) - }; - - let mut frames = self.0.layout(ctx, &pod, styles); - let Constrained { item: frame, cts } = &mut frames[0]; - - // If expansion is off, zero all sizes so that we don't take up any - // space in our parent. Otherwise, respect the expand settings. - let target = regions.expand.select(regions.current, Size::zero()); - Rc::make_mut(frame).resize(target, Align::LEFT_TOP); - - // Set base constraint because our pod size is base and exact - // constraints if we needed to expand or offset. - *cts = Constraints::new(regions.expand); - cts.base = regions.base.map(Some); - cts.exact = regions.current.filter(regions.expand | out_of_flow); - - frames - } -} - -impl PlaceNode { - /// Whether this node wants to be placed relative to its its parent's base - /// origin. instead of relative to the parent's current flow/cursor - /// position. - pub fn out_of_flow(&self) -> bool { - self.0 - .downcast::() - .map_or(false, |node| node.aligns.y.is_some()) - } -} diff --git a/tests/ref/style/wrap.png b/tests/ref/style/wrap.png index 346da53f..82d1c34d 100644 Binary files a/tests/ref/style/wrap.png and b/tests/ref/style/wrap.png differ diff --git a/tests/typ/code/block.typ b/tests/typ/code/block.typ index 5939ba9c..9122ea48 100644 --- a/tests/typ/code/block.typ +++ b/tests/typ/code/block.typ @@ -102,6 +102,13 @@ test(a, "a1") } +--- +// Template also creates a scope. +[#let x = 1] + +// Error: 2-3 unknown variable +{x} + --- // Multiple unseparated expressions in one line. diff --git a/tests/typ/style/wrap.typ b/tests/typ/style/wrap.typ index 5e0fdd0d..1ff3cc95 100644 --- a/tests/typ/style/wrap.typ +++ b/tests/typ/style/wrap.typ @@ -18,6 +18,12 @@ in booklovers and the great fulfiller of human need. // Test wrap in template. A [_B #wrap c in [*#c*]; C] D +--- +// Test wrap style precedence. +#set text(fill: eastern, size: 150%) +#wrap body in text(fill: forest, body) +Forest + --- // Error: 6-17 wrap is only allowed directly in markup {1 + wrap x in y} -- cgit v1.2.3