1 | /* |
2 | * Copyright (C) 2005-2016 Apple Inc. All rights reserved. |
3 | * 2010 Dirk Schulze <[email protected]> |
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 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. |
25 | */ |
26 | |
27 | #ifndef AffineTransform_h |
28 | #define AffineTransform_h |
29 | |
30 | #include <array> |
31 | #include <wtf/FastMalloc.h> |
32 | #include <wtf/Forward.h> |
33 | |
34 | #if USE(CG) |
35 | typedef struct CGAffineTransform CGAffineTransform; |
36 | #endif |
37 | |
38 | #if PLATFORM(WIN) |
39 | struct D2D_MATRIX_3X2_F; |
40 | typedef D2D_MATRIX_3X2_F D2D1_MATRIX_3X2_F; |
41 | #endif |
42 | |
43 | namespace WTF { |
44 | class TextStream; |
45 | } |
46 | |
47 | namespace WebCore { |
48 | |
49 | class FloatPoint; |
50 | class FloatQuad; |
51 | class FloatRect; |
52 | class FloatSize; |
53 | class IntPoint; |
54 | class IntSize; |
55 | class IntRect; |
56 | class Region; |
57 | class TransformationMatrix; |
58 | |
59 | class AffineTransform { |
60 | WTF_MAKE_FAST_ALLOCATED; |
61 | public: |
62 | WEBCORE_EXPORT AffineTransform(); |
63 | WEBCORE_EXPORT AffineTransform(double a, double b, double c, double d, double e, double f); |
64 | |
65 | #if USE(CG) |
66 | WEBCORE_EXPORT AffineTransform(const CGAffineTransform&); |
67 | #endif |
68 | |
69 | #if PLATFORM(WIN) |
70 | AffineTransform(const D2D1_MATRIX_3X2_F&); |
71 | #endif |
72 | |
73 | void setMatrix(double a, double b, double c, double d, double e, double f); |
74 | |
75 | void map(double x, double y, double& x2, double& y2) const; |
76 | |
77 | // Rounds the mapped point to the nearest integer value. |
78 | WEBCORE_EXPORT IntPoint mapPoint(const IntPoint&) const; |
79 | |
80 | WEBCORE_EXPORT FloatPoint mapPoint(const FloatPoint&) const; |
81 | |
82 | WEBCORE_EXPORT IntSize mapSize(const IntSize&) const; |
83 | |
84 | WEBCORE_EXPORT FloatSize mapSize(const FloatSize&) const; |
85 | |
86 | // Rounds the resulting mapped rectangle out. This is helpful for bounding |
87 | // box computations but may not be what is wanted in other contexts. |
88 | WEBCORE_EXPORT IntRect mapRect(const IntRect&) const; |
89 | |
90 | WEBCORE_EXPORT FloatRect mapRect(const FloatRect&) const; |
91 | WEBCORE_EXPORT FloatQuad mapQuad(const FloatQuad&) const; |
92 | |
93 | WEBCORE_EXPORT Region mapRegion(const Region&) const; |
94 | |
95 | WEBCORE_EXPORT bool isIdentity() const; |
96 | |
97 | double a() const { return m_transform[0]; } |
98 | void setA(double a) { m_transform[0] = a; } |
99 | double b() const { return m_transform[1]; } |
100 | void setB(double b) { m_transform[1] = b; } |
101 | double c() const { return m_transform[2]; } |
102 | void setC(double c) { m_transform[2] = c; } |
103 | double d() const { return m_transform[3]; } |
104 | void setD(double d) { m_transform[3] = d; } |
105 | double e() const { return m_transform[4]; } |
106 | void setE(double e) { m_transform[4] = e; } |
107 | double f() const { return m_transform[5]; } |
108 | void setF(double f) { m_transform[5] = f; } |
109 | |
110 | WEBCORE_EXPORT void makeIdentity(); |
111 | |
112 | WEBCORE_EXPORT AffineTransform& multiply(const AffineTransform& other); |
113 | WEBCORE_EXPORT AffineTransform& scale(double); |
114 | AffineTransform& scale(double sx, double sy); |
115 | WEBCORE_EXPORT AffineTransform& scaleNonUniform(double sx, double sy); // Same as scale(sx, sy). |
116 | WEBCORE_EXPORT AffineTransform& scale(const FloatSize&); |
117 | WEBCORE_EXPORT AffineTransform& rotate(double); |
118 | AffineTransform& rotateFromVector(double x, double y); |
119 | WEBCORE_EXPORT AffineTransform& translate(double tx, double ty); |
120 | WEBCORE_EXPORT AffineTransform& translate(const FloatPoint&); |
121 | WEBCORE_EXPORT AffineTransform& translate(const FloatSize&); |
122 | WEBCORE_EXPORT AffineTransform& shear(double sx, double sy); |
123 | WEBCORE_EXPORT AffineTransform& flipX(); |
124 | WEBCORE_EXPORT AffineTransform& flipY(); |
125 | WEBCORE_EXPORT AffineTransform& skew(double angleX, double angleY); |
126 | AffineTransform& skewX(double angle); |
127 | AffineTransform& skewY(double angle); |
128 | |
129 | // These functions get the length of an axis-aligned unit vector |
130 | // once it has been mapped through the transform |
131 | WEBCORE_EXPORT double xScale() const; |
132 | WEBCORE_EXPORT double yScale() const; |
133 | |
134 | bool isInvertible() const; // If you call this this, you're probably doing it wrong. |
135 | WEBCORE_EXPORT Optional<AffineTransform> inverse() const; |
136 | |
137 | WEBCORE_EXPORT void blend(const AffineTransform& from, double progress); |
138 | |
139 | WEBCORE_EXPORT TransformationMatrix toTransformationMatrix() const; |
140 | |
141 | bool isIdentityOrTranslation() const |
142 | { |
143 | return m_transform[0] == 1 && m_transform[1] == 0 && m_transform[2] == 0 && m_transform[3] == 1; |
144 | } |
145 | |
146 | bool isIdentityOrTranslationOrFlipped() const |
147 | { |
148 | return m_transform[0] == 1 && m_transform[1] == 0 && m_transform[2] == 0 && (m_transform[3] == 1 || m_transform[3] == -1); |
149 | } |
150 | |
151 | bool preservesAxisAlignment() const |
152 | { |
153 | return (m_transform[1] == 0 && m_transform[2] == 0) || (m_transform[0] == 0 && m_transform[3] == 0); |
154 | } |
155 | |
156 | bool operator== (const AffineTransform& m2) const |
157 | { |
158 | return (m_transform[0] == m2.m_transform[0] |
159 | && m_transform[1] == m2.m_transform[1] |
160 | && m_transform[2] == m2.m_transform[2] |
161 | && m_transform[3] == m2.m_transform[3] |
162 | && m_transform[4] == m2.m_transform[4] |
163 | && m_transform[5] == m2.m_transform[5]); |
164 | } |
165 | |
166 | bool operator!=(const AffineTransform& other) const { return !(*this == other); } |
167 | |
168 | // *this = *this * t (i.e., a multRight) |
169 | AffineTransform& operator*=(const AffineTransform& t) |
170 | { |
171 | return multiply(t); |
172 | } |
173 | |
174 | // result = *this * t (i.e., a multRight) |
175 | AffineTransform operator*(const AffineTransform& t) const |
176 | { |
177 | AffineTransform result = *this; |
178 | result *= t; |
179 | return result; |
180 | } |
181 | |
182 | #if USE(CG) |
183 | WEBCORE_EXPORT operator CGAffineTransform() const; |
184 | #endif |
185 | |
186 | #if PLATFORM(WIN) |
187 | operator D2D1_MATRIX_3X2_F() const; |
188 | #endif |
189 | |
190 | static AffineTransform translation(double x, double y) |
191 | { |
192 | return AffineTransform(1, 0, 0, 1, x, y); |
193 | } |
194 | |
195 | // decompose the matrix into its component parts |
196 | typedef struct { |
197 | double scaleX, scaleY; |
198 | double angle; |
199 | double remainderA, remainderB, remainderC, remainderD; |
200 | double translateX, translateY; |
201 | } DecomposedType; |
202 | |
203 | bool decompose(DecomposedType&) const; |
204 | void recompose(const DecomposedType&); |
205 | |
206 | private: |
207 | std::array<double, 6> m_transform; |
208 | }; |
209 | |
210 | WEBCORE_EXPORT AffineTransform makeMapBetweenRects(const FloatRect& source, const FloatRect& dest); |
211 | |
212 | WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const AffineTransform&); |
213 | |
214 | } |
215 | |
216 | #endif |
217 | |