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