Upgrade resvg to 0.34 and tiny_skia to 0.10

This commit is contained in:
Alec Deason 2023-06-10 13:18:42 -07:00 committed by Héctor Ramón Jiménez
parent 9f2be29a28
commit af386fd0a3
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
7 changed files with 85 additions and 51 deletions

View file

@ -1,6 +1,7 @@
use crate::core::svg::{Data, Handle};
use crate::core::{Color, Rectangle, Size};
use bytemuck::cast;
use resvg::usvg;
use rustc_hash::{FxHashMap, FxHashSet};
@ -130,30 +131,41 @@ impl Cache {
let mut image = tiny_skia::Pixmap::new(size.width, size.height)?;
resvg::render(
tree,
if size.width > size.height {
resvg::FitTo::Width(size.width)
} else {
resvg::FitTo::Height(size.height)
},
tiny_skia::Transform::default(),
image.as_mut(),
)?;
let tree_size = tree.size.to_int_size();
let target_size;
if size.width > size.height {
target_size = tree_size.scale_to_width(size.width);
} else {
target_size = tree_size.scale_to_height(size.height);
}
let transform;
if let Some(target_size) = target_size {
let tree_size = tree_size.to_size();
let target_size = target_size.to_size();
transform = tiny_skia::Transform::from_scale(
target_size.width() / tree_size.width(),
target_size.height() / tree_size.height(),
);
} else {
transform = tiny_skia::Transform::default();
}
resvg::Tree::from_usvg(tree).render(transform, &mut image.as_mut());
if let Some([r, g, b, _]) = key.color {
// Apply color filter
for pixel in
bytemuck::cast_slice_mut::<u8, u32>(image.data_mut())
{
*pixel = tiny_skia::ColorU8::from_rgba(
b,
g,
r,
(*pixel >> 24) as u8,
)
.premultiply()
.get();
*pixel = cast(
tiny_skia::ColorU8::from_rgba(
b,
g,
r,
(*pixel >> 24) as u8,
)
.premultiply(),
);
}
} else {
// Swap R and B channels for `softbuffer` presentation