script/dom/canvas/2d/
canvasrenderingcontext2d.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use base::Epoch;
6use canvas_traits::canvas::{Canvas2dMsg, CanvasId};
7use dom_struct::dom_struct;
8use euclid::default::Size2D;
9use ipc_channel::ipc;
10use pixels::Snapshot;
11use servo_url::ServoUrl;
12use webrender_api::ImageKey;
13
14use super::canvas_state::CanvasState;
15use crate::canvas_context::{CanvasContext, CanvasHelpers, HTMLCanvasElementOrOffscreenCanvas};
16use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
17    CanvasDirection, CanvasFillRule, CanvasImageSource, CanvasLineCap, CanvasLineJoin,
18    CanvasRenderingContext2DMethods, CanvasTextAlign, CanvasTextBaseline,
19};
20use crate::dom::bindings::codegen::Bindings::DOMMatrixBinding::DOMMatrix2DInit;
21use crate::dom::bindings::codegen::UnionTypes::{
22    HTMLCanvasElementOrOffscreenCanvas as RootedHTMLCanvasElementOrOffscreenCanvas,
23    StringOrCanvasGradientOrCanvasPattern,
24};
25use crate::dom::bindings::error::{ErrorResult, Fallible};
26use crate::dom::bindings::num::Finite;
27use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
28use crate::dom::bindings::root::{Dom, DomRoot};
29use crate::dom::bindings::str::DOMString;
30use crate::dom::canvasgradient::CanvasGradient;
31use crate::dom::canvaspattern::CanvasPattern;
32use crate::dom::dommatrix::DOMMatrix;
33use crate::dom::globalscope::GlobalScope;
34use crate::dom::html::htmlcanvaselement::HTMLCanvasElement;
35use crate::dom::imagedata::ImageData;
36use crate::dom::path2d::Path2D;
37use crate::dom::textmetrics::TextMetrics;
38use crate::script_runtime::CanGc;
39
40// https://html.spec.whatwg.org/multipage/#canvasrenderingcontext2d
41#[dom_struct]
42pub(crate) struct CanvasRenderingContext2D {
43    reflector_: Reflector,
44    canvas: HTMLCanvasElementOrOffscreenCanvas,
45    canvas_state: CanvasState,
46}
47
48impl CanvasRenderingContext2D {
49    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
50    pub(crate) fn new_inherited(
51        global: &GlobalScope,
52        canvas: HTMLCanvasElementOrOffscreenCanvas,
53        size: Size2D<u32>,
54    ) -> Option<CanvasRenderingContext2D> {
55        let canvas_state =
56            CanvasState::new(global, Size2D::new(size.width as u64, size.height as u64))?;
57        Some(CanvasRenderingContext2D {
58            reflector_: Reflector::new(),
59            canvas,
60            canvas_state,
61        })
62    }
63
64    pub(crate) fn new(
65        global: &GlobalScope,
66        canvas: &HTMLCanvasElement,
67        size: Size2D<u32>,
68        can_gc: CanGc,
69    ) -> Option<DomRoot<CanvasRenderingContext2D>> {
70        CanvasRenderingContext2D::new_inherited(
71            global,
72            HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(Dom::from_ref(canvas)),
73            size,
74        )
75        .map(|context| reflect_dom_object(Box::new(context), global, can_gc))
76    }
77
78    pub(crate) fn take_missing_image_urls(&self) -> Vec<ServoUrl> {
79        std::mem::take(&mut self.canvas_state.get_missing_image_urls().borrow_mut())
80    }
81
82    pub(crate) fn get_canvas_id(&self) -> CanvasId {
83        self.canvas_state.get_canvas_id()
84    }
85
86    pub(crate) fn send_canvas_2d_msg(&self, msg: Canvas2dMsg) {
87        self.canvas_state.send_canvas_2d_msg(msg)
88    }
89
90    pub(crate) fn set_image_key(&self, image_key: ImageKey) {
91        self.canvas_state.set_image_key(image_key);
92    }
93
94    pub(crate) fn update_rendering(&self, canvas_epoch: Epoch) -> bool {
95        if !self.onscreen() {
96            return false;
97        }
98        self.canvas_state.update_rendering(Some(canvas_epoch))
99    }
100}
101
102impl CanvasContext for CanvasRenderingContext2D {
103    type ID = CanvasId;
104
105    fn context_id(&self) -> Self::ID {
106        self.canvas_state.get_canvas_id()
107    }
108
109    fn canvas(&self) -> Option<RootedHTMLCanvasElementOrOffscreenCanvas> {
110        Some(RootedHTMLCanvasElementOrOffscreenCanvas::from(&self.canvas))
111    }
112
113    fn resize(&self) {
114        self.canvas_state.set_bitmap_dimensions(self.size().cast());
115    }
116
117    fn reset_bitmap(&self) {
118        self.canvas_state.reset_bitmap()
119    }
120
121    fn get_image_data(&self) -> Option<Snapshot> {
122        if !self.canvas_state.is_paintable() {
123            return None;
124        }
125
126        let (sender, receiver) = ipc::channel().unwrap();
127        self.canvas_state
128            .send_canvas_2d_msg(Canvas2dMsg::GetImageData(None, sender));
129        Some(receiver.recv().unwrap().to_owned())
130    }
131
132    fn origin_is_clean(&self) -> bool {
133        self.canvas_state.origin_is_clean()
134    }
135
136    fn mark_as_dirty(&self) {
137        self.canvas.mark_as_dirty();
138    }
139}
140
141// We add a guard to each of methods by the spec:
142// http://www.w3.org/html/wg/drafts/2dcontext/html5_canvas_CR/
143//
144// > Except where otherwise specified, for the 2D context interface,
145// > any method call with a numeric argument whose value is infinite or a NaN value must be ignored.
146//
147//  Restricted values are guarded in glue code. Therefore we need not add a guard.
148//
149// FIXME: this behavior should might be generated by some annotations to idl.
150impl CanvasRenderingContext2DMethods<crate::DomTypeHolder> for CanvasRenderingContext2D {
151    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-canvas>
152    fn Canvas(&self) -> DomRoot<HTMLCanvasElement> {
153        match &self.canvas {
154            HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(canvas) => canvas.as_rooted(),
155            _ => panic!("Should not be called from offscreen canvas"),
156        }
157    }
158
159    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-save>
160    fn Save(&self) {
161        self.canvas_state.save()
162    }
163
164    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-restore>
165    fn Restore(&self) {
166        self.canvas_state.restore()
167    }
168
169    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-reset>
170    fn Reset(&self) {
171        self.canvas_state.reset()
172    }
173
174    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-scale>
175    fn Scale(&self, x: f64, y: f64) {
176        self.canvas_state.scale(x, y)
177    }
178
179    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-rotate>
180    fn Rotate(&self, angle: f64) {
181        self.canvas_state.rotate(angle)
182    }
183
184    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-translate>
185    fn Translate(&self, x: f64, y: f64) {
186        self.canvas_state.translate(x, y)
187    }
188
189    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-transform>
190    fn Transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
191        self.canvas_state.transform(a, b, c, d, e, f)
192    }
193
194    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-gettransform>
195    fn GetTransform(&self, can_gc: CanGc) -> DomRoot<DOMMatrix> {
196        self.canvas_state.get_transform(&self.global(), can_gc)
197    }
198
199    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform>
200    fn SetTransform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) -> ErrorResult {
201        self.canvas_state.set_transform(a, b, c, d, e, f);
202        Ok(())
203    }
204
205    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform-matrix>
206    fn SetTransform_(&self, transform: &DOMMatrix2DInit) -> ErrorResult {
207        self.canvas_state.set_transform_(transform)
208    }
209
210    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-resettransform>
211    fn ResetTransform(&self) {
212        self.canvas_state.reset_transform()
213    }
214
215    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha>
216    fn GlobalAlpha(&self) -> f64 {
217        self.canvas_state.global_alpha()
218    }
219
220    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha>
221    fn SetGlobalAlpha(&self, alpha: f64) {
222        self.canvas_state.set_global_alpha(alpha)
223    }
224
225    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation>
226    fn GlobalCompositeOperation(&self) -> DOMString {
227        self.canvas_state.global_composite_operation()
228    }
229
230    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation>
231    fn SetGlobalCompositeOperation(&self, op_str: DOMString) {
232        self.canvas_state.set_global_composite_operation(op_str)
233    }
234
235    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-fillrect>
236    fn FillRect(&self, x: f64, y: f64, width: f64, height: f64) {
237        self.canvas_state.fill_rect(x, y, width, height);
238        self.mark_as_dirty();
239    }
240
241    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-clearrect>
242    fn ClearRect(&self, x: f64, y: f64, width: f64, height: f64) {
243        self.canvas_state.clear_rect(x, y, width, height);
244        self.mark_as_dirty();
245    }
246
247    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokerect>
248    fn StrokeRect(&self, x: f64, y: f64, width: f64, height: f64) {
249        self.canvas_state.stroke_rect(x, y, width, height);
250        self.mark_as_dirty();
251    }
252
253    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-beginpath>
254    fn BeginPath(&self) {
255        self.canvas_state.begin_path()
256    }
257
258    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-closepath>
259    fn ClosePath(&self) {
260        self.canvas_state.close_path()
261    }
262
263    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-fill>
264    fn Fill(&self, fill_rule: CanvasFillRule) {
265        self.canvas_state.fill(fill_rule);
266        self.mark_as_dirty();
267    }
268
269    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-fill>
270    fn Fill_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
271        self.canvas_state.fill_(path.segments(), fill_rule);
272        self.mark_as_dirty();
273    }
274
275    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke>
276    fn Stroke(&self) {
277        self.canvas_state.stroke();
278        self.mark_as_dirty();
279    }
280
281    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke>
282    fn Stroke_(&self, path: &Path2D) {
283        self.canvas_state.stroke_(path.segments());
284        self.mark_as_dirty();
285    }
286
287    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-clip>
288    fn Clip(&self, fill_rule: CanvasFillRule) {
289        self.canvas_state.clip(fill_rule)
290    }
291
292    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-clip>
293    fn Clip_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
294        self.canvas_state.clip_(path.segments(), fill_rule)
295    }
296
297    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath>
298    fn IsPointInPath(&self, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
299        self.canvas_state
300            .is_point_in_path(&self.global(), x, y, fill_rule)
301    }
302
303    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath>
304    fn IsPointInPath_(&self, path: &Path2D, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
305        self.canvas_state
306            .is_point_in_path_(&self.global(), path.segments(), x, y, fill_rule)
307    }
308
309    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-filltext>
310    fn FillText(&self, text: DOMString, x: f64, y: f64, max_width: Option<f64>) {
311        self.canvas_state.fill_text(
312            &self.global(),
313            self.canvas.canvas().as_deref(),
314            text,
315            x,
316            y,
317            max_width,
318        );
319        self.mark_as_dirty();
320    }
321
322    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-stroketext>
323    fn StrokeText(&self, text: DOMString, x: f64, y: f64, max_width: Option<f64>) {
324        self.canvas_state.stroke_text(
325            &self.global(),
326            self.canvas.canvas().as_deref(),
327            text,
328            x,
329            y,
330            max_width,
331        );
332        self.mark_as_dirty();
333    }
334
335    /// <https://html.spec.whatwg.org/multipage/#textmetrics>
336    fn MeasureText(&self, text: DOMString, can_gc: CanGc) -> DomRoot<TextMetrics> {
337        self.canvas_state.measure_text(
338            &self.global(),
339            self.canvas.canvas().as_deref(),
340            text,
341            can_gc,
342        )
343    }
344
345    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-font>
346    fn Font(&self) -> DOMString {
347        self.canvas_state.font()
348    }
349
350    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-font>
351    fn SetFont(&self, value: DOMString) {
352        self.canvas_state
353            .set_font(self.canvas.canvas().as_deref(), value)
354    }
355
356    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-textalign>
357    fn TextAlign(&self) -> CanvasTextAlign {
358        self.canvas_state.text_align()
359    }
360
361    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-textalign>
362    fn SetTextAlign(&self, value: CanvasTextAlign) {
363        self.canvas_state.set_text_align(value)
364    }
365
366    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-textbaseline>
367    fn TextBaseline(&self) -> CanvasTextBaseline {
368        self.canvas_state.text_baseline()
369    }
370
371    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-textbaseline>
372    fn SetTextBaseline(&self, value: CanvasTextBaseline) {
373        self.canvas_state.set_text_baseline(value)
374    }
375
376    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-direction>
377    fn Direction(&self) -> CanvasDirection {
378        self.canvas_state.direction()
379    }
380
381    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-direction>
382    fn SetDirection(&self, value: CanvasDirection) {
383        self.canvas_state.set_direction(value)
384    }
385
386    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage>
387    fn DrawImage(&self, image: CanvasImageSource, dx: f64, dy: f64) -> ErrorResult {
388        self.canvas_state
389            .draw_image(self.canvas.canvas().as_deref(), image, dx, dy)
390    }
391
392    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage>
393    fn DrawImage_(
394        &self,
395        image: CanvasImageSource,
396        dx: f64,
397        dy: f64,
398        dw: f64,
399        dh: f64,
400    ) -> ErrorResult {
401        self.canvas_state
402            .draw_image_(self.canvas.canvas().as_deref(), image, dx, dy, dw, dh)
403    }
404
405    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage>
406    fn DrawImage__(
407        &self,
408        image: CanvasImageSource,
409        sx: f64,
410        sy: f64,
411        sw: f64,
412        sh: f64,
413        dx: f64,
414        dy: f64,
415        dw: f64,
416        dh: f64,
417    ) -> ErrorResult {
418        self.canvas_state.draw_image__(
419            self.canvas.canvas().as_deref(),
420            image,
421            sx,
422            sy,
423            sw,
424            sh,
425            dx,
426            dy,
427            dw,
428            dh,
429        )
430    }
431
432    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-moveto>
433    fn MoveTo(&self, x: f64, y: f64) {
434        self.canvas_state.move_to(x, y)
435    }
436
437    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-lineto>
438    fn LineTo(&self, x: f64, y: f64) {
439        self.canvas_state.line_to(x, y)
440    }
441
442    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-rect>
443    fn Rect(&self, x: f64, y: f64, width: f64, height: f64) {
444        self.canvas_state.rect(x, y, width, height)
445    }
446
447    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-quadraticcurveto>
448    fn QuadraticCurveTo(&self, cpx: f64, cpy: f64, x: f64, y: f64) {
449        self.canvas_state.quadratic_curve_to(cpx, cpy, x, y)
450    }
451
452    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-beziercurveto>
453    fn BezierCurveTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, x: f64, y: f64) {
454        self.canvas_state
455            .bezier_curve_to(cp1x, cp1y, cp2x, cp2y, x, y)
456    }
457
458    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-arc>
459    fn Arc(&self, x: f64, y: f64, r: f64, start: f64, end: f64, ccw: bool) -> ErrorResult {
460        self.canvas_state.arc(x, y, r, start, end, ccw)
461    }
462
463    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-arcto>
464    fn ArcTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, r: f64) -> ErrorResult {
465        self.canvas_state.arc_to(cp1x, cp1y, cp2x, cp2y, r)
466    }
467
468    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-ellipse>
469    fn Ellipse(
470        &self,
471        x: f64,
472        y: f64,
473        rx: f64,
474        ry: f64,
475        rotation: f64,
476        start: f64,
477        end: f64,
478        ccw: bool,
479    ) -> ErrorResult {
480        self.canvas_state
481            .ellipse(x, y, rx, ry, rotation, start, end, ccw)
482    }
483
484    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled>
485    fn ImageSmoothingEnabled(&self) -> bool {
486        self.canvas_state.image_smoothing_enabled()
487    }
488
489    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled>
490    fn SetImageSmoothingEnabled(&self, value: bool) {
491        self.canvas_state.set_image_smoothing_enabled(value)
492    }
493
494    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle>
495    fn StrokeStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
496        self.canvas_state.stroke_style()
497    }
498
499    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle>
500    fn SetStrokeStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
501        self.canvas_state
502            .set_stroke_style(self.canvas.canvas().as_deref(), value)
503    }
504
505    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle>
506    fn FillStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
507        self.canvas_state.fill_style()
508    }
509
510    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle>
511    fn SetFillStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
512        self.canvas_state
513            .set_fill_style(self.canvas.canvas().as_deref(), value)
514    }
515
516    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createimagedata>
517    fn CreateImageData(&self, sw: i32, sh: i32, can_gc: CanGc) -> Fallible<DomRoot<ImageData>> {
518        self.canvas_state
519            .create_image_data(&self.global(), sw, sh, can_gc)
520    }
521
522    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createimagedata>
523    fn CreateImageData_(
524        &self,
525        imagedata: &ImageData,
526        can_gc: CanGc,
527    ) -> Fallible<DomRoot<ImageData>> {
528        self.canvas_state
529            .create_image_data_(&self.global(), imagedata, can_gc)
530    }
531
532    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-getimagedata>
533    fn GetImageData(
534        &self,
535        sx: i32,
536        sy: i32,
537        sw: i32,
538        sh: i32,
539        can_gc: CanGc,
540    ) -> Fallible<DomRoot<ImageData>> {
541        self.canvas_state
542            .get_image_data(self.canvas.size(), &self.global(), sx, sy, sw, sh, can_gc)
543    }
544
545    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-putimagedata>
546    fn PutImageData(&self, imagedata: &ImageData, dx: i32, dy: i32) {
547        self.canvas_state
548            .put_image_data(self.canvas.size(), imagedata, dx, dy)
549    }
550
551    // https://html.spec.whatwg.org/multipage/#dom-context-2d-putimagedata
552    fn PutImageData_(
553        &self,
554        imagedata: &ImageData,
555        dx: i32,
556        dy: i32,
557        dirty_x: i32,
558        dirty_y: i32,
559        dirty_width: i32,
560        dirty_height: i32,
561    ) {
562        self.canvas_state.put_image_data_(
563            self.canvas.size(),
564            imagedata,
565            dx,
566            dy,
567            dirty_x,
568            dirty_y,
569            dirty_width,
570            dirty_height,
571        );
572        self.mark_as_dirty();
573    }
574
575    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createlineargradient>
576    fn CreateLinearGradient(
577        &self,
578        x0: Finite<f64>,
579        y0: Finite<f64>,
580        x1: Finite<f64>,
581        y1: Finite<f64>,
582        can_gc: CanGc,
583    ) -> DomRoot<CanvasGradient> {
584        self.canvas_state
585            .create_linear_gradient(&self.global(), x0, y0, x1, y1, can_gc)
586    }
587
588    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createradialgradient>
589    fn CreateRadialGradient(
590        &self,
591        x0: Finite<f64>,
592        y0: Finite<f64>,
593        r0: Finite<f64>,
594        x1: Finite<f64>,
595        y1: Finite<f64>,
596        r1: Finite<f64>,
597        can_gc: CanGc,
598    ) -> Fallible<DomRoot<CanvasGradient>> {
599        self.canvas_state
600            .create_radial_gradient(&self.global(), x0, y0, r0, x1, y1, r1, can_gc)
601    }
602
603    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-createpattern>
604    fn CreatePattern(
605        &self,
606        image: CanvasImageSource,
607        repetition: DOMString,
608        can_gc: CanGc,
609    ) -> Fallible<Option<DomRoot<CanvasPattern>>> {
610        self.canvas_state
611            .create_pattern(&self.global(), image, repetition, can_gc)
612    }
613
614    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth>
615    fn LineWidth(&self) -> f64 {
616        self.canvas_state.line_width()
617    }
618
619    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth>
620    fn SetLineWidth(&self, width: f64) {
621        self.canvas_state.set_line_width(width)
622    }
623
624    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap>
625    fn LineCap(&self) -> CanvasLineCap {
626        self.canvas_state.line_cap()
627    }
628
629    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap>
630    fn SetLineCap(&self, cap: CanvasLineCap) {
631        self.canvas_state.set_line_cap(cap)
632    }
633
634    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin>
635    fn LineJoin(&self) -> CanvasLineJoin {
636        self.canvas_state.line_join()
637    }
638
639    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin>
640    fn SetLineJoin(&self, join: CanvasLineJoin) {
641        self.canvas_state.set_line_join(join)
642    }
643
644    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit>
645    fn MiterLimit(&self) -> f64 {
646        self.canvas_state.miter_limit()
647    }
648
649    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit>
650    fn SetMiterLimit(&self, limit: f64) {
651        self.canvas_state.set_miter_limit(limit)
652    }
653
654    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-setlinedash>
655    fn SetLineDash(&self, segments: Vec<f64>) {
656        self.canvas_state.set_line_dash(segments);
657    }
658
659    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-getlinedash>
660    fn GetLineDash(&self) -> Vec<f64> {
661        self.canvas_state.line_dash()
662    }
663
664    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
665    fn LineDashOffset(&self) -> f64 {
666        self.canvas_state.line_dash_offset()
667    }
668
669    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
670    fn SetLineDashOffset(&self, offset: f64) {
671        self.canvas_state.set_line_dash_offset(offset);
672    }
673
674    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx>
675    fn ShadowOffsetX(&self) -> f64 {
676        self.canvas_state.shadow_offset_x()
677    }
678
679    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx>
680    fn SetShadowOffsetX(&self, value: f64) {
681        self.canvas_state.set_shadow_offset_x(value)
682    }
683
684    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety>
685    fn ShadowOffsetY(&self) -> f64 {
686        self.canvas_state.shadow_offset_y()
687    }
688
689    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety>
690    fn SetShadowOffsetY(&self, value: f64) {
691        self.canvas_state.set_shadow_offset_y(value)
692    }
693
694    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur>
695    fn ShadowBlur(&self) -> f64 {
696        self.canvas_state.shadow_blur()
697    }
698
699    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur>
700    fn SetShadowBlur(&self, value: f64) {
701        self.canvas_state.set_shadow_blur(value)
702    }
703
704    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor>
705    fn ShadowColor(&self) -> DOMString {
706        self.canvas_state.shadow_color()
707    }
708
709    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor>
710    fn SetShadowColor(&self, value: DOMString) {
711        self.canvas_state
712            .set_shadow_color(self.canvas.canvas().as_deref(), value)
713    }
714}