use crate::renderer::render_tree::{BoundingBox, ImageNode};
use web_sys::{CanvasRenderingContext2d, Path2d};
pub(super) struct DestinationCoverage {
context: CanvasRenderingContext2d,
}
impl DestinationCoverage {
pub(super) fn for_print(
context: &CanvasRenderingContext2d,
(x, y, width, height): (f64, f64, f64, f64),
profile: crate::paint::RenderProfile,
) -> Option<Self> {
if profile != crate::paint::RenderProfile::Print
|| ![x, y, width, height].into_iter().all(f64::is_finite)
|| width <= 0.0
|| height <= 0.0
{
return None;
}
let matrix = context.get_transform().ok()?;
if matrix.b() != 0.0 || matrix.c() != 0.0 {
return None;
}
let path = Path2d::new().ok()?;
path.rect(x, y, width, height);
context.save();
context.clip_with_path_2d(&path);
Some(Self {
context: context.clone(),
})
}
}
impl Drop for DestinationCoverage {
fn drop(&mut self) {
self.context.restore();
}
}
pub(super) fn bounds(
context: &CanvasRenderingContext2d,
bbox: BoundingBox,
image: &ImageNode,
print: bool,
) -> BoundingBox {
if !print || !crate::paint::device_image::eligible(image) {
return bbox;
}
context
.get_transform()
.ok()
.and_then(|m| {
crate::paint::device_image::bounds(bbox, [m.a(), m.b(), m.c(), m.d(), m.e(), m.f()])
})
.unwrap_or(bbox)
}