Skip to content

Commit

Permalink
Send fill/stroke style along with drawing message
Browse files Browse the repository at this point in the history
  • Loading branch information
pylbrecht committed Jan 25, 2020
1 parent ebdf469 commit 5a773bf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
27 changes: 19 additions & 8 deletions components/canvas/canvas_paint_thread.rs
Expand Up @@ -121,16 +121,29 @@ impl<'a> CanvasPaintThread<'a> {

fn process_canvas_2d_message(&mut self, message: Canvas2dMsg, canvas_id: CanvasId) {
match message {
Canvas2dMsg::FillText(text, x, y, max_width) => {
self.canvas(canvas_id).fill_text(text, x, y, max_width)
Canvas2dMsg::FillText(text, x, y, max_width, style) => {
self.canvas(canvas_id).set_fill_style(style);
self.canvas(canvas_id).fill_text(text, x, y, max_width);
},
Canvas2dMsg::FillRect(rect, style) => {
self.canvas(canvas_id).set_fill_style(style);
self.canvas(canvas_id).fill_rect(&rect);
},
Canvas2dMsg::StrokeRect(rect, style) => {
self.canvas(canvas_id).set_stroke_style(style);
self.canvas(canvas_id).stroke_rect(&rect);
},
Canvas2dMsg::FillRect(ref rect) => self.canvas(canvas_id).fill_rect(rect),
Canvas2dMsg::StrokeRect(ref rect) => self.canvas(canvas_id).stroke_rect(rect),
Canvas2dMsg::ClearRect(ref rect) => self.canvas(canvas_id).clear_rect(rect),
Canvas2dMsg::BeginPath => self.canvas(canvas_id).begin_path(),
Canvas2dMsg::ClosePath => self.canvas(canvas_id).close_path(),
Canvas2dMsg::Fill => self.canvas(canvas_id).fill(),
Canvas2dMsg::Stroke => self.canvas(canvas_id).stroke(),
Canvas2dMsg::Fill(style) => {
self.canvas(canvas_id).set_fill_style(style);
self.canvas(canvas_id).fill();
},
Canvas2dMsg::Stroke(style) => {
self.canvas(canvas_id).set_stroke_style(style);
self.canvas(canvas_id).stroke();
},
Canvas2dMsg::Clip => self.canvas(canvas_id).clip(),
Canvas2dMsg::IsPointInPath(x, y, fill_rule, chan) => self
.canvas(canvas_id)
Expand Down Expand Up @@ -192,8 +205,6 @@ impl<'a> CanvasPaintThread<'a> {
.ellipse(center, radius_x, radius_y, rotation, start, end, ccw),
Canvas2dMsg::RestoreContext => self.canvas(canvas_id).restore_context_state(),
Canvas2dMsg::SaveContext => self.canvas(canvas_id).save_context_state(),
Canvas2dMsg::SetFillStyle(style) => self.canvas(canvas_id).set_fill_style(style),
Canvas2dMsg::SetStrokeStyle(style) => self.canvas(canvas_id).set_stroke_style(style),
Canvas2dMsg::SetLineWidth(width) => self.canvas(canvas_id).set_line_width(width),
Canvas2dMsg::SetLineCap(cap) => self.canvas(canvas_id).set_line_cap(cap),
Canvas2dMsg::SetLineJoin(join) => self.canvas(canvas_id).set_line_join(join),
Expand Down
12 changes: 5 additions & 7 deletions components/canvas_traits/canvas.rs
Expand Up @@ -44,9 +44,9 @@ pub enum Canvas2dMsg {
Clip,
ClosePath,
Ellipse(Point2D<f32>, f32, f32, f32, f32, f32, bool),
Fill,
FillText(String, f64, f64, Option<f64>),
FillRect(Rect<f32>),
Fill(FillOrStrokeStyle),
FillText(String, f64, f64, Option<f64>, FillOrStrokeStyle),
FillRect(Rect<f32>, FillOrStrokeStyle),
GetImageData(Rect<u64>, Size2D<u64>, IpcBytesSender),
IsPointInPath(f64, f64, FillRule, IpcSender<bool>),
LineTo(Point2D<f32>),
Expand All @@ -56,10 +56,8 @@ pub enum Canvas2dMsg {
Rect(Rect<f32>),
RestoreContext,
SaveContext,
StrokeRect(Rect<f32>),
Stroke,
SetFillStyle(FillOrStrokeStyle),
SetStrokeStyle(FillOrStrokeStyle),
StrokeRect(Rect<f32>, FillOrStrokeStyle),
Stroke(FillOrStrokeStyle),
SetLineWidth(f32),
SetLineCap(LineCapStyle),
SetLineJoin(LineJoinStyle),
Expand Down
48 changes: 20 additions & 28 deletions components/script/canvas_state.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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));
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5a773bf

Please sign in to comment.