2 * Copyright (C) 2005, 2006 Apple Inc. All rights reserved.
3 * 2010 Dirk Schulze <krit@webkit.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef AffineTransform_h
28 #define AffineTransform_h
31 #include <wtf/FastMalloc.h>
34 typedef struct CGAffineTransform CGAffineTransform;
48 class TransformationMatrix;
50 class AffineTransform {
51 WTF_MAKE_FAST_ALLOCATED;
53 WEBCORE_EXPORT AffineTransform();
54 WEBCORE_EXPORT AffineTransform(double a, double b, double c, double d, double e, double f);
57 AffineTransform(const CGAffineTransform&);
60 void setMatrix(double a, double b, double c, double d, double e, double f);
62 void map(double x, double y, double& x2, double& y2) const;
64 // Rounds the mapped point to the nearest integer value.
65 WEBCORE_EXPORT IntPoint mapPoint(const IntPoint&) const;
67 WEBCORE_EXPORT FloatPoint mapPoint(const FloatPoint&) const;
69 IntSize mapSize(const IntSize&) const;
71 FloatSize mapSize(const FloatSize&) const;
73 // Rounds the resulting mapped rectangle out. This is helpful for bounding
74 // box computations but may not be what is wanted in other contexts.
75 IntRect mapRect(const IntRect&) const;
77 WEBCORE_EXPORT FloatRect mapRect(const FloatRect&) const;
78 FloatQuad mapQuad(const FloatQuad&) const;
80 WEBCORE_EXPORT bool isIdentity() const;
82 double a() const { return m_transform[0]; }
83 void setA(double a) { m_transform[0] = a; }
84 double b() const { return m_transform[1]; }
85 void setB(double b) { m_transform[1] = b; }
86 double c() const { return m_transform[2]; }
87 void setC(double c) { m_transform[2] = c; }
88 double d() const { return m_transform[3]; }
89 void setD(double d) { m_transform[3] = d; }
90 double e() const { return m_transform[4]; }
91 void setE(double e) { m_transform[4] = e; }
92 double f() const { return m_transform[5]; }
93 void setF(double f) { m_transform[5] = f; }
97 WEBCORE_EXPORT AffineTransform& multiply(const AffineTransform& other);
98 WEBCORE_EXPORT AffineTransform& scale(double);
99 AffineTransform& scale(double sx, double sy);
100 AffineTransform& scaleNonUniform(double sx, double sy);
101 AffineTransform& scale(const FloatSize&);
102 AffineTransform& rotate(double d);
103 AffineTransform& rotateFromVector(double x, double y);
104 WEBCORE_EXPORT AffineTransform& translate(double tx, double ty);
105 AffineTransform& translate(const FloatPoint&);
106 AffineTransform& shear(double sx, double sy);
107 AffineTransform& flipX();
108 WEBCORE_EXPORT AffineTransform& flipY();
109 AffineTransform& skew(double angleX, double angleY);
110 AffineTransform& skewX(double angle);
111 AffineTransform& skewY(double angle);
113 // These functions get the length of an axis-aligned unit vector
114 // once it has been mapped through the transform
115 WEBCORE_EXPORT double xScale() const;
116 WEBCORE_EXPORT double yScale() const;
119 WEBCORE_EXPORT bool isInvertible() const;
120 WEBCORE_EXPORT AffineTransform inverse() const;
122 void blend(const AffineTransform& from, double progress);
124 TransformationMatrix toTransformationMatrix() const;
126 bool isIdentityOrTranslation() const
128 return m_transform[0] == 1 && m_transform[1] == 0 && m_transform[2] == 0 && m_transform[3] == 1;
131 bool isIdentityOrTranslationOrFlipped() const
133 return m_transform[0] == 1 && m_transform[1] == 0 && m_transform[2] == 0 && (m_transform[3] == 1 || m_transform[3] == -1);
136 bool preservesAxisAlignment() const
138 return (m_transform[1] == 0 && m_transform[2] == 0) || (m_transform[0] == 0 && m_transform[3] == 0);
141 bool operator== (const AffineTransform& m2) const
143 return (m_transform[0] == m2.m_transform[0]
144 && m_transform[1] == m2.m_transform[1]
145 && m_transform[2] == m2.m_transform[2]
146 && m_transform[3] == m2.m_transform[3]
147 && m_transform[4] == m2.m_transform[4]
148 && m_transform[5] == m2.m_transform[5]);
151 bool operator!=(const AffineTransform& other) const { return !(*this == other); }
153 // *this = *this * t (i.e., a multRight)
154 AffineTransform& operator*=(const AffineTransform& t)
159 // result = *this * t (i.e., a multRight)
160 AffineTransform operator*(const AffineTransform& t) const
162 AffineTransform result = *this;
168 operator CGAffineTransform() const;
170 operator cairo_matrix_t() const;
173 static AffineTransform translation(double x, double y)
175 return AffineTransform(1, 0, 0, 1, x, y);
178 // decompose the matrix into its component parts
180 double scaleX, scaleY;
182 double remainderA, remainderB, remainderC, remainderD;
183 double translateX, translateY;
186 bool decompose(DecomposedType&) const;
187 void recompose(const DecomposedType&);
190 std::array<double, 6> m_transform;
193 AffineTransform makeMapBetweenRects(const FloatRect& source, const FloatRect& dest);