Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(947)

Side by Side Diff: third_party/WebKit/Source/core/dom/DOMMatrixReadOnly.cpp

Issue 2283363003: GeometryInterface: Add DOMMatrixInit and fromMatrix(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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("An invalid number sequence is specified. The sequence must contain 6 elements for 2D matrix and 16 elements for 3D matrix ."); 85 exceptionState.throwTypeError("An invalid number sequence is specified. The sequence must contain 6 elements for 2D matrix and 16 elements for 3D matrix .");
13 return nullptr; 86 return nullptr;
14 } 87 }
15 return new DOMMatrixReadOnly(sequence); 88 return new DOMMatrixReadOnly(sequence);
16 } 89 }
17 90
91 DOMMatrixReadOnly* DOMMatrixReadOnly::fromMatrix(DOMMatrixInit& other, Exception State& exceptionState)
92 {
93 if (!validateAndFixup(other, exceptionState))
94 return nullptr;
95
96 if (other.is2D()) {
97 return new DOMMatrixReadOnly({
98 other.m11(), other.m12(), other.m21(),
99 other.m22(), other.m41(), other.m42()});
100 }
101 return new DOMMatrixReadOnly({
102 other.m11(), other.m12(), other.m13(), other.m14(),
103 other.m21(), other.m22(), other.m23(), other.m24(),
104 other.m31(), other.m32(), other.m33(), other.m34(),
105 other.m41(), other.m42(), other.m43(), other.m44()});
106 }
107
18 DOMMatrixReadOnly::DOMMatrixReadOnly(Vector<double> sequence) 108 DOMMatrixReadOnly::DOMMatrixReadOnly(Vector<double> sequence)
19 { 109 {
20 if (sequence.size() == 6) { 110 if (sequence.size() == 6) {
21 m_matrix = TransformationMatrix::create( 111 m_matrix = TransformationMatrix::create(
22 sequence[0], sequence[1], sequence[2], sequence[3], 112 sequence[0], sequence[1], sequence[2], sequence[3],
23 sequence[4], sequence[5]); 113 sequence[4], sequence[5]);
24 m_is2D = true; 114 m_is2D = true;
25 } else if (sequence.size() == 16) { 115 } else if (sequence.size() == 16) {
26 m_matrix = TransformationMatrix::create( 116 m_matrix = TransformationMatrix::create(
27 sequence[0], sequence[1], sequence[2], sequence[3], 117 sequence[0], sequence[1], sequence[2], sequence[3],
(...skipping 13 matching lines...) Expand all
41 bool DOMMatrixReadOnly::is2D() const 131 bool DOMMatrixReadOnly::is2D() const
42 { 132 {
43 return m_is2D; 133 return m_is2D;
44 } 134 }
45 135
46 bool DOMMatrixReadOnly::isIdentity() const 136 bool DOMMatrixReadOnly::isIdentity() const
47 { 137 {
48 return m_matrix->isIdentity(); 138 return m_matrix->isIdentity();
49 } 139 }
50 140
51 DOMMatrix* DOMMatrixReadOnly::multiply(DOMMatrix* other) 141 DOMMatrix* DOMMatrixReadOnly::multiply(DOMMatrixInit& other, ExceptionState& exc eptionState)
52 { 142 {
53 return DOMMatrix::create(this)->multiplySelf(other); 143 return DOMMatrix::create(this)->multiplySelf(other, exceptionState);
54 } 144 }
55 145
56 DOMMatrix* DOMMatrixReadOnly::translate(double tx, double ty, double tz) 146 DOMMatrix* DOMMatrixReadOnly::translate(double tx, double ty, double tz)
57 { 147 {
58 return DOMMatrix::create(this)->translateSelf(tx, ty, tz); 148 return DOMMatrix::create(this)->translateSelf(tx, ty, tz);
59 } 149 }
60 150
61 DOMMatrix* DOMMatrixReadOnly::scale(double scale, double ox, double oy) 151 DOMMatrix* DOMMatrixReadOnly::scale(double scale, double ox, double oy)
62 { 152 {
63 return DOMMatrix::create(this)->scaleSelf(scale, ox, oy); 153 return DOMMatrix::create(this)->scaleSelf(scale, ox, oy);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 << m21() << ", " << m22() << ", " << m23() << ", " << m24() << ", " 236 << m21() << ", " << m22() << ", " << m23() << ", " << m24() << ", "
147 << m31() << ", " << m32() << ", " << m33() << ", " << m34() << ", " 237 << m31() << ", " << m32() << ", " << m33() << ", " << m34() << ", "
148 << m41() << ", " << m42() << ", " << m43() << ", " << m44(); 238 << m41() << ", " << m42() << ", " << m43() << ", " << m44();
149 } 239 }
150 stream << ")"; 240 stream << ")";
151 241
152 return String(stream.str().c_str()); 242 return String(stream.str().c_str());
153 } 243 }
154 244
155 } // namespace blink 245 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698