From 8013a0d0e6c282f3d0ddc8e9734bb298831bceb7 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 13 Jan 2022 14:14:34 +0100 Subject: [PATCH] More heading configuration --- src/library/heading.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/library/heading.rs b/src/library/heading.rs index d3beb4eef..4c7bcc7d5 100644 --- a/src/library/heading.rs +++ b/src/library/heading.rs @@ -17,9 +17,16 @@ pub struct HeadingNode { impl HeadingNode { /// The heading's font family. pub const FAMILY: Smart = Smart::Auto; + /// The size of text in the heading. Just the surrounding text size if + /// `auto`. + pub const SIZE: Smart = Smart::Auto; /// The fill color of text in the heading. Just the surrounding text color /// if `auto`. pub const FILL: Smart = Smart::Auto; + /// The extra padding above the heading. + pub const ABOVE: Length = Length::zero(); + /// The extra padding below the heading. + pub const BELOW: Length = Length::zero(); fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult { Ok(Node::block(Self { @@ -30,7 +37,10 @@ impl HeadingNode { fn set(args: &mut Args, styles: &mut StyleMap) -> TypResult<()> { styles.set_opt(Self::FAMILY, args.named("family")?); + styles.set_opt(Self::SIZE, args.named("size")?); styles.set_opt(Self::FILL, args.named("fill")?); + styles.set_opt(Self::ABOVE, args.named("above")?); + styles.set_opt(Self::BELOW, args.named("below")?); Ok(()) } } @@ -46,7 +56,10 @@ impl Layout for HeadingNode { let mut passed = StyleMap::new(); passed.set(TextNode::STRONG, true); - passed.set(TextNode::SIZE, Relative::new(upscale).into()); + passed.set( + TextNode::SIZE, + styles.get(Self::SIZE).unwrap_or(Relative::new(upscale).into()), + ); if let Smart::Custom(family) = styles.get_ref(Self::FAMILY) { passed.set( @@ -62,6 +75,18 @@ impl Layout for HeadingNode { passed.set(TextNode::FILL, fill); } - self.child.layout(ctx, regions, passed.chain(&styles)) + let mut frames = self.child.layout(ctx, regions, passed.chain(&styles)); + + let above = styles.get(Self::ABOVE); + let below = styles.get(Self::BELOW); + + // FIXME: Constraints and region size. + for Constrained { item: frame, .. } in &mut frames { + let frame = Rc::make_mut(frame); + frame.size.y += above + below; + frame.translate(Point::with_y(above)); + } + + frames } }