diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2020-03-03 10:22:22 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-03 10:22:22 -0500 |
commit | ad6f55687628f2e981dc8262df783767c375fbf0 (patch) | |
tree | 5af4ad8a75c813ee6a55f1498ae3c4474a5e81a9 | |
parent | c0d9ee0ce241ee0dff86c4194588d59fe436efe6 (diff) | |
parent | 967fabb0d0b2e64ea92050e3b053e78d50045f6c (diff) | |
download | servo-ad6f55687628f2e981dc8262df783767c375fbf0.tar.gz servo-ad6f55687628f2e981dc8262df783767c375fbf0.zip |
Auto merge of #25867 - pylbrecht:apply.transform, r=jdm
Make CanvasRenderingContext2D.fillRect() draw patterns correctly
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix part of #25331
<!-- Either: -->
- [x] There are tests for these changes
<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
5 files changed, 42 insertions, 47 deletions
diff --git a/components/canvas/canvas_data.rs b/components/canvas/canvas_data.rs index 823999b3c98..f31b375222f 100644 --- a/components/canvas/canvas_data.rs +++ b/components/canvas/canvas_data.rs @@ -3,6 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use crate::canvas_paint_thread::AntialiasMode; +use crate::raqote_backend::Repetition; use canvas_traits::canvas::*; use cssparser::RGBA; use euclid::default::{Point2D, Rect, Size2D, Transform2D, Vector2D}; @@ -78,7 +79,6 @@ pub trait Backend { ); fn create_drawtarget(&self, size: Size2D<u64>) -> Box<dyn GenericDrawTarget>; fn recreate_paint_state<'a>(&self, state: &CanvasPaintState<'a>) -> CanvasPaintState<'a>; - fn size_from_pattern(&self, rect: &Rect<f32>, pattern: &Pattern) -> Option<Size2D<f32>>; } /// A generic PathBuilder that abstracts the interface for azure's and raqote's PathBuilder. @@ -470,12 +470,41 @@ impl<'a> CanvasData<'a> { return; // Paint nothing if gradient size is zero. } - let draw_rect = Rect::new( - rect.origin, - self.backend - .size_from_pattern(&rect, &self.state.fill_style) - .unwrap_or(rect.size), - ); + let draw_rect = match &self.state.fill_style { + Pattern::Raqote(pattern) => match pattern { + crate::raqote_backend::Pattern::Surface(pattern) => { + let pattern_rect = Rect::new(Point2D::origin(), pattern.size()); + let mut draw_rect = rect.intersection(&pattern_rect).unwrap_or(Rect::zero()); + + match pattern.repetition() { + Repetition::NoRepeat => { + draw_rect.size.width = + draw_rect.size.width.min(pattern_rect.size.width); + draw_rect.size.height = + draw_rect.size.height.min(pattern_rect.size.height); + }, + Repetition::RepeatX => { + draw_rect.size.width = rect.size.width; + draw_rect.size.height = + draw_rect.size.height.min(pattern_rect.size.height); + }, + Repetition::RepeatY => { + draw_rect.size.height = rect.size.height; + draw_rect.size.width = + draw_rect.size.width.min(pattern_rect.size.width); + }, + Repetition::Repeat => { + draw_rect = *rect; + }, + } + + draw_rect + }, + crate::raqote_backend::Pattern::Color(..) | + crate::raqote_backend::Pattern::LinearGradient(..) | + crate::raqote_backend::Pattern::RadialGradient(..) => *rect, + }, + }; if self.need_to_draw_shadow() { self.draw_with_shadow(&draw_rect, |new_draw_target: &mut dyn GenericDrawTarget| { diff --git a/components/canvas/raqote_backend.rs b/components/canvas/raqote_backend.rs index 8c8e6254c0b..72eae8b40c8 100644 --- a/components/canvas/raqote_backend.rs +++ b/components/canvas/raqote_backend.rs @@ -28,31 +28,6 @@ impl Backend for RaqoteBackend { color.as_raqote().a != 0 } - fn size_from_pattern( - &self, - rect: &Rect<f32>, - pattern: &canvas_data::Pattern, - ) -> Option<Size2D<f32>> { - match pattern { - canvas_data::Pattern::Raqote(Pattern::Surface(pattern)) => match pattern.repeat { - Repetition::RepeatX => Some(Size2D::new( - rect.size.width as f32, - pattern.image.height as f32, - )), - Repetition::RepeatY => Some(Size2D::new( - pattern.image.width as f32, - rect.size.height as f32, - )), - Repetition::Repeat => Some(rect.size), - Repetition::NoRepeat => Some(Size2D::new( - pattern.image.width as f32, - pattern.image.height as f32, - )), - }, - _ => None, - } - } - fn set_shadow_color<'a>(&mut self, color: RGBA, state: &mut CanvasPaintState<'a>) { state.shadow_color = Color::Raqote(color.to_raqote_style()); } @@ -208,6 +183,12 @@ impl<'a> SurfacePattern<'a> { fn set_transform(&mut self, transform: Transform2D<f32>) { self.transform = transform; } + pub fn size(&self) -> Size2D<f32> { + Size2D::new(self.image.width as f32, self.image.height as f32) + } + pub fn repetition(&self) -> &Repetition { + &self.repeat + } } #[derive(Clone)] diff --git a/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.pattern.paint.norepeat.coord3.html.ini b/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.pattern.paint.norepeat.coord3.html.ini deleted file mode 100644 index c1a72e7e5d2..00000000000 --- a/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.pattern.paint.norepeat.coord3.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.pattern.paint.norepeat.coord3.html] - type: testharness - [Canvas test: 2d.pattern.paint.norepeat.coord3] - expected: FAIL - diff --git a/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.pattern.paint.repeatx.coord1.html.ini b/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.pattern.paint.repeatx.coord1.html.ini deleted file mode 100644 index 929441d8dbc..00000000000 --- a/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.pattern.paint.repeatx.coord1.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.pattern.paint.repeatx.coord1.html] - type: testharness - [Canvas test: 2d.pattern.paint.repeatx.coord1] - expected: FAIL - diff --git a/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.pattern.paint.repeaty.coord1.html.ini b/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.pattern.paint.repeaty.coord1.html.ini deleted file mode 100644 index 0f1c5251d5d..00000000000 --- a/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.pattern.paint.repeaty.coord1.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.pattern.paint.repeaty.coord1.html] - type: testharness - [Canvas test: 2d.pattern.paint.repeaty.coord1] - expected: FAIL - |