1use pltrs_core::path::convert::SketchParams;
4use pltrs_core::{Affine2D, Bbox, Path, Rgba};
5
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
8pub enum CapStyle {
9 #[default]
11 Butt,
12 Projecting,
14 Round,
16}
17
18pltrs_core::archive_enum!(CapStyle {
19 Butt,
20 Projecting,
21 Round,
22});
23
24#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
26pub enum JoinStyle {
27 Miter,
29 #[default]
31 Round,
32 Bevel,
34}
35
36pltrs_core::archive_enum!(JoinStyle {
37 Miter,
38 Round,
39 Bevel,
40});
41
42impl CapStyle {
43 pub fn from_name(name: &str) -> Option<Self> {
45 Some(match name {
46 "butt" => Self::Butt,
47 "projecting" => Self::Projecting,
48 "round" => Self::Round,
49 _ => return None,
50 })
51 }
52
53 pub const fn name(self) -> &'static str {
55 match self {
56 Self::Butt => "butt",
57 Self::Projecting => "projecting",
58 Self::Round => "round",
59 }
60 }
61}
62
63impl JoinStyle {
64 pub fn from_name(name: &str) -> Option<Self> {
66 Some(match name {
67 "miter" => Self::Miter,
68 "round" => Self::Round,
69 "bevel" => Self::Bevel,
70 _ => return None,
71 })
72 }
73
74 pub const fn name(self) -> &'static str {
76 match self {
77 Self::Miter => "miter",
78 Self::Round => "round",
79 Self::Bevel => "bevel",
80 }
81 }
82}
83
84#[derive(Clone, Debug, Default, PartialEq)]
98pub struct Dashes {
99 pub offset: f64,
101 pub pattern: Option<Vec<f64>>,
103}
104
105pltrs_core::archive_struct!(Dashes { offset, pattern });
106
107impl Dashes {
108 pub const fn solid() -> Self {
110 Self {
111 offset: 0.0,
112 pattern: None,
113 }
114 }
115
116 pub const fn new(offset: f64, pattern: Vec<f64>) -> Self {
119 Self {
120 offset,
121 pattern: Some(pattern),
122 }
123 }
124
125 pub fn lengths(&self) -> &[f64] {
127 self.pattern.as_deref().unwrap_or_default()
128 }
129
130 pub fn is_solid(&self) -> bool {
132 self.lengths().iter().all(|&len| len <= 0.0)
133 }
134}
135
136#[derive(Clone, Debug, PartialEq)]
138pub struct Hatch {
139 pub pattern: String,
141 pub color: Rgba,
143 pub linewidth: f64,
145}
146
147pltrs_core::archive_struct!(Hatch {
148 pattern,
149 color,
150 linewidth
151});
152
153#[derive(Clone, Copy, Debug, PartialEq)]
155pub struct Sketch {
156 pub scale: f64,
158 pub length: f64,
160 pub randomness: f64,
162}
163
164pltrs_core::archive_struct!(Sketch {
165 scale,
166 length,
167 randomness
168});
169
170impl Sketch {
171 pub(crate) fn params(self) -> SketchParams {
173 SketchParams::new(self.scale, Some(self.length), Some(self.randomness))
174 }
175}
176
177#[derive(Clone, Debug, PartialEq)]
179pub struct ClipPath {
180 pub path: Path,
182 pub transform: Affine2D,
184 pub rebuilt: bool,
190}
191
192impl ClipPath {
193 #[must_use]
195 pub fn new(path: Path, transform: Affine2D) -> Self {
196 Self {
197 path,
198 transform,
199 rebuilt: false,
200 }
201 }
202
203 #[must_use]
206 pub fn with_rebuilt(self, rebuilt: bool) -> Self {
207 Self { rebuilt, ..self }
208 }
209}
210
211#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
213pub enum SnapMode {
214 #[default]
216 Auto,
217 Always,
219 Never,
221}
222
223pltrs_core::archive_enum!(SnapMode {
224 Auto,
225 Always,
226 Never,
227});
228
229#[derive(Clone, Debug, PartialEq)]
233pub struct GraphicsContext {
234 pub foreground: Rgba,
236 pub alpha: Option<f64>,
238 pub antialiased: bool,
240 pub cap: CapStyle,
242 pub join: JoinStyle,
244 pub linewidth: f64,
246 pub dashes: Dashes,
248 pub clip_rect: Option<Bbox>,
250 pub clip_path: Option<ClipPath>,
252 pub hatch: Option<Hatch>,
254 pub snap: SnapMode,
256 pub sketch: Option<Sketch>,
258 pub url: Option<String>,
260 pub gid: Option<String>,
262}
263
264impl Default for GraphicsContext {
265 fn default() -> Self {
266 Self {
267 foreground: Rgba::BLACK,
268 alpha: None,
269 antialiased: true,
270 cap: CapStyle::Butt,
271 join: JoinStyle::Round,
272 linewidth: 1.0,
273 dashes: Dashes::solid(),
274 clip_rect: None,
275 clip_path: None,
276 hatch: None,
277 snap: SnapMode::Auto,
278 sketch: None,
279 url: None,
280 gid: None,
281 }
282 }
283}
284
285impl GraphicsContext {
286 pub fn stroke_color(&self) -> Rgba {
288 self.apply_alpha(self.foreground)
289 }
290
291 pub fn fill_color(&self, face: Rgba) -> Rgba {
293 self.apply_alpha(face)
294 }
295
296 pub fn strokes(&self) -> bool {
298 self.linewidth > 0.0 && !self.stroke_color().is_transparent()
299 }
300
301 fn apply_alpha(&self, color: Rgba) -> Rgba {
302 self.alpha.map_or(color, |a| color.with_alpha(a))
303 }
304}
305
306#[cfg(test)]
307mod tests {
308 use super::*;
309
310 #[test]
311 fn alpha_override_applies_to_stroke_and_fill() {
312 let gc = GraphicsContext {
313 foreground: Rgba::new(1.0, 0.0, 0.0, 0.2),
314 alpha: Some(0.5),
315 ..GraphicsContext::default()
316 };
317 assert_eq!(gc.stroke_color().a, 0.5);
318 assert_eq!(gc.fill_color(Rgba::WHITE).a, 0.5);
319 assert!(gc.strokes());
320 }
321
322 #[test]
323 fn style_names_round_trip() {
324 for cap in [CapStyle::Butt, CapStyle::Projecting, CapStyle::Round] {
325 assert_eq!(CapStyle::from_name(cap.name()), Some(cap));
326 }
327 for join in [JoinStyle::Miter, JoinStyle::Round, JoinStyle::Bevel] {
328 assert_eq!(JoinStyle::from_name(join.name()), Some(join));
329 }
330 assert_eq!(CapStyle::from_name("square"), None);
331 }
332
333 #[test]
334 fn zero_length_dash_patterns_are_solid() {
335 assert!(Dashes::solid().is_solid());
336 assert!(Dashes::new(0.0, vec![0.0, 0.0]).is_solid());
337 assert!(Dashes::new(2.0, Vec::new()).is_solid());
338 assert!(!Dashes::new(0.0, vec![3.0, 1.0]).is_solid());
339 assert_eq!(Dashes::solid().lengths(), []);
340 }
341}