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 {
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))
},
}
}
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))
}
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)),
)
}
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)),
)
}
fn M11(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M11()
}
fn SetM11(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m11(value);
}
fn M12(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M12()
}
fn SetM12(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m12(value);
}
fn M13(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M13()
}
fn SetM13(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m13(value);
}
fn M14(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M14()
}
fn SetM14(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m14(value);
}
fn M21(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M21()
}
fn SetM21(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m21(value);
}
fn M22(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M22()
}
fn SetM22(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m22(value);
}
fn M23(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M23()
}
fn SetM23(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m23(value);
}
fn M24(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M24()
}
fn SetM24(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m24(value);
}
fn M31(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M31()
}
fn SetM31(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m31(value);
}
fn M32(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M32()
}
fn SetM32(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m32(value);
}
fn M33(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M33()
}
fn SetM33(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m33(value);
}
fn M34(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M34()
}
fn SetM34(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m34(value);
}
fn M41(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M41()
}
fn SetM41(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m41(value);
}
fn M42(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M42()
}
fn SetM42(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m42(value);
}
fn M43(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M43()
}
fn SetM43(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m43(value);
}
fn M44(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().M44()
}
fn SetM44(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m44(value);
}
fn A(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().A()
}
fn SetA(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m11(value);
}
fn B(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().B()
}
fn SetB(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m12(value);
}
fn C(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().C()
}
fn SetC(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m21(value);
}
fn D(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().D()
}
fn SetD(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m22(value);
}
fn E(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().E()
}
fn SetE(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m41(value);
}
fn F(&self) -> f64 {
self.upcast::<DOMMatrixReadOnly>().F()
}
fn SetF(&self, value: f64) {
self.upcast::<DOMMatrixReadOnly>().set_m42(value);
}
fn MultiplySelf(&self, other: &DOMMatrixInit) -> Fallible<DomRoot<DOMMatrix>> {
self.upcast::<DOMMatrixReadOnly>()
.multiply_self(other)
.and(Ok(DomRoot::from_ref(self)))
}
fn PreMultiplySelf(&self, other: &DOMMatrixInit) -> Fallible<DomRoot<DOMMatrix>> {
self.upcast::<DOMMatrixReadOnly>()
.pre_multiply_self(other)
.and(Ok(DomRoot::from_ref(self)))
}
fn TranslateSelf(&self, tx: f64, ty: f64, tz: f64) -> DomRoot<DOMMatrix> {
self.upcast::<DOMMatrixReadOnly>()
.translate_self(tx, ty, tz);
DomRoot::from_ref(self)
}
fn ScaleSelf(
&self,
scaleX: f64,
scaleY: Option<f64>,
scaleZ: f64,
originX: f64,
originY: f64,
originZ: f64,
) -> DomRoot<DOMMatrix> {
self.upcast::<DOMMatrixReadOnly>()
.scale_self(scaleX, scaleY, scaleZ, originX, originY, originZ);
DomRoot::from_ref(self)
}
fn Scale3dSelf(
&self,
scale: f64,
originX: f64,
originY: f64,
originZ: f64,
) -> DomRoot<DOMMatrix> {
self.upcast::<DOMMatrixReadOnly>()
.scale_3d_self(scale, originX, originY, originZ);
DomRoot::from_ref(self)
}
fn RotateSelf(&self, rotX: f64, rotY: Option<f64>, rotZ: Option<f64>) -> DomRoot<DOMMatrix> {
self.upcast::<DOMMatrixReadOnly>()
.rotate_self(rotX, rotY, rotZ);
DomRoot::from_ref(self)
}
fn RotateFromVectorSelf(&self, x: f64, y: f64) -> DomRoot<DOMMatrix> {
self.upcast::<DOMMatrixReadOnly>()
.rotate_from_vector_self(x, y);
DomRoot::from_ref(self)
}
fn RotateAxisAngleSelf(&self, x: f64, y: f64, z: f64, angle: f64) -> DomRoot<DOMMatrix> {
self.upcast::<DOMMatrixReadOnly>()
.rotate_axis_angle_self(x, y, z, angle);
DomRoot::from_ref(self)
}
fn SkewXSelf(&self, sx: f64) -> DomRoot<DOMMatrix> {
self.upcast::<DOMMatrixReadOnly>().skew_x_self(sx);
DomRoot::from_ref(self)
}
fn SkewYSelf(&self, sy: f64) -> DomRoot<DOMMatrix> {
self.upcast::<DOMMatrixReadOnly>().skew_y_self(sy);
DomRoot::from_ref(self)
}
fn InvertSelf(&self) -> DomRoot<DOMMatrix> {
self.upcast::<DOMMatrixReadOnly>().invert_self();
DomRoot::from_ref(self)
}
}