Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / transforms / AffineTransform.h
1 /*
2  * Copyright (C) 2005, 2006 Apple Computer, Inc.  All rights reserved.
3  *               2010 Dirk Schulze <krit@webkit.org>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, 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 COMPUTER, 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.
25  */
26
27 #ifndef AffineTransform_h
28 #define AffineTransform_h
29
30 #include "platform/transforms/TransformationMatrix.h"
31
32 #include <string.h> // for memcpy
33 #include "wtf/FastAllocBase.h"
34
35 namespace blink {
36
37 class FloatPoint;
38 class FloatQuad;
39 class FloatRect;
40 class IntPoint;
41 class IntRect;
42 class TransformationMatrix;
43
44 class PLATFORM_EXPORT AffineTransform {
45     WTF_MAKE_FAST_ALLOCATED;
46 public:
47     typedef double Transform[6];
48
49     AffineTransform();
50     AffineTransform(double a, double b, double c, double d, double e, double f);
51
52     void setMatrix(double a, double b, double c, double d, double e, double f);
53
54     void map(double x, double y, double& x2, double& y2) const;
55
56     // Rounds the mapped point to the nearest integer value.
57     IntPoint mapPoint(const IntPoint&) const;
58
59     FloatPoint mapPoint(const FloatPoint&) const;
60
61     IntSize mapSize(const IntSize&) const;
62
63     FloatSize mapSize(const FloatSize&) const;
64
65     // Rounds the resulting mapped rectangle out. This is helpful for bounding
66     // box computations but may not be what is wanted in other contexts.
67     IntRect mapRect(const IntRect&) const;
68
69     FloatRect mapRect(const FloatRect&) const;
70     FloatQuad mapQuad(const FloatQuad&) const;
71
72     bool isIdentity() const;
73
74     double a() const { return m_transform[0]; }
75     void setA(double a) { m_transform[0] = a; }
76     double b() const { return m_transform[1]; }
77     void setB(double b) { m_transform[1] = b; }
78     double c() const { return m_transform[2]; }
79     void setC(double c) { m_transform[2] = c; }
80     double d() const { return m_transform[3]; }
81     void setD(double d) { m_transform[3] = d; }
82     double e() const { return m_transform[4]; }
83     void setE(double e) { m_transform[4] = e; }
84     double f() const { return m_transform[5]; }
85     void setF(double f) { m_transform[5] = f; }
86
87     void makeIdentity();
88
89     AffineTransform& multiply(const AffineTransform& other);
90     AffineTransform& scale(double);
91     AffineTransform& scale(double sx, double sy);
92     AffineTransform& scaleNonUniform(double sx, double sy);
93     AffineTransform& rotate(double a);
94     AffineTransform& rotateRadians(double a);
95     AffineTransform& rotateFromVector(double x, double y);
96     AffineTransform& translate(double tx, double ty);
97     AffineTransform& shear(double sx, double sy);
98     AffineTransform& flipX();
99     AffineTransform& flipY();
100     AffineTransform& skew(double angleX, double angleY);
101     AffineTransform& skewX(double angle);
102     AffineTransform& skewY(double angle);
103
104     double xScale() const;
105     double yScale() const;
106
107     double det() const;
108     bool isInvertible() const;
109     AffineTransform inverse() const;
110
111     TransformationMatrix toTransformationMatrix() const;
112
113     bool isIdentityOrTranslation() const
114     {
115         return m_transform[0] == 1 && m_transform[1] == 0 && m_transform[2] == 0 && m_transform[3] == 1;
116     }
117
118     bool isIdentityOrTranslationOrFlipped() const
119     {
120         return m_transform[0] == 1 && m_transform[1] == 0 && m_transform[2] == 0 && (m_transform[3] == 1 || m_transform[3] == -1);
121     }
122
123     bool preservesAxisAlignment() const
124     {
125         return (m_transform[1] == 0 && m_transform[2] == 0) || (m_transform[0] == 0 && m_transform[3] == 0);
126     }
127
128     bool operator== (const AffineTransform& m2) const
129     {
130         return (m_transform[0] == m2.m_transform[0]
131              && m_transform[1] == m2.m_transform[1]
132              && m_transform[2] == m2.m_transform[2]
133              && m_transform[3] == m2.m_transform[3]
134              && m_transform[4] == m2.m_transform[4]
135              && m_transform[5] == m2.m_transform[5]);
136     }
137
138     bool operator!=(const AffineTransform& other) const { return !(*this == other); }
139
140     // *this = *this * t (i.e., a multRight)
141     AffineTransform& operator*=(const AffineTransform& t)
142     {
143         return multiply(t);
144     }
145
146     // result = *this * t (i.e., a multRight)
147     AffineTransform operator*(const AffineTransform& t) const
148     {
149         AffineTransform result = *this;
150         result *= t;
151         return result;
152     }
153
154     static AffineTransform translation(double x, double y)
155     {
156         return AffineTransform(1, 0, 0, 1, x, y);
157     }
158
159     // decompose the matrix into its component parts
160     typedef struct {
161         double scaleX, scaleY;
162         double angle;
163         double remainderA, remainderB, remainderC, remainderD;
164         double translateX, translateY;
165     } DecomposedType;
166
167     bool decompose(DecomposedType&) const;
168     void recompose(const DecomposedType&);
169
170 private:
171     void setMatrix(const Transform m)
172     {
173         if (m && m != m_transform)
174             memcpy(m_transform, m, sizeof(Transform));
175     }
176
177     Transform m_transform;
178 };
179
180 PLATFORM_EXPORT AffineTransform makeMapBetweenRects(const FloatRect& source, const FloatRect& dest);
181
182 }
183
184 #endif