1use crate::Error;
3use glory_style::Color;
4
5pub const MAX_GRAPHIC_COMMANDS: usize = 256;
6pub const MAX_GRAPHIC_COORDINATE: f64 = 1_000_000.0;
7
8#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct GraphicPoint {
10 pub x: f64,
11 pub y: f64,
12}
13impl GraphicPoint {
14 pub const fn new(x: f64, y: f64) -> Self {
15 Self { x, y }
16 }
17 fn validate(self) -> Result<(), Error> {
18 if [self.x, self.y]
19 .into_iter()
20 .any(|value| !value.is_finite() || value.abs() > MAX_GRAPHIC_COORDINATE)
21 {
22 return Err(Error::InvalidProps(
23 "graphic coordinates must be finite and within +/-1000000",
24 ));
25 }
26 Ok(())
27 }
28}
29#[derive(Clone, Copy, Debug, PartialEq)]
30pub enum PathCommand {
31 Move(GraphicPoint),
32 Line(GraphicPoint),
33 Cubic {
34 first: GraphicPoint,
35 second: GraphicPoint,
36 end: GraphicPoint,
37 },
38 Close,
39}
40#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
41pub enum StrokeCap {
42 #[default]
43 Butt,
44 Round,
45 Square,
46}
47#[derive(Clone, Copy, Debug, PartialEq)]
48pub struct GraphicStroke {
49 pub color: Color,
50 pub width: f64,
51 pub cap: StrokeCap,
52}
53
54#[derive(Clone, Debug, PartialEq)]
57pub struct GraphicProps {
58 pub width: f64,
59 pub height: f64,
60 pub commands: Vec<PathCommand>,
61 pub fill: Option<Color>,
62 pub stroke: Option<GraphicStroke>,
63 pub alternative: Option<String>,
65 pub animation: Option<crate::GraphicAnimation>,
66 pub transition: Option<crate::GraphicTransition>,
67}
68impl Default for GraphicProps {
69 fn default() -> Self {
70 Self {
71 width: 24.0,
72 height: 24.0,
73 commands: Vec::new(),
74 fill: None,
75 stroke: None,
76 alternative: None,
77 animation: None,
78 transition: None,
79 }
80 }
81}
82impl GraphicProps {
83 pub(crate) fn transition_from(&self, previous: &Self) -> Result<(), Error> {
84 if self.transition.is_some()
85 && !crate::GraphicTransition::compatible(&previous.commands, &self.commands)
86 {
87 return Err(Error::InvalidProps(
88 "graphic transition requires compatible command topology",
89 ));
90 }
91 Ok(())
92 }
93 pub fn validate(&self) -> Result<(), Error> {
94 if let Some(transition) = self.transition {
95 transition.validate()?;
96 if self.animation.is_some() {
97 return Err(Error::InvalidProps(
98 "graphic animation and transition are mutually exclusive",
99 ));
100 }
101 }
102 if let Some(animation) = self.animation {
103 animation.validate()?;
104 }
105 if [self.width, self.height].into_iter().any(|value| {
106 !value.is_finite() || !(0.000001..=MAX_GRAPHIC_COORDINATE).contains(&value)
107 }) {
108 return Err(Error::InvalidProps(
109 "graphic viewport dimensions must be finite and in 0.000001..=1000000",
110 ));
111 }
112 if self.commands.len() > MAX_GRAPHIC_COMMANDS {
113 return Err(Error::InvalidProps(
114 "graphic path exceeds its 256 command budget",
115 ));
116 }
117 if self
118 .alternative
119 .as_ref()
120 .is_some_and(|text| text.trim().is_empty() || text.len() > 1024)
121 {
122 return Err(Error::InvalidProps(
123 "graphic alternative must be nonempty and at most 1024 UTF-8 bytes",
124 ));
125 }
126 if self.stroke.is_some_and(|stroke| {
127 !stroke.width.is_finite() || !(0.000001..=10000.0).contains(&stroke.width)
128 }) {
129 return Err(Error::InvalidProps(
130 "graphic stroke width must be finite and in 0.000001..=10000",
131 ));
132 }
133 let mut open = false;
134 for command in &self.commands {
135 match command {
136 PathCommand::Move(point) => {
137 point.validate()?;
138 open = true;
139 }
140 PathCommand::Line(point) => {
141 if !open {
142 return Err(Error::InvalidProps(
143 "graphic segments require a preceding Move",
144 ));
145 }
146 point.validate()?;
147 }
148 PathCommand::Cubic { first, second, end } => {
149 if !open {
150 return Err(Error::InvalidProps(
151 "graphic segments require a preceding Move",
152 ));
153 }
154 first.validate()?;
155 second.validate()?;
156 end.validate()?;
157 }
158 PathCommand::Close => {
159 if !open {
160 return Err(Error::InvalidProps(
161 "graphic Close requires an open subpath",
162 ));
163 }
164 open = false;
165 }
166 }
167 }
168 Ok(())
169 }
170}
171
172#[cfg(test)]
173mod tests {
174 use super::*;
175 fn point() -> GraphicPoint {
176 GraphicPoint::new(1.0, 2.0)
177 }
178 #[test]
179 fn every_command_checks_order_and_all_coordinates() {
180 let mut props = GraphicProps::default();
181 for command in [
182 PathCommand::Line(point()),
183 PathCommand::Cubic {
184 first: point(),
185 second: point(),
186 end: point(),
187 },
188 PathCommand::Close,
189 ] {
190 props.commands = vec![command];
191 assert!(props.validate().is_err());
192 }
193 for value in [
194 f64::NAN,
195 f64::INFINITY,
196 f64::NEG_INFINITY,
197 1_000_001.0,
198 -1_000_001.0,
199 ] {
200 for bad in [GraphicPoint::new(value, 0.0), GraphicPoint::new(0.0, value)] {
201 for command in [
202 PathCommand::Move(bad),
203 PathCommand::Line(bad),
204 PathCommand::Cubic {
205 first: bad,
206 second: point(),
207 end: point(),
208 },
209 PathCommand::Cubic {
210 first: point(),
211 second: bad,
212 end: point(),
213 },
214 PathCommand::Cubic {
215 first: point(),
216 second: point(),
217 end: bad,
218 },
219 ] {
220 props.commands = vec![PathCommand::Move(point()), command];
221 assert!(props.validate().is_err());
222 }
223 }
224 }
225 props.commands = vec![
226 PathCommand::Move(point()),
227 PathCommand::Line(point()),
228 PathCommand::Cubic {
229 first: point(),
230 second: point(),
231 end: point(),
232 },
233 PathCommand::Close,
234 PathCommand::Move(point()),
235 ];
236 props.validate().unwrap();
237 props.commands.push(PathCommand::Close);
238 props.commands.push(PathCommand::Close);
239 assert!(props.validate().is_err());
240 }
241 #[test]
242 fn viewport_stroke_alternative_and_command_budgets_are_independent() {
243 for value in [0.0, -1.0, f64::NAN, f64::INFINITY, 0.0000001, 1_000_001.0] {
244 assert!(
245 GraphicProps {
246 width: value,
247 ..Default::default()
248 }
249 .validate()
250 .is_err()
251 );
252 assert!(
253 GraphicProps {
254 height: value,
255 ..Default::default()
256 }
257 .validate()
258 .is_err()
259 );
260 }
261 for width in [0.0, -1.0, f64::NAN, f64::INFINITY, 0.0000001, 10001.0] {
262 assert!(
263 GraphicProps {
264 stroke: Some(GraphicStroke {
265 color: Color(0, 0, 0, 255),
266 width,
267 cap: StrokeCap::Butt
268 }),
269 ..Default::default()
270 }
271 .validate()
272 .is_err()
273 );
274 }
275 for cap in [StrokeCap::Butt, StrokeCap::Round, StrokeCap::Square] {
276 GraphicProps {
277 stroke: Some(GraphicStroke {
278 color: Color(0, 0, 0, 255),
279 width: 1.0,
280 cap,
281 }),
282 ..Default::default()
283 }
284 .validate()
285 .unwrap();
286 }
287 for alternative in ["".to_string(), " ".to_string(), "x".repeat(1025)] {
288 assert!(
289 GraphicProps {
290 alternative: Some(alternative),
291 ..Default::default()
292 }
293 .validate()
294 .is_err()
295 );
296 }
297 let mut props = GraphicProps {
298 commands: vec![PathCommand::Move(point()); 256],
299 ..Default::default()
300 };
301 props.validate().unwrap();
302 props.commands.push(PathCommand::Move(point()));
303 assert!(props.validate().is_err());
304 }
305}