WebKitCSSMatrix.cpp 6.7 KB

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