1use canvas_traits::canvas::Canvas2dMsg;
6use dom_struct::dom_struct;
7use euclid::default::Size2D;
8use pixels::Snapshot;
9
10use crate::canvas_context::{CanvasContext, HTMLCanvasElementOrOffscreenCanvas};
11use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
12 CanvasDirection, CanvasFillRule, CanvasImageSource, CanvasLineCap, CanvasLineJoin,
13 CanvasRenderingContext2DMethods, CanvasTextAlign, CanvasTextBaseline,
14};
15use crate::dom::bindings::codegen::Bindings::DOMMatrixBinding::DOMMatrix2DInit;
16use crate::dom::bindings::codegen::Bindings::OffscreenCanvasRenderingContext2DBinding::OffscreenCanvasRenderingContext2DMethods;
17use crate::dom::bindings::codegen::UnionTypes::{
18 HTMLCanvasElementOrOffscreenCanvas as RootedHTMLCanvasElementOrOffscreenCanvas,
19 StringOrCanvasGradientOrCanvasPattern,
20};
21use crate::dom::bindings::error::{ErrorResult, Fallible};
22use crate::dom::bindings::num::Finite;
23use crate::dom::bindings::reflector::reflect_dom_object;
24use crate::dom::bindings::root::{Dom, DomRoot};
25use crate::dom::bindings::str::DOMString;
26use crate::dom::canvasgradient::CanvasGradient;
27use crate::dom::canvaspattern::CanvasPattern;
28use crate::dom::canvasrenderingcontext2d::CanvasRenderingContext2D;
29use crate::dom::dommatrix::DOMMatrix;
30use crate::dom::globalscope::GlobalScope;
31use crate::dom::imagedata::ImageData;
32use crate::dom::offscreencanvas::OffscreenCanvas;
33use crate::dom::path2d::Path2D;
34use crate::dom::textmetrics::TextMetrics;
35use crate::script_runtime::CanGc;
36
37#[dom_struct]
38pub(crate) struct OffscreenCanvasRenderingContext2D {
39 context: CanvasRenderingContext2D,
40}
41
42impl OffscreenCanvasRenderingContext2D {
43 #[cfg_attr(crown, expect(crown::unrooted_must_root))]
44 fn new_inherited(
45 global: &GlobalScope,
46 canvas: HTMLCanvasElementOrOffscreenCanvas,
47 size: Size2D<u32>,
48 ) -> Option<OffscreenCanvasRenderingContext2D> {
49 Some(OffscreenCanvasRenderingContext2D {
50 context: CanvasRenderingContext2D::new_inherited(global, canvas, size)?,
51 })
52 }
53
54 pub(crate) fn new(
55 global: &GlobalScope,
56 canvas: &OffscreenCanvas,
57 size: Size2D<u32>,
58 can_gc: CanGc,
59 ) -> Option<DomRoot<OffscreenCanvasRenderingContext2D>> {
60 OffscreenCanvasRenderingContext2D::new_inherited(
61 global,
62 HTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(Dom::from_ref(canvas)),
63 size,
64 )
65 .map(|context| reflect_dom_object(Box::new(context), global, can_gc))
66 }
67
68 pub(crate) fn send_canvas_2d_msg(&self, msg: Canvas2dMsg) {
69 self.context.send_canvas_2d_msg(msg)
70 }
71}
72
73impl CanvasContext for OffscreenCanvasRenderingContext2D {
74 type ID = <CanvasRenderingContext2D as CanvasContext>::ID;
75
76 fn context_id(&self) -> Self::ID {
77 self.context.context_id()
78 }
79
80 fn canvas(&self) -> Option<RootedHTMLCanvasElementOrOffscreenCanvas> {
81 self.context.canvas()
82 }
83
84 fn resize(&self) {
85 self.context.resize()
86 }
87
88 fn reset_bitmap(&self) {
89 self.context.reset_bitmap()
90 }
91
92 fn get_image_data(&self) -> Option<Snapshot> {
93 self.context.get_image_data()
94 }
95
96 fn origin_is_clean(&self) -> bool {
97 self.context.origin_is_clean()
98 }
99
100 fn mark_as_dirty(&self) {}
101}
102
103impl OffscreenCanvasRenderingContext2DMethods<crate::DomTypeHolder>
104 for OffscreenCanvasRenderingContext2D
105{
106 fn Canvas(&self) -> DomRoot<OffscreenCanvas> {
108 match self.context.canvas() {
109 Some(RootedHTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(canvas)) => canvas,
110 _ => panic!("Should not be called from onscreen canvas"),
111 }
112 }
113
114 fn FillRect(&self, x: f64, y: f64, width: f64, height: f64) {
116 self.context.FillRect(x, y, width, height);
117 }
118
119 fn ClearRect(&self, x: f64, y: f64, width: f64, height: f64) {
121 self.context.ClearRect(x, y, width, height);
122 }
123
124 fn StrokeRect(&self, x: f64, y: f64, width: f64, height: f64) {
126 self.context.StrokeRect(x, y, width, height);
127 }
128
129 fn ShadowOffsetX(&self) -> f64 {
131 self.context.ShadowOffsetX()
132 }
133
134 fn SetShadowOffsetX(&self, value: f64) {
136 self.context.SetShadowOffsetX(value)
137 }
138
139 fn ShadowOffsetY(&self) -> f64 {
141 self.context.ShadowOffsetY()
142 }
143
144 fn SetShadowOffsetY(&self, value: f64) {
146 self.context.SetShadowOffsetY(value)
147 }
148
149 fn ShadowBlur(&self) -> f64 {
151 self.context.ShadowBlur()
152 }
153
154 fn SetShadowBlur(&self, value: f64) {
156 self.context.SetShadowBlur(value)
157 }
158
159 fn ShadowColor(&self) -> DOMString {
161 self.context.ShadowColor()
162 }
163
164 fn SetShadowColor(&self, value: DOMString) {
166 self.context.SetShadowColor(value)
167 }
168
169 fn StrokeStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
171 self.context.StrokeStyle()
172 }
173
174 fn SetStrokeStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
176 self.context.SetStrokeStyle(value)
177 }
178
179 fn FillStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
181 self.context.FillStyle()
182 }
183
184 fn SetFillStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) {
186 self.context.SetFillStyle(value)
187 }
188
189 fn CreateLinearGradient(
191 &self,
192 x0: Finite<f64>,
193 y0: Finite<f64>,
194 x1: Finite<f64>,
195 y1: Finite<f64>,
196 can_gc: CanGc,
197 ) -> DomRoot<CanvasGradient> {
198 self.context.CreateLinearGradient(x0, y0, x1, y1, can_gc)
199 }
200
201 fn CreateRadialGradient(
203 &self,
204 x0: Finite<f64>,
205 y0: Finite<f64>,
206 r0: Finite<f64>,
207 x1: Finite<f64>,
208 y1: Finite<f64>,
209 r1: Finite<f64>,
210 can_gc: CanGc,
211 ) -> Fallible<DomRoot<CanvasGradient>> {
212 self.context
213 .CreateRadialGradient(x0, y0, r0, x1, y1, r1, can_gc)
214 }
215
216 fn CreatePattern(
218 &self,
219 image: CanvasImageSource,
220 repetition: DOMString,
221 can_gc: CanGc,
222 ) -> Fallible<Option<DomRoot<CanvasPattern>>> {
223 self.context.CreatePattern(image, repetition, can_gc)
224 }
225
226 fn Save(&self) {
228 self.context.Save()
229 }
230
231 fn Restore(&self) {
233 self.context.Restore()
234 }
235
236 fn Reset(&self) {
238 self.context.Reset()
239 }
240
241 fn GlobalAlpha(&self) -> f64 {
243 self.context.GlobalAlpha()
244 }
245
246 fn SetGlobalAlpha(&self, alpha: f64) {
248 self.context.SetGlobalAlpha(alpha)
249 }
250
251 fn GlobalCompositeOperation(&self) -> DOMString {
253 self.context.GlobalCompositeOperation()
254 }
255
256 fn SetGlobalCompositeOperation(&self, op_str: DOMString) {
258 self.context.SetGlobalCompositeOperation(op_str)
259 }
260
261 fn ImageSmoothingEnabled(&self) -> bool {
263 self.context.ImageSmoothingEnabled()
264 }
265
266 fn SetImageSmoothingEnabled(&self, value: bool) {
268 self.context.SetImageSmoothingEnabled(value)
269 }
270
271 fn FillText(&self, text: DOMString, x: f64, y: f64, max_width: Option<f64>) {
273 self.context.FillText(text, x, y, max_width)
274 }
275
276 fn StrokeText(&self, text: DOMString, x: f64, y: f64, max_width: Option<f64>) {
278 self.context.StrokeText(text, x, y, max_width)
279 }
280
281 fn MeasureText(&self, text: DOMString, can_gc: CanGc) -> DomRoot<TextMetrics> {
283 self.context.MeasureText(text, can_gc)
284 }
285
286 fn Font(&self) -> DOMString {
288 self.context.Font()
289 }
290
291 fn SetFont(&self, value: DOMString) {
293 self.context.SetFont(value)
294 }
295
296 fn TextAlign(&self) -> CanvasTextAlign {
298 self.context.TextAlign()
299 }
300
301 fn SetTextAlign(&self, value: CanvasTextAlign) {
303 self.context.SetTextAlign(value)
304 }
305
306 fn TextBaseline(&self) -> CanvasTextBaseline {
308 self.context.TextBaseline()
309 }
310
311 fn SetTextBaseline(&self, value: CanvasTextBaseline) {
313 self.context.SetTextBaseline(value)
314 }
315
316 fn Direction(&self) -> CanvasDirection {
318 self.context.Direction()
319 }
320
321 fn SetDirection(&self, value: CanvasDirection) {
323 self.context.SetDirection(value)
324 }
325
326 fn LineWidth(&self) -> f64 {
328 self.context.LineWidth()
329 }
330
331 fn SetLineWidth(&self, width: f64) {
333 self.context.SetLineWidth(width)
334 }
335
336 fn LineCap(&self) -> CanvasLineCap {
338 self.context.LineCap()
339 }
340
341 fn SetLineCap(&self, cap: CanvasLineCap) {
343 self.context.SetLineCap(cap)
344 }
345
346 fn LineJoin(&self) -> CanvasLineJoin {
348 self.context.LineJoin()
349 }
350
351 fn SetLineJoin(&self, join: CanvasLineJoin) {
353 self.context.SetLineJoin(join)
354 }
355
356 fn MiterLimit(&self) -> f64 {
358 self.context.MiterLimit()
359 }
360
361 fn SetMiterLimit(&self, limit: f64) {
363 self.context.SetMiterLimit(limit)
364 }
365
366 fn SetLineDash(&self, segments: Vec<f64>) {
368 self.context.SetLineDash(segments)
369 }
370
371 fn GetLineDash(&self) -> Vec<f64> {
373 self.context.GetLineDash()
374 }
375
376 fn LineDashOffset(&self) -> f64 {
378 self.context.LineDashOffset()
379 }
380
381 fn SetLineDashOffset(&self, offset: f64) {
383 self.context.SetLineDashOffset(offset)
384 }
385
386 fn CreateImageData(&self, sw: i32, sh: i32, can_gc: CanGc) -> Fallible<DomRoot<ImageData>> {
388 self.context.CreateImageData(sw, sh, can_gc)
389 }
390
391 fn CreateImageData_(
393 &self,
394 imagedata: &ImageData,
395 can_gc: CanGc,
396 ) -> Fallible<DomRoot<ImageData>> {
397 self.context.CreateImageData_(imagedata, can_gc)
398 }
399
400 fn GetImageData(
402 &self,
403 sx: i32,
404 sy: i32,
405 sw: i32,
406 sh: i32,
407 can_gc: CanGc,
408 ) -> Fallible<DomRoot<ImageData>> {
409 self.context.GetImageData(sx, sy, sw, sh, can_gc)
410 }
411
412 fn PutImageData(&self, imagedata: &ImageData, dx: i32, dy: i32) {
414 self.context.PutImageData(imagedata, dx, dy)
415 }
416
417 fn PutImageData_(
419 &self,
420 imagedata: &ImageData,
421 dx: i32,
422 dy: i32,
423 dirty_x: i32,
424 dirty_y: i32,
425 dirty_width: i32,
426 dirty_height: i32,
427 ) {
428 self.context.PutImageData_(
429 imagedata,
430 dx,
431 dy,
432 dirty_x,
433 dirty_y,
434 dirty_width,
435 dirty_height,
436 )
437 }
438
439 fn DrawImage(&self, image: CanvasImageSource, dx: f64, dy: f64) -> ErrorResult {
441 self.context.DrawImage(image, dx, dy)
442 }
443
444 fn DrawImage_(
446 &self,
447 image: CanvasImageSource,
448 dx: f64,
449 dy: f64,
450 dw: f64,
451 dh: f64,
452 ) -> ErrorResult {
453 self.context.DrawImage_(image, dx, dy, dw, dh)
454 }
455
456 fn DrawImage__(
458 &self,
459 image: CanvasImageSource,
460 sx: f64,
461 sy: f64,
462 sw: f64,
463 sh: f64,
464 dx: f64,
465 dy: f64,
466 dw: f64,
467 dh: f64,
468 ) -> ErrorResult {
469 self.context
470 .DrawImage__(image, sx, sy, sw, sh, dx, dy, dw, dh)
471 }
472
473 fn BeginPath(&self) {
475 self.context.BeginPath()
476 }
477
478 fn Fill(&self, fill_rule: CanvasFillRule) {
480 self.context.Fill(fill_rule)
481 }
482
483 fn Fill_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
485 self.context.Fill_(path, fill_rule)
486 }
487
488 fn Stroke(&self) {
490 self.context.Stroke()
491 }
492
493 fn Stroke_(&self, path: &Path2D) {
495 self.context.Stroke_(path)
496 }
497
498 fn Clip(&self, fill_rule: CanvasFillRule) {
500 self.context.Clip(fill_rule)
501 }
502
503 fn Clip_(&self, path: &Path2D, fill_rule: CanvasFillRule) {
505 self.context.Clip_(path, fill_rule)
506 }
507
508 fn IsPointInPath(&self, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
510 self.context.IsPointInPath(x, y, fill_rule)
511 }
512
513 fn IsPointInPath_(&self, path: &Path2D, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
515 self.context.IsPointInPath_(path, x, y, fill_rule)
516 }
517
518 fn Scale(&self, x: f64, y: f64) {
520 self.context.Scale(x, y)
521 }
522
523 fn Rotate(&self, angle: f64) {
525 self.context.Rotate(angle)
526 }
527
528 fn Translate(&self, x: f64, y: f64) {
530 self.context.Translate(x, y)
531 }
532
533 fn Transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
535 self.context.Transform(a, b, c, d, e, f)
536 }
537
538 fn GetTransform(&self, can_gc: CanGc) -> DomRoot<DOMMatrix> {
540 self.context.GetTransform(can_gc)
541 }
542
543 fn SetTransform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) -> ErrorResult {
545 self.context.SetTransform(a, b, c, d, e, f)
546 }
547
548 fn SetTransform_(&self, transform: &DOMMatrix2DInit) -> ErrorResult {
550 self.context.SetTransform_(transform)
551 }
552
553 fn ResetTransform(&self) {
555 self.context.ResetTransform()
556 }
557
558 fn ClosePath(&self) {
560 self.context.ClosePath()
561 }
562
563 fn MoveTo(&self, x: f64, y: f64) {
565 self.context.MoveTo(x, y)
566 }
567
568 fn LineTo(&self, x: f64, y: f64) {
570 self.context.LineTo(x, y)
571 }
572
573 fn Rect(&self, x: f64, y: f64, width: f64, height: f64) {
575 self.context.Rect(x, y, width, height)
576 }
577
578 fn QuadraticCurveTo(&self, cpx: f64, cpy: f64, x: f64, y: f64) {
580 self.context.QuadraticCurveTo(cpx, cpy, x, y)
581 }
582
583 fn BezierCurveTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, x: f64, y: f64) {
585 self.context.BezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
586 }
587
588 fn Arc(&self, x: f64, y: f64, r: f64, start: f64, end: f64, ccw: bool) -> ErrorResult {
590 self.context.Arc(x, y, r, start, end, ccw)
591 }
592
593 fn ArcTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, r: f64) -> ErrorResult {
595 self.context.ArcTo(cp1x, cp1y, cp2x, cp2y, r)
596 }
597
598 fn Ellipse(
600 &self,
601 x: f64,
602 y: f64,
603 rx: f64,
604 ry: f64,
605 rotation: f64,
606 start: f64,
607 end: f64,
608 ccw: bool,
609 ) -> ErrorResult {
610 self.context
611 .Ellipse(x, y, rx, ry, rotation, start, end, ccw)
612 }
613}