use resvg::{tiny_skia, usvg};
fn contains_image(group: &usvg::Group) -> bool {
group.children().iter().any(|node| match node {
usvg::Node::Image(_) => true,
usvg::Node::Group(group) => contains_image(group),
_ => false,
})
}
fn uses_image_pattern(group: &usvg::Group) -> bool {
group.children().iter().any(|node| match node {
usvg::Node::Group(group) => uses_image_pattern(group),
usvg::Node::Text(text) => uses_image_pattern(text.flattened()),
usvg::Node::Path(path) => path
.fill()
.map(|fill| fill.paint())
.into_iter()
.chain(path.stroke().map(|stroke| stroke.paint()))
.any(|paint| matches!(paint, usvg::Paint::Pattern(pattern) if contains_image(pattern.root()))),
_ => false,
})
}
pub(super) fn rasterize_before_reduction(
tree: &usvg::Tree,
width: u32,
height: u32,
transform: tiny_skia::Transform,
pixel_limit: u64,
) -> Option<Vec<u8>> {
let size = tree.size();
if (size.width() <= width as f32 && size.height() <= height as f32)
|| !tree
.masks()
.iter()
.any(|mask| uses_image_pattern(mask.root()))
{
return None;
}
if !(transform.sx > 0.0 && transform.sy > 0.0) {
return None;
}
let source_width = ((width as f32 / transform.sx).ceil() as u64).max(u64::from(width));
let source_height = ((height as f32 / transform.sy).ceil() as u64).max(u64::from(height));
if source_width.checked_mul(source_height)? > pixel_limit {
return None;
}
let source_width = u32::try_from(source_width).ok()?;
let source_height = u32::try_from(source_height).ok()?;
let mut pixmap = tiny_skia::Pixmap::new(source_width, source_height)?;
let transform = tiny_skia::Transform::from_translate(
(transform.tx / transform.sx).round(),
(transform.ty / transform.sy).round(),
);
resvg::render(tree, transform, &mut pixmap.as_mut());
encode_reduced(&pixmap, width, height)
}
pub(super) fn encode_reduced(
pixmap: &tiny_skia::Pixmap,
width: u32,
height: u32,
) -> Option<Vec<u8>> {
let source = image::ImageBuffer::<image::Rgba<u8>, _>::from_raw(
pixmap.width(),
pixmap.height(),
pixmap.data(),
)?;
let mut reduced = image::imageops::resize(
&source,
width,
height,
image::imageops::FilterType::Lanczos3,
);
for pixel in reduced.pixels_mut() {
for channel in 0..3 {
pixel[channel] = pixel[channel].min(pixel[3]);
}
}
let mut output = tiny_skia::Pixmap::new(width, height)?;
output.data_mut().copy_from_slice(reduced.as_raw());
output.encode_png().ok()
}