• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 #include "CSSParser.h"
29 #include "CSSStyleSelector.h"
30 #include "CSSMutableStyleDeclaration.h"
31 #include "CSSPropertyNames.h"
32 #include "ExceptionCode.h"
33 #include "RenderStyle.h"
34 #include "WebKitCSSMatrix.h"
35 
36 namespace WebCore {
37 
WebKitCSSMatrix()38 WebKitCSSMatrix::WebKitCSSMatrix()
39     : StyleBase(0)
40 {
41 }
42 
WebKitCSSMatrix(const WebKitCSSMatrix & m)43 WebKitCSSMatrix::WebKitCSSMatrix(const WebKitCSSMatrix& m)
44     : StyleBase(0)
45     , m_matrix(m.m_matrix)
46 {
47 }
48 
WebKitCSSMatrix(const TransformationMatrix & m)49 WebKitCSSMatrix::WebKitCSSMatrix(const TransformationMatrix& m)
50     : StyleBase(0)
51     , m_matrix(m)
52 {
53 }
54 
WebKitCSSMatrix(const String & s,ExceptionCode & ec)55 WebKitCSSMatrix::WebKitCSSMatrix(const String& s, ExceptionCode& ec)
56     : StyleBase(0)
57 {
58     setMatrixValue(s, ec);
59 }
60 
~WebKitCSSMatrix()61 WebKitCSSMatrix::~WebKitCSSMatrix()
62 {
63 }
64 
setMatrixValue(const String & string,ExceptionCode & ec)65 void WebKitCSSMatrix::setMatrixValue(const String& string, ExceptionCode& ec)
66 {
67     CSSParser p(useStrictParsing());
68     RefPtr<CSSMutableStyleDeclaration> styleDeclaration = CSSMutableStyleDeclaration::create();
69     if (p.parseValue(styleDeclaration.get(), CSSPropertyWebkitTransform, string, true)) {
70         // Convert to TransformOperations. This can fail if a property
71         // requires style (i.e., param uses 'ems' or 'exs')
72         PassRefPtr<CSSValue> val =  styleDeclaration->getPropertyCSSValue(CSSPropertyWebkitTransform);
73         TransformOperations operations;
74         if (!CSSStyleSelector::createTransformOperations(val.get(), 0, operations)) {
75             ec = SYNTAX_ERR;
76             return;
77         }
78 
79         // Convert transform operations to a TransformationMatrix. This can fail
80         // if a param has a percentage ('%')
81         TransformationMatrix t;
82         for (unsigned i = 0; i < operations.operations().size(); ++i) {
83             if (operations.operations()[i].get()->apply(t, IntSize(0, 0))) {
84                 ec = SYNTAX_ERR;
85                 return;
86             }
87         }
88 
89         // set the matrix
90         m_matrix = t;
91     } else if (!string.isEmpty()) // There is something there but parsing failed
92         ec = SYNTAX_ERR;
93 }
94 
95 // This is a multRight (this = this * secondMatrix)
multiply(WebKitCSSMatrix * secondMatrix)96 PassRefPtr<WebKitCSSMatrix> WebKitCSSMatrix::multiply(WebKitCSSMatrix* secondMatrix)
97 {
98     TransformationMatrix tmp(m_matrix);
99     tmp.multiply(secondMatrix->m_matrix);
100     return WebKitCSSMatrix::create(tmp);
101 }
102 
inverse(ExceptionCode & ec)103 PassRefPtr<WebKitCSSMatrix> WebKitCSSMatrix::inverse(ExceptionCode& ec)
104 {
105     if (!m_matrix.isInvertible()) {
106         ec = NOT_SUPPORTED_ERR;
107         return 0;
108     }
109 
110     return WebKitCSSMatrix::create(m_matrix.inverse());
111 }
112 
translate(float x,float y)113 PassRefPtr<WebKitCSSMatrix> WebKitCSSMatrix::translate(float x, float y)
114 {
115     if (isnan(x))
116         x = 0;
117     if (isnan(y))
118         y = 0;
119     return WebKitCSSMatrix::create(m_matrix.translate(x, y));
120 }
121 
scale(float scaleX,float scaleY)122 PassRefPtr<WebKitCSSMatrix> WebKitCSSMatrix::scale(float scaleX, float scaleY)
123 {
124     if (isnan(scaleX))
125         scaleX = 1;
126     if (isnan(scaleY))
127         scaleY = scaleX;
128     return WebKitCSSMatrix::create(m_matrix.scale(scaleX,scaleY));
129 }
130 
rotate(float rot)131 PassRefPtr<WebKitCSSMatrix> WebKitCSSMatrix::rotate(float rot)
132 {
133     if (isnan(rot))
134         rot = 0;
135     return WebKitCSSMatrix::create(m_matrix.rotate(rot));
136 }
137 
toString()138 String WebKitCSSMatrix::toString()
139 {
140     return String::format("matrix(%f, %f, %f, %f, %f, %f)",
141                             m_matrix.a(), m_matrix.b(), m_matrix.c(), m_matrix.d(), m_matrix.e(), m_matrix.f());
142 }
143 
144 } // namespace WebCore
145