script/dom/canvas/2d/
paintrenderingcontext2d.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 std::cell::Cell;
6
7use dom_struct::dom_struct;
8use euclid::{Scale, Size2D};
9use script_bindings::reflector::Reflector;
10use servo_url::ServoUrl;
11use style_traits::CSSPixel;
12use webrender_api::ImageKey;
13use webrender_api::units::DevicePixel;
14
15use super::canvas_state::CanvasState;
16use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
17    CanvasFillRule, CanvasImageSource, CanvasLineCap, CanvasLineJoin,
18};
19use crate::dom::bindings::codegen::Bindings::DOMMatrixBinding::DOMMatrix2DInit;
20use crate::dom::bindings::codegen::Bindings::PaintRenderingContext2DBinding::PaintRenderingContext2DMethods;
21use crate::dom::bindings::codegen::UnionTypes::StringOrCanvasGradientOrCanvasPattern;
22use crate::dom::bindings::error::{ErrorResult, Fallible};
23use crate::dom::bindings::inheritance::Castable;
24use crate::dom::bindings::num::Finite;
25use crate::dom::bindings::reflector::{DomGlobal as _, reflect_dom_object};
26use crate::dom::bindings::root::DomRoot;
27use crate::dom::bindings::str::DOMString;
28use crate::dom::canvasgradient::CanvasGradient;
29use crate::dom::canvaspattern::CanvasPattern;
30use crate::dom::dommatrix::DOMMatrix;
31use crate::dom::paintworkletglobalscope::PaintWorkletGlobalScope;
32use crate::dom::path2d::Path2D;
33use crate::script_runtime::CanGc;
34
35#[dom_struct]
36pub(crate) struct PaintRenderingContext2D {
37    reflector_: Reflector,
38    canvas_state: CanvasState,
39    #[no_trace]
40    device_pixel_ratio: Cell<Scale<f32, CSSPixel, DevicePixel>>,
41}
42
43impl PaintRenderingContext2D {
44    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
45    fn new_inherited(global: &PaintWorkletGlobalScope) -> Option<PaintRenderingContext2D> {
46        Some(PaintRenderingContext2D {
47            reflector_: Reflector::new(),
48            canvas_state: CanvasState::new(global.upcast(), Size2D::zero())?,
49            device_pixel_ratio: Cell::new(Scale::new(1.0)),
50        })
51    }
52
53    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
54    pub(crate) fn new(
55        global: &PaintWorkletGlobalScope,
56        can_gc: CanGc,
57    ) -> Option<DomRoot<PaintRenderingContext2D>> {
58        Some(reflect_dom_object(
59            Box::new(PaintRenderingContext2D::new_inherited(global)?),
60            global,
61            can_gc,
62        ))
63    }
64
65    pub(crate) fn update_rendering(&self) -> bool {
66        self.canvas_state.update_rendering(None)
67    }
68
69    /// Send update to canvas paint thread and returns [`ImageKey`]
70    pub(crate) fn image_key(&self) -> ImageKey {
71        self.canvas_state.image_key()
72    }
73
74    pub(crate) fn take_missing_image_urls(&self) -> Vec<ServoUrl> {
75        std::mem::take(&mut self.canvas_state.get_missing_image_urls().borrow_mut())
76    }
77
78    pub(crate) fn set_bitmap_dimensions(
79        &self,
80        size: Size2D<f32, CSSPixel>,
81        device_pixel_ratio: Scale<f32, CSSPixel, DevicePixel>,
82    ) {
83        let size = size * device_pixel_ratio;
84        self.device_pixel_ratio.set(device_pixel_ratio);
85        self.canvas_state
86            .set_bitmap_dimensions(size.to_untyped().to_u64());
87        self.scale_by_device_pixel_ratio();
88    }
89
90    fn scale_by_device_pixel_ratio(&self) {
91        let device_pixel_ratio = self.device_pixel_ratio.get().get() as f64;
92        if device_pixel_ratio != 1.0 {
93            self.Scale(device_pixel_ratio, device_pixel_ratio);
94        }
95    }
96}
97
98impl PaintRenderingContext2DMethods<crate::DomTypeHolder> for PaintRenderingContext2D {
99    // https://html.spec.whatwg.org/multipage/#dom-context-2d-save
100    fn Save(&self) {
101        self.canvas_state.save()
102    }
103
104    // https://html.spec.whatwg.org/multipage/#dom-context-2d-restore
105    fn Restore(&self) {
106        self.canvas_state.restore()
107    }
108
109    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-reset>
110    fn Reset(&self) {
111        self.canvas_state.reset()
112    }
113
114    // https://html.spec.whatwg.org/multipage/#dom-context-2d-scale
115    fn Scale(&self, x: f64, y: f64) {
116        self.canvas_state.scale(x, y)
117    }
118
119    // https://html.spec.whatwg.org/multipage/#dom-context-2d-rotate
120    fn Rotate(&self, angle: f64) {
121        self.canvas_state.rotate(angle)
122    }
123
124    // https://html.spec.whatwg.org/multipage/#dom-context-2d-translate
125    fn Translate(&self, x: f64, y: f64) {
126        self.canvas_state.translate(x, y)
127    }
128
129    // https://html.spec.whatwg.org/multipage/#dom-context-2d-transform
130    fn Transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
131        self.canvas_state.transform(a, b, c, d, e, f)
132    }
133
134    // https://html.spec.whatwg.org/multipage/#dom-context-2d-gettransform
135    fn GetTransform(&self, can_gc: CanGc) -> DomRoot<DOMMatrix> {
136        self.canvas_state.get_transform(&self.global(), can_gc)
137    }
138
139    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform>
140    fn SetTransform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) -> ErrorResult {
141        self.canvas_state.set_transform(a, b, c, d, e, f);
142        self.scale_by_device_pixel_ratio();
143        Ok(())
144    }
145
146    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform-matrix>
147    fn SetTransform_(&self, transform: &DOMMatrix2DInit) -> ErrorResult {
148        self.canvas_state.set_transform_(transform)?;
149        self.scale_by_device_pixel_ratio();
150        Ok(())
151    }
152
153    // https://html.spec.whatwg.org/multipage/#dom-context-2d-resettransform
154    fn ResetTransform(&self) {
155        self.canvas_state.reset_transform();
156        self.scale_by_device_pixel_ratio();
157    }
158
159    // https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha
160    fn GlobalAlpha(&self) -> f64 {
161        self.canvas_state.global_alpha()
162    }
163
164    // https://html.spec.whatwg.org/multipage/#dom-context-2d-globalalpha
165    fn SetGlobalAlpha(&self, alpha: f64) {
166        self.canvas_state.set_global_alpha(alpha)
167    }
168
169    // https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation
170    fn GlobalCompositeOperation(&self) -> DOMString {
171        self.canvas_state.global_composite_operation()
172    }
173
174    // https://html.spec.whatwg.org/multipage/#dom-context-2d-globalcompositeoperation
175    fn SetGlobalCompositeOperation(&self, op_str: DOMString) {
176        self.canvas_state.set_global_composite_operation(op_str)
177    }
178
179    // https://html.spec.whatwg.org/multipage/#dom-context-2d-fillrect
180    fn FillRect(&self, x: f64, y: f64, width: f64, height: f64) {
181        self.canvas_state.fill_rect(x, y, width, height)
182    }
183
184    // https://html.spec.whatwg.org/multipage/#dom-context-2d-clearrect
185    fn ClearRect(&self, x: f64, y: f64, width: f64, height: f64) {
186        self.canvas_state.clear_rect(x, y, width, height)
187    }
188
189    // https://html.spec.whatwg.org/multipage/#dom-context-2d-strokerect
190    fn StrokeRect(&self, x: f64, y: f64, width: f64, height: f64) {
191        self.canvas_state.stroke_rect(x, y, width, height)
192    }
193
194    // https://html.spec.whatwg.org/multipage/#dom-context-2d-beginpath
195    fn BeginPath(&self) {
196        self.canvas_state.begin_path()
197    }
198
199    // https://html.spec.whatwg.org/multipage/#dom-context-2d-closepath
200    fn ClosePath(&self) {
201        self.canvas_state.close_path()
202    }
203
204    // https://html.spec.whatwg.org/multipage/#dom-context-2d-fill
205    fn Fill(&self, fill_rule: CanvasFillRule) {
206        self.canvas_state.fill(fill_rule)
207    }
208
209    // https://html.spec.whatwg.org/multipage/#dom-context-2d-fill
210    fn Fill_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
211        self.canvas_state.fill_(path.segments(), fill_rule)
212    }
213
214    // https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke
215    fn Stroke(&self) {
216        self.canvas_state.stroke()
217    }
218
219    // https://html.spec.whatwg.org/multipage/#dom-context-2d-stroke
220    fn Stroke_(&self, path: &Path2D) {
221        self.canvas_state.stroke_(path.segments())
222    }
223
224    // https://html.spec.whatwg.org/multipage/#dom-context-2d-clip
225    fn Clip(&self, fill_rule: CanvasFillRule) {
226        self.canvas_state.clip(fill_rule)
227    }
228
229    // https://html.spec.whatwg.org/multipage/#dom-context-2d-clip
230    fn Clip_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
231        self.canvas_state.clip_(path.segments(), fill_rule)
232    }
233
234    // https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath
235    fn IsPointInPath(&self, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
236        self.canvas_state
237            .is_point_in_path(&self.global(), x, y, fill_rule)
238    }
239
240    // https://html.spec.whatwg.org/multipage/#dom-context-2d-ispointinpath
241    fn IsPointInPath_(&self, path: &Path2D, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
242        self.canvas_state
243            .is_point_in_path_(&self.global(), path.segments(), x, y, fill_rule)
244    }
245
246    // https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
247    fn DrawImage(&self, image: CanvasImageSource, dx: f64, dy: f64) -> ErrorResult {
248        self.canvas_state.draw_image(None, image, dx, dy)
249    }
250
251    // https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
252    fn DrawImage_(
253        &self,
254        image: CanvasImageSource,
255        dx: f64,
256        dy: f64,
257        dw: f64,
258        dh: f64,
259    ) -> ErrorResult {
260        self.canvas_state.draw_image_(None, image, dx, dy, dw, dh)
261    }
262
263    // https://html.spec.whatwg.org/multipage/#dom-context-2d-drawimage
264    fn DrawImage__(
265        &self,
266        image: CanvasImageSource,
267        sx: f64,
268        sy: f64,
269        sw: f64,
270        sh: f64,
271        dx: f64,
272        dy: f64,
273        dw: f64,
274        dh: f64,
275    ) -> ErrorResult {
276        self.canvas_state
277            .draw_image__(None, image, sx, sy, sw, sh, dx, dy, dw, dh)
278    }
279
280    // https://html.spec.whatwg.org/multipage/#dom-context-2d-moveto
281    fn MoveTo(&self, x: f64, y: f64) {
282        self.canvas_state.move_to(x, y)
283    }
284
285    // https://html.spec.whatwg.org/multipage/#dom-context-2d-lineto
286    fn LineTo(&self, x: f64, y: f64) {
287        self.canvas_state.line_to(x, y)
288    }
289
290    // https://html.spec.whatwg.org/multipage/#dom-context-2d-rect
291    fn Rect(&self, x: f64, y: f64, width: f64, height: f64) {
292        self.canvas_state.rect(x, y, width, height)
293    }
294
295    // https://html.spec.whatwg.org/multipage/#dom-context-2d-quadraticcurveto
296    fn QuadraticCurveTo(&self, cpx: f64, cpy: f64, x: f64, y: f64) {
297        self.canvas_state.quadratic_curve_to(cpx, cpy, x, y)
298    }
299
300    // https://html.spec.whatwg.org/multipage/#dom-context-2d-beziercurveto
301    fn BezierCurveTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, x: f64, y: f64) {
302        self.canvas_state
303            .bezier_curve_to(cp1x, cp1y, cp2x, cp2y, x, y)
304    }
305
306    // https://html.spec.whatwg.org/multipage/#dom-context-2d-arc
307    fn Arc(&self, x: f64, y: f64, r: f64, start: f64, end: f64, ccw: bool) -> ErrorResult {
308        self.canvas_state.arc(x, y, r, start, end, ccw)
309    }
310
311    // https://html.spec.whatwg.org/multipage/#dom-context-2d-arcto
312    fn ArcTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, r: f64) -> ErrorResult {
313        self.canvas_state.arc_to(cp1x, cp1y, cp2x, cp2y, r)
314    }
315
316    // https://html.spec.whatwg.org/multipage/#dom-context-2d-ellipse
317    fn Ellipse(
318        &self,
319        x: f64,
320        y: f64,
321        rx: f64,
322        ry: f64,
323        rotation: f64,
324        start: f64,
325        end: f64,
326        ccw: bool,
327    ) -> ErrorResult {
328        self.canvas_state
329            .ellipse(x, y, rx, ry, rotation, start, end, ccw)
330    }
331
332    // https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled
333    fn ImageSmoothingEnabled(&self) -> bool {
334        self.canvas_state.image_smoothing_enabled()
335    }
336
337    // https://html.spec.whatwg.org/multipage/#dom-context-2d-imagesmoothingenabled
338    fn SetImageSmoothingEnabled(&self, value: bool) {
339        self.canvas_state.set_image_smoothing_enabled(value)
340    }
341
342    // https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
343    fn StrokeStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
344        self.canvas_state.stroke_style()
345    }
346
347    // https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
348    fn SetStrokeStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
349        self.canvas_state.set_stroke_style(None, value)
350    }
351
352    // https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
353    fn FillStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
354        self.canvas_state.fill_style()
355    }
356
357    // https://html.spec.whatwg.org/multipage/#dom-context-2d-strokestyle
358    fn SetFillStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
359        self.canvas_state.set_fill_style(None, value)
360    }
361
362    // https://html.spec.whatwg.org/multipage/#dom-context-2d-createlineargradient
363    fn CreateLinearGradient(
364        &self,
365        x0: Finite<f64>,
366        y0: Finite<f64>,
367        x1: Finite<f64>,
368        y1: Finite<f64>,
369    ) -> DomRoot<CanvasGradient> {
370        self.canvas_state
371            .create_linear_gradient(&self.global(), x0, y0, x1, y1, CanGc::note())
372    }
373
374    // https://html.spec.whatwg.org/multipage/#dom-context-2d-createradialgradient
375    fn CreateRadialGradient(
376        &self,
377        x0: Finite<f64>,
378        y0: Finite<f64>,
379        r0: Finite<f64>,
380        x1: Finite<f64>,
381        y1: Finite<f64>,
382        r1: Finite<f64>,
383    ) -> Fallible<DomRoot<CanvasGradient>> {
384        self.canvas_state.create_radial_gradient(
385            &self.global(),
386            x0,
387            y0,
388            r0,
389            x1,
390            y1,
391            r1,
392            CanGc::note(),
393        )
394    }
395
396    // https://html.spec.whatwg.org/multipage/#dom-context-2d-createpattern
397    fn CreatePattern(
398        &self,
399        image: CanvasImageSource,
400        repetition: DOMString,
401    ) -> Fallible<Option<DomRoot<CanvasPattern>>> {
402        self.canvas_state
403            .create_pattern(&self.global(), image, repetition, CanGc::note())
404    }
405
406    // https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth
407    fn LineWidth(&self) -> f64 {
408        self.canvas_state.line_width()
409    }
410
411    // https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth
412    fn SetLineWidth(&self, width: f64) {
413        self.canvas_state.set_line_width(width)
414    }
415
416    // https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap
417    fn LineCap(&self) -> CanvasLineCap {
418        self.canvas_state.line_cap()
419    }
420
421    // https://html.spec.whatwg.org/multipage/#dom-context-2d-linecap
422    fn SetLineCap(&self, cap: CanvasLineCap) {
423        self.canvas_state.set_line_cap(cap)
424    }
425
426    // https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin
427    fn LineJoin(&self) -> CanvasLineJoin {
428        self.canvas_state.line_join()
429    }
430
431    // https://html.spec.whatwg.org/multipage/#dom-context-2d-linejoin
432    fn SetLineJoin(&self, join: CanvasLineJoin) {
433        self.canvas_state.set_line_join(join)
434    }
435
436    // https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit
437    fn MiterLimit(&self) -> f64 {
438        self.canvas_state.miter_limit()
439    }
440
441    // https://html.spec.whatwg.org/multipage/#dom-context-2d-miterlimit
442    fn SetMiterLimit(&self, limit: f64) {
443        self.canvas_state.set_miter_limit(limit)
444    }
445
446    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-setlinedash>
447    fn SetLineDash(&self, segments: Vec<f64>) {
448        self.canvas_state.set_line_dash(segments);
449    }
450
451    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-getlinedash>
452    fn GetLineDash(&self) -> Vec<f64> {
453        self.canvas_state.line_dash()
454    }
455
456    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
457    fn LineDashOffset(&self) -> f64 {
458        self.canvas_state.line_dash_offset()
459    }
460
461    /// <https://html.spec.whatwg.org/multipage/#dom-context-2d-linedashoffset>
462    fn SetLineDashOffset(&self, offset: f64) {
463        self.canvas_state.set_line_dash_offset(offset);
464    }
465
466    // https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx
467    fn ShadowOffsetX(&self) -> f64 {
468        self.canvas_state.shadow_offset_x()
469    }
470
471    // https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsetx
472    fn SetShadowOffsetX(&self, value: f64) {
473        self.canvas_state.set_shadow_offset_x(value)
474    }
475
476    // https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety
477    fn ShadowOffsetY(&self) -> f64 {
478        self.canvas_state.shadow_offset_y()
479    }
480
481    // https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowoffsety
482    fn SetShadowOffsetY(&self, value: f64) {
483        self.canvas_state.set_shadow_offset_y(value)
484    }
485
486    // https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur
487    fn ShadowBlur(&self) -> f64 {
488        self.canvas_state.shadow_blur()
489    }
490
491    // https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowblur
492    fn SetShadowBlur(&self, value: f64) {
493        self.canvas_state.set_shadow_blur(value)
494    }
495
496    // https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor
497    fn ShadowColor(&self) -> DOMString {
498        self.canvas_state.shadow_color()
499    }
500
501    // https://html.spec.whatwg.org/multipage/#dom-context-2d-shadowcolor
502    fn SetShadowColor(&self, value: DOMString) {
503        self.canvas_state.set_shadow_color(None, value)
504    }
505}