Deal with fallout from mutable trait method change.

This commit is contained in:
Josh Matthews 2019-07-03 21:24:48 -04:00 committed by Bastien Orivel
parent b43c5c30e3
commit 4e51caeb7e
2 changed files with 85 additions and 76 deletions

View file

@ -343,11 +343,12 @@ pub enum SourceSurface {
Raqote(()), Raqote(()),
} }
#[derive(Clone)]
pub enum Path { pub enum Path {
#[cfg(feature = "canvas2d-azure")] #[cfg(feature = "canvas2d-azure")]
Azure(azure::azure_hl::Path), Azure(azure::azure_hl::Path),
#[cfg(feature = "canvas2d-raqote")] #[cfg(feature = "canvas2d-raqote")]
Raqote(()), Raqote(raqote::Path),
} }
#[derive(Clone)] #[derive(Clone)]
@ -437,7 +438,7 @@ impl<'a> CanvasData<'a> {
} }
pub fn draw_image( pub fn draw_image(
&self, &mut self,
image_data: Vec<u8>, image_data: Vec<u8>,
image_size: Size2D<f64>, image_size: Size2D<f64>,
dest_rect: Rect<f64>, dest_rect: Rect<f64>,
@ -453,14 +454,15 @@ impl<'a> CanvasData<'a> {
image_data.into() image_data.into()
}; };
let writer = |draw_target: &dyn GenericDrawTarget| { let draw_options = self.state.draw_options.clone();
let writer = |draw_target: &mut dyn GenericDrawTarget| {
write_image( write_image(
draw_target, draw_target,
image_data, image_data,
source_rect.size, source_rect.size,
dest_rect, dest_rect,
smoothing_enabled, smoothing_enabled,
&self.state.draw_options, &draw_options,
); );
}; };
@ -473,7 +475,7 @@ impl<'a> CanvasData<'a> {
// TODO(pylbrecht) pass another closure for raqote // TODO(pylbrecht) pass another closure for raqote
self.draw_with_shadow(&rect, writer); self.draw_with_shadow(&rect, writer);
} else { } else {
writer(&*self.drawtarget); writer(&mut *self.drawtarget);
} }
} }
@ -496,7 +498,7 @@ impl<'a> CanvasData<'a> {
); );
} }
pub fn fill_rect(&self, rect: &Rect<f32>) { pub fn fill_rect(&mut self, rect: &Rect<f32>) {
if self.state.fill_style.is_zero_size_gradient() { if self.state.fill_style.is_zero_size_gradient() {
return; // Paint nothing if gradient size is zero. return; // Paint nothing if gradient size is zero.
} }
@ -509,7 +511,7 @@ impl<'a> CanvasData<'a> {
); );
if self.need_to_draw_shadow() { if self.need_to_draw_shadow() {
self.draw_with_shadow(&draw_rect, |new_draw_target: &dyn GenericDrawTarget| { self.draw_with_shadow(&draw_rect, |new_draw_target: &mut dyn GenericDrawTarget| {
new_draw_target.fill_rect( new_draw_target.fill_rect(
&draw_rect, &draw_rect,
self.state.fill_style.clone(), self.state.fill_style.clone(),
@ -529,13 +531,13 @@ impl<'a> CanvasData<'a> {
self.drawtarget.clear_rect(rect); self.drawtarget.clear_rect(rect);
} }
pub fn stroke_rect(&self, rect: &Rect<f32>) { pub fn stroke_rect(&mut self, rect: &Rect<f32>) {
if self.state.stroke_style.is_zero_size_gradient() { if self.state.stroke_style.is_zero_size_gradient() {
return; // Paint nothing if gradient size is zero. return; // Paint nothing if gradient size is zero.
} }
if self.need_to_draw_shadow() { if self.need_to_draw_shadow() {
self.draw_with_shadow(&rect, |new_draw_target: &dyn GenericDrawTarget| { self.draw_with_shadow(&rect, |new_draw_target: &mut dyn GenericDrawTarget| {
new_draw_target.stroke_rect( new_draw_target.stroke_rect(
rect, rect,
self.state.stroke_style.clone(), self.state.stroke_style.clone(),
@ -642,7 +644,7 @@ impl<'a> CanvasData<'a> {
self.ensure_path(); self.ensure_path();
self.drawtarget.fill( self.drawtarget.fill(
&self.path(), &self.path().clone(),
self.state.fill_style.clone(), self.state.fill_style.clone(),
&self.state.draw_options, &self.state.draw_options,
); );
@ -655,7 +657,7 @@ impl<'a> CanvasData<'a> {
self.ensure_path(); self.ensure_path();
self.drawtarget.stroke( self.drawtarget.stroke(
&self.path(), &self.path().clone(),
self.state.stroke_style.clone(), self.state.stroke_style.clone(),
&self.state.stroke_opts, &self.state.stroke_opts,
&self.state.draw_options, &self.state.draw_options,
@ -664,7 +666,7 @@ impl<'a> CanvasData<'a> {
pub fn clip(&mut self) { pub fn clip(&mut self) {
self.ensure_path(); self.ensure_path();
let path = self.path(); let path = self.path().clone();
self.drawtarget.push_clip(&path); self.drawtarget.push_clip(&path);
} }
@ -1037,7 +1039,7 @@ impl<'a> CanvasData<'a> {
} }
fn create_draw_target_for_shadow(&self, source_rect: &Rect<f32>) -> Box<dyn GenericDrawTarget> { fn create_draw_target_for_shadow(&self, source_rect: &Rect<f32>) -> Box<dyn GenericDrawTarget> {
let draw_target = self.drawtarget.create_similar_draw_target( let mut draw_target = self.drawtarget.create_similar_draw_target(
&Size2D::new( &Size2D::new(
source_rect.size.width as i32, source_rect.size.width as i32,
source_rect.size.height as i32, source_rect.size.height as i32,
@ -1053,11 +1055,11 @@ impl<'a> CanvasData<'a> {
fn draw_with_shadow<F>(&self, rect: &Rect<f32>, draw_shadow_source: F) fn draw_with_shadow<F>(&self, rect: &Rect<f32>, draw_shadow_source: F)
where where
F: FnOnce(&dyn GenericDrawTarget), F: FnOnce(&mut dyn GenericDrawTarget),
{ {
let shadow_src_rect = self.state.transform.transform_rect(rect); let shadow_src_rect = self.state.transform.transform_rect(rect);
let new_draw_target = self.create_draw_target_for_shadow(&shadow_src_rect); let mut new_draw_target = self.create_draw_target_for_shadow(&shadow_src_rect);
draw_shadow_source(&*new_draw_target); draw_shadow_source(&mut *new_draw_target);
self.drawtarget.draw_surface_with_shadow( self.drawtarget.draw_surface_with_shadow(
new_draw_target.snapshot(), new_draw_target.snapshot(),
&Point2D::new( &Point2D::new(

View file

@ -143,72 +143,71 @@ impl Path {
} }
impl GenericDrawTarget for raqote::DrawTarget { impl GenericDrawTarget for raqote::DrawTarget {
fn clear_rect(&self, rect: &Rect<f32>) { fn clear_rect(&self, _rect: &Rect<f32>) {
unimplemented!(); unimplemented!();
} }
fn copy_surface( fn copy_surface(
&mut self, &mut self,
surface: SourceSurface, _surface: SourceSurface,
source: Rect<i32>, _source: Rect<i32>,
destination: Point2D<i32>, _destination: Point2D<i32>,
) { ) {
unimplemented!(); unimplemented!();
} }
fn create_gradient_stops( fn create_gradient_stops(
&self, &self,
gradient_stops: Vec<GradientStop>, _gradient_stops: Vec<GradientStop>,
extend_mode: ExtendMode, _extend_mode: ExtendMode,
) -> GradientStops { ) -> GradientStops {
unimplemented!(); unimplemented!();
} }
fn create_path_builder(&self) -> Box<dyn GenericPathBuilder> { fn create_path_builder(&self) -> Box<dyn GenericPathBuilder> {
unimplemented!(); Box::new(PathBuilder::new())
} }
fn create_similar_draw_target( fn create_similar_draw_target(
&self, &self,
size: &Size2D<i32>, _size: &Size2D<i32>,
format: SurfaceFormat, _format: SurfaceFormat,
) -> Box<dyn GenericDrawTarget> { ) -> Box<dyn GenericDrawTarget> {
unimplemented!(); unimplemented!();
} }
fn create_source_surface_from_data( fn create_source_surface_from_data(
&self, &self,
data: &[u8], _data: &[u8],
size: Size2D<i32>, _size: Size2D<i32>,
stride: i32, _stride: i32,
) -> Option<SourceSurface> { ) -> Option<SourceSurface> {
unimplemented!(); unimplemented!();
} }
fn draw_surface( fn draw_surface(
&self, &self,
surface: SourceSurface, _surface: SourceSurface,
dest: Rect<f64>, _dest: Rect<f64>,
source: Rect<f64>, _source: Rect<f64>,
filter: Filter, _filter: Filter,
draw_options: &DrawOptions, _draw_options: &DrawOptions,
) { ) {
unimplemented!(); unimplemented!();
} }
fn draw_surface_with_shadow( fn draw_surface_with_shadow(
&self, &self,
surface: SourceSurface, _surface: SourceSurface,
dest: &Point2D<f32>, _dest: &Point2D<f32>,
color: &Color, _color: &Color,
offset: &Vector2D<f32>, _offset: &Vector2D<f32>,
sigma: f32, _sigma: f32,
operator: CompositionOp, _operator: CompositionOp,
) { ) {
unimplemented!(); unimplemented!();
} }
fn fill(&mut self, path: &Path, pattern: Pattern, draw_options: &DrawOptions) { fn fill(&mut self, _path: &Path, _pattern: Pattern, _draw_options: &DrawOptions) {
unimplemented!(); unimplemented!();
} }
fn fill_rect( fn fill_rect(
&mut self, &mut self,
rect: &Rect<f32>, _rect: &Rect<f32>,
pattern: Pattern, _pattern: Pattern,
draw_options: Option<&DrawOptions>, _draw_options: Option<&DrawOptions>,
) { ) {
unimplemented!(); unimplemented!();
} }
@ -224,10 +223,10 @@ impl GenericDrawTarget for raqote::DrawTarget {
fn pop_clip(&mut self) { fn pop_clip(&mut self) {
unimplemented!(); unimplemented!();
} }
fn push_clip(&mut self, path: &Path) { fn push_clip(&mut self, _path: &Path) {
unimplemented!(); unimplemented!();
} }
fn set_transform(&mut self, matrix: &Transform2D<f32>) { fn set_transform(&mut self, _matrix: &Transform2D<f32>) {
unimplemented!(); unimplemented!();
} }
fn snapshot(&self) -> SourceSurface { fn snapshot(&self) -> SourceSurface {
@ -235,33 +234,33 @@ impl GenericDrawTarget for raqote::DrawTarget {
} }
fn stroke( fn stroke(
&mut self, &mut self,
path: &Path, _path: &Path,
pattern: Pattern, _pattern: Pattern,
stroke_options: &StrokeOptions, _stroke_options: &StrokeOptions,
draw_options: &DrawOptions, _draw_options: &DrawOptions,
) { ) {
unimplemented!(); unimplemented!();
} }
fn stroke_line( fn stroke_line(
&mut self, &mut self,
start: Point2D<f32>, _start: Point2D<f32>,
end: Point2D<f32>, _end: Point2D<f32>,
pattern: Pattern, _pattern: Pattern,
stroke_options: &StrokeOptions, _stroke_options: &StrokeOptions,
draw_options: &DrawOptions, _draw_options: &DrawOptions,
) { ) {
unimplemented!(); unimplemented!();
} }
fn stroke_rect( fn stroke_rect(
&mut self, &mut self,
rect: &Rect<f32>, _rect: &Rect<f32>,
pattern: Pattern, _pattern: Pattern,
stroke_options: &StrokeOptions, _stroke_options: &StrokeOptions,
draw_options: &DrawOptions, _draw_options: &DrawOptions,
) { ) {
unimplemented!(); unimplemented!();
} }
fn snapshot_data(&self, f: &Fn(&[u8]) -> Vec<u8>) -> Vec<u8> { fn snapshot_data(&self, _f: &dyn Fn(&[u8]) -> Vec<u8>) -> Vec<u8> {
unimplemented!(); unimplemented!();
} }
fn snapshot_data_owned(&self) -> Vec<u8> { fn snapshot_data_owned(&self) -> Vec<u8> {
@ -269,16 +268,24 @@ impl GenericDrawTarget for raqote::DrawTarget {
} }
} }
impl GenericPathBuilder for raqote::PathBuilder { struct PathBuilder(Option<raqote::PathBuilder>);
impl PathBuilder {
fn new() -> PathBuilder {
PathBuilder(Some(raqote::PathBuilder::new()))
}
}
impl GenericPathBuilder for PathBuilder {
fn arc( fn arc(
&mut self, &mut self,
origin: Point2D<f32>, origin: Point2D<f32>,
radius: f32, radius: f32,
start_angle: f32, start_angle: f32,
end_angle: f32, end_angle: f32,
anticlockwise: bool, _anticlockwise: bool,
) { ) {
self.arc(origin.x, origin.y, radius, start_angle, end_angle); self.0.as_mut().unwrap().arc(origin.x, origin.y, radius, start_angle, end_angle);
} }
fn bezier_curve_to( fn bezier_curve_to(
&mut self, &mut self,
@ -286,7 +293,7 @@ impl GenericPathBuilder for raqote::PathBuilder {
control_point2: &Point2D<f32>, control_point2: &Point2D<f32>,
control_point3: &Point2D<f32>, control_point3: &Point2D<f32>,
) { ) {
self.cubic_to( self.0.as_mut().unwrap().cubic_to(
control_point1.x, control_point1.x,
control_point1.y, control_point1.y,
control_point2.x, control_point2.x,
@ -296,17 +303,17 @@ impl GenericPathBuilder for raqote::PathBuilder {
); );
} }
fn close(&mut self) { fn close(&mut self) {
self.close(); self.0.as_mut().unwrap().close();
} }
fn ellipse( fn ellipse(
&mut self, &mut self,
origin: Point2D<f32>, _origin: Point2D<f32>,
radius_x: f32, _radius_x: f32,
radius_y: f32, _radius_y: f32,
rotation_angle: f32, _rotation_angle: f32,
start_angle: f32, _start_angle: f32,
end_angle: f32, _end_angle: f32,
anticlockwise: bool, _anticlockwise: bool,
) { ) {
unimplemented!(); unimplemented!();
} }
@ -314,16 +321,16 @@ impl GenericPathBuilder for raqote::PathBuilder {
unimplemented!(); unimplemented!();
} }
fn line_to(&mut self, point: Point2D<f32>) { fn line_to(&mut self, point: Point2D<f32>) {
self.line_to(point.x, point.y); self.0.as_mut().unwrap().line_to(point.x, point.y);
} }
fn move_to(&mut self, point: Point2D<f32>) { fn move_to(&mut self, point: Point2D<f32>) {
self.move_to(point.x, point.y); self.0.as_mut().unwrap().move_to(point.x, point.y);
} }
fn quadratic_curve_to(&mut self, control_point: &Point2D<f32>, end_point: &Point2D<f32>) { fn quadratic_curve_to(&mut self, control_point: &Point2D<f32>, end_point: &Point2D<f32>) {
self.quad_to(control_point.x, control_point.y, end_point.x, end_point.y); self.0.as_mut().unwrap().quad_to(control_point.x, control_point.y, end_point.x, end_point.y);
} }
fn finish(&mut self) -> Path { fn finish(&mut self) -> Path {
self.finish() Path::Raqote(self.0.take().unwrap().finish())
} }
} }