reflexo/vector/ir/visualize.rs
1use super::preludes::*;
2use crate::hash::StaticHash128;
3
4/// Item representing an `<image/>` element.
5#[derive(Debug, Clone, Hash, PartialEq, Eq)]
6#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
7#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
8pub struct ImageItem {
9 /// The source image data.
10 pub image: Arc<Image>,
11 /// The target size of the image.
12 pub size: Size,
13}
14
15/// Data of an `<image/>` element.
16#[derive(Debug, Clone, PartialEq, Eq)]
17#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
18#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
19pub struct Image {
20 /// The encoded image data.
21 pub data: Arc<[u8]>,
22 /// The format of the encoded `buffer`.
23 pub format: ImmutStr,
24 /// The size of the image.
25 pub size: Axes<u32>,
26 /// prehashed image content.
27 pub hash: Fingerprint,
28 /// Attributes that is applicable to the [`Image`].
29 pub attrs: Vec<ImageAttr>,
30}
31
32impl Image {
33 /// Returns the width of the image.
34 pub fn width(&self) -> u32 {
35 self.size.x
36 }
37 /// Returns the height of the image.
38 pub fn height(&self) -> u32 {
39 self.size.y
40 }
41}
42
43/// Prehashed image data.
44impl Hash for Image {
45 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
46 self.hash.hash(state);
47 }
48}
49
50impl StaticHash128 for Image {
51 /// Returns the hash of the image data.
52 fn get_hash(&self) -> u128 {
53 self.hash.to_u128()
54 }
55}
56
57/// Attributes that is applicable to the [`Image`].
58#[derive(Debug, Clone, Hash, PartialEq, Eq)]
59#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
60#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
61pub enum ImageAttr {
62 /// A text describing the image.
63 Alt(ImmutStr),
64 /// The approach to rendering the image.
65 ImageRendering(ImmutStr),
66}
67
68/// Item representing an `<path/>` element.
69#[derive(Debug, Clone, Hash, PartialEq, Eq)]
70#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
71#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
72pub struct PathItem {
73 /// The path instruction.
74 pub d: ImmutStr,
75 /// bbox of the path.
76 pub size: Option<Size>,
77 /// The path style.
78 /// See [`PathStyle`] for more information.
79 pub styles: Vec<PathStyle>,
80}
81
82/// Attributes that is applicable to the [`PathItem`].
83#[derive(Debug, Clone, Hash, PartialEq, Eq)]
84#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
85#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
86pub enum PathStyle {
87 /// `fill` attribute.
88 /// See <https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill>
89 Fill(ImmutStr),
90
91 /// `stroke` attribute.
92 /// See <https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke>
93 Stroke(ImmutStr),
94
95 /// `stroke-linecap` attribute.
96 /// See <https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-linecap>
97 StrokeLineCap(ImmutStr),
98
99 /// `stroke-linejoin` attribute.
100 /// See <https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-linejoin>
101 StrokeLineJoin(ImmutStr),
102
103 /// `stroke-miterlimit` attribute.
104 /// See <https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-miterlimit>
105 StrokeMitterLimit(Scalar),
106
107 /// `stroke-dashoffset` attribute.
108 /// See <https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dashoffset>
109 StrokeDashOffset(Abs),
110
111 /// `stroke-dasharray` attribute.
112 /// See <https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dasharray>
113 StrokeDashArray(Arc<[Abs]>),
114
115 /// `stroke-width` attribute.
116 /// See <https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-width>
117 StrokeWidth(Abs),
118
119 /// `fill-rule` attribute.
120 /// See <https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill-rule>
121 FillRule(ImmutStr),
122}
123
124/// Item representing an `<pattern/>` element.
125#[derive(Debug, Clone, Hash, PartialEq, Eq)]
126#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
127#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
128pub struct PatternItem {
129 /// The pattern's rendered content.
130 pub frame: Fingerprint,
131 /// The pattern's tile size.
132 pub size: Size,
133 /// The pattern's tile spacing.
134 pub spacing: Size,
135}