File indexing completed on 2024-04-28 11:39:23

0001 /*
0002     Copyright (C) 2004, 2005 Nikolas Zimmermann <wildfox@kde.org>
0003                   2004, 2005 Rob Buis <buis@kde.org>
0004 
0005     This file is part of the KDE project
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #include "wtf/Platform.h"
0024 #if ENABLE(SVG)
0025 
0026 #include "FloatSize.h"
0027 #include "SVGAngle.h"
0028 #include "SVGSVGElement.h"
0029 #include "SVGTransform.h"
0030 
0031 #include <math.h>
0032 
0033 using namespace WebCore;
0034 
0035 SVGTransform::SVGTransform()
0036     : m_type(SVG_TRANSFORM_UNKNOWN)
0037     , m_angle(0)
0038 {
0039 }
0040 
0041 SVGTransform::SVGTransform(SVGTransformType type)
0042     : m_type(type)
0043     , m_angle(0)
0044     , m_center(FloatPoint())
0045     , m_matrix(AffineTransform())
0046 {
0047 }
0048 
0049 SVGTransform::SVGTransform(const AffineTransform &matrix)
0050     : m_type(SVG_TRANSFORM_MATRIX)
0051     , m_angle(0)
0052     , m_matrix(matrix)
0053 {
0054 }
0055 
0056 SVGTransform::~SVGTransform()
0057 {
0058 }
0059 
0060 bool SVGTransform::isValid()
0061 {
0062     return (m_type != SVG_TRANSFORM_UNKNOWN);
0063 }
0064 
0065 SVGTransform::SVGTransformType SVGTransform::type() const
0066 {
0067     return m_type;
0068 }
0069 
0070 AffineTransform SVGTransform::matrix() const
0071 {
0072     return m_matrix;
0073 }
0074 
0075 float SVGTransform::angle() const
0076 {
0077     return m_angle;
0078 }
0079 
0080 FloatPoint SVGTransform::rotationCenter() const
0081 {
0082     return m_center;
0083 }
0084 
0085 void SVGTransform::setMatrix(const AffineTransform &matrix)
0086 {
0087     m_type = SVG_TRANSFORM_MATRIX;
0088     m_angle = 0;
0089 
0090     m_matrix = matrix;
0091 }
0092 
0093 void SVGTransform::setTranslate(float tx, float ty)
0094 {
0095     m_type = SVG_TRANSFORM_TRANSLATE;
0096     m_angle = 0;
0097 
0098     m_matrix.reset();
0099     m_matrix.translate(tx, ty);
0100 }
0101 
0102 FloatPoint SVGTransform::translate() const
0103 {
0104     return FloatPoint::narrowPrecision(m_matrix.e(), m_matrix.f());
0105 }
0106 
0107 void SVGTransform::setScale(float sx, float sy)
0108 {
0109     m_type = SVG_TRANSFORM_SCALE;
0110     m_angle = 0;
0111     m_center = FloatPoint();
0112 
0113     m_matrix.reset();
0114     m_matrix.scale(sx, sy);
0115 }
0116 
0117 FloatSize SVGTransform::scale() const
0118 {
0119     return FloatSize::narrowPrecision(m_matrix.a(), m_matrix.d());
0120 }
0121 
0122 void SVGTransform::setRotate(float angle, float cx, float cy)
0123 {
0124     m_type = SVG_TRANSFORM_ROTATE;
0125     m_angle = angle;
0126     m_center = FloatPoint(cx, cy);
0127 
0128     // TODO: toString() implementation, which can show cx, cy (need to be stored?)
0129     m_matrix.reset();
0130     m_matrix.translate(cx, cy);
0131     m_matrix.rotate(angle);
0132     m_matrix.translate(-cx, -cy);
0133 }
0134 
0135 void SVGTransform::setSkewX(float angle)
0136 {
0137     m_type = SVG_TRANSFORM_SKEWX;
0138     m_angle = angle;
0139 
0140     m_matrix.reset();
0141     m_matrix.skewX(angle);
0142 }
0143 
0144 void SVGTransform::setSkewY(float angle)
0145 {
0146     m_type = SVG_TRANSFORM_SKEWY;
0147     m_angle = angle;
0148 
0149     m_matrix.reset();
0150     m_matrix.skewY(angle);
0151 }
0152 
0153 #endif // ENABLE(SVG)
0154