Skip to content

Commit

Permalink
Upgrade resvg to 0.34 and tiny_skia to 0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
alec-deason committed Jun 10, 2023
1 parent 60cd864 commit 4985e41
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 52 deletions.
2 changes: 1 addition & 1 deletion graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ version = "0.9"
path = "../core"

[dependencies.tiny-skia]
version = "0.9"
version = "0.10"
optional = true

[dependencies.image]
Expand Down
4 changes: 2 additions & 2 deletions tiny_skia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ geometry = ["iced_graphics/geometry"]
[dependencies]
raw-window-handle = "0.5"
softbuffer = "0.2"
tiny-skia = "0.9"
tiny-skia = "0.10"
bytemuck = "1"
rustc-hash = "1.1"
kurbo = "0.9"
Expand All @@ -35,5 +35,5 @@ version = "1.6.1"
features = ["std"]

[dependencies.resvg]
version = "0.32"
version = "0.34"
optional = true
10 changes: 9 additions & 1 deletion tiny_skia/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,15 @@ fn adjust_clip_mask(clip_mask: &mut tiny_skia::Mask, bounds: Rectangle) {

let path = {
let mut builder = tiny_skia::PathBuilder::new();
builder.push_rect(bounds.x, bounds.y, bounds.width, bounds.height);
builder.push_rect(
tiny_skia::Rect::from_xywh(
bounds.x,
bounds.y,
bounds.width,
bounds.height,
)
.unwrap(),
);

builder.finish().unwrap()
};
Expand Down
6 changes: 3 additions & 3 deletions tiny_skia/src/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::core::image as raster;
use crate::core::{Rectangle, Size};
use crate::graphics;

use bytemuck::cast;
use rustc_hash::{FxHashMap, FxHashSet};
use std::cell::RefCell;
use std::collections::hash_map;
Expand Down Expand Up @@ -80,9 +81,8 @@ impl Cache {
for (i, pixel) in image.pixels().enumerate() {
let [r, g, b, a] = pixel.0;

buffer[i] = tiny_skia::ColorU8::from_rgba(b, g, r, a)
.premultiply()
.get();
buffer[i] = cast(tiny_skia::ColorU8::from_rgba(b, g, r, a)
.premultiply());
}

entry.insert(Some(Entry {
Expand Down
35 changes: 19 additions & 16 deletions tiny_skia/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::core::font::{self, Font};
use crate::core::text::{Hit, LineHeight, Shaping};
use crate::core::{Color, Pixels, Point, Rectangle, Size};

use bytemuck::cast;
use rustc_hash::{FxHashMap, FxHashSet};
use std::borrow::Cow;
use std::cell::RefCell;
Expand Down Expand Up @@ -304,14 +305,15 @@ impl GlyphCache {

for _y in 0..image.placement.height {
for _x in 0..image.placement.width {
buffer[i] = tiny_skia::ColorU8::from_rgba(
b,
g,
r,
image.data[i],
)
.premultiply()
.get();
buffer[i] = cast(
tiny_skia::ColorU8::from_rgba(
b,
g,
r,
image.data[i],
)
.premultiply(),
);

i += 1;
}
Expand All @@ -323,14 +325,15 @@ impl GlyphCache {
for _y in 0..image.placement.height {
for _x in 0..image.placement.width {
// TODO: Blend alpha
buffer[i >> 2] = tiny_skia::ColorU8::from_rgba(
image.data[i + 2],
image.data[i + 1],
image.data[i],
image.data[i + 3],
)
.premultiply()
.get();
buffer[i >> 2] = cast(
tiny_skia::ColorU8::from_rgba(
image.data[i + 2],
image.data[i + 1],
image.data[i],
image.data[i + 3],
)
.premultiply(),
);

i += 4;
}
Expand Down
48 changes: 30 additions & 18 deletions tiny_skia/src/vector.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ version = "1.0"
optional = true

[dependencies.resvg]
version = "0.32"
version = "0.34"
optional = true

[dependencies.tracing]
Expand Down
31 changes: 21 additions & 10 deletions wgpu/src/image/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,27 @@ impl Cache {
// It would be cool to be able to smooth resize the `svg` example.
let mut img = tiny_skia::Pixmap::new(width, height)?;

resvg::render(
tree,
if width > height {
resvg::FitTo::Width(width)
} else {
resvg::FitTo::Height(height)
},
tiny_skia::Transform::default(),
img.as_mut(),
)?;
let tree_size = tree.size.to_int_size();
let target_size;
if width > height {
target_size = tree_size.scale_to_width(width);
} else {
target_size = tree_size.scale_to_height(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 img.as_mut());

let mut rgba = img.take();

Expand Down

0 comments on commit 4985e41

Please sign in to comment.