1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use dom_struct::dom_struct;
use euclid::default::Transform3D;
use js::rust::{CustomAutoRooterGuard, HandleObject};
use js::typedarray::{Float32Array, Float64Array};

use crate::dom::bindings::codegen::Bindings::DOMMatrixBinding::{DOMMatrixInit, DOMMatrixMethods};
use crate::dom::bindings::codegen::Bindings::DOMMatrixReadOnlyBinding::DOMMatrixReadOnlyMethods;
use crate::dom::bindings::codegen::UnionTypes::StringOrUnrestrictedDoubleSequence;
use crate::dom::bindings::error;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
use crate::dom::bindings::root::DomRoot;
use crate::dom::dommatrixreadonly::{
    dommatrixinit_to_matrix, entries_to_matrix, transform_to_matrix, DOMMatrixReadOnly,
};
use crate::dom::globalscope::GlobalScope;
use crate::dom::window::Window;
use crate::script_runtime::CanGc;

#[dom_struct]
pub struct DOMMatrix {
    parent: DOMMatrixReadOnly,
}

#[allow(non_snake_case)]
impl DOMMatrix {
    pub fn new(
        global: &GlobalScope,
        is2D: bool,
        matrix: Transform3D<f64>,
        can_gc: CanGc,
    ) -> DomRoot<Self> {
        Self::new_with_proto(global, None, is2D, matrix, can_gc)
    }

    #[allow(crown::unrooted_must_root)]
    fn new_with_proto(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        is2D: bool,
        matrix: Transform3D<f64>,
        can_gc: CanGc,
    ) -> DomRoot<Self> {
        let dommatrix = Self::new_inherited(is2D, matrix);
        reflect_dom_object_with_proto(Box::new(dommatrix), global, proto, can_gc)
    }

    pub fn new_inherited(is2D: bool, matrix: Transform3D<f64>) -> Self {
        DOMMatrix {
            parent: DOMMatrixReadOnly::new_inherited(is2D, matrix),
        }
    }

    pub fn from_readonly(
        global: &GlobalScope,
        ro: &DOMMatrixReadOnly,
        can_gc: CanGc,
    ) -> DomRoot<Self> {
        Self::new(global, ro.is2D(), *ro.matrix(), can_gc)
    }
}

#[allow(non_snake_case)]
impl DOMMatrixMethods<crate::DomTypeHolder> for DOMMatrix {
    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-dommatrixreadonly
    fn Constructor(
        global: &GlobalScope,
        proto: Option<HandleObject>,
        can_gc: CanGc,
        init: Option<StringOrUnrestrictedDoubleSequence>,
    ) -> Fallible<DomRoot<Self>> {
        if init.is_none() {
            return Ok(Self::new_with_proto(
                global,
                proto,
                true,
                Transform3D::identity(),
                can_gc,
            ));
        }
        match init.unwrap() {
            StringOrUnrestrictedDoubleSequence::String(ref s) => {
                if !global.is::<Window>() {
                    return Err(error::Error::Type(
                        "String constructor is only supported in the main thread.".to_owned(),
                    ));
                }
                if s.is_empty() {
                    return Ok(Self::new(global, true, Transform3D::identity(), can_gc));
                }
                transform_to_matrix(s.to_string())
                    .map(|(is2D, matrix)| Self::new_with_proto(global, proto, is2D, matrix, can_gc))
            },
            StringOrUnrestrictedDoubleSequence::UnrestrictedDoubleSequence(ref entries) => {
                entries_to_matrix(&entries[..])
                    .map(|(is2D, matrix)| Self::new_with_proto(global, proto, is2D, matrix, can_gc))
            },
        }
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-frommatrix
    fn FromMatrix(
        global: &GlobalScope,
        other: &DOMMatrixInit,
        can_gc: CanGc,
    ) -> Fallible<DomRoot<Self>> {
        dommatrixinit_to_matrix(other).map(|(is2D, matrix)| Self::new(global, is2D, matrix, can_gc))
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-fromfloat32array
    fn FromFloat32Array(
        global: &GlobalScope,
        array: CustomAutoRooterGuard<Float32Array>,
        can_gc: CanGc,
    ) -> Fallible<DomRoot<DOMMatrix>> {
        let vec: Vec<f64> = array.to_vec().iter().map(|&x| x as f64).collect();
        DOMMatrix::Constructor(
            global,
            None,
            can_gc,
            Some(StringOrUnrestrictedDoubleSequence::UnrestrictedDoubleSequence(vec)),
        )
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-fromfloat64array
    fn FromFloat64Array(
        global: &GlobalScope,
        array: CustomAutoRooterGuard<Float64Array>,
        can_gc: CanGc,
    ) -> Fallible<DomRoot<DOMMatrix>> {
        let vec: Vec<f64> = array.to_vec();
        DOMMatrix::Constructor(
            global,
            None,
            can_gc,
            Some(StringOrUnrestrictedDoubleSequence::UnrestrictedDoubleSequence(vec)),
        )
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m11
    fn M11(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M11()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m11
    fn SetM11(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m11(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m12
    fn M12(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M12()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m12
    fn SetM12(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m12(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m13
    fn M13(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M13()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m13
    fn SetM13(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m13(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m14
    fn M14(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M14()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m14
    fn SetM14(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m14(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m21
    fn M21(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M21()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m21
    fn SetM21(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m21(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m22
    fn M22(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M22()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m22
    fn SetM22(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m22(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m23
    fn M23(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M23()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m23
    fn SetM23(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m23(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m24
    fn M24(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M24()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m24
    fn SetM24(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m24(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m31
    fn M31(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M31()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m31
    fn SetM31(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m31(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m32
    fn M32(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M32()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m32
    fn SetM32(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m32(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m33
    fn M33(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M33()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m33
    fn SetM33(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m33(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m34
    fn M34(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M34()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m34
    fn SetM34(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m34(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m41
    fn M41(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M41()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m41
    fn SetM41(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m41(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m42
    fn M42(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M42()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m42
    fn SetM42(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m42(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m43
    fn M43(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M43()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m43
    fn SetM43(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m43(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m44
    fn M44(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().M44()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m44
    fn SetM44(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m44(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-a
    fn A(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().A()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-a
    fn SetA(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m11(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-b
    fn B(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().B()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-b
    fn SetB(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m12(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-c
    fn C(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().C()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-c
    fn SetC(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m21(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-d
    fn D(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().D()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-d
    fn SetD(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m22(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-e
    fn E(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().E()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-e
    fn SetE(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m41(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-f
    fn F(&self) -> f64 {
        self.upcast::<DOMMatrixReadOnly>().F()
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-f
    fn SetF(&self, value: f64) {
        self.upcast::<DOMMatrixReadOnly>().set_m42(value);
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-multiplyself
    fn MultiplySelf(&self, other: &DOMMatrixInit) -> Fallible<DomRoot<DOMMatrix>> {
        // Steps 1-3.
        self.upcast::<DOMMatrixReadOnly>()
            .multiply_self(other)
            // Step 4.
            .and(Ok(DomRoot::from_ref(self)))
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-premultiplyself
    fn PreMultiplySelf(&self, other: &DOMMatrixInit) -> Fallible<DomRoot<DOMMatrix>> {
        // Steps 1-3.
        self.upcast::<DOMMatrixReadOnly>()
            .pre_multiply_self(other)
            // Step 4.
            .and(Ok(DomRoot::from_ref(self)))
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-translateself
    fn TranslateSelf(&self, tx: f64, ty: f64, tz: f64) -> DomRoot<DOMMatrix> {
        // Steps 1-2.
        self.upcast::<DOMMatrixReadOnly>()
            .translate_self(tx, ty, tz);
        // Step 3.
        DomRoot::from_ref(self)
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-scaleself
    fn ScaleSelf(
        &self,
        scaleX: f64,
        scaleY: Option<f64>,
        scaleZ: f64,
        originX: f64,
        originY: f64,
        originZ: f64,
    ) -> DomRoot<DOMMatrix> {
        // Steps 1-6.
        self.upcast::<DOMMatrixReadOnly>()
            .scale_self(scaleX, scaleY, scaleZ, originX, originY, originZ);
        // Step 7.
        DomRoot::from_ref(self)
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-scale3dself
    fn Scale3dSelf(
        &self,
        scale: f64,
        originX: f64,
        originY: f64,
        originZ: f64,
    ) -> DomRoot<DOMMatrix> {
        // Steps 1-4.
        self.upcast::<DOMMatrixReadOnly>()
            .scale_3d_self(scale, originX, originY, originZ);
        // Step 5.
        DomRoot::from_ref(self)
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-rotateself
    fn RotateSelf(&self, rotX: f64, rotY: Option<f64>, rotZ: Option<f64>) -> DomRoot<DOMMatrix> {
        // Steps 1-7.
        self.upcast::<DOMMatrixReadOnly>()
            .rotate_self(rotX, rotY, rotZ);
        // Step 8.
        DomRoot::from_ref(self)
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-rotatefromvectorself
    fn RotateFromVectorSelf(&self, x: f64, y: f64) -> DomRoot<DOMMatrix> {
        // Step 1.
        self.upcast::<DOMMatrixReadOnly>()
            .rotate_from_vector_self(x, y);
        // Step 2.
        DomRoot::from_ref(self)
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-rotateaxisangleself
    fn RotateAxisAngleSelf(&self, x: f64, y: f64, z: f64, angle: f64) -> DomRoot<DOMMatrix> {
        // Steps 1-2.
        self.upcast::<DOMMatrixReadOnly>()
            .rotate_axis_angle_self(x, y, z, angle);
        // Step 3.
        DomRoot::from_ref(self)
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-skewxself
    fn SkewXSelf(&self, sx: f64) -> DomRoot<DOMMatrix> {
        // Step 1.
        self.upcast::<DOMMatrixReadOnly>().skew_x_self(sx);
        // Step 2.
        DomRoot::from_ref(self)
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-skewyself
    fn SkewYSelf(&self, sy: f64) -> DomRoot<DOMMatrix> {
        // Step 1.
        self.upcast::<DOMMatrixReadOnly>().skew_y_self(sy);
        // Step 2.
        DomRoot::from_ref(self)
    }

    // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-invertself
    fn InvertSelf(&self) -> DomRoot<DOMMatrix> {
        // Steps 1-2.
        self.upcast::<DOMMatrixReadOnly>().invert_self();
        // Step 3.
        DomRoot::from_ref(self)
    }
}