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

Side by Side Diff: third_party/WebKit/Source/core/css/CSSMatrix.cpp

Issue 2709763004: Make WebKitCSSMatrix an alias of DOMMatrix (Closed)
Patch Set: Make WebkitCSSMatrix an alias of DOMMatrix Created 3 years, 7 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
(Empty)
1 /*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "core/css/CSSMatrix.h"
27
28 #include "bindings/core/v8/ExceptionState.h"
29 #include "core/CSSPropertyNames.h"
30 #include "core/CSSValueKeywords.h"
31 #include "core/css/CSSIdentifierValue.h"
32 #include "core/css/CSSToLengthConversionData.h"
33 #include "core/css/StylePropertySet.h"
34 #include "core/css/parser/CSSParser.h"
35 #include "core/css/resolver/TransformBuilder.h"
36 #include "core/dom/ExceptionCode.h"
37 #include "core/frame/UseCounter.h"
38 #include "core/layout/api/LayoutViewItem.h"
39 #include "core/style/ComputedStyle.h"
40 #include "core/style/StyleInheritedData.h"
41 #include "platform/wtf/MathExtras.h"
42
43 namespace blink {
44
45 CSSMatrix* CSSMatrix::Create(ExecutionContext* execution_context,
46 const String& s,
47 ExceptionState& exception_state) {
48 UseCounter::Count(execution_context, UseCounter::kWebKitCSSMatrix);
49 if (!s.IsEmpty()) {
50 UseCounter::Count(execution_context,
51 UseCounter::kWebkitCSSMatrixConstructFromString);
52 }
53 return new CSSMatrix(s, exception_state);
54 }
55
56 CSSMatrix::CSSMatrix(const TransformationMatrix& m)
57 : matrix_(TransformationMatrix::Create(m)) {}
58
59 CSSMatrix::CSSMatrix(const String& s, ExceptionState& exception_state)
60 : matrix_(TransformationMatrix::Create()) {
61 setMatrixValue(s, exception_state);
62 }
63
64 static inline PassRefPtr<ComputedStyle> CreateInitialStyle() {
65 RefPtr<ComputedStyle> initial_style = ComputedStyle::Create();
66 initial_style->GetFont().Update(nullptr);
67 return initial_style;
68 }
69
70 void CSSMatrix::setMatrixValue(const String& string,
71 ExceptionState& exception_state) {
72 if (string.IsEmpty())
73 return;
74
75 if (const CSSValue* value =
76 CSSParser::ParseSingleValue(CSSPropertyTransform, string)) {
77 // Check for a "none" transform. In these cases we can use the default
78 // identity matrix.
79 if (value->IsIdentifierValue() &&
80 (ToCSSIdentifierValue(value))->GetValueID() == CSSValueNone)
81 return;
82
83 DEFINE_STATIC_REF(ComputedStyle, initial_style, CreateInitialStyle());
84 TransformOperations operations =
85 TransformBuilder::CreateTransformOperations(
86 *value, CSSToLengthConversionData(initial_style, initial_style,
87 LayoutViewItem(nullptr), 1.0f));
88
89 // Convert transform operations to a TransformationMatrix. This can fail
90 // if a param has a percentage ('%')
91 if (operations.DependsOnBoxSize())
92 exception_state.ThrowDOMException(kSyntaxError,
93 "The transformation depends on the box "
94 "size, which is not supported.");
95 matrix_ = TransformationMatrix::Create();
96 operations.Apply(FloatSize(0, 0), *matrix_);
97 } else { // There is something there but parsing failed.
98 exception_state.ThrowDOMException(kSyntaxError,
99 "Failed to parse '" + string + "'.");
100 }
101 }
102
103 // Perform a concatenation of the matrices (this * secondMatrix)
104 CSSMatrix* CSSMatrix::multiply(CSSMatrix* second_matrix) const {
105 if (!second_matrix)
106 return nullptr;
107
108 return CSSMatrix::Create(
109 TransformationMatrix(*matrix_).Multiply(*second_matrix->matrix_));
110 }
111
112 CSSMatrix* CSSMatrix::inverse(ExceptionState& exception_state) const {
113 if (!matrix_->IsInvertible()) {
114 exception_state.ThrowDOMException(kNotSupportedError,
115 "The matrix is not invertable.");
116 return nullptr;
117 }
118
119 return CSSMatrix::Create(matrix_->Inverse());
120 }
121
122 CSSMatrix* CSSMatrix::translate(double x, double y, double z) const {
123 if (std::isnan(x))
124 x = 0;
125 if (std::isnan(y))
126 y = 0;
127 if (std::isnan(z))
128 z = 0;
129 return CSSMatrix::Create(TransformationMatrix(*matrix_).Translate3d(x, y, z));
130 }
131
132 CSSMatrix* CSSMatrix::scale(double scale_x,
133 double scale_y,
134 double scale_z) const {
135 if (std::isnan(scale_x))
136 scale_x = 1;
137 if (std::isnan(scale_y))
138 scale_y = scale_x;
139 if (std::isnan(scale_z))
140 scale_z = 1;
141 return CSSMatrix::Create(
142 TransformationMatrix(*matrix_).Scale3d(scale_x, scale_y, scale_z));
143 }
144
145 CSSMatrix* CSSMatrix::rotate(double rot_x, double rot_y, double rot_z) const {
146 if (std::isnan(rot_x))
147 rot_x = 0;
148
149 if (std::isnan(rot_y) && std::isnan(rot_z)) {
150 rot_z = rot_x;
151 rot_x = 0;
152 rot_y = 0;
153 }
154
155 if (std::isnan(rot_y))
156 rot_y = 0;
157 if (std::isnan(rot_z))
158 rot_z = 0;
159 return CSSMatrix::Create(
160 TransformationMatrix(*matrix_).Rotate3d(rot_x, rot_y, rot_z));
161 }
162
163 CSSMatrix* CSSMatrix::rotateAxisAngle(double x,
164 double y,
165 double z,
166 double angle) const {
167 if (std::isnan(x))
168 x = 0;
169 if (std::isnan(y))
170 y = 0;
171 if (std::isnan(z))
172 z = 0;
173 if (std::isnan(angle))
174 angle = 0;
175 if (!x && !y && !z)
176 z = 1;
177 return CSSMatrix::Create(
178 TransformationMatrix(*matrix_).Rotate3d(x, y, z, angle));
179 }
180
181 CSSMatrix* CSSMatrix::skewX(double angle) const {
182 if (std::isnan(angle))
183 angle = 0;
184 return CSSMatrix::Create(TransformationMatrix(*matrix_).SkewX(angle));
185 }
186
187 CSSMatrix* CSSMatrix::skewY(double angle) const {
188 if (std::isnan(angle))
189 angle = 0;
190 return CSSMatrix::Create(TransformationMatrix(*matrix_).SkewY(angle));
191 }
192
193 String CSSMatrix::toString() const {
194 // FIXME - Need to ensure valid CSS floating point values
195 // (https://bugs.webkit.org/show_bug.cgi?id=20674)
196 if (matrix_->IsAffine())
197 return String::Format("matrix(%f, %f, %f, %f, %f, %f)", matrix_->A(),
198 matrix_->B(), matrix_->C(), matrix_->D(),
199 matrix_->E(), matrix_->F());
200 return String::Format(
201 "matrix3d(%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, "
202 "%f)",
203 matrix_->M11(), matrix_->M12(), matrix_->M13(), matrix_->M14(),
204 matrix_->M21(), matrix_->M22(), matrix_->M23(), matrix_->M24(),
205 matrix_->M31(), matrix_->M32(), matrix_->M33(), matrix_->M34(),
206 matrix_->M41(), matrix_->M42(), matrix_->M43(), matrix_->M44());
207 }
208
209 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698