From 7c6460443c773e3f0ce8d3135c729d4eb9e81829 Mon Sep 17 00:00:00 2001 From: pylbrecht Date: Sat, 18 May 2019 00:16:29 +0200 Subject: [PATCH] Implement GenericDrawTarget for azure_hl::DrawTarget --- components/canvas/canvas_data.rs | 208 +++++++++++++++++++++++++++++-- 1 file changed, 197 insertions(+), 11 deletions(-) diff --git a/components/canvas/canvas_data.rs b/components/canvas/canvas_data.rs index 4f583b6d33b..b33855b9038 100644 --- a/components/canvas/canvas_data.rs +++ b/components/canvas/canvas_data.rs @@ -8,7 +8,7 @@ use azure::azure_hl::SurfacePattern; use azure::azure_hl::{AntialiasMode, AsAzurePoint, CapStyle, JoinStyle}; use azure::azure_hl::{BackendType, DrawTarget}; use azure::azure_hl::{ColorPattern, DrawSurfaceOptions, Filter, PathBuilder}; -use azure::azure_hl::{ExtendMode, LinearGradientPattern, RadialGradientPattern}; +use azure::azure_hl::{LinearGradientPattern, RadialGradientPattern}; use canvas_traits::canvas::*; use cssparser::RGBA; use euclid::{Point2D, Rect, Size2D, Transform2D, Vector2D}; @@ -167,22 +167,28 @@ trait GenericDrawTarget { fn copy_surface(&self, surface: SourceSurface, source: Rect, destination: Point2D); fn create_gradient_stops( &self, - gradient_stops: &[GradientStop], + gradient_stops: Vec, extend_mode: ExtendMode, ) -> GradientStops; fn create_path_builder(&self); fn create_similar_draw_target( &self, - size: Size2D, + size: &Size2D, format: SurfaceFormat, ) -> Box; - fn create_source_surface_from_data(&self); + fn create_source_surface_from_data( + &self, + data: &[u8], + size: Size2D, + stride: i32, + format: SurfaceFormat, + ); fn draw_surface_with_shadow( &self, surface: SourceSurface, dest: &Point2D, color: &Color, - offset: Vector2D, + offset: &Vector2D, sigma: f32, operator: CompositionOp, ); @@ -219,6 +225,165 @@ trait GenericDrawTarget { ); } +impl GenericDrawTarget for azure_hl::DrawTarget { + fn clear_rect(&self, rect: &Rect) { + self.clear_rect(rect as &Rect); + } + + fn copy_surface(&self, surface: SourceSurface, source: Rect, destination: Point2D) { + self.copy_surface(surface.into_azure(), source, destination); + } + + fn create_gradient_stops( + &self, + gradient_stops: Vec, + extend_mode: ExtendMode, + ) -> GradientStops { + let gradient_stops: Vec = + gradient_stops.into_iter().map(|x| x.into_azure()).collect(); + GradientStops::Azure(self.create_gradient_stops(&gradient_stops, extend_mode.into_azure())) + } + + fn create_path_builder(&self) { + self.create_path_builder(); + } + + fn create_similar_draw_target( + &self, + size: &Size2D, + format: SurfaceFormat, + ) -> Box { + Box::new(self.create_similar_draw_target(size, format.into_azure())) + } + fn create_source_surface_from_data( + &self, + data: &[u8], + size: Size2D, + stride: i32, + format: SurfaceFormat, + ) { + self.create_source_surface_from_data(data, size, stride, format.into_azure()); + } + fn draw_surface_with_shadow( + &self, + surface: SourceSurface, + dest: &Point2D, + color: &Color, + offset: &Vector2D, + sigma: f32, + operator: CompositionOp, + ) { + self.draw_surface_with_shadow( + surface.into_azure(), + dest as &Point2D, + color.as_azure(), + offset as &Vector2D, + sigma as AzFloat, + operator.into_azure(), + ); + } + fn fill(&self, path: &Path, pattern: PatternRef, draw_options: &DrawOptions) { + self.fill( + path.as_azure(), + pattern.into_azure(), + draw_options.as_azure(), + ); + } + fn fill_rect(&self, rect: &Rect, pattern: PatternRef, draw_options: Option<&DrawOptions>) { + self.fill_rect( + rect as &Rect, + pattern.into_azure(), + draw_options.map(|x| x.as_azure()), + ); + } + fn get_format(&self) -> SurfaceFormat { + SurfaceFormat::Azure(self.get_format()) + } + fn get_size(&self) -> IntSize { + IntSize::Azure(self.get_size()) + } + fn get_transform(&self) -> Transform2D { + self.get_transform() as Transform2D + } + fn pop_clip(&self) { + self.pop_clip(); + } + fn push_clip(&self, path: &Path) { + self.push_clip(path.as_azure()); + } + fn set_transform(&self, matrix: &Transform2D) { + self.set_transform(matrix as &Transform2D); + } + fn snapshot(&self) -> SourceSurface { + SourceSurface::Azure(self.snapshot()) + } + fn stroke( + &self, + path: &Path, + pattern: PatternRef, + stroke_options: &StrokeOptions, + draw_options: &DrawOptions, + ) { + self.stroke( + path.as_azure(), + pattern.into_azure(), + stroke_options.as_azure(), + draw_options.as_azure(), + ); + } + fn stroke_line( + &self, + start: Point2D, + end: Point2D, + pattern: PatternRef, + stroke_options: &StrokeOptions, + draw_options: &DrawOptions, + ) { + self.stroke_line( + start as Point2D, + end as Point2D, + pattern.into_azure(), + stroke_options.as_azure(), + draw_options.as_azure(), + ); + } + fn stroke_rect( + &self, + rect: &Rect, + pattern: PatternRef, + stroke_options: &StrokeOptions, + draw_options: &DrawOptions, + ) { + self.stroke_rect( + rect as &Rect, + pattern.into_azure(), + stroke_options.as_azure(), + draw_options.as_azure(), + ); + } +} + +enum ExtendMode { + Azure(azure_hl::ExtendMode), + Raqote(()), +} + +impl ExtendMode { + fn as_azure(&self) -> &azure_hl::ExtendMode { + match self { + ExtendMode::Azure(m) => m, + _ => unreachable!(), + } + } + + fn into_azure(self) -> azure_hl::ExtendMode { + match self { + ExtendMode::Azure(m) => m, + _ => unreachable!(), + } + } +} + enum GradientStop { Azure(AzGradientStop), Raqote(()), @@ -231,6 +396,13 @@ impl GradientStop { _ => unreachable!(), } } + + fn into_azure(self) -> AzGradientStop { + match self { + GradientStop::Azure(s) => s, + _ => unreachable!(), + } + } } enum GradientStops { @@ -245,6 +417,13 @@ impl GradientStops { _ => unreachable!(), } } + + fn into_azure(self) -> azure_hl::GradientStops { + match self { + GradientStops::Azure(s) => s, + _ => unreachable!(), + } + } } enum Color { @@ -267,7 +446,7 @@ enum CompositionOp { } impl CompositionOp { - fn as_azure(&self) -> &azure_hl::CompositionOp { + fn into_azure(self) -> azure_hl::CompositionOp { match self { CompositionOp::Azure(s) => s, _ => unreachable!(), @@ -281,7 +460,7 @@ enum SurfaceFormat { } impl SurfaceFormat { - fn as_azure(&self) -> &azure_hl::SurfaceFormat { + fn into_azure(self) -> azure_hl::SurfaceFormat { match self { SurfaceFormat::Azure(s) => s, _ => unreachable!(), @@ -295,7 +474,7 @@ enum SourceSurface { } impl SourceSurface { - fn as_azure(&self) -> &azure_hl::SourceSurface { + fn into_azure(self) -> azure_hl::SourceSurface { match self { SourceSurface::Azure(s) => s, _ => unreachable!(), @@ -348,6 +527,13 @@ impl<'a> PatternRef<'a> { _ => unreachable!(), } } + + fn into_azure(self) -> azure_hl::PatternRef<'a> { + match self { + PatternRef::Azure(p) => p, + _ => unreachable!(), + } + } } impl Pattern { @@ -379,7 +565,7 @@ enum StrokeOptions<'a> { } impl<'a> StrokeOptions<'a> { - fn as_azure_ref(&self) -> &azure_hl::StrokeOptions<'a> { + fn as_azure(&self) -> &azure_hl::StrokeOptions<'a> { match self { StrokeOptions::Azure(options) => options, _ => unreachable!(), @@ -1400,7 +1586,7 @@ impl ToAzurePattern for FillOrStrokeStyle { linear_gradient_style.x1 as AzFloat, linear_gradient_style.y1 as AzFloat, ), - drawtarget.create_gradient_stops(&gradient_stops, ExtendMode::Clamp), + drawtarget.create_gradient_stops(&gradient_stops, azure_hl::ExtendMode::Clamp), &Transform2D::identity(), )) }, @@ -1425,7 +1611,7 @@ impl ToAzurePattern for FillOrStrokeStyle { ), radial_gradient_style.r0 as AzFloat, radial_gradient_style.r1 as AzFloat, - drawtarget.create_gradient_stops(&gradient_stops, ExtendMode::Clamp), + drawtarget.create_gradient_stops(&gradient_stops, azure_hl::ExtendMode::Clamp), &Transform2D::identity(), )) },