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