diff options
author | pylbrecht <palbrecht@mailbox.org> | 2020-01-25 21:29:36 +0100 |
---|---|---|
committer | pylbrecht <palbrecht@mailbox.org> | 2020-01-25 21:29:36 +0100 |
commit | 5a773bf55a3dfd1b486887c3c36bafe0cbe58f2e (patch) | |
tree | a035188bd3b0dae0489e0b6c1dff074be3e69088 /components/script/canvas_state.rs | |
parent | ebdf4693abcd3f05a88cfb9d20664c0a489f4964 (diff) | |
download | servo-5a773bf55a3dfd1b486887c3c36bafe0cbe58f2e.tar.gz servo-5a773bf55a3dfd1b486887c3c36bafe0cbe58f2e.zip |
Send fill/stroke style along with drawing message
Diffstat (limited to 'components/script/canvas_state.rs')
-rw-r--r-- | components/script/canvas_state.rs | 48 |
1 files changed, 20 insertions, 28 deletions
diff --git a/components/script/canvas_state.rs b/components/script/canvas_state.rs index c59cb3c8823..c9492ed5da5 100644 --- a/components/script/canvas_state.rs +++ b/components/script/canvas_state.rs @@ -64,6 +64,16 @@ pub(crate) enum CanvasFillOrStrokeStyle { Pattern(Dom<CanvasPattern>), } +impl CanvasFillOrStrokeStyle { + fn to_fill_or_stroke_style(&self) -> FillOrStrokeStyle { + match self { + CanvasFillOrStrokeStyle::Color(rgba) => FillOrStrokeStyle::Color(*rgba), + CanvasFillOrStrokeStyle::Gradient(gradient) => gradient.to_fill_or_stroke_style(), + CanvasFillOrStrokeStyle::Pattern(pattern) => pattern.to_fill_or_stroke_style(), + } + } +} + #[unrooted_must_root_lint::must_root] #[derive(Clone, JSTraceable, MallocSizeOf)] pub(crate) struct CanvasContextState { @@ -654,8 +664,8 @@ impl CanvasState { // https://html.spec.whatwg.org/multipage/#dom-context-2d-fillrect pub fn fill_rect(&self, x: f64, y: f64, width: f64, height: f64) { if let Some(rect) = self.create_drawable_rect(x, y, width, height) { - self.update_fill_style(); - self.send_canvas_2d_msg(Canvas2dMsg::FillRect(rect)); + let style = self.state.borrow().fill_style.to_fill_or_stroke_style(); + self.send_canvas_2d_msg(Canvas2dMsg::FillRect(rect, style)); } } @@ -669,8 +679,8 @@ impl CanvasState { // https://html.spec.whatwg.org/multipage/#dom-context-2d-strokerect pub fn stroke_rect(&self, x: f64, y: f64, width: f64, height: f64) { if let Some(rect) = self.create_drawable_rect(x, y, width, height) { - self.update_stroke_style(); - self.send_canvas_2d_msg(Canvas2dMsg::StrokeRect(rect)); + let style = self.state.borrow().stroke_style.to_fill_or_stroke_style(); + self.send_canvas_2d_msg(Canvas2dMsg::StrokeRect(rect, style)); } } @@ -774,15 +784,6 @@ impl CanvasState { } } - fn update_stroke_style(&self) { - let style = match &self.state.borrow().stroke_style { - CanvasFillOrStrokeStyle::Color(rgba) => FillOrStrokeStyle::Color(*rgba), - CanvasFillOrStrokeStyle::Gradient(gradient) => gradient.to_fill_or_stroke_style(), - CanvasFillOrStrokeStyle::Pattern(pattern) => pattern.to_fill_or_stroke_style(), - }; - self.send_canvas_2d_msg(Canvas2dMsg::SetStrokeStyle(style)); - } - // https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle pub fn fill_style(&self) -> StringOrCanvasGradientOrCanvasPattern { match self.state.borrow().fill_style { @@ -826,15 +827,6 @@ impl CanvasState { } } - fn update_fill_style(&self) { - let style = match &self.state.borrow().fill_style { - CanvasFillOrStrokeStyle::Color(rgba) => FillOrStrokeStyle::Color(*rgba), - CanvasFillOrStrokeStyle::Gradient(gradient) => gradient.to_fill_or_stroke_style(), - CanvasFillOrStrokeStyle::Pattern(pattern) => pattern.to_fill_or_stroke_style(), - }; - self.send_canvas_2d_msg(Canvas2dMsg::SetFillStyle(style)); - } - // https://html.spec.whatwg.org/multipage/#dom-context-2d-createlineargradient pub fn create_linear_gradient( &self, @@ -997,8 +989,8 @@ impl CanvasState { // https://html.spec.whatwg.org/multipage/#dom-context-2d-filltext pub fn fill_text(&self, text: DOMString, x: f64, y: f64, max_width: Option<f64>) { let parsed_text: String = text.into(); - self.update_fill_style(); - self.send_canvas_2d_msg(Canvas2dMsg::FillText(parsed_text, x, y, max_width)); + let style = self.state.borrow().fill_style.to_fill_or_stroke_style(); + self.send_canvas_2d_msg(Canvas2dMsg::FillText(parsed_text, x, y, max_width, style)); } // https://html.spec.whatwg.org/multipage/#textmetrics @@ -1305,14 +1297,14 @@ impl CanvasState { // https://html.spec.whatwg.org/multipage/#dom-context-2d-fill pub fn fill(&self, _fill_rule: CanvasFillRule) { // TODO: Process fill rule - self.update_fill_style(); - self.send_canvas_2d_msg(Canvas2dMsg::Fill); + let style = self.state.borrow().fill_style.to_fill_or_stroke_style(); + self.send_canvas_2d_msg(Canvas2dMsg::Fill(style)); } // https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke pub fn stroke(&self) { - self.update_stroke_style(); - self.send_canvas_2d_msg(Canvas2dMsg::Stroke); + let style = self.state.borrow().stroke_style.to_fill_or_stroke_style(); + self.send_canvas_2d_msg(Canvas2dMsg::Stroke(style)); } // https://html.spec.whatwg.org/multipage/#dom-context-2d-clip |