OLD | NEW |
| (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 "platform/wtf/MathExtras.h" | |
41 | |
42 namespace blink { | |
43 | |
44 CSSMatrix* CSSMatrix::Create(ExecutionContext* execution_context, | |
45 const String& s, | |
46 ExceptionState& exception_state) { | |
47 UseCounter::Count(execution_context, UseCounter::kWebKitCSSMatrix); | |
48 if (!s.IsEmpty()) { | |
49 UseCounter::Count(execution_context, | |
50 UseCounter::kWebkitCSSMatrixConstructFromString); | |
51 } | |
52 return new CSSMatrix(s, exception_state); | |
53 } | |
54 | |
55 CSSMatrix::CSSMatrix(const TransformationMatrix& m) | |
56 : matrix_(TransformationMatrix::Create(m)) {} | |
57 | |
58 CSSMatrix::CSSMatrix(const String& s, ExceptionState& exception_state) | |
59 : matrix_(TransformationMatrix::Create()) { | |
60 setMatrixValue(s, exception_state); | |
61 } | |
62 | |
63 static inline PassRefPtr<ComputedStyle> CreateInitialStyle() { | |
64 RefPtr<ComputedStyle> initial_style = ComputedStyle::Create(); | |
65 initial_style->GetFont().Update(nullptr); | |
66 return initial_style; | |
67 } | |
68 | |
69 void CSSMatrix::setMatrixValue(const String& string, | |
70 ExceptionState& exception_state) { | |
71 if (string.IsEmpty()) | |
72 return; | |
73 | |
74 if (const CSSValue* value = | |
75 CSSParser::ParseSingleValue(CSSPropertyTransform, string)) { | |
76 // Check for a "none" transform. In these cases we can use the default | |
77 // identity matrix. | |
78 if (value->IsIdentifierValue() && | |
79 (ToCSSIdentifierValue(value))->GetValueID() == CSSValueNone) | |
80 return; | |
81 | |
82 DEFINE_STATIC_REF(ComputedStyle, initial_style, CreateInitialStyle()); | |
83 TransformOperations operations = | |
84 TransformBuilder::CreateTransformOperations( | |
85 *value, CSSToLengthConversionData(initial_style, initial_style, | |
86 LayoutViewItem(nullptr), 1.0f)); | |
87 | |
88 // Convert transform operations to a TransformationMatrix. This can fail | |
89 // if a param has a percentage ('%') | |
90 if (operations.DependsOnBoxSize()) | |
91 exception_state.ThrowDOMException(kSyntaxError, | |
92 "The transformation depends on the box " | |
93 "size, which is not supported."); | |
94 matrix_ = TransformationMatrix::Create(); | |
95 operations.Apply(FloatSize(0, 0), *matrix_); | |
96 } else { // There is something there but parsing failed. | |
97 exception_state.ThrowDOMException(kSyntaxError, | |
98 "Failed to parse '" + string + "'."); | |
99 } | |
100 } | |
101 | |
102 // Perform a concatenation of the matrices (this * secondMatrix) | |
103 CSSMatrix* CSSMatrix::multiply(CSSMatrix* second_matrix) const { | |
104 if (!second_matrix) | |
105 return nullptr; | |
106 | |
107 return CSSMatrix::Create( | |
108 TransformationMatrix(*matrix_).Multiply(*second_matrix->matrix_)); | |
109 } | |
110 | |
111 CSSMatrix* CSSMatrix::inverse(ExceptionState& exception_state) const { | |
112 if (!matrix_->IsInvertible()) { | |
113 exception_state.ThrowDOMException(kNotSupportedError, | |
114 "The matrix is not invertable."); | |
115 return nullptr; | |
116 } | |
117 | |
118 return CSSMatrix::Create(matrix_->Inverse()); | |
119 } | |
120 | |
121 CSSMatrix* CSSMatrix::translate(double x, double y, double z) const { | |
122 if (std::isnan(x)) | |
123 x = 0; | |
124 if (std::isnan(y)) | |
125 y = 0; | |
126 if (std::isnan(z)) | |
127 z = 0; | |
128 return CSSMatrix::Create(TransformationMatrix(*matrix_).Translate3d(x, y, z)); | |
129 } | |
130 | |
131 CSSMatrix* CSSMatrix::scale(double scale_x, | |
132 double scale_y, | |
133 double scale_z) const { | |
134 if (std::isnan(scale_x)) | |
135 scale_x = 1; | |
136 if (std::isnan(scale_y)) | |
137 scale_y = scale_x; | |
138 if (std::isnan(scale_z)) | |
139 scale_z = 1; | |
140 return CSSMatrix::Create( | |
141 TransformationMatrix(*matrix_).Scale3d(scale_x, scale_y, scale_z)); | |
142 } | |
143 | |
144 CSSMatrix* CSSMatrix::rotate(double rot_x, double rot_y, double rot_z) const { | |
145 if (std::isnan(rot_x)) | |
146 rot_x = 0; | |
147 | |
148 if (std::isnan(rot_y) && std::isnan(rot_z)) { | |
149 rot_z = rot_x; | |
150 rot_x = 0; | |
151 rot_y = 0; | |
152 } | |
153 | |
154 if (std::isnan(rot_y)) | |
155 rot_y = 0; | |
156 if (std::isnan(rot_z)) | |
157 rot_z = 0; | |
158 return CSSMatrix::Create( | |
159 TransformationMatrix(*matrix_).Rotate3d(rot_x, rot_y, rot_z)); | |
160 } | |
161 | |
162 CSSMatrix* CSSMatrix::rotateAxisAngle(double x, | |
163 double y, | |
164 double z, | |
165 double angle) const { | |
166 if (std::isnan(x)) | |
167 x = 0; | |
168 if (std::isnan(y)) | |
169 y = 0; | |
170 if (std::isnan(z)) | |
171 z = 0; | |
172 if (std::isnan(angle)) | |
173 angle = 0; | |
174 if (!x && !y && !z) | |
175 z = 1; | |
176 return CSSMatrix::Create( | |
177 TransformationMatrix(*matrix_).Rotate3d(x, y, z, angle)); | |
178 } | |
179 | |
180 CSSMatrix* CSSMatrix::skewX(double angle) const { | |
181 if (std::isnan(angle)) | |
182 angle = 0; | |
183 return CSSMatrix::Create(TransformationMatrix(*matrix_).SkewX(angle)); | |
184 } | |
185 | |
186 CSSMatrix* CSSMatrix::skewY(double angle) const { | |
187 if (std::isnan(angle)) | |
188 angle = 0; | |
189 return CSSMatrix::Create(TransformationMatrix(*matrix_).SkewY(angle)); | |
190 } | |
191 | |
192 String CSSMatrix::toString() const { | |
193 // FIXME - Need to ensure valid CSS floating point values | |
194 // (https://bugs.webkit.org/show_bug.cgi?id=20674) | |
195 if (matrix_->IsAffine()) | |
196 return String::Format("matrix(%g, %g, %g, %g, %g, %g)", matrix_->A(), | |
197 matrix_->B(), matrix_->C(), matrix_->D(), | |
198 matrix_->E(), matrix_->F()); | |
199 return String::Format( | |
200 "matrix3d(%g, %g, %g, %g, %g, %g, %g, %g, %g, %g, %g, %g, %g, %g, %g, " | |
201 "%g)", | |
202 matrix_->M11(), matrix_->M12(), matrix_->M13(), matrix_->M14(), | |
203 matrix_->M21(), matrix_->M22(), matrix_->M23(), matrix_->M24(), | |
204 matrix_->M31(), matrix_->M32(), matrix_->M33(), matrix_->M34(), | |
205 matrix_->M41(), matrix_->M42(), matrix_->M43(), matrix_->M44()); | |
206 } | |
207 | |
208 } // namespace blink | |
OLD | NEW |