1use crate::render::backend::{FillStyle, LineStyle, PointOutline, PointStyle, StrokePattern};
4use crate::{Error, PointShape, Result};
5
6use super::{color_from_rgb, Color, RelativeSpacing};
7
8fn validate_width(width: f32) -> Result<()> {
9 if width.is_finite() && width > 0.0 {
10 Ok(())
11 } else {
12 Err(Error::Data(
13 "present stroke width must be finite and positive CSS pixels".into(),
14 ))
15 }
16}
17
18macro_rules! solid_paint {
20 ($name:ident, $documentation:literal) => {
21 #[doc = $documentation]
22 #[derive(Debug, Clone, PartialEq)]
23 pub struct $name {
24 pub(crate) paint: Color,
25 pub(crate) width: f32,
26 }
27
28 impl $name {
29 pub fn new(color: Color, width: f32) -> Result<Self> {
31 let value = Self {
32 paint: color,
33 width,
34 };
35 value.validate()?;
36 Ok(value)
37 }
38
39 pub(crate) fn validate(&self) -> Result<()> {
40 validate_width(self.get_width())
41 }
42
43 pub fn get_color(&self) -> Color {
45 self.paint
46 }
47 pub fn get_width(&self) -> f32 {
49 self.width
50 }
51
52 pub fn color(self, color: Color) -> Result<Self> {
54 Self::new(color, self.width)
55 }
56
57 pub fn width(self, width: f32) -> Result<Self> {
59 Self::new(self.paint, width)
60 }
61 }
62 };
63}
64
65solid_paint!(
66 SolidStrokeTheme,
67 "Complete solid bracket stroke; its topology has no pattern edit."
68);
69solid_paint!(
70 OutlineTheme,
71 "Complete independent outline paint and positive CSS width."
72);
73
74#[derive(Debug, Clone, PartialEq)]
76pub struct StrokeTheme {
77 pub(crate) paint: Color,
78 pub(crate) width: f32,
79 pub(crate) pattern: StrokePattern,
80}
81
82impl StrokeTheme {
83 pub(crate) fn validate(&self) -> Result<()> {
84 validate_width(self.get_width())
85 }
86 pub fn new(color: Color, width: f32, pattern: StrokePattern) -> Result<Self> {
88 validate_width(width)?;
89 Ok(Self {
90 paint: color,
91 width,
92 pattern,
93 })
94 }
95
96 pub fn get_color(&self) -> Color {
98 self.paint
99 }
100 pub fn get_width(&self) -> f32 {
102 self.width
103 }
104 pub fn get_pattern(&self) -> StrokePattern {
106 self.pattern
107 }
108
109 pub fn color(self, color: Color) -> Result<Self> {
111 Self::new(color, self.width, self.pattern)
112 }
113 pub fn width(self, width: f32) -> Result<Self> {
115 Self::new(self.paint, width, self.pattern)
116 }
117 pub fn pattern(self, pattern: StrokePattern) -> Result<Self> {
119 Self::new(self.paint, self.width, pattern)
120 }
121
122 pub(crate) fn backend_style(&self) -> LineStyle {
123 LineStyle {
124 color: self.get_color(),
125 width: self.get_width(),
126 pattern: self.get_pattern(),
127 }
128 }
129}
130
131#[derive(Debug, Clone, PartialEq, Eq)]
133pub struct PointOutlineTheme {
134 pub(crate) color: Color,
135 pub(crate) width_ratio: RelativeSpacing,
136}
137
138impl PointOutlineTheme {
139 pub fn new(color: Color, width_ratio: RelativeSpacing) -> Result<Self> {
141 if width_ratio.is_zero() {
142 return Err(Error::Data(
143 "point outline width ratio must be positive".into(),
144 ));
145 }
146 Ok(Self { color, width_ratio })
147 }
148
149 fn validate(&self) -> Result<()> {
150 if self.width_ratio.is_zero() {
151 Err(Error::Data(
152 "point outline width ratio must be positive".into(),
153 ))
154 } else {
155 Ok(())
156 }
157 }
158
159 pub fn get_color(&self) -> Color {
161 self.color
162 }
163 pub fn get_width_ratio(&self) -> &RelativeSpacing {
165 &self.width_ratio
166 }
167 pub fn color(self, color: Color) -> Result<Self> {
169 Self::new(color, self.width_ratio)
170 }
171 pub fn width_ratio(self, width_ratio: RelativeSpacing) -> Result<Self> {
173 Self::new(self.color, width_ratio)
174 }
175
176 pub(crate) fn publication() -> Result<Self> {
177 Self::new(Color::rgb(255, 255, 255), RelativeSpacing::new(0.1)?)
178 }
179
180 pub(crate) fn backend_style(&self) -> PointOutline {
182 PointOutline {
183 color: self.get_color(),
184 width_ratio: self.get_width_ratio().clone(),
185 }
186 }
187}
188
189#[derive(Debug, Clone, PartialEq)]
191pub struct DataOutlineTheme {
192 choice: DataOutline,
193}
194
195#[derive(Debug, Clone, PartialEq)]
196enum DataOutline {
197 Fixed(OutlineTheme),
198 Mapped { width: f32, alpha: u8 },
199}
200
201impl DataOutlineTheme {
202 pub fn fixed(stroke: OutlineTheme) -> Self {
204 Self {
205 choice: DataOutline::Fixed(stroke),
206 }
207 }
208
209 pub fn mapped(width: f32, alpha: u8) -> Result<Self> {
211 validate_width(width)?;
212 Ok(Self {
213 choice: DataOutline::Mapped { width, alpha },
214 })
215 }
216
217 pub fn fixed_stroke(&self) -> Option<&OutlineTheme> {
219 match &self.choice {
220 DataOutline::Fixed(stroke) => Some(stroke),
221 DataOutline::Mapped { .. } => None,
222 }
223 }
224 pub fn mapped_width(&self) -> Option<f32> {
226 match self.choice {
227 DataOutline::Fixed(_) => None,
228 DataOutline::Mapped { width, .. } => Some(width),
229 }
230 }
231 pub fn mapped_alpha(&self) -> Option<u8> {
233 match self.choice {
234 DataOutline::Fixed(_) => None,
235 DataOutline::Mapped { alpha, .. } => Some(alpha),
236 }
237 }
238
239 fn validate(&self) -> Result<()> {
240 match &self.choice {
241 DataOutline::Fixed(stroke) => stroke.validate(),
242 DataOutline::Mapped { width, .. } => validate_width(*width),
243 }
244 }
245
246 fn operands(&self, mapped: Color) -> (Color, f32) {
247 if let Some(stroke) = self.fixed_stroke() {
248 (stroke.get_color(), stroke.get_width())
249 } else {
250 (
251 mapped.alpha(self.mapped_alpha().expect("validated mapped outline")),
252 self.mapped_width().expect("validated mapped outline"),
253 )
254 }
255 }
256}
257
258#[derive(Debug, Clone, PartialEq)]
260pub struct DataFillTheme {
261 pub(crate) fill_alpha: u8,
262 pub(crate) outline: Option<DataOutlineTheme>,
263}
264
265impl DataFillTheme {
266 pub(crate) fn validate(&self) -> Result<()> {
267 if let Some(outline) = self.get_outline() {
268 outline.validate()?;
269 }
270 Ok(())
271 }
272 pub fn new(fill_alpha: u8, outline: Option<DataOutlineTheme>) -> Result<Self> {
274 let value = Self {
275 fill_alpha,
276 outline,
277 };
278 value.validate()?;
279 Ok(value)
280 }
281 pub fn get_fill_alpha(&self) -> u8 {
283 self.fill_alpha
284 }
285 pub fn get_outline(&self) -> Option<&DataOutlineTheme> {
287 self.outline.as_ref()
288 }
289 pub fn fill_alpha(self, fill_alpha: u8) -> Result<Self> {
291 Self::new(fill_alpha, self.outline)
292 }
293 pub fn outline(self, outline: Option<DataOutlineTheme>) -> Result<Self> {
295 Self::new(self.fill_alpha, outline)
296 }
297 pub(crate) fn backend_style(&self, rgb: (u8, u8, u8)) -> FillStyle {
298 let color = color_from_rgb(rgb);
299 let (stroke, width) = self
300 .get_outline()
301 .map_or((Color::rgba(0, 0, 0, 0), 0.0), |outline| {
302 outline.operands(color)
303 });
304 FillStyle::new(color.alpha(self.get_fill_alpha()), stroke, width)
305 }
306}
307
308#[derive(Debug, Clone, PartialEq)]
310pub struct FixedFillTheme {
311 pub(crate) paint: Color,
312 pub(crate) outline: Option<OutlineTheme>,
313}
314
315impl FixedFillTheme {
316 pub(crate) fn validate(&self) -> Result<()> {
317 if let Some(outline) = self.get_outline() {
318 outline.validate()?;
319 }
320 Ok(())
321 }
322 pub fn new(color: Color, outline: Option<OutlineTheme>) -> Result<Self> {
324 let value = Self {
325 paint: color,
326 outline,
327 };
328 value.validate()?;
329 Ok(value)
330 }
331 pub fn get_color(&self) -> Color {
333 self.paint
334 }
335 pub fn get_outline(&self) -> Option<&OutlineTheme> {
337 self.outline.as_ref()
338 }
339 pub fn color(self, color: Color) -> Result<Self> {
341 Self::new(color, self.outline)
342 }
343 pub fn outline(self, outline: Option<OutlineTheme>) -> Result<Self> {
345 Self::new(self.paint, outline)
346 }
347 pub(crate) fn backend_style(&self) -> FillStyle {
348 self.get_outline().map_or_else(
349 || FillStyle::new(self.get_color(), Color::rgba(0, 0, 0, 0), 0.0),
350 |outline| outline.backend_style(self.get_color()),
351 )
352 }
353}
354
355impl OutlineTheme {
356 pub(crate) fn backend_style(&self, fill: Color) -> FillStyle {
357 FillStyle::new(fill, self.get_color(), self.get_width())
358 }
359}
360
361#[derive(Debug, Clone, PartialEq)]
363pub struct PointTheme {
364 pub(crate) fill_alpha: u8,
365 pub(crate) shape: PointShape,
366 pub(crate) outline: Option<PointOutlineTheme>,
367}
368
369impl PointTheme {
370 pub(crate) fn validate(&self) -> Result<()> {
371 if let Some(outline) = self.get_outline() {
372 outline.validate()?;
373 }
374 Ok(())
375 }
376 pub fn new(
378 fill_alpha: u8,
379 shape: PointShape,
380 outline: Option<PointOutlineTheme>,
381 ) -> Result<Self> {
382 let value = Self {
383 fill_alpha,
384 shape,
385 outline,
386 };
387 value.validate()?;
388 Ok(value)
389 }
390 pub fn get_fill_alpha(&self) -> u8 {
392 self.fill_alpha
393 }
394 pub fn get_shape(&self) -> PointShape {
396 self.shape
397 }
398 pub fn get_outline(&self) -> Option<&PointOutlineTheme> {
400 self.outline.as_ref()
401 }
402 pub fn fill_alpha(self, fill_alpha: u8) -> Result<Self> {
404 Self::new(fill_alpha, self.shape, self.outline)
405 }
406 pub fn shape(self, shape: PointShape) -> Result<Self> {
408 Self::new(self.fill_alpha, shape, self.outline)
409 }
410 pub fn outline(self, outline: Option<PointOutlineTheme>) -> Result<Self> {
412 Self::new(self.fill_alpha, self.shape, outline)
413 }
414 pub(crate) fn backend_style(
415 &self,
416 rgb: (u8, u8, u8),
417 radius: u32,
418 outline_visible: bool,
419 ) -> PointStyle {
420 PointStyle {
421 color: color_from_rgb(rgb).alpha(self.get_fill_alpha()),
422 radius,
423 shape: self.get_shape(),
424 outline: self
425 .get_outline()
426 .filter(|_| outline_visible)
427 .map(PointOutlineTheme::backend_style),
428 }
429 }
430}
431
432#[derive(Debug, Clone, PartialEq)]
434pub struct DataLineTheme {
435 pub(crate) width: f32,
436 pub(crate) alpha: u8,
437 pub(crate) pattern: StrokePattern,
438}
439
440impl DataLineTheme {
441 pub(crate) fn validate(&self) -> Result<()> {
442 validate_width(self.get_width())
443 }
444 pub fn new(width: f32, alpha: u8, pattern: StrokePattern) -> Result<Self> {
446 let value = Self {
447 width,
448 alpha,
449 pattern,
450 };
451 value.validate()?;
452 Ok(value)
453 }
454 pub fn get_width(&self) -> f32 {
456 self.width
457 }
458 pub fn get_alpha(&self) -> u8 {
460 self.alpha
461 }
462 pub fn get_pattern(&self) -> StrokePattern {
464 self.pattern
465 }
466 pub fn width(self, width: f32) -> Result<Self> {
468 Self::new(width, self.alpha, self.pattern)
469 }
470 pub fn alpha(self, alpha: u8) -> Result<Self> {
472 Self::new(self.width, alpha, self.pattern)
473 }
474 pub fn pattern(self, pattern: StrokePattern) -> Result<Self> {
476 Self::new(self.width, self.alpha, pattern)
477 }
478 pub(crate) fn backend_style(&self, rgb: (u8, u8, u8)) -> LineStyle {
479 LineStyle {
480 color: color_from_rgb(rgb).alpha(self.get_alpha()),
481 width: self.get_width(),
482 pattern: self.get_pattern(),
483 }
484 }
485}