From a28d00013c442076326eb1f5088f0f1ed9847413 Mon Sep 17 00:00:00 2001 From: Bastien Orivel Date: Thu, 8 Aug 2019 23:46:52 +0200 Subject: [PATCH] Implement set_shadow_color, set_global_composition and stroke_rect --- components/canvas/raqote_backend.rs | 105 +++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 10 deletions(-) diff --git a/components/canvas/raqote_backend.rs b/components/canvas/raqote_backend.rs index 0c8c6f5b1ac..d4675f2c2dc 100644 --- a/components/canvas/raqote_backend.rs +++ b/components/canvas/raqote_backend.rs @@ -34,8 +34,8 @@ impl Backend for RaqoteBackend { } } - fn set_shadow_color<'a>(&mut self, _color: RGBA, _state: &mut CanvasPaintState<'a>) { - unimplemented!() + fn set_shadow_color<'a>(&mut self, color: RGBA, state: &mut CanvasPaintState<'a>) { + state.shadow_color = Color::Raqote(color.to_raqote_style()); } fn set_fill_style<'a>( @@ -62,10 +62,10 @@ impl Backend for RaqoteBackend { fn set_global_composition<'a>( &mut self, - _op: CompositionOrBlending, - _state: &mut CanvasPaintState<'a>, + op: CompositionOrBlending, + state: &mut CanvasPaintState<'a>, ) { - unimplemented!() + state.draw_options.as_raqote_mut().blend_mode = op.to_raqote_style(); } fn create_drawtarget(&self, size: Size2D) -> Box { @@ -168,6 +168,11 @@ impl DrawOptions { DrawOptions::Raqote(options) => options, } } + fn as_raqote_mut(&mut self) -> &mut raqote::DrawOptions { + match self { + DrawOptions::Raqote(options) => options, + } + } } impl Path { @@ -364,12 +369,24 @@ impl GenericDrawTarget for raqote::DrawTarget { } fn stroke_rect( &mut self, - _rect: &Rect, - _pattern: Pattern, - _stroke_options: &StrokeOptions, - _draw_options: &DrawOptions, + rect: &Rect, + pattern: Pattern, + stroke_options: &StrokeOptions, + draw_options: &DrawOptions, ) { - unimplemented!(); + let mut pb = raqote::PathBuilder::new(); + pb.rect( + rect.origin.x, + rect.origin.y, + rect.size.width, + rect.size.height, + ); + + self.stroke(&pb.finish(), + pattern.as_raqote(), + stroke_options.as_raqote(), + draw_options.as_raqote(), + ); } #[allow(unsafe_code)] fn snapshot_data(&self, f: &dyn Fn(&[u8]) -> Vec) -> Vec { @@ -534,3 +551,71 @@ impl Color { } } } + +impl ToRaqoteStyle for RGBA { + type Target = raqote::SolidSource; + + fn to_raqote_style(self) -> Self::Target { + raqote::SolidSource { + r: self.red, + g: self.green, + b: self.blue, + a: self.alpha, + } + } +} + +impl ToRaqoteStyle for CompositionOrBlending { + type Target = raqote::BlendMode; + + fn to_raqote_style(self) -> Self::Target { + match self { + CompositionOrBlending::Composition(op) => op.to_raqote_style(), + CompositionOrBlending::Blending(op) => op.to_raqote_style(), + } + } +} + +impl ToRaqoteStyle for BlendingStyle { + type Target = raqote::BlendMode; + + fn to_raqote_style(self) -> Self::Target { + match self { + BlendingStyle::Multiply => raqote::BlendMode::Multiply, + BlendingStyle::Screen => raqote::BlendMode::Screen, + BlendingStyle::Overlay => raqote::BlendMode::Overlay, + BlendingStyle::Darken => raqote::BlendMode::Darken, + BlendingStyle::Lighten => raqote::BlendMode::Lighten, + BlendingStyle::ColorDodge => raqote::BlendMode::ColorDodge, + BlendingStyle::HardLight => raqote::BlendMode::HardLight, + BlendingStyle::SoftLight => raqote::BlendMode::SoftLight, + BlendingStyle::Difference => raqote::BlendMode::Difference, + BlendingStyle::Exclusion => raqote::BlendMode::Exclusion, + BlendingStyle::Hue => raqote::BlendMode::Hue, + BlendingStyle::Saturation => raqote::BlendMode::Saturation, + BlendingStyle::Color => raqote::BlendMode::Color, + BlendingStyle::Luminosity => raqote::BlendMode::Luminosity, + BlendingStyle::ColorBurn => unimplemented!("raqote doesn't support colorburn"), + } + } +} + +impl ToRaqoteStyle for CompositionStyle { + type Target = raqote::BlendMode; + + fn to_raqote_style(self) -> Self::Target { + match self { + CompositionStyle::SrcIn => raqote::BlendMode::SrcIn, + CompositionStyle::SrcOut => raqote::BlendMode::SrcOut, + CompositionStyle::SrcOver => raqote::BlendMode::SrcOver, + CompositionStyle::SrcAtop => raqote::BlendMode::SrcAtop, + CompositionStyle::DestIn => raqote::BlendMode::DstIn, + CompositionStyle::DestOut => raqote::BlendMode::DstOut, + CompositionStyle::DestOver => raqote::BlendMode::DstOver, + CompositionStyle::DestAtop => raqote::BlendMode::DstAtop, + CompositionStyle::Copy => raqote::BlendMode::Src, + CompositionStyle::Lighter => raqote::BlendMode::Add, + CompositionStyle::Xor => raqote::BlendMode::Xor, + } + } +}