OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/dom/DOMMatrix.h" | 5 #include "core/dom/DOMMatrix.h" |
| 6 #include "core/dom/DOMMatrixInit.h" |
6 | 7 |
7 namespace blink { | 8 namespace blink { |
| 9 namespace { |
| 10 |
| 11 void setDictionaryMembers(DOMMatrixInit& other) |
| 12 { |
| 13 if (!other.hasM11()) |
| 14 other.setM11(other.hasA() ? other.a() : 1); |
| 15 |
| 16 if (!other.hasM12()) |
| 17 other.setM12(other.hasB() ? other.b() : 0); |
| 18 |
| 19 if (!other.hasM21()) |
| 20 other.setM21(other.hasC() ? other.c() : 0); |
| 21 |
| 22 if (!other.hasM22()) |
| 23 other.setM22(other.hasD() ? other.d() : 1); |
| 24 |
| 25 if (!other.hasM41()) |
| 26 other.setM41(other.hasE() ? other.e() : 0); |
| 27 |
| 28 if (!other.hasM42()) |
| 29 other.setM42(other.hasF() ? other.f() : 0); |
| 30 } |
| 31 |
| 32 String getErrorMessage(const char* a, const char* b) |
| 33 { |
| 34 return String::format("The '%s' property should eqaul the '%s' property.", a
, b); |
| 35 } |
| 36 |
| 37 } // namespace |
| 38 |
| 39 bool DOMMatrixReadOnly::validateAndFixup(DOMMatrixInit& other, ExceptionState& e
xceptionState) |
| 40 { |
| 41 if (other.hasA() && other.hasM11() && other.a() != other.m11()) { |
| 42 exceptionState.throwTypeError(getErrorMessage("a", "m11")); |
| 43 return false; |
| 44 } |
| 45 if (other.hasB() && other.hasM12() && other.b() != other.m12()) { |
| 46 exceptionState.throwTypeError(getErrorMessage("b", "m12")); |
| 47 return false; |
| 48 } |
| 49 if (other.hasC() && other.hasM21() && other.c() != other.m21()) { |
| 50 exceptionState.throwTypeError(getErrorMessage("c", "m21")); |
| 51 return false; |
| 52 } |
| 53 if (other.hasD() && other.hasM22() && other.d() != other.m22()) { |
| 54 exceptionState.throwTypeError(getErrorMessage("d", "m22")); |
| 55 return false; |
| 56 } |
| 57 if (other.hasE() && other.hasM41() && other.e() != other.m41()) { |
| 58 exceptionState.throwTypeError(getErrorMessage("e", "m41")); |
| 59 return false; |
| 60 } |
| 61 if (other.hasF() && other.hasM42() && other.f() != other.m42()) { |
| 62 exceptionState.throwTypeError(getErrorMessage("f", "m42")); |
| 63 return false; |
| 64 } |
| 65 if (other.hasIs2D() && other.is2D() && (other.m31() || other.m32() |
| 66 || other.m13() || other.m23() || other.m43() || other.m14() |
| 67 || other.m24() || other.m34() || other.m33() != 1 || other.m44() != 1))
{ |
| 68 exceptionState.throwTypeError("The is2D member is set to true but the in
put matrix is 3d matrix."); |
| 69 return false; |
| 70 } |
| 71 |
| 72 setDictionaryMembers(other); |
| 73 if (!other.hasIs2D()) { |
| 74 bool is2D = !(other.m31() || other.m32() || other.m13() || other.m23() |
| 75 || other.m43() || other.m14() || other.m24() || other.m34() |
| 76 || other.m33() != 1 || other.m44() != 1); |
| 77 other.setIs2D(is2D); |
| 78 } |
| 79 return true; |
| 80 } |
8 | 81 |
9 DOMMatrixReadOnly* DOMMatrixReadOnly::create(Vector<double> sequence, ExceptionS
tate& exceptionState) | 82 DOMMatrixReadOnly* DOMMatrixReadOnly::create(Vector<double> sequence, ExceptionS
tate& exceptionState) |
10 { | 83 { |
11 if (sequence.size() != 6 && sequence.size() != 16) { | 84 if (sequence.size() != 6 && sequence.size() != 16) { |
12 exceptionState.throwTypeError("The sequence must contain 6 elements for
a 2D matrix or 16 elements for a 3D matrix."); | 85 exceptionState.throwTypeError("The sequence must contain 6 elements for
a 2D matrix or 16 elements for a 3D matrix."); |
13 return nullptr; | 86 return nullptr; |
14 } | 87 } |
15 return new DOMMatrixReadOnly(sequence, sequence.size()); | 88 return new DOMMatrixReadOnly(sequence, sequence.size()); |
16 } | 89 } |
17 | 90 |
18 DOMMatrixReadOnly* DOMMatrixReadOnly::fromFloat32Array(DOMFloat32Array* float32A
rray, ExceptionState& exceptionState) | 91 DOMMatrixReadOnly* DOMMatrixReadOnly::fromFloat32Array(DOMFloat32Array* float32A
rray, ExceptionState& exceptionState) |
19 { | 92 { |
20 if (float32Array->length() != 6 && float32Array->length() != 16) { | 93 if (float32Array->length() != 6 && float32Array->length() != 16) { |
21 exceptionState.throwTypeError("The sequence must contain 6 elements for
a 2D matrix or 16 elements a for 3D matrix."); | 94 exceptionState.throwTypeError("The sequence must contain 6 elements for
a 2D matrix or 16 elements a for 3D matrix."); |
22 return nullptr; | 95 return nullptr; |
23 } | 96 } |
24 return new DOMMatrixReadOnly(float32Array->data(), float32Array->length()); | 97 return new DOMMatrixReadOnly(float32Array->data(), float32Array->length()); |
25 } | 98 } |
26 | 99 |
27 | 100 |
28 DOMMatrixReadOnly* DOMMatrixReadOnly::fromFloat64Array(DOMFloat64Array* float64A
rray, ExceptionState& exceptionState) | 101 DOMMatrixReadOnly* DOMMatrixReadOnly::fromFloat64Array(DOMFloat64Array* float64A
rray, ExceptionState& exceptionState) |
29 { | 102 { |
30 if (float64Array->length() != 6 && float64Array->length() != 16) { | 103 if (float64Array->length() != 6 && float64Array->length() != 16) { |
31 exceptionState.throwTypeError("The sequence must contain 6 elements for
a 2D matrix or 16 elements for a 3D matrix."); | 104 exceptionState.throwTypeError("The sequence must contain 6 elements for
a 2D matrix or 16 elements for a 3D matrix."); |
32 return nullptr; | 105 return nullptr; |
33 } | 106 } |
34 return new DOMMatrixReadOnly(float64Array->data(), float64Array->length()); | 107 return new DOMMatrixReadOnly(float64Array->data(), float64Array->length()); |
35 } | 108 } |
36 | 109 |
| 110 DOMMatrixReadOnly* DOMMatrixReadOnly::fromMatrix(DOMMatrixInit& other, Exception
State& exceptionState) |
| 111 { |
| 112 if (!validateAndFixup(other, exceptionState)) |
| 113 return nullptr; |
| 114 |
| 115 if (other.is2D()) { |
| 116 double args[] = { |
| 117 other.m11(), other.m12(), other.m21(), |
| 118 other.m22(), other.m41(), other.m42() |
| 119 }; |
| 120 return new DOMMatrixReadOnly(args, 6); |
| 121 } |
| 122 |
| 123 double args[] = { |
| 124 other.m11(), other.m12(), other.m13(), other.m14(), |
| 125 other.m21(), other.m22(), other.m23(), other.m24(), |
| 126 other.m31(), other.m32(), other.m33(), other.m34(), |
| 127 other.m41(), other.m42(), other.m43(), other.m44() |
| 128 }; |
| 129 return new DOMMatrixReadOnly(args, 16); |
| 130 } |
| 131 |
37 DOMMatrixReadOnly::~DOMMatrixReadOnly() | 132 DOMMatrixReadOnly::~DOMMatrixReadOnly() |
38 { | 133 { |
39 } | 134 } |
40 | 135 |
41 bool DOMMatrixReadOnly::is2D() const | 136 bool DOMMatrixReadOnly::is2D() const |
42 { | 137 { |
43 return m_is2D; | 138 return m_is2D; |
44 } | 139 } |
45 | 140 |
46 bool DOMMatrixReadOnly::isIdentity() const | 141 bool DOMMatrixReadOnly::isIdentity() const |
47 { | 142 { |
48 return m_matrix->isIdentity(); | 143 return m_matrix->isIdentity(); |
49 } | 144 } |
50 | 145 |
51 DOMMatrix* DOMMatrixReadOnly::multiply(DOMMatrix* other) | 146 DOMMatrix* DOMMatrixReadOnly::multiply(DOMMatrixInit& other, ExceptionState& exc
eptionState) |
52 { | 147 { |
53 return DOMMatrix::create(this)->multiplySelf(other); | 148 return DOMMatrix::create(this)->multiplySelf(other, exceptionState); |
54 } | 149 } |
55 | 150 |
56 DOMMatrix* DOMMatrixReadOnly::translate(double tx, double ty, double tz) | 151 DOMMatrix* DOMMatrixReadOnly::translate(double tx, double ty, double tz) |
57 { | 152 { |
58 return DOMMatrix::create(this)->translateSelf(tx, ty, tz); | 153 return DOMMatrix::create(this)->translateSelf(tx, ty, tz); |
59 } | 154 } |
60 | 155 |
61 DOMMatrix* DOMMatrixReadOnly::scale(double scale, double ox, double oy) | 156 DOMMatrix* DOMMatrixReadOnly::scale(double scale, double ox, double oy) |
62 { | 157 { |
63 return DOMMatrix::create(this)->scaleSelf(scale, ox, oy); | 158 return DOMMatrix::create(this)->scaleSelf(scale, ox, oy); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 << m21() << ", " << m22() << ", " << m23() << ", " << m24() << ", " | 241 << m21() << ", " << m22() << ", " << m23() << ", " << m24() << ", " |
147 << m31() << ", " << m32() << ", " << m33() << ", " << m34() << ", " | 242 << m31() << ", " << m32() << ", " << m33() << ", " << m34() << ", " |
148 << m41() << ", " << m42() << ", " << m43() << ", " << m44(); | 243 << m41() << ", " << m42() << ", " << m43() << ", " << m44(); |
149 } | 244 } |
150 stream << ")"; | 245 stream << ")"; |
151 | 246 |
152 return String(stream.str().c_str()); | 247 return String(stream.str().c_str()); |
153 } | 248 } |
154 | 249 |
155 } // namespace blink | 250 } // namespace blink |
OLD | NEW |