Home | History | Annotate | Download | only in svg
      1 /*
      2     Copyright (C) 2004, 2005 Nikolas Zimmermann <wildfox (at) kde.org>
      3                   2004, 2005 Rob Buis <buis (at) kde.org>
      4 
      5     This library is free software; you can redistribute it and/or
      6     modify it under the terms of the GNU Library General Public
      7     License as published by the Free Software Foundation; either
      8     version 2 of the License, or (at your option) any later version.
      9 
     10     This library is distributed in the hope that it will be useful,
     11     but WITHOUT ANY WARRANTY; without even the implied warranty of
     12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13     Library General Public License for more details.
     14 
     15     You should have received a copy of the GNU Library General Public License
     16     along with this library; see the file COPYING.LIB.  If not, write to
     17     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18     Boston, MA 02110-1301, USA.
     19 */
     20 
     21 #include "config.h"
     22 #if ENABLE(SVG)
     23 
     24 #include "FloatPoint.h"
     25 #include "FloatSize.h"
     26 #include "SVGAngle.h"
     27 #include "SVGSVGElement.h"
     28 #include "SVGTransform.h"
     29 
     30 #include <math.h>
     31 
     32 using namespace WebCore;
     33 
     34 SVGTransform::SVGTransform()
     35     : m_type(SVG_TRANSFORM_UNKNOWN)
     36     , m_angle(0)
     37 {
     38 }
     39 
     40 SVGTransform::SVGTransform(SVGTransformType type)
     41     : m_type(type)
     42     , m_angle(0)
     43     , m_center(FloatPoint())
     44     , m_matrix(AffineTransform())
     45 {
     46 }
     47 
     48 SVGTransform::SVGTransform(const AffineTransform& matrix)
     49     : m_type(SVG_TRANSFORM_MATRIX)
     50     , m_angle(0)
     51     , m_matrix(matrix)
     52 {
     53 }
     54 
     55 SVGTransform::~SVGTransform()
     56 {
     57 }
     58 
     59 bool SVGTransform::isValid()
     60 {
     61     return (m_type != SVG_TRANSFORM_UNKNOWN);
     62 }
     63 
     64 SVGTransform::SVGTransformType SVGTransform::type() const
     65 {
     66     return m_type;
     67 }
     68 
     69 AffineTransform SVGTransform::matrix() const
     70 {
     71     return m_matrix;
     72 }
     73 
     74 float SVGTransform::angle() const
     75 {
     76     return m_angle;
     77 }
     78 
     79 FloatPoint SVGTransform::rotationCenter() const
     80 {
     81     return m_center;
     82 }
     83 
     84 void SVGTransform::setMatrix(AffineTransform matrix)
     85 {
     86     m_type = SVG_TRANSFORM_MATRIX;
     87     m_angle = 0;
     88 
     89     m_matrix = matrix;
     90 }
     91 
     92 void SVGTransform::setTranslate(float tx, float ty)
     93 {
     94     m_type = SVG_TRANSFORM_TRANSLATE;
     95     m_angle = 0;
     96 
     97     m_matrix.makeIdentity();
     98     m_matrix.translate(tx, ty);
     99 }
    100 
    101 FloatPoint SVGTransform::translate() const
    102 {
    103     return FloatPoint::narrowPrecision(m_matrix.e(), m_matrix.f());
    104 }
    105 
    106 void SVGTransform::setScale(float sx, float sy)
    107 {
    108     m_type = SVG_TRANSFORM_SCALE;
    109     m_angle = 0;
    110     m_center = FloatPoint();
    111 
    112     m_matrix.makeIdentity();
    113     m_matrix.scaleNonUniform(sx, sy);
    114 }
    115 
    116 FloatSize SVGTransform::scale() const
    117 {
    118     return FloatSize::narrowPrecision(m_matrix.a(), m_matrix.d());
    119 }
    120 
    121 void SVGTransform::setRotate(float angle, float cx, float cy)
    122 {
    123     m_type = SVG_TRANSFORM_ROTATE;
    124     m_angle = angle;
    125     m_center = FloatPoint(cx, cy);
    126 
    127     // TODO: toString() implementation, which can show cx, cy (need to be stored?)
    128     m_matrix.makeIdentity();
    129     m_matrix.translate(cx, cy);
    130     m_matrix.rotate(angle);
    131     m_matrix.translate(-cx, -cy);
    132 }
    133 
    134 void SVGTransform::setSkewX(float angle)
    135 {
    136     m_type = SVG_TRANSFORM_SKEWX;
    137     m_angle = angle;
    138 
    139     m_matrix.makeIdentity();
    140     m_matrix.skewX(angle);
    141 }
    142 
    143 void SVGTransform::setSkewY(float angle)
    144 {
    145     m_type = SVG_TRANSFORM_SKEWY;
    146     m_angle = angle;
    147 
    148     m_matrix.makeIdentity();
    149     m_matrix.skewY(angle);
    150 }
    151 
    152 #endif // ENABLE(SVG)
    153 
    154