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 used for intermediate representations of the -moz-transform property.
41 #include "nsStyleTransformMatrix.h"
42 #include "nsAutoPtr.h"
43 #include "nsCSSValue.h"
44 #include "nsStyleContext.h"
45 #include "nsPresContext.h"
46 #include "nsRuleNode.h"
47 #include "nsCSSKeywords.h"
48 #include "nsMathUtils.h"
50 /* Note on floating point precision: The transform matrix is an array
51 * of single precision 'float's, and so are most of the input values
52 * we get from the style system, but intermediate calculations
53 * involving angles need to be done in 'double'.
56 /* Force small values to zero. We do this to avoid having sin(360deg)
57 * evaluate to a tiny but nonzero value.
59 static double FlushToZero(double aVal
)
61 if (-FLT_EPSILON
< aVal
&& aVal
< FLT_EPSILON
)
67 /* Computes tan(aTheta). For values of aTheta such that tan(aTheta) is
68 * undefined or very large, SafeTangent returns a manageably large value
69 * of the correct sign.
71 static double SafeTangent(double aTheta
)
73 const double kEpsilon
= 0.0001;
75 /* tan(theta) = sin(theta)/cos(theta); problems arise when
76 * cos(theta) is too close to zero. Limit cos(theta) to the
77 * range [-1, -epsilon] U [epsilon, 1].
79 double sinTheta
= sin(aTheta
);
80 double cosTheta
= cos(aTheta
);
82 if (cosTheta
>= 0 && cosTheta
< kEpsilon
)
84 else if (cosTheta
< 0 && cosTheta
>= -kEpsilon
)
87 return FlushToZero(sinTheta
/ cosTheta
);
90 /* Constructor sets the data to the identity matrix. */
91 nsStyleTransformMatrix::nsStyleTransformMatrix()
96 /* SetToIdentity just fills in the appropriate values. */
97 void nsStyleTransformMatrix::SetToIdentity()
99 /* Set the main matrix to the identity. */
107 /* Both translation matrices are zero. */
114 /* Adds the constant translation to the scale factor translation components. */
115 nscoord
nsStyleTransformMatrix::GetXTranslation(const nsRect
& aBounds
) const
117 return NSToCoordRound(aBounds
.width
* mX
[0] + aBounds
.height
* mY
[0]) +
120 nscoord
nsStyleTransformMatrix::GetYTranslation(const nsRect
& aBounds
) const
122 return NSToCoordRound(aBounds
.width
* mX
[1] + aBounds
.height
* mY
[1]) +
126 /* GetThebesMatrix converts the stored matrix in a few steps. */
127 gfxMatrix
nsStyleTransformMatrix::GetThebesMatrix(const nsRect
& aBounds
,
130 /* Compute the graphics matrix. We take the stored main elements, along with
131 * the delta, and add in the matrices:
138 * | 0 0 dy2| * height
141 return gfxMatrix(mMain
[0], mMain
[1], mMain
[2], mMain
[3],
142 NSAppUnitsToFloatPixels(GetXTranslation(aBounds
), aScale
),
143 NSAppUnitsToFloatPixels(GetYTranslation(aBounds
), aScale
));
146 /* Performs the matrix multiplication necessary to multiply the two matrices,
147 * then hands back a reference to ourself.
149 nsStyleTransformMatrix
&
150 nsStyleTransformMatrix::operator *= (const nsStyleTransformMatrix
&aOther
)
152 /* We'll buffer all of our results into a temporary storage location
153 * during this operation since we don't want to overwrite the values of
154 * the old matrix with the values of the new.
162 * |a1 c1 e1| |a0 c0 e0| |a0a1 + b0c1 c0a1 + d0c1 e0a1 + f0c1 + e1|
163 * |b1 d1 f1|x|b0 d0 f0| = |a0b1 + b0d1 c0b1 + d0d1 e0b1 + f0d1 + f1|
164 * |0 0 1 | | 0 0 1| | 0 0 1|
166 newMatrix
[0] = aOther
.mMain
[0] * mMain
[0] + aOther
.mMain
[1] * mMain
[2];
167 newMatrix
[1] = aOther
.mMain
[0] * mMain
[1] + aOther
.mMain
[1] * mMain
[3];
168 newMatrix
[2] = aOther
.mMain
[2] * mMain
[0] + aOther
.mMain
[3] * mMain
[2];
169 newMatrix
[3] = aOther
.mMain
[2] * mMain
[1] + aOther
.mMain
[3] * mMain
[3];
170 newDelta
[0] = NSToCoordRound(aOther
.mDelta
[0] * mMain
[0] +
171 aOther
.mDelta
[1] * mMain
[2]) + mDelta
[0];
172 newDelta
[1] = NSToCoordRound(aOther
.mDelta
[0] * mMain
[1] +
173 aOther
.mDelta
[1] * mMain
[3]) + mDelta
[1];
175 /* For consistent terminology, let u0, u1, v0, and v1 be the four transform
176 * coordinates from our matrix, and let x0, x1, y0, and y1 be the four
177 * transform coordinates from the other matrix. Then the new transform
180 * u0' = a1u0 + c1u1 + x0
181 * u1' = b1u0 + d1u1 + x1
182 * v0' = a1v0 + c1v1 + y0
183 * v1' = b1v0 + d1v1 + y1
185 newX
[0] = mMain
[0] * aOther
.mX
[0] + mMain
[2] * aOther
.mX
[1] + mX
[0];
186 newX
[1] = mMain
[1] * aOther
.mX
[0] + mMain
[3] * aOther
.mX
[1] + mX
[1];
187 newY
[0] = mMain
[0] * aOther
.mY
[0] + mMain
[2] * aOther
.mY
[1] + mY
[0];
188 newY
[1] = mMain
[1] * aOther
.mY
[0] + mMain
[3] * aOther
.mY
[1] + mY
[1];
190 /* Now, write everything back in. */
191 for (PRInt32 index
= 0; index
< 4; ++index
)
192 mMain
[index
] = newMatrix
[index
];
193 for (PRInt32 index
= 0; index
< 2; ++index
) {
194 mDelta
[index
] = newDelta
[index
];
195 mX
[index
] = newX
[index
];
196 mY
[index
] = newY
[index
];
199 /* As promised, return a reference to ourselves. */
203 /* op* is implemented in terms of op*=. */
204 const nsStyleTransformMatrix
205 nsStyleTransformMatrix::operator *(const nsStyleTransformMatrix
&aOther
) const
207 return nsStyleTransformMatrix(*this) *= aOther
;
210 /* Helper function to fill in an nscoord with the specified nsCSSValue. */
211 static void SetCoordToValue(const nsCSSValue
&aValue
,
212 nsStyleContext
* aContext
,
213 nsPresContext
* aPresContext
,
214 PRBool
&aCanStoreInRuleTree
, nscoord
&aOut
)
216 aOut
= nsRuleNode::CalcLength(aValue
, aContext
, aPresContext
,
217 aCanStoreInRuleTree
);
220 /* Helper function to process a matrix entry. */
221 static void ProcessMatrix(float aMain
[4], nscoord aDelta
[2],
222 float aX
[2], float aY
[2],
223 const nsCSSValue::Array
* aData
,
224 nsStyleContext
* aContext
,
225 nsPresContext
* aPresContext
,
226 PRBool
& aCanStoreInRuleTree
)
228 NS_PRECONDITION(aData
->Count() == 7, "Invalid array!");
230 /* Take the first four elements out of the array as floats and store
233 for (PRUint16 index
= 1; index
<= 4; ++index
)
234 aMain
[index
- 1] = aData
->Item(index
).GetFloatValue();
236 /* For the fifth element, if it's a percentage, store it in aX[0].
237 * Otherwise, it's a length that needs to go in aDelta[0]
239 if (aData
->Item(5).GetUnit() == eCSSUnit_Percent
)
240 aX
[0] = aData
->Item(5).GetPercentValue();
242 SetCoordToValue(aData
->Item(5), aContext
, aPresContext
, aCanStoreInRuleTree
,
245 /* For the final element, if it's a percentage, store it in aY[1].
246 * Otherwise, it's a length that needs to go in aDelta[1].
248 if (aData
->Item(6).GetUnit() == eCSSUnit_Percent
)
249 aY
[1] = aData
->Item(6).GetPercentValue();
251 SetCoordToValue(aData
->Item(6), aContext
, aPresContext
, aCanStoreInRuleTree
,
255 /* Helper function to process a translatex function. */
256 static void ProcessTranslateX(nscoord aDelta
[2], float aX
[2],
257 const nsCSSValue::Array
* aData
,
258 nsStyleContext
* aContext
,
259 nsPresContext
* aPresContext
,
260 PRBool
& aCanStoreInRuleTree
)
262 NS_PRECONDITION(aData
->Count() == 2, "Invalid array!");
264 /* There are two cases. If we have a number, we want our matrix to look
272 * Otherwise, we might have a percentage, so we want to set the dX component
275 if (aData
->Item(1).GetUnit() != eCSSUnit_Percent
)
276 SetCoordToValue(aData
->Item(1), aContext
, aPresContext
, aCanStoreInRuleTree
,
279 aX
[0] = aData
->Item(1).GetPercentValue();
282 /* Helper function to process a translatey function. */
283 static void ProcessTranslateY(nscoord aDelta
[2], float aY
[2],
284 const nsCSSValue::Array
* aData
,
285 nsStyleContext
* aContext
,
286 nsPresContext
* aPresContext
,
287 PRBool
& aCanStoreInRuleTree
)
289 NS_PRECONDITION(aData
->Count() == 2, "Invalid array!");
291 /* There are two cases. If we have a number, we want our matrix to look
299 * Otherwise, we might have a percentage, so we want to set the dY component
302 if (aData
->Item(1).GetUnit() != eCSSUnit_Percent
)
303 SetCoordToValue(aData
->Item(1), aContext
, aPresContext
, aCanStoreInRuleTree
,
306 aY
[1] = aData
->Item(1).GetPercentValue();
309 /* Helper function to process a translate function. */
310 static void ProcessTranslate(nscoord aDelta
[2], float aX
[2], float aY
[2],
311 const nsCSSValue::Array
* aData
,
312 nsStyleContext
* aContext
,
313 nsPresContext
* aPresContext
,
314 PRBool
& aCanStoreInRuleTree
)
316 NS_PRECONDITION(aData
->Count() == 2 || aData
->Count() == 3, "Invalid array!");
318 /* There are several cases to consider.
319 * First, we might have one value, or we might have two. If we have
320 * two, we need to consider both dX and dY components.
321 * Next, the values might be lengths, or they might be percents. If they're
322 * percents, store them in the dX and dY components. Otherwise, store them in
326 const nsCSSValue
&dx
= aData
->Item(1);
327 if (dx
.GetUnit() == eCSSUnit_Percent
)
328 aX
[0] = dx
.GetPercentValue();
330 SetCoordToValue(dx
, aContext
, aPresContext
, aCanStoreInRuleTree
, aDelta
[0]);
332 /* If we read in a Y component, set it appropriately */
333 if (aData
->Count() == 3) {
334 const nsCSSValue
&dy
= aData
->Item(2);
335 if (dy
.GetUnit() == eCSSUnit_Percent
)
336 aY
[1] = dy
.GetPercentValue();
338 SetCoordToValue(dy
, aContext
, aPresContext
, aCanStoreInRuleTree
,
343 /* Helper function to set up a scale matrix. */
344 static void ProcessScaleHelper(float aXScale
, float aYScale
, float aMain
[4])
346 /* We want our matrix to look like this:
356 /* Process a scalex function. */
357 static void ProcessScaleX(float aMain
[4], const nsCSSValue::Array
* aData
)
359 NS_PRECONDITION(aData
->Count() == 2, "Bad array!");
360 ProcessScaleHelper(aData
->Item(1).GetFloatValue(), 1.0f
, aMain
);
363 /* Process a scaley function. */
364 static void ProcessScaleY(float aMain
[4], const nsCSSValue::Array
* aData
)
366 NS_PRECONDITION(aData
->Count() == 2, "Bad array!");
367 ProcessScaleHelper(1.0f
, aData
->Item(1).GetFloatValue(), aMain
);
370 /* Process a scale function. */
371 static void ProcessScale(float aMain
[4], const nsCSSValue::Array
* aData
)
373 NS_PRECONDITION(aData
->Count() == 2 || aData
->Count() == 3, "Bad array!");
374 /* We either have one element or two. If we have one, it's for both X and Y.
375 * Otherwise it's one for each.
377 const nsCSSValue
& scaleX
= aData
->Item(1);
378 const nsCSSValue
& scaleY
= (aData
->Count() == 2 ? scaleX
:
381 ProcessScaleHelper(scaleX
.GetFloatValue(),
382 scaleY
.GetFloatValue(), aMain
);
385 /* Helper function that, given a set of angles, constructs the appropriate
388 static void ProcessSkewHelper(double aXAngle
, double aYAngle
, float aMain
[4])
390 /* We want our matrix to look like this:
394 * However, to avoid infinite values, we'll use the SafeTangent function
395 * instead of the C standard tan function.
397 aMain
[2] = SafeTangent(aXAngle
);
398 aMain
[1] = SafeTangent(aYAngle
);
401 /* Function that converts a skewx transform into a matrix. */
402 static void ProcessSkewX(float aMain
[4], const nsCSSValue::Array
* aData
)
404 NS_ASSERTION(aData
->Count() == 2, "Bad array!");
405 ProcessSkewHelper(aData
->Item(1).GetAngleValueInRadians(), 0.0, aMain
);
408 /* Function that converts a skewy transform into a matrix. */
409 static void ProcessSkewY(float aMain
[4], const nsCSSValue::Array
* aData
)
411 NS_ASSERTION(aData
->Count() == 2, "Bad array!");
412 ProcessSkewHelper(0.0, aData
->Item(1).GetAngleValueInRadians(), aMain
);
415 /* Function that converts a skew transform into a matrix. */
416 static void ProcessSkew(float aMain
[4], const nsCSSValue::Array
* aData
)
418 NS_ASSERTION(aData
->Count() == 2 || aData
->Count() == 3, "Bad array!");
420 double xSkew
= aData
->Item(1).GetAngleValueInRadians();
421 double ySkew
= (aData
->Count() == 2
422 ? 0.0 : aData
->Item(2).GetAngleValueInRadians());
424 ProcessSkewHelper(xSkew
, ySkew
, aMain
);
427 /* Function that converts a rotate transform into a matrix. */
428 static void ProcessRotate(float aMain
[4], const nsCSSValue::Array
* aData
)
430 NS_PRECONDITION(aData
->Count() == 2, "Invalid array!");
432 /* We want our matrix to look like this:
433 * | cos(theta) -sin(theta) 0|
434 * | sin(theta) cos(theta) 0|
436 * (see http://www.w3.org/TR/SVG/coords.html#RotationDefined)
438 double theta
= aData
->Item(1).GetAngleValueInRadians();
439 float cosTheta
= FlushToZero(cos(theta
));
440 float sinTheta
= FlushToZero(sin(theta
));
444 aMain
[2] = -sinTheta
;
449 * SetToTransformFunction is essentially a giant switch statement that fans
450 * out to many smaller helper functions.
453 nsStyleTransformMatrix::SetToTransformFunction(const nsCSSValue::Array
* aData
,
454 nsStyleContext
* aContext
,
455 nsPresContext
* aPresContext
,
456 PRBool
& aCanStoreInRuleTree
)
458 NS_PRECONDITION(aData
, "Why did you want to get data from a null array?");
459 NS_PRECONDITION(aContext
, "Need a context for unit conversion!");
460 NS_PRECONDITION(aPresContext
, "Need a context for unit conversion!");
462 /* Reset the matrix to the identity so that each subfunction can just
463 * worry about its own components.
467 /* Get the keyword for the transform. */
468 nsAutoString keyword
;
469 aData
->Item(0).GetStringValue(keyword
);
470 switch (nsCSSKeywords::LookupKeyword(keyword
)) {
471 case eCSSKeyword_translatex
:
472 ProcessTranslateX(mDelta
, mX
, aData
, aContext
, aPresContext
,
473 aCanStoreInRuleTree
);
475 case eCSSKeyword_translatey
:
476 ProcessTranslateY(mDelta
, mY
, aData
, aContext
, aPresContext
,
477 aCanStoreInRuleTree
);
479 case eCSSKeyword_translate
:
480 ProcessTranslate(mDelta
, mX
, mY
, aData
, aContext
, aPresContext
,
481 aCanStoreInRuleTree
);
483 case eCSSKeyword_scalex
:
484 ProcessScaleX(mMain
, aData
);
486 case eCSSKeyword_scaley
:
487 ProcessScaleY(mMain
, aData
);
489 case eCSSKeyword_scale
:
490 ProcessScale(mMain
, aData
);
492 case eCSSKeyword_skewx
:
493 ProcessSkewX(mMain
, aData
);
495 case eCSSKeyword_skewy
:
496 ProcessSkewY(mMain
, aData
);
498 case eCSSKeyword_skew
:
499 ProcessSkew(mMain
, aData
);
501 case eCSSKeyword_rotate
:
502 ProcessRotate(mMain
, aData
);
504 case eCSSKeyword_matrix
:
505 ProcessMatrix(mMain
, mDelta
, mX
, mY
, aData
, aContext
, aPresContext
,
506 aCanStoreInRuleTree
);
509 NS_NOTREACHED("Unknown transform function!");
513 /* Does an element-by-element comparison and returns whether or not the
514 * matrices are equal.
517 nsStyleTransformMatrix::operator ==(const nsStyleTransformMatrix
&aOther
) const
519 for (PRInt32 index
= 0; index
< 4; ++index
)
520 if (mMain
[index
] != aOther
.mMain
[index
])
523 for (PRInt32 index
= 0; index
< 2; ++index
)
524 if (mDelta
[index
] != aOther
.mDelta
[index
] ||
525 mX
[index
] != aOther
.mX
[index
] ||
526 mY
[index
] != aOther
.mY
[index
])