summaryrefslogtreecommitdiff
path: root/library/src/visualize/polygon.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-04-06 12:57:26 +0200
committerLaurenz <laurmaedje@gmail.com>2023-04-06 12:58:27 +0200
commit4f4af02acea0022a5c1966d9b7b4150b35749edd (patch)
tree4e7107d8df1e169ce941ef8bf2c65e52600b36cc /library/src/visualize/polygon.rs
parentf9b9be16f96d01ffb0587f65c8c32da9e9e4b3f5 (diff)
Fix path and polygon strokes
Diffstat (limited to 'library/src/visualize/polygon.rs')
-rw-r--r--library/src/visualize/polygon.rs58
1 files changed, 37 insertions, 21 deletions
diff --git a/library/src/visualize/polygon.rs b/library/src/visualize/polygon.rs
index 4b4adf7b..559ebec3 100644
--- a/library/src/visualize/polygon.rs
+++ b/library/src/visualize/polygon.rs
@@ -7,12 +7,12 @@ use crate::prelude::*;
/// ## Example
/// ```example
/// #polygon(
-/// fill: red,
-/// stroke: 2pt + black,
-/// (0pt, 0pt),
-/// (50%, 0pt),
-/// (50%, 4cm),
-/// (20%, 4cm),
+/// fill: blue.lighten(80%),
+/// stroke: blue,
+/// (20%, 0pt),
+/// (60%, 0pt),
+/// (80%, 2cm),
+/// (0%, 2cm),
/// )
/// ```
///
@@ -27,11 +27,20 @@ pub struct PolygonElem {
/// [non-zero winding rule](https://en.wikipedia.org/wiki/Nonzero-rule).
pub fill: Option<Paint>,
- /// How to stroke the polygon. See the [lines's
- /// documentation]($func/line.stroke) for more details.
+ /// How to stroke the polygon. This can be:
+ ///
+ /// - `{none}` to disable the stroke.
+ /// - `{auto}` for a stroke of `{1pt}` black if and if only if no fill is
+ /// given.
+ /// - A length specifying the stroke's thickness. The color is inherited,
+ /// defaulting to black.
+ /// - A color to use for the stroke. The thickness is inherited, defaulting
+ /// to `{1pt}`.
+ /// - A stroke combined from color and thickness using the `+` operator as
+ /// in `{2pt + red}`.
#[resolve]
#[fold]
- pub stroke: Option<PartialStroke>,
+ pub stroke: Smart<Option<PartialStroke>>,
/// The vertices of the polygon. Each point is specified as an array of two
/// [relative lengths]($type/relative-length).
@@ -61,21 +70,28 @@ impl Layout for PolygonElem {
let mut frame = Frame::new(size);
// Only create a path if there are more than zero points.
- if !points.is_empty() {
- let fill = self.fill(styles);
- let stroke = self.stroke(styles).map(PartialStroke::unwrap_or_default);
+ if points.is_empty() {
+ return Ok(Fragment::frame(frame));
+ }
- // Construct a closed path given all points.
- let mut path = Path::new();
- path.move_to(points[0]);
- for &point in &points[1..] {
- path.line_to(point);
- }
- path.close_path();
+ // Prepare fill and stroke.
+ let fill = self.fill(styles);
+ let stroke = match self.stroke(styles) {
+ Smart::Auto if fill.is_none() => Some(Stroke::default()),
+ Smart::Auto => None,
+ Smart::Custom(stroke) => stroke.map(PartialStroke::unwrap_or_default),
+ };
- let shape = Shape { geometry: Geometry::Path(path), stroke, fill };
- frame.push(Point::zero(), FrameItem::Shape(shape, self.span()));
+ // Construct a closed path given all points.
+ let mut path = Path::new();
+ path.move_to(points[0]);
+ for &point in &points[1..] {
+ path.line_to(point);
}
+ path.close_path();
+
+ let shape = Shape { geometry: Geometry::Path(path), stroke, fill };
+ frame.push(Point::zero(), FrameItem::Shape(shape, self.span()));
Ok(Fragment::frame(frame))
}