1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! Stroke properties for geometry outlines.
//!
//! Defines how paths are stroked: width, caps, joins, and dash patterns.
use crate::Paint;
use astrelis_render::Color;
/// Line cap style for stroke endpoints.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LineCap {
/// Flat cap ending at the endpoint.
#[default]
Butt,
/// Round cap extending beyond the endpoint.
Round,
/// Square cap extending beyond the endpoint.
Square,
}
/// Line join style for stroke corners.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LineJoin {
/// Miter join (sharp corner).
#[default]
Miter,
/// Round join (rounded corner).
Round,
/// Bevel join (flat corner).
Bevel,
}
/// Dash pattern for stroked lines.
#[derive(Debug, Clone, PartialEq)]
pub struct DashPattern {
/// Alternating on/off lengths.
pub pattern: Vec<f32>,
/// Offset into the pattern to start.
pub offset: f32,
}
impl DashPattern {
/// Create a new dash pattern.
pub fn new(pattern: Vec<f32>, offset: f32) -> Self {
Self { pattern, offset }
}
/// Create a simple dashed line.
pub fn dashed(dash: f32, gap: f32) -> Self {
Self {
pattern: vec![dash, gap],
offset: 0.0,
}
}
/// Create a dotted line.
pub fn dotted(gap: f32) -> Self {
Self {
pattern: vec![0.0, gap],
offset: 0.0,
}
}
/// Create a dash-dot pattern.
pub fn dash_dot(dash: f32, gap: f32, dot: f32) -> Self {
Self {
pattern: vec![dash, gap, dot, gap],
offset: 0.0,
}
}
}
/// Stroke properties for geometry outlines.
#[derive(Debug, Clone, PartialEq)]
pub struct Stroke {
/// Stroke width in logical pixels
pub width: f32,
/// Paint for the stroke color/gradient
pub paint: Paint,
/// Line cap style
pub line_cap: LineCap,
/// Line join style
pub line_join: LineJoin,
/// Miter limit for miter joins
pub miter_limit: f32,
/// Optional dash pattern
pub dash: Option<DashPattern>,
/// Opacity multiplier (0.0 to 1.0)
pub opacity: f32,
}
impl Stroke {
/// Create a solid color stroke.
pub fn solid(color: Color, width: f32) -> Self {
Self {
width,
paint: Paint::Solid(color),
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
miter_limit: 4.0,
dash: None,
opacity: 1.0,
}
}
/// Create a stroke from a paint.
pub fn from_paint(paint: Paint, width: f32) -> Self {
Self {
width,
paint,
line_cap: LineCap::Butt,
line_join: LineJoin::Miter,
miter_limit: 4.0,
dash: None,
opacity: 1.0,
}
}
/// Set the line cap style.
pub fn with_line_cap(mut self, cap: LineCap) -> Self {
self.line_cap = cap;
self
}
/// Set the line join style.
pub fn with_line_join(mut self, join: LineJoin) -> Self {
self.line_join = join;
self
}
/// Set the miter limit.
pub fn with_miter_limit(mut self, limit: f32) -> Self {
self.miter_limit = limit.max(1.0);
self
}
/// Set a dash pattern.
pub fn with_dash(mut self, pattern: DashPattern) -> Self {
self.dash = Some(pattern);
self
}
/// Set a simple dashed pattern.
pub fn dashed(mut self, dash: f32, gap: f32) -> Self {
self.dash = Some(DashPattern::dashed(dash, gap));
self
}
/// Set the opacity.
pub fn with_opacity(mut self, opacity: f32) -> Self {
self.opacity = opacity.clamp(0.0, 1.0);
self
}
/// Get the effective color (for solid strokes).
pub fn effective_color(&self) -> Option<Color> {
match &self.paint {
Paint::Solid(color) => Some(Color::rgba(
color.r,
color.g,
color.b,
color.a * self.opacity,
)),
_ => None,
}
}
/// Check if the stroke is visible.
pub fn is_visible(&self) -> bool {
self.width > 0.0 && self.opacity > 0.0
}
}
impl Default for Stroke {
fn default() -> Self {
Self::solid(Color::BLACK, 1.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_solid_stroke() {
let stroke = Stroke::solid(Color::RED, 2.0);
assert_eq!(stroke.width, 2.0);
assert!(stroke.is_visible());
}
#[test]
fn test_dashed_stroke() {
let stroke = Stroke::solid(Color::BLUE, 1.0).dashed(5.0, 3.0);
assert!(stroke.dash.is_some());
}
#[test]
fn test_stroke_visibility() {
let stroke = Stroke::solid(Color::RED, 0.0);
assert!(!stroke.is_visible());
let stroke = Stroke::solid(Color::RED, 1.0).with_opacity(0.0);
assert!(!stroke.is_visible());
}
}