Send fill/stroke style along with drawing message

This commit is contained in:
pylbrecht 2020-01-25 21:29:36 +01:00
parent ebdf4693ab
commit 5a773bf55a
3 changed files with 44 additions and 43 deletions

View file

@ -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