1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 mozilla.org code.
17 * The Initial Developer of the Original Code is
21 * Keith Schwarz <kschwarz@mozilla.com> (original author)
23 * Alternatively, the contents of this file may be used under the terms of
24 * either of the GNU General Public License Version 2 or later (the "GPL"),
25 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 * A class representing three matrices that can be used for style transforms.
41 #ifndef nsStyleTransformMatrix_h_
42 #define nsStyleTransformMatrix_h_
44 #include "nsCSSValue.h"
45 #include "gfxMatrix.h"
48 struct nsCSSValueList
;
51 * A class representing a style transformation matrix. The class actually
52 * wraps three different matrices, a constant matrix and two matrices
53 * whose values are scaled by the width and the height of the bounding
54 * rectangle for the object to transform. Thus, given a frame rectangle
55 * of dimensions (width, height) and a point (x, y) to transform, the matrix
56 * corresponds to the transform operation
58 * | a c e | |0 0 dX1| |0 0 dY1| | x |
59 *(| b d f | + |0 0 dX2| (width) + |0 0 dY2| (height)) | y |
60 * | 0 0 1 | |0 0 0| |0 0 0| | 1 |
62 * Note that unlike the Thebes gfxMatrix, vectors are column vectors and
63 * consequently the multiplication of a matrix A and a vector x is Ax, not xA.
67 class nsStyleTransformMatrix
71 * Constructor sets the matrix to the identity.
73 nsStyleTransformMatrix();
76 * Given a frame's bounding rectangle, returns a gfxMatrix
77 * corresponding to the transformation represented by this
78 * matrix. The transformation takes points in the frame's
79 * local space and converts them to points in the frame's
82 * @param aBounds The frame's bounding rectangle.
83 * @param aFactor The number of app units per device pixel.
84 * @return A Thebes matrix corresponding to the transform.
86 gfxMatrix
GetThebesMatrix(const nsRect
& aBounds
, float aFactor
) const;
89 * Multiplies this matrix by another matrix, in that order. If A'
90 * is the value of A after A *= B, then for any vector x, the
91 * equivalence A'(x) == A(B(x)) holds.
93 * @param aOther The matrix to multiply this matrix by.
94 * @return A reference to this matrix.
96 nsStyleTransformMatrix
& operator *= (const nsStyleTransformMatrix
&aOther
);
99 * Returns a new nsStyleTransformMatrix that is equal to one matrix
100 * multiplied by another matrix, in that order. If C is the result of
101 * A * B, then for any vector x, the equivalence C(x) = A(B(x)).
103 * @param aOther The matrix to multiply this matrix by.
104 * @return A new nsStyleTransformMatrix equal to this matrix multiplied
105 * by the other matrix.
107 const nsStyleTransformMatrix
108 operator * (const nsStyleTransformMatrix
&aOther
) const;
111 * Return the transform function, as an nsCSSKeyword, for the given
112 * nsCSSValue::Array from a transform list.
114 static nsCSSKeyword
TransformFunctionOf(const nsCSSValue::Array
* aData
);
117 * Given an nsCSSValue::Array* containing a -moz-transform function,
118 * updates this matrix to hold the value of that function.
120 * @param aData The nsCSSValue::Array* containing the transform function.
121 * @param aContext The style context, used for unit conversion.
122 * @param aPresContext The presentation context, used for unit conversion.
123 * @param aCanStoreInRuleTree Set to false if the result cannot be cached
124 * in the rule tree, otherwise untouched.
126 * aContext and aPresContext may be null if all of the (non-percent)
127 * length values in aData are already known to have been converted to
128 * eCSSUnit_Pixel (as they are in an nsStyleAnimation::Value)
130 void SetToTransformFunction(const nsCSSValue::Array
* aData
,
131 nsStyleContext
* aContext
,
132 nsPresContext
* aPresContext
,
133 PRBool
& aCanStoreInRuleTree
);
136 * The same as SetToTransformFunction, but for a list of transform
139 static nsStyleTransformMatrix
ReadTransforms(const nsCSSValueList
* aList
,
140 nsStyleContext
* aContext
,
141 nsPresContext
* aPresContext
,
142 PRBool
&aCanStoreInRuleTree
);
144 * Sets this matrix to be the identity matrix.
146 void SetToIdentity();
149 * Returns the value of the entry at the 2x2 submatrix of the
150 * transform matrix that defines the non-affine linear transform.
151 * The order is given as
155 * @param aIndex The element index.
156 * @return The value of the element at that index.
158 float GetMainMatrixEntry(PRInt32 aIndex
) const
160 NS_PRECONDITION(aIndex
>= 0 && aIndex
< 4, "Index out of bounds!");
161 return mMain
[aIndex
];
165 * Returns the value of the X or Y translation component of the matrix,
166 * given the specified bounds.
168 * @param aBounds The bounds of the element.
169 * @return The value of the X or Ytranslation component.
171 nscoord
GetXTranslation(const nsRect
& aBounds
) const;
172 nscoord
GetYTranslation(const nsRect
& aBounds
) const;
175 * Get the raw components used for GetXTranslation and GetYTranslation.
177 nscoord
GetCoordXTranslation() const { return mDelta
[0]; }
178 nscoord
GetCoordYTranslation() const { return mDelta
[1]; }
179 float GetWidthRelativeXTranslation() const { return mX
[0]; }
180 float GetWidthRelativeYTranslation() const { return mX
[1]; }
181 float GetHeightRelativeXTranslation() const { return mY
[0]; }
182 float GetHeightRelativeYTranslation() const { return mY
[1]; }
185 * Returns whether the two matrices are equal or not.
187 * @param aOther The matrix to compare to.
188 * @return Whether the two matrices are equal.
190 PRBool
operator== (const nsStyleTransformMatrix
& aOther
) const;
191 PRBool
operator!= (const nsStyleTransformMatrix
& aOther
) const
193 return !(*this == aOther
);
197 /* The three matrices look like this:
198 * |mMain[0] mMain[2] mDelta[0]|
199 * |mMain[1] mMain[3] mDelta[1]| <-- Constant matrix
203 * | 0 0 mX[1]| <-- Scaled by width of element
207 * | 0 0 mY[1]| <-- Scaled by height of element