diff options
Diffstat (limited to 'core/src/styles.rs')
-rw-r--r-- | core/src/styles.rs | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/core/src/styles.rs b/core/src/styles.rs new file mode 100644 index 0000000..c061c58 --- /dev/null +++ b/core/src/styles.rs | |||
@@ -0,0 +1,161 @@ | |||
1 | use super::colors::Color; | ||
2 | use super::types::Float; | ||
3 | |||
4 | #[derive(Copy, Clone, PartialEq, Debug)] | ||
5 | #[non_exhaustive] | ||
6 | pub enum Pattern { | ||
7 | Solid(Color), | ||
8 | None, | ||
9 | } | ||
10 | |||
11 | impl Pattern { | ||
12 | pub fn is_none(&self) -> bool { | ||
13 | match self { | ||
14 | Pattern::None => true, | ||
15 | _ => false, | ||
16 | } | ||
17 | } | ||
18 | } | ||
19 | |||
20 | impl Default for Pattern { | ||
21 | fn default() -> Self { | ||
22 | Pattern::Solid(Color::default()) | ||
23 | } | ||
24 | } | ||
25 | |||
26 | #[derive(Clone, PartialEq, Debug)] | ||
27 | pub struct FillStyle { | ||
28 | pub(crate) pattern: Pattern, | ||
29 | } | ||
30 | |||
31 | impl FillStyle { | ||
32 | pub fn solid(color: Color) -> Self { | ||
33 | FillStyle { | ||
34 | pattern: Pattern::Solid(color), | ||
35 | } | ||
36 | } | ||
37 | } | ||
38 | |||
39 | impl Default for FillStyle { | ||
40 | fn default() -> Self { | ||
41 | FillStyle { | ||
42 | pattern: Pattern::None, | ||
43 | } | ||
44 | } | ||
45 | } | ||
46 | |||
47 | // TODO: probably move Float into an enum with Defined(f64) and Variable(handle) | ||
48 | #[derive(Clone, Debug)] | ||
49 | pub struct StrokeStyle { | ||
50 | pub(crate) pattern: Pattern, | ||
51 | pub(crate) dash: Option<DashStyle>, | ||
52 | pub(crate) line_width: Float, | ||
53 | } | ||
54 | |||
55 | impl StrokeStyle { | ||
56 | pub fn solid(color: Color) -> Self { | ||
57 | StrokeStyle { | ||
58 | pattern: Pattern::Solid(color), | ||
59 | dash: None, | ||
60 | line_width: Float::Fixed(0.), | ||
61 | } | ||
62 | } | ||
63 | |||
64 | pub fn builder() -> StrokeStyleBuilder { | ||
65 | StrokeStyleBuilder::default() | ||
66 | } | ||
67 | } | ||
68 | |||
69 | impl Default for StrokeStyle { | ||
70 | fn default() -> Self { | ||
71 | Self::builder().build() | ||
72 | } | ||
73 | } | ||
74 | |||
75 | #[derive(Clone, Default)] | ||
76 | pub struct StrokeStyleBuilder { | ||
77 | pattern: Option<Pattern>, | ||
78 | dash: Option<DashStyle>, | ||
79 | line_width: Option<Float>, | ||
80 | } | ||
81 | |||
82 | impl StrokeStyleBuilder { | ||
83 | pub fn pattern(&mut self, pattern: Pattern) -> &mut Self { | ||
84 | self.pattern = Some(pattern); | ||
85 | self | ||
86 | } | ||
87 | |||
88 | pub fn dash(&mut self, dash: DashStyle) -> &mut Self { | ||
89 | self.dash = Some(dash); | ||
90 | self | ||
91 | } | ||
92 | |||
93 | pub fn line_width(&mut self, width: Float) -> &mut Self { | ||
94 | self.line_width = Some(width); | ||
95 | self | ||
96 | } | ||
97 | |||
98 | pub fn build(&mut self) -> StrokeStyle { | ||
99 | StrokeStyle { | ||
100 | pattern: self.pattern.clone().unwrap_or_default(), | ||
101 | dash: self.dash.clone(), | ||
102 | line_width: self.line_width.unwrap_or(Float::Fixed(2.)), | ||
103 | } | ||
104 | } | ||
105 | |||
106 | } | ||
107 | |||
108 | |||
109 | |||
110 | |||
111 | #[derive(Clone, Debug, Default)] | ||
112 | pub struct DashStyle { | ||
113 | pub(crate) dashes: Vec<Float>, | ||
114 | pub(crate) offset: Float, | ||
115 | } | ||
116 | |||
117 | impl DashStyle { | ||
118 | pub fn new(dashes: Vec<Float>, offset: Float) -> Self { | ||
119 | Self { dashes, offset } | ||
120 | } | ||
121 | |||
122 | pub fn dashes(&self) -> &[Float] { | ||
123 | &self.dashes | ||
124 | } | ||
125 | |||
126 | pub fn dashes_mut(&mut self) -> &mut Vec<Float> { | ||
127 | &mut self.dashes | ||
128 | } | ||
129 | |||
130 | pub fn offset(&self) -> Float { | ||
131 | self.offset | ||
132 | } | ||
133 | |||
134 | // TODO: does this makes sense? | ||
135 | pub fn offset_mut(&mut self) -> &mut Float { | ||
136 | &mut self.offset | ||
137 | } | ||
138 | } | ||
139 | |||
140 | #[derive(Clone, PartialEq, Debug, Default)] | ||
141 | pub struct DefinedStrokeStyle { | ||
142 | pub(crate) pattern: Pattern, | ||
143 | pub(crate) dash: Option<DefinedDashStyle>, | ||
144 | pub(crate) line_width: f64, | ||
145 | } | ||
146 | |||
147 | #[derive(Clone, PartialEq, Debug, Default)] | ||
148 | pub struct DefinedDashStyle { | ||
149 | pub(crate) dashes: Vec<f64>, | ||
150 | pub(crate) offset: f64, | ||
151 | } | ||
152 | |||
153 | impl DefinedDashStyle { | ||
154 | pub fn dashes(&self) -> &[f64] { | ||
155 | &self.dashes | ||
156 | } | ||
157 | |||
158 | pub fn offset(&self) -> f64 { | ||
159 | self.offset | ||
160 | } | ||
161 | } | ||