Home | History | Annotate | Download | only in svg
      1 /*
      2  * Copyright (C) 2014 Google 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "core/svg/SVGMatrixTearOff.h"
     33 
     34 #include "bindings/v8/ExceptionState.h"
     35 #include "core/dom/ExceptionCode.h"
     36 #include "core/svg/SVGTransformTearOff.h"
     37 
     38 namespace WebCore {
     39 
     40 SVGMatrixTearOff::SVGMatrixTearOff(const AffineTransform& staticValue)
     41     : m_staticValue(staticValue)
     42     , m_contextTransform(0)
     43 {
     44     ScriptWrappable::init(this);
     45 }
     46 
     47 SVGMatrixTearOff::SVGMatrixTearOff(SVGTransformTearOff* transform)
     48     : m_contextTransform(transform)
     49 {
     50     ASSERT(transform);
     51     ScriptWrappable::init(this);
     52 }
     53 
     54 SVGMatrixTearOff::~SVGMatrixTearOff()
     55 {
     56 }
     57 
     58 const AffineTransform& SVGMatrixTearOff::value() const
     59 {
     60     return m_contextTransform ? m_contextTransform->target()->matrix() : m_staticValue;
     61 }
     62 
     63 AffineTransform* SVGMatrixTearOff::mutableValue()
     64 {
     65     return m_contextTransform ? m_contextTransform->target()->mutableMatrix() : &m_staticValue;
     66 }
     67 
     68 void SVGMatrixTearOff::commitChange()
     69 {
     70     if (!m_contextTransform)
     71         return;
     72 
     73     m_contextTransform->target()->onMatrixChange();
     74     m_contextTransform->commitChange();
     75 }
     76 
     77 #define DEFINE_SETTER(ATTRIBUTE) \
     78     void SVGMatrixTearOff::set##ATTRIBUTE(double f, ExceptionState& exceptionState) \
     79     { \
     80         if (m_contextTransform && m_contextTransform->isImmutable()) { \
     81             exceptionState.throwDOMException(NoModificationAllowedError, "The attribute is read-only."); \
     82             return; \
     83         } \
     84         mutableValue()->set##ATTRIBUTE(f); \
     85         commitChange(); \
     86     }
     87 
     88 DEFINE_SETTER(A);
     89 DEFINE_SETTER(B);
     90 DEFINE_SETTER(C);
     91 DEFINE_SETTER(D);
     92 DEFINE_SETTER(E);
     93 DEFINE_SETTER(F);
     94 
     95 #undef DEFINE_SETTER
     96 
     97 PassRefPtr<SVGMatrixTearOff> SVGMatrixTearOff::translate(double tx, double ty)
     98 {
     99     RefPtr<SVGMatrixTearOff> matrix = create(value());
    100     matrix->mutableValue()->translate(tx, ty);
    101     return matrix.release();
    102 }
    103 
    104 PassRefPtr<SVGMatrixTearOff> SVGMatrixTearOff::scale(double s)
    105 {
    106     RefPtr<SVGMatrixTearOff> matrix = create(value());
    107     matrix->mutableValue()->scale(s, s);
    108     return matrix.release();
    109 }
    110 
    111 PassRefPtr<SVGMatrixTearOff> SVGMatrixTearOff::scaleNonUniform(double sx, double sy)
    112 {
    113     RefPtr<SVGMatrixTearOff> matrix = create(value());
    114     matrix->mutableValue()->scale(sx, sy);
    115     return matrix.release();
    116 }
    117 
    118 PassRefPtr<SVGMatrixTearOff> SVGMatrixTearOff::rotate(double d)
    119 {
    120     RefPtr<SVGMatrixTearOff> matrix = create(value());
    121     matrix->mutableValue()->rotate(d);
    122     return matrix.release();
    123 }
    124 
    125 PassRefPtr<SVGMatrixTearOff> SVGMatrixTearOff::flipX()
    126 {
    127     RefPtr<SVGMatrixTearOff> matrix = create(value());
    128     matrix->mutableValue()->flipX();
    129     return matrix.release();
    130 }
    131 
    132 PassRefPtr<SVGMatrixTearOff> SVGMatrixTearOff::flipY()
    133 {
    134     RefPtr<SVGMatrixTearOff> matrix = create(value());
    135     matrix->mutableValue()->flipY();
    136     return matrix.release();
    137 }
    138 
    139 PassRefPtr<SVGMatrixTearOff> SVGMatrixTearOff::skewX(double angle)
    140 {
    141     RefPtr<SVGMatrixTearOff> matrix = create(value());
    142     matrix->mutableValue()->skewX(angle);
    143     return matrix.release();
    144 }
    145 
    146 PassRefPtr<SVGMatrixTearOff> SVGMatrixTearOff::skewY(double angle)
    147 {
    148     RefPtr<SVGMatrixTearOff> matrix = create(value());
    149     matrix->mutableValue()->skewY(angle);
    150     return matrix.release();
    151 }
    152 
    153 PassRefPtr<SVGMatrixTearOff> SVGMatrixTearOff::multiply(PassRefPtr<SVGMatrixTearOff> other)
    154 {
    155     RefPtr<SVGMatrixTearOff> matrix = create(value());
    156     *matrix->mutableValue() *= other->value();
    157     return matrix.release();
    158 }
    159 
    160 PassRefPtr<SVGMatrixTearOff> SVGMatrixTearOff::inverse(ExceptionState& exceptionState)
    161 {
    162     AffineTransform transform = value().inverse();
    163     if (!value().isInvertible())
    164         exceptionState.throwDOMException(InvalidStateError, "The matrix is not invertible.");
    165 
    166     return create(transform);
    167 }
    168 
    169 PassRefPtr<SVGMatrixTearOff> SVGMatrixTearOff::rotateFromVector(double x, double y, ExceptionState& exceptionState)
    170 {
    171     if (!x || !y)
    172         exceptionState.throwDOMException(InvalidAccessError, "Arguments cannot be zero.");
    173 
    174     AffineTransform copy = value();
    175     copy.rotateFromVector(x, y);
    176     return create(copy);
    177 }
    178 
    179 }
    180