use wasm_bindgen::JsValue;
use web_sys::CanvasRenderingContext2d;
pub(super) fn draw_metafile_surface(
context: &CanvasRenderingContext2d,
bytes: &[u8],
(x, y, width, height): (f64, f64, f64, f64),
) -> bool {
if !crate::renderer::image_resolver::png_sampling::metafile_surface(bytes) {
return false;
}
let draw = || {
let matrix = context.get_transform().ok()?;
if matrix.b() != 0.0 || matrix.c() != 0.0 || matrix.a() <= 0.0 || matrix.d() <= 0.0 {
return None;
}
let header = crate::renderer::image_header::canvaskit_encoded_image_header(bytes)
.filter(|header| header.is_within_decode_limits())?;
if u64::from(header.width) * u64::from(header.height) > 32 * 1024 * 1024 {
return None;
}
let source = image::load_from_memory_with_format(bytes, image::ImageFormat::Png)
.ok()?
.into_rgba8();
let raster = crate::renderer::mipmap_sampling::rasterize(
source.as_raw(),
source.dimensions(),
(
matrix.a() * x + matrix.e(),
matrix.d() * y + matrix.f(),
matrix.a() * width,
matrix.d() * height,
),
)?;
super::print_image_sampling::replay_raster(
context,
&crate::renderer::print_image_sampling::DeviceRaster {
left: raster.left,
top: raster.top,
width: raster.width,
height: raster.height,
rgba: raster.rgba,
},
)
};
draw().is_some()
}
pub(super) struct ReductionQuality<'a> {
context: &'a CanvasRenderingContext2d,
previous: Option<JsValue>,
}
impl<'a> ReductionQuality<'a> {
pub(super) fn for_source_window(
context: &'a CanvasRenderingContext2d,
source_width: f64,
source_height: f64,
width: f64,
height: f64,
) -> Self {
let needs_wider_filter = context.get_transform().is_ok_and(|matrix| {
let scale_x = matrix.a().hypot(matrix.b());
let scale_y = matrix.c().hypot(matrix.d());
let linear_source_support = 2.0;
scale_x.is_finite()
&& scale_y.is_finite()
&& (source_width > width * scale_x * linear_source_support
|| source_height > height * scale_y * linear_source_support)
});
let key = JsValue::from_str("imageSmoothingQuality");
let previous = needs_wider_filter
.then(|| js_sys::Reflect::get(context.as_ref(), &key).ok())
.flatten()
.filter(JsValue::is_string)
.filter(|_| {
js_sys::Reflect::set(context.as_ref(), &key, &JsValue::from_str("high"))
.unwrap_or(false)
});
Self { context, previous }
}
}
impl Drop for ReductionQuality<'_> {
fn drop(&mut self) {
if let Some(previous) = self.previous.as_ref() {
let _ = js_sys::Reflect::set(
self.context.as_ref(),
&JsValue::from_str("imageSmoothingQuality"),
previous,
);
}
}
}