Skip to main content

euv_chart/display/
impl.rs

1use super::*;
2
3/// Inherent implementation of [`DisplayItem`].
4impl DisplayItem {
5    /// Returns the kind of the command.
6    ///
7    /// # Returns
8    ///
9    /// - `DrawKind` - A `DrawKind` value.
10    pub fn kind(&self) -> DrawKind {
11        match self.get_cmd() {
12            DrawCmd::Fill { .. } => DrawKind::Fill,
13            DrawCmd::Stroke { .. } => DrawKind::Stroke,
14            DrawCmd::Text { .. } => DrawKind::Text,
15            DrawCmd::Image { .. } => DrawKind::Image,
16            DrawCmd::Clip { .. } => DrawKind::Clip,
17            DrawCmd::PopClip => DrawKind::PopClip,
18            DrawCmd::Strip { .. } => DrawKind::Strip,
19        }
20    }
21}
22
23/// Inherent implementation of [`DisplayList`].
24impl DisplayList {
25    /// Returns an empty display list.
26    pub fn new() -> Self {
27        Self {
28            items: Vec::new(),
29            bounds: BBox::empty(),
30        }
31    }
32
33    /// Returns the items, in paint order.
34    ///
35    /// # Returns
36    ///
37    /// - `&[DisplayItem]` - A `[DisplayItem]` value.
38    pub fn items(&self) -> &[DisplayItem] {
39        self.get_items()
40    }
41
42    /// Returns the number of items in the list.
43    ///
44    /// # Returns
45    ///
46    /// - `usize` - A non-negative integer.
47    pub fn len(&self) -> usize {
48        self.get_items().len()
49    }
50
51    /// Returns `true` when the list holds no items.
52    ///
53    /// # Returns
54    ///
55    /// - `bool` - A boolean.
56    pub fn is_empty(&self) -> bool {
57        self.get_items().is_empty()
58    }
59
60    /// Returns the bounds of everything in the list.
61    ///
62    /// # Returns
63    ///
64    /// - `BBox` - A `BBox` value.
65    pub fn bounds(&self) -> BBox {
66        *self.get_bounds()
67    }
68
69    /// Removes every item from the list, keeping the allocated capacity.
70    pub fn clear(&mut self) {
71        self.get_mut_items().clear();
72        self.set_bounds(BBox::empty());
73    }
74
75    /// Adds a filled path.
76    ///
77    /// # Arguments
78    ///
79    /// - `Path` - A `Path` parameter.
80    /// - `Fill` - A `Fill` parameter.
81    /// - `f64` - A 64-bit float (`f64`).
82    /// - `i32` - A 32-bit signed integer (`i32`).
83    pub fn fill(&mut self, path: Path, paint: Fill, opacity: f64, z: i32) {
84        self.get_mut_bounds().extend_box(&path.bounds());
85        self.get_mut_items().push(DisplayItem {
86            cmd: DrawCmd::Fill {
87                path,
88                paint,
89                opacity,
90            },
91            z,
92            element_id: 0,
93            series_index: None,
94            data_index: None,
95        });
96    }
97
98    /// Adds a stroked path.
99    ///
100    /// # Arguments
101    ///
102    /// - `Path` - A `Path` parameter.
103    /// - `Stroke` - A `Stroke` parameter.
104    /// - `LineStyle` - A `LineStyle` parameter.
105    /// - `f64` - A 64-bit float (`f64`).
106    /// - `i32` - A 32-bit signed integer (`i32`).
107    pub fn stroke(&mut self, path: Path, paint: Stroke, style: LineStyle, opacity: f64, z: i32) {
108        self.get_mut_bounds().extend_box(&path.bounds());
109        self.get_mut_items().push(DisplayItem {
110            cmd: DrawCmd::Stroke {
111                path,
112                paint,
113                style,
114                opacity,
115            },
116            z,
117            element_id: 0,
118            series_index: None,
119            data_index: None,
120        });
121    }
122
123    /// Adds a text run.
124    ///
125    /// # Arguments
126    ///
127    /// - `String` - A `String` parameter.
128    /// - `f64` - A 64-bit float (`f64`).
129    /// - `TextStyle` - A `TextStyle` parameter.
130    /// - `i32` - A 32-bit signed integer (`i32`).
131    pub fn text(&mut self, text: String, x: f64, y: f64, style: TextStyle, z: i32) {
132        self.get_mut_items().push(DisplayItem {
133            cmd: DrawCmd::Text { text, x, y, style },
134            z,
135            element_id: 0,
136            series_index: None,
137            data_index: None,
138        });
139    }
140
141    /// Adds an image.
142    ///
143    /// # Arguments
144    ///
145    /// - `HtmlImageElement` - A `HtmlImageElement` parameter.
146    /// - `f64` - A 64-bit float (`f64`).
147    /// - `i32` - A 32-bit signed integer (`i32`).
148    pub fn image(
149        &mut self,
150        image: HtmlImageElement,
151        x: f64,
152        y: f64,
153        width: f64,
154        height: f64,
155        z: i32,
156    ) {
157        self.get_mut_items().push(DisplayItem {
158            cmd: DrawCmd::Image {
159                image,
160                x,
161                y,
162                width,
163                height,
164            },
165            z,
166            element_id: 0,
167            series_index: None,
168            data_index: None,
169        });
170    }
171
172    /// Adds a clip instruction.
173    ///
174    /// # Arguments
175    ///
176    /// - `Path` - A `Path` parameter.
177    /// - `i32` - A 32-bit signed integer (`i32`).
178    pub fn clip(&mut self, path: Path, z: i32) {
179        self.get_mut_bounds().extend_box(&path.bounds());
180        self.get_mut_items().push(DisplayItem {
181            cmd: DrawCmd::Clip { path },
182            z,
183            element_id: 0,
184            series_index: None,
185            data_index: None,
186        });
187    }
188
189    /// Adds a clip-restore instruction.
190    ///
191    /// # Arguments
192    ///
193    /// - `i32` - A 32-bit signed integer (`i32`).
194    pub fn pop_clip(&mut self, z: i32) {
195        self.get_mut_items().push(DisplayItem {
196            cmd: DrawCmd::PopClip,
197            z,
198            element_id: 0,
199            series_index: None,
200            data_index: None,
201        });
202    }
203
204    /// Adds a filled triangle strip.
205    ///
206    /// # Arguments
207    ///
208    /// - `Vec<Point>` - A `Vec<Point>` parameter.
209    /// - `f32` - A 32-bit float (`f32`).
210    /// - `Fill` - A `Fill` parameter.
211    /// - `f64` - A 64-bit float (`f64`).
212    /// - `i32` - A 32-bit signed integer (`i32`).
213    pub fn strip(&mut self, points: Vec<Point>, baseline: f32, paint: Fill, opacity: f64, z: i32) {
214        for point in &points {
215            self.get_mut_bounds()
216                .extend_point(point.x as f64, point.y as f64);
217        }
218        self.get_mut_bounds().extend_point(0.0, baseline as f64);
219        self.get_mut_items().push(DisplayItem {
220            cmd: DrawCmd::Strip {
221                points,
222                baseline,
223                paint,
224                opacity,
225            },
226            z,
227            element_id: 0,
228            series_index: None,
229            data_index: None,
230        });
231    }
232
233    /// Marks the item at the given index as belonging to a series and a
234    /// data point.
235    ///
236    /// # Arguments
237    ///
238    /// - `usize` - A non-negative integer (`usize`).
239    /// - `u32` - A 32-bit unsigned integer (`u32`).
240    pub fn mark(&mut self, index: usize, element_id: u32, series_index: usize, data_index: usize) {
241        if let Some(item) = self.get_mut_items().get_mut(index) {
242            item.set_element_id(element_id);
243            item.set_series_index(Some(series_index));
244            item.set_data_index(Some(data_index));
245        }
246    }
247}