use canvas_traits::canvas::{Canvas2dMsg, CanvasId, CanvasMsg};
use dom_struct::dom_struct;
use euclid::default::Size2D;
use ipc_channel::ipc::IpcSender;
use crate::canvas_state::CanvasState;
use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
CanvasDirection, CanvasFillRule, CanvasImageSource, CanvasLineCap, CanvasLineJoin,
CanvasTextAlign, CanvasTextBaseline,
};
use crate::dom::bindings::codegen::Bindings::OffscreenCanvasRenderingContext2DBinding::OffscreenCanvasRenderingContext2DMethods;
use crate::dom::bindings::codegen::UnionTypes::StringOrCanvasGradientOrCanvasPattern;
use crate::dom::bindings::error::{ErrorResult, Fallible};
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::canvasgradient::CanvasGradient;
use crate::dom::canvaspattern::CanvasPattern;
use crate::dom::dommatrix::DOMMatrix;
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlcanvaselement::HTMLCanvasElement;
use crate::dom::imagedata::ImageData;
use crate::dom::offscreencanvas::OffscreenCanvas;
use crate::dom::textmetrics::TextMetrics;
use crate::script_runtime::CanGc;
#[dom_struct]
pub struct OffscreenCanvasRenderingContext2D {
reflector_: Reflector,
canvas: Dom<OffscreenCanvas>,
canvas_state: CanvasState,
htmlcanvas: Option<Dom<HTMLCanvasElement>>,
}
impl OffscreenCanvasRenderingContext2D {
fn new_inherited(
global: &GlobalScope,
canvas: &OffscreenCanvas,
htmlcanvas: Option<&HTMLCanvasElement>,
) -> OffscreenCanvasRenderingContext2D {
OffscreenCanvasRenderingContext2D {
reflector_: Reflector::new(),
canvas: Dom::from_ref(canvas),
htmlcanvas: htmlcanvas.map(Dom::from_ref),
canvas_state: CanvasState::new(global, canvas.get_size()),
}
}
pub fn new(
global: &GlobalScope,
canvas: &OffscreenCanvas,
htmlcanvas: Option<&HTMLCanvasElement>,
) -> DomRoot<OffscreenCanvasRenderingContext2D> {
let boxed = Box::new(OffscreenCanvasRenderingContext2D::new_inherited(
global, canvas, htmlcanvas,
));
reflect_dom_object(boxed, global)
}
pub fn set_canvas_bitmap_dimensions(&self, size: Size2D<u64>) {
self.canvas_state.set_bitmap_dimensions(size);
}
pub fn send_canvas_2d_msg(&self, msg: Canvas2dMsg) {
self.canvas_state.send_canvas_2d_msg(msg)
}
pub fn origin_is_clean(&self) -> bool {
self.canvas_state.origin_is_clean()
}
pub fn get_canvas_id(&self) -> CanvasId {
self.canvas_state.get_canvas_id()
}
pub fn get_ipc_renderer(&self) -> IpcSender<CanvasMsg> {
self.canvas_state.get_ipc_renderer().clone()
}
}
impl OffscreenCanvasRenderingContext2DMethods for OffscreenCanvasRenderingContext2D {
fn Canvas(&self) -> DomRoot<OffscreenCanvas> {
DomRoot::from_ref(&self.canvas)
}
fn FillRect(&self, x: f64, y: f64, width: f64, height: f64) {
self.canvas_state.fill_rect(x, y, width, height);
}
fn ClearRect(&self, x: f64, y: f64, width: f64, height: f64) {
self.canvas_state.clear_rect(x, y, width, height);
}
fn StrokeRect(&self, x: f64, y: f64, width: f64, height: f64) {
self.canvas_state.stroke_rect(x, y, width, height);
}
fn ShadowOffsetX(&self) -> f64 {
self.canvas_state.shadow_offset_x()
}
fn SetShadowOffsetX(&self, value: f64) {
self.canvas_state.set_shadow_offset_x(value)
}
fn ShadowOffsetY(&self) -> f64 {
self.canvas_state.shadow_offset_y()
}
fn SetShadowOffsetY(&self, value: f64) {
self.canvas_state.set_shadow_offset_y(value)
}
fn ShadowBlur(&self) -> f64 {
self.canvas_state.shadow_blur()
}
fn SetShadowBlur(&self, value: f64) {
self.canvas_state.set_shadow_blur(value)
}
fn ShadowColor(&self) -> DOMString {
self.canvas_state.shadow_color()
}
fn SetShadowColor(&self, value: DOMString, can_gc: CanGc) {
self.canvas_state
.set_shadow_color(self.htmlcanvas.as_deref(), value, can_gc)
}
fn StrokeStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
self.canvas_state.stroke_style()
}
fn SetStrokeStyle(&self, value: StringOrCanvasGradientOrCanvasPattern, can_gc: CanGc) {
self.canvas_state
.set_stroke_style(self.htmlcanvas.as_deref(), value, can_gc)
}
fn FillStyle(&self) -> StringOrCanvasGradientOrCanvasPattern {
self.canvas_state.fill_style()
}
fn SetFillStyle(&self, value: StringOrCanvasGradientOrCanvasPattern, can_gc: CanGc) {
self.canvas_state
.set_fill_style(self.htmlcanvas.as_deref(), value, can_gc)
}
fn CreateLinearGradient(
&self,
x0: Finite<f64>,
y0: Finite<f64>,
x1: Finite<f64>,
y1: Finite<f64>,
) -> DomRoot<CanvasGradient> {
self.canvas_state
.create_linear_gradient(&self.global(), x0, y0, x1, y1)
}
fn CreateRadialGradient(
&self,
x0: Finite<f64>,
y0: Finite<f64>,
r0: Finite<f64>,
x1: Finite<f64>,
y1: Finite<f64>,
r1: Finite<f64>,
) -> Fallible<DomRoot<CanvasGradient>> {
self.canvas_state
.create_radial_gradient(&self.global(), x0, y0, r0, x1, y1, r1)
}
fn CreatePattern(
&self,
image: CanvasImageSource,
repetition: DOMString,
) -> Fallible<Option<DomRoot<CanvasPattern>>> {
self.canvas_state
.create_pattern(&self.global(), image, repetition)
}
fn Save(&self) {
self.canvas_state.save()
}
#[allow(crown::unrooted_must_root)]
fn Restore(&self) {
self.canvas_state.restore()
}
fn Reset(&self) {
self.canvas_state.reset()
}
fn GlobalAlpha(&self) -> f64 {
self.canvas_state.global_alpha()
}
fn SetGlobalAlpha(&self, alpha: f64) {
self.canvas_state.set_global_alpha(alpha)
}
fn GlobalCompositeOperation(&self) -> DOMString {
self.canvas_state.global_composite_operation()
}
fn SetGlobalCompositeOperation(&self, op_str: DOMString) {
self.canvas_state.set_global_composite_operation(op_str)
}
fn ImageSmoothingEnabled(&self) -> bool {
self.canvas_state.image_smoothing_enabled()
}
fn SetImageSmoothingEnabled(&self, value: bool) {
self.canvas_state.set_image_smoothing_enabled(value)
}
fn FillText(&self, text: DOMString, x: f64, y: f64, max_width: Option<f64>, can_gc: CanGc) {
self.canvas_state
.fill_text(self.htmlcanvas.as_deref(), text, x, y, max_width, can_gc)
}
fn MeasureText(&self, text: DOMString, can_gc: CanGc) -> DomRoot<TextMetrics> {
self.canvas_state
.measure_text(&self.global(), self.htmlcanvas.as_deref(), text, can_gc)
}
fn Font(&self) -> DOMString {
self.canvas_state.font()
}
fn SetFont(&self, value: DOMString, can_gc: CanGc) {
self.canvas_state
.set_font(self.htmlcanvas.as_deref(), value, can_gc)
}
fn TextAlign(&self) -> CanvasTextAlign {
self.canvas_state.text_align()
}
fn SetTextAlign(&self, value: CanvasTextAlign) {
self.canvas_state.set_text_align(value)
}
fn TextBaseline(&self) -> CanvasTextBaseline {
self.canvas_state.text_baseline()
}
fn SetTextBaseline(&self, value: CanvasTextBaseline) {
self.canvas_state.set_text_baseline(value)
}
fn Direction(&self) -> CanvasDirection {
self.canvas_state.direction()
}
fn SetDirection(&self, value: CanvasDirection) {
self.canvas_state.set_direction(value)
}
fn LineWidth(&self) -> f64 {
self.canvas_state.line_width()
}
fn SetLineWidth(&self, width: f64) {
self.canvas_state.set_line_width(width)
}
fn LineCap(&self) -> CanvasLineCap {
self.canvas_state.line_cap()
}
fn SetLineCap(&self, cap: CanvasLineCap) {
self.canvas_state.set_line_cap(cap)
}
fn LineJoin(&self) -> CanvasLineJoin {
self.canvas_state.line_join()
}
fn SetLineJoin(&self, join: CanvasLineJoin) {
self.canvas_state.set_line_join(join)
}
fn MiterLimit(&self) -> f64 {
self.canvas_state.miter_limit()
}
fn SetMiterLimit(&self, limit: f64) {
self.canvas_state.set_miter_limit(limit)
}
fn CreateImageData(&self, sw: i32, sh: i32, can_gc: CanGc) -> Fallible<DomRoot<ImageData>> {
self.canvas_state
.create_image_data(&self.global(), sw, sh, can_gc)
}
fn CreateImageData_(
&self,
imagedata: &ImageData,
can_gc: CanGc,
) -> Fallible<DomRoot<ImageData>> {
self.canvas_state
.create_image_data_(&self.global(), imagedata, can_gc)
}
fn GetImageData(
&self,
sx: i32,
sy: i32,
sw: i32,
sh: i32,
can_gc: CanGc,
) -> Fallible<DomRoot<ImageData>> {
self.canvas_state.get_image_data(
self.canvas.get_size(),
&self.global(),
sx,
sy,
sw,
sh,
can_gc,
)
}
fn PutImageData(&self, imagedata: &ImageData, dx: i32, dy: i32) {
self.canvas_state
.put_image_data(self.canvas.get_size(), imagedata, dx, dy)
}
#[allow(unsafe_code)]
fn PutImageData_(
&self,
imagedata: &ImageData,
dx: i32,
dy: i32,
dirty_x: i32,
dirty_y: i32,
dirty_width: i32,
dirty_height: i32,
) {
self.canvas_state.put_image_data_(
self.canvas.get_size(),
imagedata,
dx,
dy,
dirty_x,
dirty_y,
dirty_width,
dirty_height,
)
}
fn DrawImage(&self, image: CanvasImageSource, dx: f64, dy: f64) -> ErrorResult {
self.canvas_state
.draw_image(self.htmlcanvas.as_deref(), image, dx, dy)
}
fn DrawImage_(
&self,
image: CanvasImageSource,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
) -> ErrorResult {
self.canvas_state
.draw_image_(self.htmlcanvas.as_deref(), image, dx, dy, dw, dh)
}
fn DrawImage__(
&self,
image: CanvasImageSource,
sx: f64,
sy: f64,
sw: f64,
sh: f64,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
) -> ErrorResult {
self.canvas_state.draw_image__(
self.htmlcanvas.as_deref(),
image,
sx,
sy,
sw,
sh,
dx,
dy,
dw,
dh,
)
}
fn BeginPath(&self) {
self.canvas_state.begin_path()
}
fn Fill(&self, fill_rule: CanvasFillRule) {
self.canvas_state.fill(fill_rule)
}
fn Stroke(&self) {
self.canvas_state.stroke()
}
fn Clip(&self, fill_rule: CanvasFillRule) {
self.canvas_state.clip(fill_rule)
}
fn IsPointInPath(&self, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool {
self.canvas_state
.is_point_in_path(&self.global(), x, y, fill_rule)
}
fn Scale(&self, x: f64, y: f64) {
self.canvas_state.scale(x, y)
}
fn Rotate(&self, angle: f64) {
self.canvas_state.rotate(angle)
}
fn Translate(&self, x: f64, y: f64) {
self.canvas_state.translate(x, y)
}
fn Transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
self.canvas_state.transform(a, b, c, d, e, f)
}
fn GetTransform(&self, can_gc: CanGc) -> DomRoot<DOMMatrix> {
self.canvas_state.get_transform(&self.global(), can_gc)
}
fn SetTransform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
self.canvas_state.set_transform(a, b, c, d, e, f)
}
fn ResetTransform(&self) {
self.canvas_state.reset_transform()
}
fn ClosePath(&self) {
self.canvas_state.close_path()
}
fn MoveTo(&self, x: f64, y: f64) {
self.canvas_state.move_to(x, y)
}
fn LineTo(&self, x: f64, y: f64) {
self.canvas_state.line_to(x, y)
}
fn Rect(&self, x: f64, y: f64, width: f64, height: f64) {
self.canvas_state.rect(x, y, width, height)
}
fn QuadraticCurveTo(&self, cpx: f64, cpy: f64, x: f64, y: f64) {
self.canvas_state.quadratic_curve_to(cpx, cpy, x, y)
}
fn BezierCurveTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, x: f64, y: f64) {
self.canvas_state
.bezier_curve_to(cp1x, cp1y, cp2x, cp2y, x, y)
}
fn Arc(&self, x: f64, y: f64, r: f64, start: f64, end: f64, ccw: bool) -> ErrorResult {
self.canvas_state.arc(x, y, r, start, end, ccw)
}
fn ArcTo(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, r: f64) -> ErrorResult {
self.canvas_state.arc_to(cp1x, cp1y, cp2x, cp2y, r)
}
fn Ellipse(
&self,
x: f64,
y: f64,
rx: f64,
ry: f64,
rotation: f64,
start: f64,
end: f64,
ccw: bool,
) -> ErrorResult {
self.canvas_state
.ellipse(x, y, rx, ry, rotation, start, end, ccw)
}
}