rootvg_tessellation/stroke.rs
1// The following code was copied and modified from
2// https://github.com/iced-rs/iced/blob/31d1d5fecbef50fa319cabd5d4194f1e4aaefa21/graphics/src/geometry/stroke.rs
3// Iced license (MIT): https://github.com/iced-rs/iced/blob/31d1d5fecbef50fa319cabd5d4194f1e4aaefa21/LICENSE
4
5use rootvg_core::color::PackedSrgb;
6
7use crate::fill::FillStyle;
8
9/// The style of a stroke.
10#[derive(Debug, Clone)]
11pub struct Stroke<'a> {
12 /// The color or gradient of the stroke.
13 ///
14 /// By default, it is set to a [`Style::Solid`] with [`Color::BLACK`].
15 pub style: FillStyle,
16 /// The distance between the two edges of the stroke.
17 pub width: f32,
18 /// The shape to be used at the end of open subpaths when they are stroked.
19 pub line_cap: LineCap,
20 /// The shape to be used at the corners of paths or basic shapes when they
21 /// are stroked.
22 pub line_join: LineJoin,
23 /// The dash pattern used when stroking the line.
24 pub line_dash: LineDash<'a>,
25}
26
27impl<'a> Stroke<'a> {
28 /// Sets the color of the [`Stroke`].
29 pub fn with_color(self, color: PackedSrgb) -> Self {
30 Stroke {
31 style: FillStyle::Solid(color),
32 ..self
33 }
34 }
35
36 /// Sets the width of the [`Stroke`].
37 pub fn with_width(self, width: f32) -> Self {
38 Stroke { width, ..self }
39 }
40
41 /// Sets the [`LineCap`] of the [`Stroke`].
42 pub fn with_line_cap(self, line_cap: LineCap) -> Self {
43 Stroke { line_cap, ..self }
44 }
45
46 /// Sets the [`LineJoin`] of the [`Stroke`].
47 pub fn with_line_join(self, line_join: LineJoin) -> Self {
48 Stroke { line_join, ..self }
49 }
50}
51
52impl<'a> Default for Stroke<'a> {
53 fn default() -> Self {
54 Stroke {
55 style: FillStyle::Solid(PackedSrgb([0.0, 0.0, 0.0, 1.0])),
56 width: 1.0,
57 line_cap: LineCap::default(),
58 line_join: LineJoin::default(),
59 line_dash: LineDash::default(),
60 }
61 }
62}
63
64/// The shape used at the end of open subpaths when they are stroked.
65#[derive(Debug, Clone, Copy, Default)]
66pub enum LineCap {
67 /// The stroke for each sub-path does not extend beyond its two endpoints.
68 #[default]
69 Butt,
70 /// At the end of each sub-path, the shape representing the stroke will be
71 /// extended by a square.
72 Square,
73 /// At the end of each sub-path, the shape representing the stroke will be
74 /// extended by a semicircle.
75 Round,
76}
77
78/// The shape used at the corners of paths or basic shapes when they are
79/// stroked.
80#[derive(Debug, Clone, Copy, Default)]
81pub enum LineJoin {
82 /// A sharp corner.
83 #[default]
84 Miter,
85 /// A round corner.
86 Round,
87 /// A bevelled corner.
88 Bevel,
89}
90
91/// The dash pattern used when stroking the line.
92#[derive(Debug, Clone, Copy, Default)]
93pub struct LineDash<'a> {
94 /// The alternating lengths of lines and gaps which describe the pattern.
95 pub segments: &'a [f32],
96
97 /// The offset of [`LineDash::segments`] to start the pattern.
98 pub offset: usize,
99}