Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / gfx / thebes / public / gfxMatrix.h
blob4d45ae4009ad667a9b8a6273b5d9f2e6f83138ef
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Oracle Corporation code.
17 * The Initial Developer of the Original Code is Oracle Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Stuart Parmenter <pavlov@pavlov.net>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef GFX_MATRIX_H
39 #define GFX_MATRIX_H
41 #include "gfxPoint.h"
42 #include "gfxTypes.h"
43 #include "gfxRect.h"
44 #include "gfxUtils.h"
45 #include "nsMathUtils.h"
47 // XX - I don't think this class should use gfxFloat at all,
48 // but should use 'double' and be called gfxDoubleMatrix;
49 // we can then typedef that to gfxMatrix where we typedef
50 // double to be gfxFloat.
52 /**
53 * A matrix that represents an affine transformation. Projective
54 * transformations are not supported. This matrix looks like:
56 * / a b 0 \
57 * | c d 0 |
58 * \ tx ty 1 /
60 * So, transforming a point (x, y) results in:
62 * / a b 0 \ / a * x + c * y + tx \ T
63 * (x y 1) * | c d 0 | = | b * x + d * y + ty |
64 * \ tx ty 1 / \ 1 /
67 struct THEBES_API gfxMatrix {
68 double xx; double yx;
69 double xy; double yy;
70 double x0; double y0;
72 public:
73 /**
74 * Initializes this matrix as the identity matrix.
76 gfxMatrix() { Reset(); }
78 /**
79 * Initializes the matrix from individual components. See the class
80 * description for the layout of the matrix.
82 gfxMatrix(gfxFloat a, gfxFloat b, gfxFloat c, gfxFloat d, gfxFloat tx, gfxFloat ty) :
83 xx(a), yx(b),
84 xy(c), yy(d),
85 x0(tx), y0(ty) { }
87 /**
88 * Post-multiplies m onto the matrix.
90 const gfxMatrix& operator *= (const gfxMatrix& m) {
91 return Multiply(m);
94 /**
95 * Multiplies *this with m and returns the result.
97 gfxMatrix operator * (const gfxMatrix& m) const {
98 return gfxMatrix(*this).Multiply(m);
101 // matrix operations
103 * Resets this matrix to the identity matrix.
105 const gfxMatrix& Reset();
108 * Inverts this matrix, if possible. Otherwise, the matrix is left
109 * unchanged.
111 * XXX should this do something with the return value of
112 * cairo_matrix_invert?
114 const gfxMatrix& Invert();
117 * Check if matrix is singular (no inverse exists).
119 PRBool IsSingular() const {
120 // if the determinant (ad - bc) is zero it's singular
121 return (xx * yy) == (yx * xy);
125 * Scales this matrix. The scale is pre-multiplied onto this matrix,
126 * i.e. the scaling takes place before the other transformations.
128 const gfxMatrix& Scale(gfxFloat x, gfxFloat y);
131 * Translates this matrix. The translation is pre-multiplied onto this matrix,
132 * i.e. the translation takes place before the other transformations.
134 const gfxMatrix& Translate(const gfxPoint& pt);
137 * Rotates this matrix. The rotation is pre-multiplied onto this matrix,
138 * i.e. the translation takes place after the other transformations.
140 * @param radians Angle in radians.
142 const gfxMatrix& Rotate(gfxFloat radians);
145 * Multiplies the current matrix with m.
146 * This is a post-multiplication, i.e. the transformations of m are
147 * applied _after_ the existing transformations.
149 * XXX is that difference (compared to Rotate etc) a good thing?
151 const gfxMatrix& Multiply(const gfxMatrix& m);
154 * Transforms a point according to this matrix.
156 gfxPoint Transform(const gfxPoint& point) const;
160 * Transform a distance according to this matrix. This does not apply
161 * any translation components.
163 gfxSize Transform(const gfxSize& size) const;
166 * Transforms both the point and distance according to this matrix.
168 gfxRect Transform(const gfxRect& rect) const;
170 gfxRect TransformBounds(const gfxRect& rect) const;
173 * Returns the translation component of this matrix.
175 gfxPoint GetTranslation() const {
176 return gfxPoint(x0, y0);
180 * Returns true if the matrix is anything other than a straight
181 * translation by integers.
183 PRBool HasNonIntegerTranslation() const {
184 return HasNonTranslation() ||
185 !gfxUtils::FuzzyEqual(x0, NS_floor(x0 + 0.5)) ||
186 !gfxUtils::FuzzyEqual(y0, NS_floor(y0 + 0.5));
190 * Returns true if the matrix has any transform other
191 * than a straight translation
193 PRBool HasNonTranslation() const {
194 return !gfxUtils::FuzzyEqual(xx, 1.0) || !gfxUtils::FuzzyEqual(yy, 1.0) ||
195 !gfxUtils::FuzzyEqual(xy, 0.0) || !gfxUtils::FuzzyEqual(yx, 0.0);
199 * Returns true if the matrix has any transform other
200 * than a translation or a -1 y scale (y axis flip)
202 PRBool HasNonTranslationOrFlip() const {
203 return !gfxUtils::FuzzyEqual(xx, 1.0) ||
204 (!gfxUtils::FuzzyEqual(yy, 1.0) && !gfxUtils::FuzzyEqual(yy, -1.0)) ||
205 !gfxUtils::FuzzyEqual(xy, 0.0) || !gfxUtils::FuzzyEqual(yx, 0.0);
209 * Returns true if the matrix has any transform other
210 * than a translation or scale; this is, if there is
211 * no rotation.
213 PRBool HasNonAxisAlignedTransform() const {
214 return !gfxUtils::FuzzyEqual(xy, 0.0) || !gfxUtils::FuzzyEqual(yx, 0.0);
218 * Computes the determinant of this matrix.
220 double Determinant() const {
221 return xx*yy - yx*xy;
224 /* Computes the scale factors of this matrix; that is,
225 * the amounts each basis vector is scaled by.
226 * The xMajor parameter indicates if the larger scale is
227 * to be assumed to be in the X direction or not.
229 gfxSize ScaleFactors(PRBool xMajor) const {
230 double det = Determinant();
232 if (det == 0.0)
233 return gfxSize(0.0, 0.0);
235 gfxSize sz((xMajor != 0 ? 1.0 : 0.0),
236 (xMajor != 0 ? 0.0 : 1.0));
237 sz = Transform(sz);
239 double major = sqrt(sz.width * sz.width + sz.height * sz.height);
240 double minor = 0.0;
242 // ignore mirroring
243 if (det < 0.0)
244 det = - det;
246 if (major)
247 minor = det / major;
249 if (xMajor)
250 return gfxSize(major, minor);
252 return gfxSize(minor, major);
256 #endif /* GFX_MATRIX_H */