From 72b60dfde751b4a2ab279aa1fcfa559b4a75eb51 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 13 Feb 2023 12:04:26 +0100 Subject: Fill and stroke properties for containers --- src/doc.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/doc.rs') diff --git a/src/doc.rs b/src/doc.rs index 64f7ae91..a15fbca9 100644 --- a/src/doc.rs +++ b/src/doc.rs @@ -7,8 +7,8 @@ use std::sync::Arc; use crate::font::Font; use crate::geom::{ - self, Abs, Align, Axes, Color, Dir, Em, Geometry, Numeric, Paint, Point, RgbaColor, - Shape, Size, Stroke, Transform, + self, rounded_rect, Abs, Align, Axes, Color, Corners, Dir, Em, Geometry, Numeric, + Paint, Point, Rel, RgbaColor, Shape, Sides, Size, Stroke, Transform, }; use crate::image::Image; use crate::model::{ @@ -271,6 +271,9 @@ impl Frame { /// Attach the metadata from this style chain to the frame. pub fn meta(&mut self, styles: StyleChain) { + if self.is_empty() { + return; + } for meta in styles.get(Meta::DATA) { if matches!(meta, Meta::Hidden) { self.clear(); @@ -280,6 +283,25 @@ impl Frame { } } + /// Add a fill and stroke with optional radius and outset to the frame. + pub fn fill_and_stroke( + &mut self, + fill: Option, + stroke: Sides>, + outset: Sides>, + radius: Corners>, + ) { + let outset = outset.relative_to(self.size()); + let size = self.size() + outset.sum_by_axis(); + let pos = Point::new(-outset.left, -outset.top); + let radius = radius.map(|side| side.relative_to(size.x.min(size.y) / 2.0)); + self.prepend_multiple( + rounded_rect(size, radius, fill, stroke) + .into_iter() + .map(|x| (pos, Element::Shape(x))), + ) + } + /// Arbitrarily transform the contents of the frame. pub fn transform(&mut self, transform: Transform) { self.group(|g| g.transform = transform); -- cgit v1.2.3