[librsvg: 1/6] Pass down opacity as a unit interval reference



commit 7e5d5fc7fc0cd7dc3f06e10dc635563a78db6784
Author: Paolo Borelli <pborelli gnome org>
Date:   Fri Apr 27 16:30:16 2018 +0200

    Pass down opacity as a unit interval reference
    
    Avoid f64 -> u8 -> f64 roundtrip

 rsvg_internals/src/draw.rs         | 12 ++++++------
 rsvg_internals/src/gradient.rs     | 18 ++++++++++--------
 rsvg_internals/src/paint_server.rs |  8 +++++---
 3 files changed, 21 insertions(+), 17 deletions(-)
---
diff --git a/rsvg_internals/src/draw.rs b/rsvg_internals/src/draw.rs
index 864449bf..88304c0f 100644
--- a/rsvg_internals/src/draw.rs
+++ b/rsvg_internals/src/draw.rs
@@ -88,7 +88,7 @@ fn stroke_and_fill(cr: &cairo::Context, draw_ctx: *mut RsvgDrawingCtx) {
         Some(Fill(ref fill)) => paint_server::_set_source_rsvg_paint_server(
             draw_ctx,
             fill,
-            u8::from(fill_opacity),
+            &fill_opacity,
             &extents.bbox,
             &current_color,
         ),
@@ -96,7 +96,7 @@ fn stroke_and_fill(cr: &cairo::Context, draw_ctx: *mut RsvgDrawingCtx) {
         _ => paint_server::_set_source_rsvg_paint_server(
             draw_ctx,
             &Fill::default().0,
-            u8::from(fill_opacity),
+            &fill_opacity,
             &extents.bbox,
             &current_color,
         ),
@@ -119,7 +119,7 @@ fn stroke_and_fill(cr: &cairo::Context, draw_ctx: *mut RsvgDrawingCtx) {
         if paint_server::_set_source_rsvg_paint_server(
             draw_ctx,
             stroke,
-            u8::from(stroke_opacity),
+            &stroke_opacity,
             &extents.bbox,
             &current_color,
         ) {
@@ -428,7 +428,7 @@ pub fn draw_pango_layout(
             Some(Fill(ref fill)) => paint_server::_set_source_rsvg_paint_server(
                 draw_ctx,
                 fill,
-                u8::from(fill_opacity),
+                &fill_opacity,
                 &bbox,
                 &current_color,
             ),
@@ -436,7 +436,7 @@ pub fn draw_pango_layout(
             _ => paint_server::_set_source_rsvg_paint_server(
                 draw_ctx,
                 &Fill::default().0,
-                u8::from(fill_opacity),
+                &fill_opacity,
                 &bbox,
                 &current_color,
             ),
@@ -460,7 +460,7 @@ pub fn draw_pango_layout(
             if paint_server::_set_source_rsvg_paint_server(
                 draw_ctx,
                 stroke,
-                u8::from(stroke_opacity),
+                &stroke_opacity,
                 &bbox,
                 &current_color,
             ) {
diff --git a/rsvg_internals/src/gradient.rs b/rsvg_internals/src/gradient.rs
index 73a10123..579267b6 100644
--- a/rsvg_internals/src/gradient.rs
+++ b/rsvg_internals/src/gradient.rs
@@ -16,6 +16,7 @@ use paint_server::*;
 use parsers::{parse, Parse};
 use property_bag::PropertyBag;
 use stop::*;
+use unitinterval::UnitInterval;
 use util::*;
 
 #[derive(Copy, Clone)]
@@ -325,16 +326,17 @@ impl Gradient {
         self.common.add_color_stop(offset, rgba);
     }
 
-    fn add_color_stops_to_pattern(&self, pattern: &mut cairo::Gradient, opacity: u8) {
+    fn add_color_stops_to_pattern(&self, pattern: &mut cairo::Gradient, opacity: &UnitInterval) {
         if let Some(stops) = self.common.stops.as_ref() {
             for stop in stops {
                 let rgba = stop.rgba;
+                let &UnitInterval(o) = opacity;
                 pattern.add_color_stop_rgba(
                     stop.offset,
                     (f64::from((rgba >> 24) & 0xff)) / 255.0,
                     (f64::from((rgba >> 16) & 0xff)) / 255.0,
                     (f64::from((rgba >> 8) & 0xff)) / 255.0,
-                    f64::from((rgba & 0xff) * u32::from(opacity)) / 255.0 / 255.0,
+                    (f64::from(rgba & 0xff) * o) / 255.0,
                 );
             }
         }
@@ -387,7 +389,7 @@ fn set_common_on_pattern<P: cairo::Pattern + cairo::Gradient>(
     draw_ctx: *mut RsvgDrawingCtx,
     pattern: &mut P,
     bbox: &RsvgBbox,
-    opacity: u8,
+    opacity: &UnitInterval,
 ) {
     let cr = drawing_ctx::get_cairo_context(draw_ctx);
 
@@ -420,7 +422,7 @@ fn set_linear_gradient_on_pattern(
     gradient: &Gradient,
     draw_ctx: *mut RsvgDrawingCtx,
     bbox: &RsvgBbox,
-    opacity: u8,
+    opacity: &UnitInterval,
 ) -> bool {
     if let GradientVariant::Linear { x1, y1, x2, y2 } = gradient.variant {
         let units = gradient.common.units.unwrap();
@@ -499,7 +501,7 @@ fn set_radial_gradient_on_pattern(
     gradient: &Gradient,
     draw_ctx: *mut RsvgDrawingCtx,
     bbox: &RsvgBbox,
-    opacity: u8,
+    opacity: &UnitInterval,
 ) -> bool {
     if let GradientVariant::Radial { cx, cy, r, fx, fy } = gradient.variant {
         let units = gradient.common.units.unwrap();
@@ -533,7 +535,7 @@ fn set_radial_gradient_on_pattern(
 fn set_pattern_on_draw_context(
     gradient: &Gradient,
     draw_ctx: *mut RsvgDrawingCtx,
-    opacity: u8,
+    opacity: &UnitInterval,
     bbox: &RsvgBbox,
 ) -> bool {
     assert!(gradient.is_resolved());
@@ -679,7 +681,7 @@ pub extern "C" fn rsvg_node_radial_gradient_new(
 fn resolve_fallbacks_and_set_pattern(
     gradient: &Gradient,
     draw_ctx: *mut RsvgDrawingCtx,
-    opacity: u8,
+    opacity: &UnitInterval,
     bbox: &RsvgBbox,
 ) -> bool {
     if bbox.is_empty() {
@@ -694,7 +696,7 @@ fn resolve_fallbacks_and_set_pattern(
 pub fn gradient_resolve_fallbacks_and_set_pattern(
     node: &RsvgNode,
     draw_ctx: *mut RsvgDrawingCtx,
-    opacity: u8,
+    opacity: &UnitInterval,
     bbox: &RsvgBbox,
 ) -> bool {
     assert!(
diff --git a/rsvg_internals/src/paint_server.rs b/rsvg_internals/src/paint_server.rs
index 5617bff0..4a60e594 100644
--- a/rsvg_internals/src/paint_server.rs
+++ b/rsvg_internals/src/paint_server.rs
@@ -8,6 +8,7 @@ use gradient;
 use node::NodeType;
 use parsers::{Parse, ParseError};
 use pattern;
+use unitinterval::UnitInterval;
 
 #[derive(Debug, Copy, Clone, PartialEq)]
 pub struct PaintServerSpread(pub cairo::enums::Extend);
@@ -80,7 +81,7 @@ impl Parse for PaintServer {
 fn _set_source_rsvg_solid_color(
     ctx: *mut drawing_ctx::RsvgDrawingCtx,
     color: &cssparser::Color,
-    opacity: u8,
+    opacity: &UnitInterval,
     current_color: &cssparser::RGBA,
 ) {
     let rgba = match *color {
@@ -88,18 +89,19 @@ fn _set_source_rsvg_solid_color(
         cssparser::Color::CurrentColor => current_color,
     };
 
+    let &UnitInterval(o) = opacity;
     drawing_ctx::get_cairo_context(ctx).set_source_rgba(
         f64::from(rgba.red_f32()),
         f64::from(rgba.green_f32()),
         f64::from(rgba.blue_f32()),
-        f64::from(rgba.alpha_f32()) * (f64::from(opacity) / 255.0),
+        f64::from(rgba.alpha_f32()) * o,
     );
 }
 
 pub fn _set_source_rsvg_paint_server(
     c_ctx: *mut drawing_ctx::RsvgDrawingCtx,
     ps: &PaintServer,
-    opacity: u8,
+    opacity: &UnitInterval,
     bbox: &RsvgBbox,
     current_color: &cssparser::RGBA,
 ) -> bool {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]