From fa9e1d96ea1924b51749b775ea0e67e69bc8a305 Mon Sep 17 00:00:00 2001
From: Héctor Ramón Jiménez <hector@hecrj.dev>
Date: Fri, 3 May 2024 13:25:58 +0200
Subject: Introduce dynamic `opacity` support for `Image` and `Svg`

---
 tiny_skia/src/engine.rs |  4 ++++
 tiny_skia/src/layer.rs  |  4 ++++
 tiny_skia/src/lib.rs    | 12 +++++++++++-
 tiny_skia/src/raster.rs |  2 ++
 tiny_skia/src/vector.rs |  6 +++++-
 5 files changed, 26 insertions(+), 2 deletions(-)

(limited to 'tiny_skia')

diff --git a/tiny_skia/src/engine.rs b/tiny_skia/src/engine.rs
index 544ff614..028b304f 100644
--- a/tiny_skia/src/engine.rs
+++ b/tiny_skia/src/engine.rs
@@ -551,6 +551,7 @@ impl Engine {
                 filter_method,
                 bounds,
                 rotation,
+                opacity,
             } => {
                 let physical_bounds = *bounds * _transformation;
 
@@ -574,6 +575,7 @@ impl Engine {
                     handle,
                     *filter_method,
                     *bounds,
+                    *opacity,
                     _pixels,
                     transform,
                     clip_mask,
@@ -585,6 +587,7 @@ impl Engine {
                 color,
                 bounds,
                 rotation,
+                opacity,
             } => {
                 let physical_bounds = *bounds * _transformation;
 
@@ -608,6 +611,7 @@ impl Engine {
                     handle,
                     *color,
                     physical_bounds,
+                    *opacity,
                     _pixels,
                     transform,
                     clip_mask,
diff --git a/tiny_skia/src/layer.rs b/tiny_skia/src/layer.rs
index c907c93c..48fca1d8 100644
--- a/tiny_skia/src/layer.rs
+++ b/tiny_skia/src/layer.rs
@@ -122,12 +122,14 @@ impl Layer {
         bounds: Rectangle,
         transformation: Transformation,
         rotation: Radians,
+        opacity: f32,
     ) {
         let image = Image::Raster {
             handle,
             filter_method,
             bounds: bounds * transformation,
             rotation,
+            opacity,
         };
 
         self.images.push(image);
@@ -140,12 +142,14 @@ impl Layer {
         bounds: Rectangle,
         transformation: Transformation,
         rotation: Radians,
+        opacity: f32,
     ) {
         let svg = Image::Vector {
             handle,
             color,
             bounds: bounds * transformation,
             rotation,
+            opacity,
         };
 
         self.images.push(svg);
diff --git a/tiny_skia/src/lib.rs b/tiny_skia/src/lib.rs
index e0cbfa0d..1aabff00 100644
--- a/tiny_skia/src/lib.rs
+++ b/tiny_skia/src/lib.rs
@@ -378,6 +378,7 @@ impl core::image::Renderer for Renderer {
         filter_method: core::image::FilterMethod,
         bounds: Rectangle,
         rotation: core::Radians,
+        opacity: f32,
     ) {
         let (layer, transformation) = self.layers.current_mut();
         layer.draw_image(
@@ -386,6 +387,7 @@ impl core::image::Renderer for Renderer {
             bounds,
             transformation,
             rotation,
+            opacity,
         );
     }
 }
@@ -405,9 +407,17 @@ impl core::svg::Renderer for Renderer {
         color: Option<Color>,
         bounds: Rectangle,
         rotation: core::Radians,
+        opacity: f32,
     ) {
         let (layer, transformation) = self.layers.current_mut();
-        layer.draw_svg(handle, color, bounds, transformation, rotation);
+        layer.draw_svg(
+            handle,
+            color,
+            bounds,
+            transformation,
+            rotation,
+            opacity,
+        );
     }
 }
 
diff --git a/tiny_skia/src/raster.rs b/tiny_skia/src/raster.rs
index 907fce7c..c40f55b2 100644
--- a/tiny_skia/src/raster.rs
+++ b/tiny_skia/src/raster.rs
@@ -31,6 +31,7 @@ impl Pipeline {
         handle: &raster::Handle,
         filter_method: raster::FilterMethod,
         bounds: Rectangle,
+        opacity: f32,
         pixels: &mut tiny_skia::PixmapMut<'_>,
         transform: tiny_skia::Transform,
         clip_mask: Option<&tiny_skia::Mask>,
@@ -56,6 +57,7 @@ impl Pipeline {
                 image,
                 &tiny_skia::PixmapPaint {
                     quality,
+                    opacity,
                     ..Default::default()
                 },
                 transform,
diff --git a/tiny_skia/src/vector.rs b/tiny_skia/src/vector.rs
index 8e3463f2..bbe08cb8 100644
--- a/tiny_skia/src/vector.rs
+++ b/tiny_skia/src/vector.rs
@@ -34,6 +34,7 @@ impl Pipeline {
         handle: &Handle,
         color: Option<Color>,
         bounds: Rectangle,
+        opacity: f32,
         pixels: &mut tiny_skia::PixmapMut<'_>,
         transform: Transform,
         clip_mask: Option<&tiny_skia::Mask>,
@@ -47,7 +48,10 @@ impl Pipeline {
                 bounds.x as i32,
                 bounds.y as i32,
                 image,
-                &tiny_skia::PixmapPaint::default(),
+                &tiny_skia::PixmapPaint {
+                    opacity,
+                    ..tiny_skia::PixmapPaint::default()
+                },
                 transform,
                 clip_mask,
             );
-- 
cgit