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
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.
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 ***** */
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.
53 * A matrix that represents an affine transformation. Projective
54 * transformations are not supported. This matrix looks like:
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 |
67 struct THEBES_API gfxMatrix
{
74 * Initializes this matrix as the identity matrix.
76 gfxMatrix() { Reset(); }
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
) :
88 * Post-multiplies m onto the matrix.
90 const gfxMatrix
& operator *= (const gfxMatrix
& m
) {
95 * Multiplies *this with m and returns the result.
97 gfxMatrix
operator * (const gfxMatrix
& m
) const {
98 return gfxMatrix(*this).Multiply(m
);
103 * Resets this matrix to the identity matrix.
105 const gfxMatrix
& Reset();
108 * Inverts this matrix, if possible. Otherwise, the matrix is left
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
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();
233 return gfxSize(0.0, 0.0);
235 gfxSize
sz((xMajor
!= 0 ? 1.0 : 0.0),
236 (xMajor
!= 0 ? 0.0 : 1.0));
239 double major
= sqrt(sz
.width
* sz
.width
+ sz
.height
* sz
.height
);
250 return gfxSize(major
, minor
);
252 return gfxSize(minor
, major
);
256 #endif /* GFX_MATRIX_H */