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 "config.h" 27 #include "core/css/CSSMatrix.h" 28 29 #include "CSSPropertyNames.h" 30 #include "CSSValueKeywords.h" 31 #include "bindings/v8/ExceptionState.h" 32 #include "core/css/CSSParser.h" 33 #include "core/css/StylePropertySet.h" 34 #include "core/css/resolver/TransformBuilder.h" 35 #include "core/dom/ExceptionCode.h" 36 #include "wtf/MathExtras.h" 37 38 namespace WebCore { 39 40 CSSMatrix::CSSMatrix(const TransformationMatrix& m) 41 : m_matrix(m) 42 { 43 ScriptWrappable::init(this); 44 } 45 46 CSSMatrix::CSSMatrix(const String& s, ExceptionState& es) 47 { 48 ScriptWrappable::init(this); 49 setMatrixValue(s, es); 50 } 51 52 CSSMatrix::~CSSMatrix() 53 { 54 } 55 56 void CSSMatrix::setMatrixValue(const String& string, ExceptionState& es) 57 { 58 if (string.isEmpty()) 59 return; 60 61 RefPtr<MutableStylePropertySet> styleDeclaration = MutableStylePropertySet::create(); 62 if (CSSParser::parseValue(styleDeclaration.get(), CSSPropertyWebkitTransform, string, true, CSSStrictMode, 0)) { 63 // Convert to TransformOperations. This can fail if a property 64 // requires style (i.e., param uses 'ems' or 'exs') 65 RefPtr<CSSValue> value = styleDeclaration->getPropertyCSSValue(CSSPropertyWebkitTransform); 66 67 // Check for a "none" or empty transform. In these cases we can use the default identity matrix. 68 if (!value || (value->isPrimitiveValue() && (toCSSPrimitiveValue(value.get()))->getValueID() == CSSValueNone)) 69 return; 70 71 TransformOperations operations; 72 if (!TransformBuilder::createTransformOperations(value.get(), 0, 0, operations)) { 73 es.throwDOMException(SyntaxError); 74 return; 75 } 76 77 // Convert transform operations to a TransformationMatrix. This can fail 78 // if a param has a percentage ('%') 79 TransformationMatrix t; 80 for (unsigned i = 0; i < operations.operations().size(); ++i) { 81 if (operations.operations()[i].get()->apply(t, IntSize(0, 0))) { 82 es.throwDOMException(SyntaxError); 83 return; 84 } 85 } 86 87 // set the matrix 88 m_matrix = t; 89 } else { // There is something there but parsing failed. 90 es.throwDOMException(SyntaxError); 91 } 92 } 93 94 // Perform a concatenation of the matrices (this * secondMatrix) 95 PassRefPtr<CSSMatrix> CSSMatrix::multiply(CSSMatrix* secondMatrix) const 96 { 97 if (!secondMatrix) 98 return 0; 99 100 return CSSMatrix::create(TransformationMatrix(m_matrix).multiply(secondMatrix->m_matrix)); 101 } 102 103 PassRefPtr<CSSMatrix> CSSMatrix::inverse(ExceptionState& es) const 104 { 105 if (!m_matrix.isInvertible()) { 106 es.throwDOMException(NotSupportedError); 107 return 0; 108 } 109 110 return CSSMatrix::create(m_matrix.inverse()); 111 } 112 113 PassRefPtr<CSSMatrix> CSSMatrix::translate(double x, double y, double z) const 114 { 115 if (std::isnan(x)) 116 x = 0; 117 if (std::isnan(y)) 118 y = 0; 119 if (std::isnan(z)) 120 z = 0; 121 return CSSMatrix::create(TransformationMatrix(m_matrix).translate3d(x, y, z)); 122 } 123 124 PassRefPtr<CSSMatrix> CSSMatrix::scale(double scaleX, double scaleY, double scaleZ) const 125 { 126 if (std::isnan(scaleX)) 127 scaleX = 1; 128 if (std::isnan(scaleY)) 129 scaleY = scaleX; 130 if (std::isnan(scaleZ)) 131 scaleZ = 1; 132 return CSSMatrix::create(TransformationMatrix(m_matrix).scale3d(scaleX, scaleY, scaleZ)); 133 } 134 135 PassRefPtr<CSSMatrix> CSSMatrix::rotate(double rotX, double rotY, double rotZ) const 136 { 137 if (std::isnan(rotX)) 138 rotX = 0; 139 140 if (std::isnan(rotY) && std::isnan(rotZ)) { 141 rotZ = rotX; 142 rotX = 0; 143 rotY = 0; 144 } 145 146 if (std::isnan(rotY)) 147 rotY = 0; 148 if (std::isnan(rotZ)) 149 rotZ = 0; 150 return CSSMatrix::create(TransformationMatrix(m_matrix).rotate3d(rotX, rotY, rotZ)); 151 } 152 153 PassRefPtr<CSSMatrix> CSSMatrix::rotateAxisAngle(double x, double y, double z, double angle) const 154 { 155 if (std::isnan(x)) 156 x = 0; 157 if (std::isnan(y)) 158 y = 0; 159 if (std::isnan(z)) 160 z = 0; 161 if (std::isnan(angle)) 162 angle = 0; 163 if (!x && !y && !z) 164 z = 1; 165 return CSSMatrix::create(TransformationMatrix(m_matrix).rotate3d(x, y, z, angle)); 166 } 167 168 PassRefPtr<CSSMatrix> CSSMatrix::skewX(double angle) const 169 { 170 if (std::isnan(angle)) 171 angle = 0; 172 return CSSMatrix::create(TransformationMatrix(m_matrix).skewX(angle)); 173 } 174 175 PassRefPtr<CSSMatrix> CSSMatrix::skewY(double angle) const 176 { 177 if (std::isnan(angle)) 178 angle = 0; 179 return CSSMatrix::create(TransformationMatrix(m_matrix).skewY(angle)); 180 } 181 182 String CSSMatrix::toString() const 183 { 184 // FIXME - Need to ensure valid CSS floating point values (https://bugs.webkit.org/show_bug.cgi?id=20674) 185 if (m_matrix.isAffine()) 186 return String::format("matrix(%f, %f, %f, %f, %f, %f)", m_matrix.a(), m_matrix.b(), m_matrix.c(), m_matrix.d(), m_matrix.e(), m_matrix.f()); 187 return String::format("matrix3d(%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)", 188 m_matrix.m11(), m_matrix.m12(), m_matrix.m13(), m_matrix.m14(), 189 m_matrix.m21(), m_matrix.m22(), m_matrix.m23(), m_matrix.m24(), 190 m_matrix.m31(), m_matrix.m32(), m_matrix.m33(), m_matrix.m34(), 191 m_matrix.m41(), m_matrix.m42(), m_matrix.m43(), m_matrix.m44()); 192 } 193 194 } // namespace WebCore 195