use super::*;
impl DisplayItem {
pub fn kind(&self) -> DrawKind {
match self.get_cmd() {
DrawCmd::Fill { .. } => DrawKind::Fill,
DrawCmd::Stroke { .. } => DrawKind::Stroke,
DrawCmd::Text { .. } => DrawKind::Text,
DrawCmd::Image { .. } => DrawKind::Image,
DrawCmd::Clip { .. } => DrawKind::Clip,
DrawCmd::PopClip => DrawKind::PopClip,
DrawCmd::Strip { .. } => DrawKind::Strip,
}
}
}
impl DisplayList {
pub fn new() -> Self {
Self {
items: Vec::new(),
bounds: BBox::empty(),
}
}
pub fn items(&self) -> &[DisplayItem] {
self.get_items()
}
pub fn len(&self) -> usize {
self.get_items().len()
}
pub fn is_empty(&self) -> bool {
self.get_items().is_empty()
}
pub fn bounds(&self) -> BBox {
*self.get_bounds()
}
pub fn clear(&mut self) {
self.get_mut_items().clear();
self.set_bounds(BBox::empty());
}
pub fn fill(&mut self, path: Path, paint: Fill, opacity: f64, z: i32) {
self.get_mut_bounds().extend_box(&path.bounds());
self.get_mut_items().push(DisplayItem {
cmd: DrawCmd::Fill {
path,
paint,
opacity,
},
z,
element_id: 0,
series_index: None,
data_index: None,
});
}
pub fn stroke(&mut self, path: Path, paint: Stroke, style: LineStyle, opacity: f64, z: i32) {
self.get_mut_bounds().extend_box(&path.bounds());
self.get_mut_items().push(DisplayItem {
cmd: DrawCmd::Stroke {
path,
paint,
style,
opacity,
},
z,
element_id: 0,
series_index: None,
data_index: None,
});
}
pub fn text(&mut self, text: String, x: f64, y: f64, style: TextStyle, z: i32) {
self.get_mut_items().push(DisplayItem {
cmd: DrawCmd::Text { text, x, y, style },
z,
element_id: 0,
series_index: None,
data_index: None,
});
}
pub fn image(
&mut self,
image: HtmlImageElement,
x: f64,
y: f64,
width: f64,
height: f64,
z: i32,
) {
self.get_mut_items().push(DisplayItem {
cmd: DrawCmd::Image {
image,
x,
y,
width,
height,
},
z,
element_id: 0,
series_index: None,
data_index: None,
});
}
pub fn clip(&mut self, path: Path, z: i32) {
self.get_mut_bounds().extend_box(&path.bounds());
self.get_mut_items().push(DisplayItem {
cmd: DrawCmd::Clip { path },
z,
element_id: 0,
series_index: None,
data_index: None,
});
}
pub fn pop_clip(&mut self, z: i32) {
self.get_mut_items().push(DisplayItem {
cmd: DrawCmd::PopClip,
z,
element_id: 0,
series_index: None,
data_index: None,
});
}
pub fn strip(&mut self, points: Vec<Point>, baseline: f32, paint: Fill, opacity: f64, z: i32) {
for point in &points {
self.get_mut_bounds()
.extend_point(point.x as f64, point.y as f64);
}
self.get_mut_bounds().extend_point(0.0, baseline as f64);
self.get_mut_items().push(DisplayItem {
cmd: DrawCmd::Strip {
points,
baseline,
paint,
opacity,
},
z,
element_id: 0,
series_index: None,
data_index: None,
});
}
pub fn mark(&mut self, index: usize, element_id: u32, series_index: usize, data_index: usize) {
if let Some(item) = self.get_mut_items().get_mut(index) {
item.set_element_id(element_id);
item.set_series_index(Some(series_index));
item.set_data_index(Some(data_index));
}
}
}