1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 use dom::bindings::codegen::Bindings::DOMMatrixBinding::{Wrap, DOMMatrixMethods, DOMMatrixInit}; 6 use dom::bindings::codegen::Bindings::DOMMatrixReadOnlyBinding::DOMMatrixReadOnlyMethods; 7 use dom::bindings::error::Fallible; 8 use dom::bindings::inheritance::Castable; 9 use dom::bindings::reflector::reflect_dom_object; 10 use dom::bindings::root::DomRoot; 11 use dom::dommatrixreadonly::{dommatrixinit_to_matrix, DOMMatrixReadOnly, entries_to_matrix}; 12 use dom::globalscope::GlobalScope; 13 use dom_struct::dom_struct; 14 use euclid::Transform3D; 15 16 17 #[dom_struct] 18 pub struct DOMMatrix { 19 parent: DOMMatrixReadOnly 20 } 21 22 impl DOMMatrix { 23 #[allow(unrooted_must_root)] new(global: &GlobalScope, is2D: bool, matrix: Transform3D<f64>) -> DomRoot<Self>24 pub fn new(global: &GlobalScope, is2D: bool, matrix: Transform3D<f64>) -> DomRoot<Self> { 25 let dommatrix = Self::new_inherited(is2D, matrix); 26 reflect_dom_object(Box::new(dommatrix), global, Wrap) 27 } 28 new_inherited(is2D: bool, matrix: Transform3D<f64>) -> Self29 pub fn new_inherited(is2D: bool, matrix: Transform3D<f64>) -> Self { 30 DOMMatrix { 31 parent: DOMMatrixReadOnly::new_inherited(is2D, matrix) 32 } 33 } 34 35 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-dommatrix Constructor(global: &GlobalScope) -> Fallible<DomRoot<Self>>36 pub fn Constructor(global: &GlobalScope) -> Fallible<DomRoot<Self>> { 37 Self::Constructor_(global, vec![1.0, 0.0, 0.0, 1.0, 0.0, 0.0]) 38 } 39 40 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-dommatrix-numbersequence Constructor_(global: &GlobalScope, entries: Vec<f64>) -> Fallible<DomRoot<Self>>41 pub fn Constructor_(global: &GlobalScope, entries: Vec<f64>) -> Fallible<DomRoot<Self>> { 42 entries_to_matrix(&entries[..]) 43 .map(|(is2D, matrix)| { 44 Self::new(global, is2D, matrix) 45 }) 46 } 47 48 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-frommatrix FromMatrix(global: &GlobalScope, other: &DOMMatrixInit) -> Fallible<DomRoot<Self>>49 pub fn FromMatrix(global: &GlobalScope, other: &DOMMatrixInit) -> Fallible<DomRoot<Self>> { 50 dommatrixinit_to_matrix(&other) 51 .map(|(is2D, matrix)| { 52 Self::new(global, is2D, matrix) 53 }) 54 } 55 from_readonly(global: &GlobalScope, ro: &DOMMatrixReadOnly) -> DomRoot<Self>56 pub fn from_readonly(global: &GlobalScope, ro: &DOMMatrixReadOnly) -> DomRoot<Self> { 57 Self::new(global, ro.is_2d(), ro.matrix().clone()) 58 } 59 } 60 61 impl DOMMatrixMethods for DOMMatrix { 62 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m11 M11(&self) -> f6463 fn M11(&self) -> f64 { 64 self.upcast::<DOMMatrixReadOnly>().M11() 65 } 66 67 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m11 SetM11(&self, value: f64)68 fn SetM11(&self, value: f64) { 69 self.upcast::<DOMMatrixReadOnly>().set_m11(value); 70 } 71 72 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m12 M12(&self) -> f6473 fn M12(&self) -> f64 { 74 self.upcast::<DOMMatrixReadOnly>().M12() 75 } 76 77 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m12 SetM12(&self, value: f64)78 fn SetM12(&self, value: f64) { 79 self.upcast::<DOMMatrixReadOnly>().set_m12(value); 80 } 81 82 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m13 M13(&self) -> f6483 fn M13(&self) -> f64 { 84 self.upcast::<DOMMatrixReadOnly>().M13() 85 } 86 87 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m13 SetM13(&self, value: f64)88 fn SetM13(&self, value: f64) { 89 self.upcast::<DOMMatrixReadOnly>().set_m13(value); 90 } 91 92 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m14 M14(&self) -> f6493 fn M14(&self) -> f64 { 94 self.upcast::<DOMMatrixReadOnly>().M14() 95 } 96 97 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m14 SetM14(&self, value: f64)98 fn SetM14(&self, value: f64) { 99 self.upcast::<DOMMatrixReadOnly>().set_m14(value); 100 } 101 102 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m21 M21(&self) -> f64103 fn M21(&self) -> f64 { 104 self.upcast::<DOMMatrixReadOnly>().M21() 105 } 106 107 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m21 SetM21(&self, value: f64)108 fn SetM21(&self, value: f64) { 109 self.upcast::<DOMMatrixReadOnly>().set_m21(value); 110 } 111 112 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m22 M22(&self) -> f64113 fn M22(&self) -> f64 { 114 self.upcast::<DOMMatrixReadOnly>().M22() 115 } 116 117 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m22 SetM22(&self, value: f64)118 fn SetM22(&self, value: f64) { 119 self.upcast::<DOMMatrixReadOnly>().set_m22(value); 120 } 121 122 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m23 M23(&self) -> f64123 fn M23(&self) -> f64 { 124 self.upcast::<DOMMatrixReadOnly>().M23() 125 } 126 127 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m23 SetM23(&self, value: f64)128 fn SetM23(&self, value: f64) { 129 self.upcast::<DOMMatrixReadOnly>().set_m23(value); 130 } 131 132 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m24 M24(&self) -> f64133 fn M24(&self) -> f64 { 134 self.upcast::<DOMMatrixReadOnly>().M24() 135 } 136 137 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m24 SetM24(&self, value: f64)138 fn SetM24(&self, value: f64) { 139 self.upcast::<DOMMatrixReadOnly>().set_m24(value); 140 } 141 142 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m31 M31(&self) -> f64143 fn M31(&self) -> f64 { 144 self.upcast::<DOMMatrixReadOnly>().M31() 145 } 146 147 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m31 SetM31(&self, value: f64)148 fn SetM31(&self, value: f64) { 149 self.upcast::<DOMMatrixReadOnly>().set_m31(value); 150 } 151 152 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m32 M32(&self) -> f64153 fn M32(&self) -> f64 { 154 self.upcast::<DOMMatrixReadOnly>().M32() 155 } 156 157 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m32 SetM32(&self, value: f64)158 fn SetM32(&self, value: f64) { 159 self.upcast::<DOMMatrixReadOnly>().set_m32(value); 160 } 161 162 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m33 M33(&self) -> f64163 fn M33(&self) -> f64 { 164 self.upcast::<DOMMatrixReadOnly>().M33() 165 } 166 167 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m33 SetM33(&self, value: f64)168 fn SetM33(&self, value: f64) { 169 self.upcast::<DOMMatrixReadOnly>().set_m33(value); 170 } 171 172 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m34 M34(&self) -> f64173 fn M34(&self) -> f64 { 174 self.upcast::<DOMMatrixReadOnly>().M34() 175 } 176 177 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m34 SetM34(&self, value: f64)178 fn SetM34(&self, value: f64) { 179 self.upcast::<DOMMatrixReadOnly>().set_m34(value); 180 } 181 182 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m41 M41(&self) -> f64183 fn M41(&self) -> f64 { 184 self.upcast::<DOMMatrixReadOnly>().M41() 185 } 186 187 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m41 SetM41(&self, value: f64)188 fn SetM41(&self, value: f64) { 189 self.upcast::<DOMMatrixReadOnly>().set_m41(value); 190 } 191 192 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m42 M42(&self) -> f64193 fn M42(&self) -> f64 { 194 self.upcast::<DOMMatrixReadOnly>().M42() 195 } 196 197 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m42 SetM42(&self, value: f64)198 fn SetM42(&self, value: f64) { 199 self.upcast::<DOMMatrixReadOnly>().set_m42(value); 200 } 201 202 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m43 M43(&self) -> f64203 fn M43(&self) -> f64 { 204 self.upcast::<DOMMatrixReadOnly>().M43() 205 } 206 207 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m43 SetM43(&self, value: f64)208 fn SetM43(&self, value: f64) { 209 self.upcast::<DOMMatrixReadOnly>().set_m43(value); 210 } 211 212 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m44 M44(&self) -> f64213 fn M44(&self) -> f64 { 214 self.upcast::<DOMMatrixReadOnly>().M44() 215 } 216 217 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-m44 SetM44(&self, value: f64)218 fn SetM44(&self, value: f64) { 219 self.upcast::<DOMMatrixReadOnly>().set_m44(value); 220 } 221 222 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-a A(&self) -> f64223 fn A(&self) -> f64 { 224 self.upcast::<DOMMatrixReadOnly>().A() 225 } 226 227 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-a SetA(&self, value: f64)228 fn SetA(&self, value: f64) { 229 self.upcast::<DOMMatrixReadOnly>().set_m11(value); 230 } 231 232 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-b B(&self) -> f64233 fn B(&self) -> f64 { 234 self.upcast::<DOMMatrixReadOnly>().B() 235 } 236 237 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-b SetB(&self, value: f64)238 fn SetB(&self, value: f64) { 239 self.upcast::<DOMMatrixReadOnly>().set_m12(value); 240 } 241 242 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-c C(&self) -> f64243 fn C(&self) -> f64 { 244 self.upcast::<DOMMatrixReadOnly>().C() 245 } 246 247 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-c SetC(&self, value: f64)248 fn SetC(&self, value: f64) { 249 self.upcast::<DOMMatrixReadOnly>().set_m21(value); 250 } 251 252 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-d D(&self) -> f64253 fn D(&self) -> f64 { 254 self.upcast::<DOMMatrixReadOnly>().D() 255 } 256 257 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-d SetD(&self, value: f64)258 fn SetD(&self, value: f64) { 259 self.upcast::<DOMMatrixReadOnly>().set_m22(value); 260 } 261 262 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-e E(&self) -> f64263 fn E(&self) -> f64 { 264 self.upcast::<DOMMatrixReadOnly>().E() 265 } 266 267 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-e SetE(&self, value: f64)268 fn SetE(&self, value: f64) { 269 self.upcast::<DOMMatrixReadOnly>().set_m41(value); 270 } 271 272 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-f F(&self) -> f64273 fn F(&self) -> f64 { 274 self.upcast::<DOMMatrixReadOnly>().F() 275 } 276 277 // https://drafts.fxtf.org/geometry-1/#dom-dommatrixreadonly-f SetF(&self, value: f64)278 fn SetF(&self, value: f64) { 279 self.upcast::<DOMMatrixReadOnly>().set_m42(value); 280 } 281 282 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-multiplyself MultiplySelf(&self, other:&DOMMatrixInit) -> Fallible<DomRoot<DOMMatrix>>283 fn MultiplySelf(&self, other:&DOMMatrixInit) -> Fallible<DomRoot<DOMMatrix>> { 284 // Steps 1-3. 285 self.upcast::<DOMMatrixReadOnly>().multiply_self(other) 286 // Step 4. 287 .and(Ok(DomRoot::from_ref(&self))) 288 } 289 290 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-premultiplyself PreMultiplySelf(&self, other:&DOMMatrixInit) -> Fallible<DomRoot<DOMMatrix>>291 fn PreMultiplySelf(&self, other:&DOMMatrixInit) -> Fallible<DomRoot<DOMMatrix>> { 292 // Steps 1-3. 293 self.upcast::<DOMMatrixReadOnly>().pre_multiply_self(other) 294 // Step 4. 295 .and(Ok(DomRoot::from_ref(&self))) 296 } 297 298 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-translateself TranslateSelf(&self, tx: f64, ty: f64, tz: f64) -> DomRoot<DOMMatrix>299 fn TranslateSelf(&self, tx: f64, ty: f64, tz: f64) -> DomRoot<DOMMatrix> { 300 // Steps 1-2. 301 self.upcast::<DOMMatrixReadOnly>().translate_self(tx, ty, tz); 302 // Step 3. 303 DomRoot::from_ref(&self) 304 } 305 306 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-scaleself ScaleSelf(&self, scaleX: f64, scaleY: Option<f64>, scaleZ: f64, originX: f64, originY: f64, originZ: f64) -> DomRoot<DOMMatrix>307 fn ScaleSelf(&self, scaleX: f64, scaleY: Option<f64>, scaleZ: f64, 308 originX: f64, originY: f64, originZ: f64) -> DomRoot<DOMMatrix> { 309 // Steps 1-6. 310 self.upcast::<DOMMatrixReadOnly>().scale_self(scaleX, scaleY, scaleZ, originX, originY, originZ); 311 // Step 7. 312 DomRoot::from_ref(&self) 313 } 314 315 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-scale3dself Scale3dSelf(&self, scale: f64, originX: f64, originY: f64, originZ: f64) -> DomRoot<DOMMatrix>316 fn Scale3dSelf(&self, scale: f64, originX: f64, originY: f64, originZ: f64) -> DomRoot<DOMMatrix> { 317 // Steps 1-4. 318 self.upcast::<DOMMatrixReadOnly>().scale_3d_self(scale, originX, originY, originZ); 319 // Step 5. 320 DomRoot::from_ref(&self) 321 } 322 323 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-rotateself RotateSelf(&self, rotX: f64, rotY: Option<f64>, rotZ: Option<f64>) -> DomRoot<DOMMatrix>324 fn RotateSelf(&self, rotX: f64, rotY: Option<f64>, rotZ: Option<f64>) -> DomRoot<DOMMatrix> { 325 // Steps 1-7. 326 self.upcast::<DOMMatrixReadOnly>().rotate_self(rotX, rotY, rotZ); 327 // Step 8. 328 DomRoot::from_ref(&self) 329 } 330 331 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-rotatefromvectorself RotateFromVectorSelf(&self, x: f64, y: f64) -> DomRoot<DOMMatrix>332 fn RotateFromVectorSelf(&self, x: f64, y: f64) -> DomRoot<DOMMatrix> { 333 // Step 1. 334 self.upcast::<DOMMatrixReadOnly>().rotate_from_vector_self(x, y); 335 // Step 2. 336 DomRoot::from_ref(&self) 337 } 338 339 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-rotateaxisangleself RotateAxisAngleSelf(&self, x: f64, y: f64, z: f64, angle: f64) -> DomRoot<DOMMatrix>340 fn RotateAxisAngleSelf(&self, x: f64, y: f64, z: f64, angle: f64) -> DomRoot<DOMMatrix> { 341 // Steps 1-2. 342 self.upcast::<DOMMatrixReadOnly>().rotate_axis_angle_self(x, y, z, angle); 343 // Step 3. 344 DomRoot::from_ref(&self) 345 } 346 347 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-skewxself SkewXSelf(&self, sx: f64) -> DomRoot<DOMMatrix>348 fn SkewXSelf(&self, sx: f64) -> DomRoot<DOMMatrix> { 349 // Step 1. 350 self.upcast::<DOMMatrixReadOnly>().skew_x_self(sx); 351 // Step 2. 352 DomRoot::from_ref(&self) 353 } 354 355 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-skewyself SkewYSelf(&self, sy: f64) -> DomRoot<DOMMatrix>356 fn SkewYSelf(&self, sy: f64) -> DomRoot<DOMMatrix> { 357 // Step 1. 358 self.upcast::<DOMMatrixReadOnly>().skew_y_self(sy); 359 // Step 2. 360 DomRoot::from_ref(&self) 361 } 362 363 // https://drafts.fxtf.org/geometry-1/#dom-dommatrix-invertself InvertSelf(&self) -> DomRoot<DOMMatrix>364 fn InvertSelf(&self) -> DomRoot<DOMMatrix> { 365 // Steps 1-2. 366 self.upcast::<DOMMatrixReadOnly>().invert_self(); 367 // Step 3. 368 DomRoot::from_ref(&self) 369 } 370 } 371