canvas: Implement line dash setters and getters (#36257)

Implement `setLineDash`, `getLineDash`, and `lineDashOffset` from
`CanvasPathDrawingStyles` mixin, according to the spec
https://html.spec.whatwg.org/multipage/canvas.html#canvaspathdrawingstyles.

Testing: Existing WPT.

---------

Signed-off-by: stevennovaryo <steven.novaryo@gmail.com>
This commit is contained in:
Steven Novaryo 2025-04-01 19:22:00 +08:00 committed by GitHub
parent bc6926d1fe
commit a77592e281
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 144 additions and 92 deletions

View file

@ -1366,6 +1366,14 @@ impl<'a> CanvasData<'a> {
self.state.stroke_opts.set_miter_limit(limit);
}
pub fn set_line_dash(&mut self, items: Vec<f32>) {
self.state.stroke_opts.set_line_dash(items);
}
pub fn set_line_dash_offset(&mut self, offset: f32) {
self.state.stroke_opts.set_line_dash_offset(offset);
}
pub fn get_transform(&self) -> Transform2D<f32> {
self.drawtarget.get_transform()
}

View file

@ -230,6 +230,10 @@ impl<'a> CanvasPaintThread<'a> {
Canvas2dMsg::SetLineCap(cap) => self.canvas(canvas_id).set_line_cap(cap),
Canvas2dMsg::SetLineJoin(join) => self.canvas(canvas_id).set_line_join(join),
Canvas2dMsg::SetMiterLimit(limit) => self.canvas(canvas_id).set_miter_limit(limit),
Canvas2dMsg::SetLineDash(items) => self.canvas(canvas_id).set_line_dash(items),
Canvas2dMsg::SetLineDashOffset(offset) => {
self.canvas(canvas_id).set_line_dash_offset(offset)
},
Canvas2dMsg::GetTransform(sender) => {
let transform = self.canvas(canvas_id).get_transform();
sender.send(transform).unwrap();

View file

@ -298,6 +298,16 @@ impl StrokeOptions {
StrokeOptions::Raqote(options) => options.cap = val.to_raqote_style(),
}
}
pub fn set_line_dash(&mut self, items: Vec<f32>) {
match self {
StrokeOptions::Raqote(options) => options.dash_array = items,
}
}
pub fn set_line_dash_offset(&mut self, offset: f32) {
match self {
StrokeOptions::Raqote(options) => options.dash_offset = offset,
}
}
pub fn as_raqote(&self) -> &raqote::StrokeStyle {
match self {
StrokeOptions::Raqote(options) => options,