use std::cell::Cell;
use std::{f64, ptr};
use cssparser::{Parser, ParserInput};
use dom_struct::dom_struct;
use euclid::default::Transform3D;
use euclid::Angle;
use js::jsapi::JSObject;
use js::rust::{CustomAutoRooterGuard, HandleObject};
use js::typedarray::{Float32Array, Float64Array};
use style::parser::ParserContext;
use url::Url;
use crate::dom::bindings::buffer_source::create_buffer_source;
use crate::dom::bindings::cell::{DomRefCell, Ref};
use crate::dom::bindings::codegen::Bindings::DOMMatrixBinding::{DOMMatrixInit, DOMMatrixMethods};
use crate::dom::bindings::codegen::Bindings::DOMMatrixReadOnlyBinding::DOMMatrixReadOnlyMethods;
use crate::dom::bindings::codegen::Bindings::DOMPointBinding::DOMPointInit;
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, DomObject, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::dommatrix::DOMMatrix;
use crate::dom::dompoint::DOMPoint;
use crate::dom::globalscope::GlobalScope;
use crate::dom::window::Window;
use crate::script_runtime::JSContext;
#[dom_struct]
#[allow(non_snake_case)]
pub struct DOMMatrixReadOnly {
reflector_: Reflector,
#[no_trace]
matrix: DomRefCell<Transform3D<f64>>,
is2D: Cell<bool>,
}
#[allow(non_snake_case)]
impl DOMMatrixReadOnly {
pub fn new(global: &GlobalScope, is2D: bool, matrix: Transform3D<f64>) -> DomRoot<Self> {
Self::new_with_proto(global, None, is2D, matrix)
}
#[allow(crown::unrooted_must_root)]
fn new_with_proto(
global: &GlobalScope,
proto: Option<HandleObject>,
is2D: bool,
matrix: Transform3D<f64>,
) -> DomRoot<Self> {
let dommatrix = Self::new_inherited(is2D, matrix);
reflect_dom_object_with_proto(Box::new(dommatrix), global, proto)
}
pub fn new_inherited(is2D: bool, matrix: Transform3D<f64>) -> Self {
DOMMatrixReadOnly {
reflector_: Reflector::new(),
matrix: DomRefCell::new(matrix),
is2D: Cell::new(is2D),
}
}
pub fn Constructor(
global: &GlobalScope,
proto: Option<HandleObject>,
init: Option<StringOrUnrestrictedDoubleSequence>,
) -> Fallible<DomRoot<Self>> {
if init.is_none() {
return Ok(Self::new_with_proto(
global,
proto,
true,
Transform3D::identity(),
));
}
match init.unwrap() {
StringOrUnrestrictedDoubleSequence::String(ref s) => {
if global.downcast::<Window>().is_none() {
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()));
}
transform_to_matrix(s.to_string())
.map(|(is2D, matrix)| Self::new_with_proto(global, proto, is2D, matrix))
},
StringOrUnrestrictedDoubleSequence::UnrestrictedDoubleSequence(ref entries) => {
entries_to_matrix(&entries[..])
.map(|(is2D, matrix)| Self::new_with_proto(global, proto, is2D, matrix))
},
}
}
pub fn FromMatrix(global: &GlobalScope, other: &DOMMatrixInit) -> Fallible<DomRoot<Self>> {
dommatrixinit_to_matrix(other).map(|(is2D, matrix)| Self::new(global, is2D, matrix))
}
pub fn matrix(&self) -> Ref<Transform3D<f64>> {
self.matrix.borrow()
}
pub fn is2D(&self) -> bool {
self.is2D.get()
}
pub fn set_m11(&self, value: f64) {
self.matrix.borrow_mut().m11 = value;
}
pub fn set_m12(&self, value: f64) {
self.matrix.borrow_mut().m12 = value;
}
pub fn set_m13(&self, value: f64) {
self.matrix.borrow_mut().m13 = value;
}
pub fn set_m14(&self, value: f64) {
self.matrix.borrow_mut().m14 = value;
}
pub fn set_m21(&self, value: f64) {
self.matrix.borrow_mut().m21 = value;
}
pub fn set_m22(&self, value: f64) {
self.matrix.borrow_mut().m22 = value;
}
pub fn set_m23(&self, value: f64) {
self.matrix.borrow_mut().m23 = value;
}
pub fn set_m24(&self, value: f64) {
self.matrix.borrow_mut().m24 = value;
}
pub fn set_m31(&self, value: f64) {
self.matrix.borrow_mut().m31 = value;
}
pub fn set_m32(&self, value: f64) {
self.matrix.borrow_mut().m32 = value;
}
pub fn set_m33(&self, value: f64) {
self.matrix.borrow_mut().m33 = value;
}
pub fn set_m34(&self, value: f64) {
self.matrix.borrow_mut().m34 = value;
}
pub fn set_m41(&self, value: f64) {
self.matrix.borrow_mut().m41 = value;
}
pub fn set_m42(&self, value: f64) {
self.matrix.borrow_mut().m42 = value;
}
pub fn set_m43(&self, value: f64) {
self.matrix.borrow_mut().m43 = value;
}
pub fn set_m44(&self, value: f64) {
self.matrix.borrow_mut().m44 = value;
}
pub fn multiply_self(&self, other: &DOMMatrixInit) -> Fallible<()> {
dommatrixinit_to_matrix(other).map(|(is2D, other_matrix)| {
let mut matrix = self.matrix.borrow_mut();
*matrix = other_matrix.then(&matrix);
if !is2D {
self.is2D.set(false);
}
})
}
pub fn pre_multiply_self(&self, other: &DOMMatrixInit) -> Fallible<()> {
dommatrixinit_to_matrix(other).map(|(is2D, other_matrix)| {
let mut matrix = self.matrix.borrow_mut();
*matrix = matrix.then(&other_matrix);
if !is2D {
self.is2D.set(false);
}
})
}
pub fn translate_self(&self, tx: f64, ty: f64, tz: f64) {
let translation = Transform3D::translation(tx, ty, tz);
let mut matrix = self.matrix.borrow_mut();
*matrix = translation.then(&matrix);
if tz != 0.0 {
self.is2D.set(false);
}
}
pub fn scale_self(
&self,
scaleX: f64,
scaleY: Option<f64>,
scaleZ: f64,
mut originX: f64,
mut originY: f64,
mut originZ: f64,
) {
self.translate_self(originX, originY, originZ);
let scaleY = scaleY.unwrap_or(scaleX);
{
let scale3D = Transform3D::scale(scaleX, scaleY, scaleZ);
let mut matrix = self.matrix.borrow_mut();
*matrix = scale3D.then(&matrix);
}
originX = -originX;
originY = -originY;
originZ = -originZ;
self.translate_self(originX, originY, originZ);
if scaleZ != 1.0 || originZ != 0.0 {
self.is2D.set(false);
}
}
pub fn scale_3d_self(&self, scale: f64, originX: f64, originY: f64, originZ: f64) {
self.translate_self(originX, originY, originZ);
{
let scale3D = Transform3D::scale(scale, scale, scale);
let mut matrix = self.matrix.borrow_mut();
*matrix = scale3D.then(&matrix);
}
self.translate_self(-originX, -originY, -originZ);
if scale != 1.0 {
self.is2D.set(false);
}
}
pub fn rotate_self(&self, mut rotX: f64, mut rotY: Option<f64>, mut rotZ: Option<f64>) {
if rotY.is_none() && rotZ.is_none() {
rotZ = Some(rotX);
rotX = 0.0;
rotY = Some(0.0);
}
let rotY = rotY.unwrap_or(0.0);
let rotZ = rotZ.unwrap_or(0.0);
if rotX != 0.0 || rotY != 0.0 {
self.is2D.set(false);
}
if rotZ != 0.0 {
let rotation = Transform3D::rotation(0.0, 0.0, 1.0, Angle::radians(rotZ.to_radians()));
let mut matrix = self.matrix.borrow_mut();
*matrix = rotation.then(&matrix);
}
if rotY != 0.0 {
let rotation = Transform3D::rotation(0.0, 1.0, 0.0, Angle::radians(rotY.to_radians()));
let mut matrix = self.matrix.borrow_mut();
*matrix = rotation.then(&matrix);
}
if rotX != 0.0 {
let rotation = Transform3D::rotation(1.0, 0.0, 0.0, Angle::radians(rotX.to_radians()));
let mut matrix = self.matrix.borrow_mut();
*matrix = rotation.then(&matrix);
}
}
pub fn rotate_from_vector_self(&self, x: f64, y: f64) {
if y != 0.0 || x < 0.0 {
let rotZ = Angle::radians(f64::atan2(y, x));
let rotation = Transform3D::rotation(0.0, 0.0, 1.0, rotZ);
let mut matrix = self.matrix.borrow_mut();
*matrix = rotation.then(&matrix);
}
}
pub fn rotate_axis_angle_self(&self, x: f64, y: f64, z: f64, angle: f64) {
let (norm_x, norm_y, norm_z) = normalize_point(x, y, z);
let rotation =
Transform3D::rotation(norm_x, norm_y, norm_z, Angle::radians(angle.to_radians()));
let mut matrix = self.matrix.borrow_mut();
*matrix = rotation.then(&matrix);
if x != 0.0 || y != 0.0 {
self.is2D.set(false);
}
}
pub fn skew_x_self(&self, sx: f64) {
let skew = Transform3D::skew(Angle::radians(sx.to_radians()), Angle::radians(0.0));
let mut matrix = self.matrix.borrow_mut();
*matrix = skew.then(&matrix);
}
pub fn skew_y_self(&self, sy: f64) {
let skew = Transform3D::skew(Angle::radians(0.0), Angle::radians(sy.to_radians()));
let mut matrix = self.matrix.borrow_mut();
*matrix = skew.then(&matrix);
}
pub fn invert_self(&self) {
let mut matrix = self.matrix.borrow_mut();
*matrix = matrix.inverse().unwrap_or_else(|| {
self.is2D.set(false);
Transform3D::new(
f64::NAN,
f64::NAN,
f64::NAN,
f64::NAN,
f64::NAN,
f64::NAN,
f64::NAN,
f64::NAN,
f64::NAN,
f64::NAN,
f64::NAN,
f64::NAN,
f64::NAN,
f64::NAN,
f64::NAN,
f64::NAN,
)
})
}
#[allow(unsafe_code)]
pub fn FromFloat32Array(
global: &GlobalScope,
array: CustomAutoRooterGuard<Float32Array>,
) -> Fallible<DomRoot<DOMMatrixReadOnly>> {
let vec: Vec<f64> = array.to_vec().iter().map(|&x| x as f64).collect();
DOMMatrixReadOnly::Constructor(
global,
None,
Some(StringOrUnrestrictedDoubleSequence::UnrestrictedDoubleSequence(vec)),
)
}
#[allow(unsafe_code)]
pub fn FromFloat64Array(
global: &GlobalScope,
array: CustomAutoRooterGuard<Float64Array>,
) -> Fallible<DomRoot<DOMMatrixReadOnly>> {
let vec: Vec<f64> = array.to_vec();
DOMMatrixReadOnly::Constructor(
global,
None,
Some(StringOrUnrestrictedDoubleSequence::UnrestrictedDoubleSequence(vec)),
)
}
}
#[allow(non_snake_case)]
impl DOMMatrixReadOnlyMethods for DOMMatrixReadOnly {
fn M11(&self) -> f64 {
self.matrix.borrow().m11
}
fn M12(&self) -> f64 {
self.matrix.borrow().m12
}
fn M13(&self) -> f64 {
self.matrix.borrow().m13
}
fn M14(&self) -> f64 {
self.matrix.borrow().m14
}
fn M21(&self) -> f64 {
self.matrix.borrow().m21
}
fn M22(&self) -> f64 {
self.matrix.borrow().m22
}
fn M23(&self) -> f64 {
self.matrix.borrow().m23
}
fn M24(&self) -> f64 {
self.matrix.borrow().m24
}
fn M31(&self) -> f64 {
self.matrix.borrow().m31
}
fn M32(&self) -> f64 {
self.matrix.borrow().m32
}
fn M33(&self) -> f64 {
self.matrix.borrow().m33
}
fn M34(&self) -> f64 {
self.matrix.borrow().m34
}
fn M41(&self) -> f64 {
self.matrix.borrow().m41
}
fn M42(&self) -> f64 {
self.matrix.borrow().m42
}
fn M43(&self) -> f64 {
self.matrix.borrow().m43
}
fn M44(&self) -> f64 {
self.matrix.borrow().m44
}
fn A(&self) -> f64 {
self.M11()
}
fn B(&self) -> f64 {
self.M12()
}
fn C(&self) -> f64 {
self.M21()
}
fn D(&self) -> f64 {
self.M22()
}
fn E(&self) -> f64 {
self.M41()
}
fn F(&self) -> f64 {
self.M42()
}
fn Is2D(&self) -> bool {
self.is2D.get()
}
fn IsIdentity(&self) -> bool {
let matrix = self.matrix.borrow();
matrix.m12 == 0.0 &&
matrix.m13 == 0.0 &&
matrix.m14 == 0.0 &&
matrix.m21 == 0.0 &&
matrix.m23 == 0.0 &&
matrix.m24 == 0.0 &&
matrix.m31 == 0.0 &&
matrix.m32 == 0.0 &&
matrix.m34 == 0.0 &&
matrix.m41 == 0.0 &&
matrix.m42 == 0.0 &&
matrix.m43 == 0.0 &&
matrix.m11 == 1.0 &&
matrix.m22 == 1.0 &&
matrix.m33 == 1.0 &&
matrix.m44 == 1.0
}
fn Translate(&self, tx: f64, ty: f64, tz: f64) -> DomRoot<DOMMatrix> {
DOMMatrix::from_readonly(&self.global(), self).TranslateSelf(tx, ty, tz)
}
fn Scale(
&self,
scaleX: f64,
scaleY: Option<f64>,
scaleZ: f64,
originX: f64,
originY: f64,
originZ: f64,
) -> DomRoot<DOMMatrix> {
DOMMatrix::from_readonly(&self.global(), self)
.ScaleSelf(scaleX, scaleY, scaleZ, originX, originY, originZ)
}
fn ScaleNonUniform(&self, scaleX: f64, scaleY: f64) -> DomRoot<DOMMatrix> {
DOMMatrix::from_readonly(&self.global(), self).ScaleSelf(
scaleX,
Some(scaleY),
1.0,
0.0,
0.0,
0.0,
)
}
fn Scale3d(&self, scale: f64, originX: f64, originY: f64, originZ: f64) -> DomRoot<DOMMatrix> {
DOMMatrix::from_readonly(&self.global(), self).Scale3dSelf(scale, originX, originY, originZ)
}
fn Rotate(&self, rotX: f64, rotY: Option<f64>, rotZ: Option<f64>) -> DomRoot<DOMMatrix> {
DOMMatrix::from_readonly(&self.global(), self).RotateSelf(rotX, rotY, rotZ)
}
fn RotateFromVector(&self, x: f64, y: f64) -> DomRoot<DOMMatrix> {
DOMMatrix::from_readonly(&self.global(), self).RotateFromVectorSelf(x, y)
}
fn RotateAxisAngle(&self, x: f64, y: f64, z: f64, angle: f64) -> DomRoot<DOMMatrix> {
DOMMatrix::from_readonly(&self.global(), self).RotateAxisAngleSelf(x, y, z, angle)
}
fn SkewX(&self, sx: f64) -> DomRoot<DOMMatrix> {
DOMMatrix::from_readonly(&self.global(), self).SkewXSelf(sx)
}
fn SkewY(&self, sy: f64) -> DomRoot<DOMMatrix> {
DOMMatrix::from_readonly(&self.global(), self).SkewYSelf(sy)
}
fn Multiply(&self, other: &DOMMatrixInit) -> Fallible<DomRoot<DOMMatrix>> {
DOMMatrix::from_readonly(&self.global(), self).MultiplySelf(other)
}
fn FlipX(&self) -> DomRoot<DOMMatrix> {
let is2D = self.is2D.get();
let flip = Transform3D::new(
-1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
);
let matrix = flip.then(&self.matrix.borrow());
DOMMatrix::new(&self.global(), is2D, matrix)
}
fn FlipY(&self) -> DomRoot<DOMMatrix> {
let is2D = self.is2D.get();
let flip = Transform3D::new(
1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
);
let matrix = flip.then(&self.matrix.borrow());
DOMMatrix::new(&self.global(), is2D, matrix)
}
fn Inverse(&self) -> DomRoot<DOMMatrix> {
DOMMatrix::from_readonly(&self.global(), self).InvertSelf()
}
fn TransformPoint(&self, point: &DOMPointInit) -> DomRoot<DOMPoint> {
let mat = self.matrix.borrow();
let x = point.x * mat.m11 + point.y * mat.m21 + point.z * mat.m31 + point.w * mat.m41;
let y = point.x * mat.m12 + point.y * mat.m22 + point.z * mat.m32 + point.w * mat.m42;
let z = point.x * mat.m13 + point.y * mat.m23 + point.z * mat.m33 + point.w * mat.m43;
let w = point.x * mat.m14 + point.y * mat.m24 + point.z * mat.m34 + point.w * mat.m44;
DOMPoint::new(&self.global(), x, y, z, w)
}
fn ToFloat32Array(&self, cx: JSContext) -> Float32Array {
let vec: Vec<f32> = self
.matrix
.borrow()
.to_array()
.iter()
.map(|&x| x as f32)
.collect();
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>());
create_buffer_source(cx, &vec, array.handle_mut())
.expect("Converting matrix to float32 array should never fail")
}
fn ToFloat64Array(&self, cx: JSContext) -> Float64Array {
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>());
create_buffer_source(cx, &self.matrix.borrow().to_array(), array.handle_mut())
.expect("Converting matrix to float64 array should never fail")
}
}
fn create_2d_matrix(entries: &[f64]) -> Transform3D<f64> {
Transform3D::new(
entries[0], entries[1], 0.0, 0.0, entries[2], entries[3], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0,
entries[4], entries[5], 0.0, 1.0,
)
}
fn create_3d_matrix(entries: &[f64]) -> Transform3D<f64> {
Transform3D::new(
entries[0],
entries[1],
entries[2],
entries[3],
entries[4],
entries[5],
entries[6],
entries[7],
entries[8],
entries[9],
entries[10],
entries[11],
entries[12],
entries[13],
entries[14],
entries[15],
)
}
pub fn entries_to_matrix(entries: &[f64]) -> Fallible<(bool, Transform3D<f64>)> {
if entries.len() == 6 {
Ok((true, create_2d_matrix(entries)))
} else if entries.len() == 16 {
Ok((false, create_3d_matrix(entries)))
} else {
let err_msg = format!("Expected 6 or 16 entries, but found {}.", entries.len());
Err(error::Error::Type(err_msg.to_owned()))
}
}
pub fn dommatrixinit_to_matrix(dict: &DOMMatrixInit) -> Fallible<(bool, Transform3D<f64>)> {
if dict.parent.a.is_some() &&
dict.parent.m11.is_some() &&
dict.parent.a.unwrap() != dict.parent.m11.unwrap() ||
dict.parent.b.is_some() &&
dict.parent.m12.is_some() &&
dict.parent.b.unwrap() != dict.parent.m12.unwrap() ||
dict.parent.c.is_some() &&
dict.parent.m21.is_some() &&
dict.parent.c.unwrap() != dict.parent.m21.unwrap() ||
dict.parent.d.is_some() &&
dict.parent.m22.is_some() &&
dict.parent.d.unwrap() != dict.parent.m22.unwrap() ||
dict.parent.e.is_some() &&
dict.parent.m41.is_some() &&
dict.parent.e.unwrap() != dict.parent.m41.unwrap() ||
dict.parent.f.is_some() &&
dict.parent.m42.is_some() &&
dict.parent.f.unwrap() != dict.parent.m42.unwrap() ||
dict.is2D.is_some() &&
dict.is2D.unwrap() &&
(dict.m31 != 0.0 ||
dict.m32 != 0.0 ||
dict.m13 != 0.0 ||
dict.m23 != 0.0 ||
dict.m43 != 0.0 ||
dict.m14 != 0.0 ||
dict.m24 != 0.0 ||
dict.m34 != 0.0 ||
dict.m33 != 1.0 ||
dict.m44 != 1.0)
{
Err(error::Error::Type("Invalid matrix initializer.".to_owned()))
} else {
let mut is_2d = dict.is2D;
let m11 = dict.parent.m11.unwrap_or(dict.parent.a.unwrap_or(1.0));
let m12 = dict.parent.m12.unwrap_or(dict.parent.b.unwrap_or(0.0));
let m21 = dict.parent.m21.unwrap_or(dict.parent.c.unwrap_or(0.0));
let m22 = dict.parent.m22.unwrap_or(dict.parent.d.unwrap_or(1.0));
let m41 = dict.parent.m41.unwrap_or(dict.parent.e.unwrap_or(0.0));
let m42 = dict.parent.m42.unwrap_or(dict.parent.f.unwrap_or(0.0));
if is_2d.is_none() &&
(dict.m31 != 0.0 ||
dict.m32 != 0.0 ||
dict.m13 != 0.0 ||
dict.m23 != 0.0 ||
dict.m43 != 0.0 ||
dict.m14 != 0.0 ||
dict.m24 != 0.0 ||
dict.m34 != 0.0 ||
dict.m33 != 1.0 ||
dict.m44 != 1.0)
{
is_2d = Some(false);
}
if is_2d.is_none() {
is_2d = Some(true);
}
let matrix = Transform3D::new(
m11, m12, dict.m13, dict.m14, m21, m22, dict.m23, dict.m24, dict.m31, dict.m32,
dict.m33, dict.m34, m41, m42, dict.m43, dict.m44,
);
Ok((is_2d.unwrap(), matrix))
}
}
#[inline]
fn normalize_point(x: f64, y: f64, z: f64) -> (f64, f64, f64) {
let len = (x * x + y * y + z * z).sqrt();
if len == 0.0 {
(0.0, 0.0, 0.0)
} else {
(x / len, y / len, z / len)
}
}
pub fn transform_to_matrix(value: String) -> Fallible<(bool, Transform3D<f64>)> {
use style::properties::longhands::transform;
let mut input = ParserInput::new(&value);
let mut parser = Parser::new(&mut input);
let url_data = Url::parse("about:blank").unwrap().into();
let context = ParserContext::new(
::style::stylesheets::Origin::Author,
&url_data,
Some(::style::stylesheets::CssRuleType::Style),
::style_traits::ParsingMode::DEFAULT,
::style::context::QuirksMode::NoQuirks,
Default::default(),
None,
None,
);
let transform = match parser.parse_entirely(|t| transform::parse(&context, t)) {
Ok(result) => result,
Err(..) => return Err(error::Error::Syntax),
};
let (m, is_3d) = match transform.to_transform_3d_matrix_f64(None) {
Ok(result) => result,
Err(..) => return Err(error::Error::Syntax),
};
Ok((!is_3d, m))
}