Bug 596094 - fix a race condition w/ipc disabled in focus related event handling...
[mozilla-central.git] / layout / style / nsStyleTransformMatrix.cpp
blobe42bba6598879ee41f86061a0424abcb85620f01
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
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Mozilla Corporation
20 * Contributor(s):
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"
49 #include "CSSCalc.h"
50 #include "nsCSSStruct.h"
52 namespace css = mozilla::css;
54 /* Note on floating point precision: The transform matrix is an array
55 * of single precision 'float's, and so are most of the input values
56 * we get from the style system, but intermediate calculations
57 * involving angles need to be done in 'double'.
60 /* Force small values to zero. We do this to avoid having sin(360deg)
61 * evaluate to a tiny but nonzero value.
63 static double FlushToZero(double aVal)
65 if (-FLT_EPSILON < aVal && aVal < FLT_EPSILON)
66 return 0.0f;
67 else
68 return aVal;
71 /* Computes tan(aTheta). For values of aTheta such that tan(aTheta) is
72 * undefined or very large, SafeTangent returns a manageably large value
73 * of the correct sign.
75 static double SafeTangent(double aTheta)
77 const double kEpsilon = 0.0001;
79 /* tan(theta) = sin(theta)/cos(theta); problems arise when
80 * cos(theta) is too close to zero. Limit cos(theta) to the
81 * range [-1, -epsilon] U [epsilon, 1].
83 double sinTheta = sin(aTheta);
84 double cosTheta = cos(aTheta);
86 if (cosTheta >= 0 && cosTheta < kEpsilon)
87 cosTheta = kEpsilon;
88 else if (cosTheta < 0 && cosTheta >= -kEpsilon)
89 cosTheta = -kEpsilon;
91 return FlushToZero(sinTheta / cosTheta);
94 /* Constructor sets the data to the identity matrix. */
95 nsStyleTransformMatrix::nsStyleTransformMatrix()
97 SetToIdentity();
100 /* SetToIdentity just fills in the appropriate values. */
101 void nsStyleTransformMatrix::SetToIdentity()
103 /* Set the main matrix to the identity. */
104 mMain[0] = 1.0f;
105 mMain[1] = 0.0f;
106 mMain[2] = 0.0f;
107 mMain[3] = 1.0f;
108 mDelta[0] = 0;
109 mDelta[1] = 0;
111 /* Both translation matrices are zero. */
112 mX[0] = 0.0f;
113 mX[1] = 0.0f;
114 mY[0] = 0.0f;
115 mY[1] = 0.0f;
118 /* Adds the constant translation to the scale factor translation components. */
119 nscoord nsStyleTransformMatrix::GetXTranslation(const nsRect& aBounds) const
121 return NSToCoordRound(aBounds.width * mX[0] + aBounds.height * mY[0]) +
122 mDelta[0];
124 nscoord nsStyleTransformMatrix::GetYTranslation(const nsRect& aBounds) const
126 return NSToCoordRound(aBounds.width * mX[1] + aBounds.height * mY[1]) +
127 mDelta[1];
130 /* GetThebesMatrix converts the stored matrix in a few steps. */
131 gfxMatrix nsStyleTransformMatrix::GetThebesMatrix(const nsRect& aBounds,
132 float aScale) const
134 /* Compute the graphics matrix. We take the stored main elements, along with
135 * the delta, and add in the matrices:
137 * | 0 0 dx1|
138 * | 0 0 dx2| * width
139 * | 0 0 0|
141 * | 0 0 dy1|
142 * | 0 0 dy2| * height
143 * | 0 0 0|
145 return gfxMatrix(mMain[0], mMain[1], mMain[2], mMain[3],
146 NSAppUnitsToFloatPixels(GetXTranslation(aBounds), aScale),
147 NSAppUnitsToFloatPixels(GetYTranslation(aBounds), aScale));
150 /* Performs the matrix multiplication necessary to multiply the two matrices,
151 * then hands back a reference to ourself.
153 nsStyleTransformMatrix&
154 nsStyleTransformMatrix::operator *= (const nsStyleTransformMatrix &aOther)
156 /* We'll buffer all of our results into a temporary storage location
157 * during this operation since we don't want to overwrite the values of
158 * the old matrix with the values of the new.
160 float newMatrix[4];
161 nscoord newDelta[2];
162 float newX[2];
163 float newY[2];
165 /* [this] [aOther]
166 * |a1 c1 e1| |a0 c0 e0| |a0a1 + b0c1 c0a1 + d0c1 e0a1 + f0c1 + e1|
167 * |b1 d1 f1|x|b0 d0 f0| = |a0b1 + b0d1 c0b1 + d0d1 e0b1 + f0d1 + f1|
168 * |0 0 1 | | 0 0 1| | 0 0 1|
170 newMatrix[0] = aOther.mMain[0] * mMain[0] + aOther.mMain[1] * mMain[2];
171 newMatrix[1] = aOther.mMain[0] * mMain[1] + aOther.mMain[1] * mMain[3];
172 newMatrix[2] = aOther.mMain[2] * mMain[0] + aOther.mMain[3] * mMain[2];
173 newMatrix[3] = aOther.mMain[2] * mMain[1] + aOther.mMain[3] * mMain[3];
174 newDelta[0] = NSToCoordRound(aOther.mDelta[0] * mMain[0] +
175 aOther.mDelta[1] * mMain[2]) + mDelta[0];
176 newDelta[1] = NSToCoordRound(aOther.mDelta[0] * mMain[1] +
177 aOther.mDelta[1] * mMain[3]) + mDelta[1];
179 /* For consistent terminology, let u0, u1, v0, and v1 be the four transform
180 * coordinates from our matrix, and let x0, x1, y0, and y1 be the four
181 * transform coordinates from the other matrix. Then the new transform
182 * coordinates are:
184 * u0' = a1u0 + c1u1 + x0
185 * u1' = b1u0 + d1u1 + x1
186 * v0' = a1v0 + c1v1 + y0
187 * v1' = b1v0 + d1v1 + y1
189 newX[0] = mMain[0] * aOther.mX[0] + mMain[2] * aOther.mX[1] + mX[0];
190 newX[1] = mMain[1] * aOther.mX[0] + mMain[3] * aOther.mX[1] + mX[1];
191 newY[0] = mMain[0] * aOther.mY[0] + mMain[2] * aOther.mY[1] + mY[0];
192 newY[1] = mMain[1] * aOther.mY[0] + mMain[3] * aOther.mY[1] + mY[1];
194 /* Now, write everything back in. */
195 for (PRInt32 index = 0; index < 4; ++index)
196 mMain[index] = newMatrix[index];
197 for (PRInt32 index = 0; index < 2; ++index) {
198 mDelta[index] = newDelta[index];
199 mX[index] = newX[index];
200 mY[index] = newY[index];
203 /* As promised, return a reference to ourselves. */
204 return *this;
207 /* op* is implemented in terms of op*=. */
208 const nsStyleTransformMatrix
209 nsStyleTransformMatrix::operator *(const nsStyleTransformMatrix &aOther) const
211 return nsStyleTransformMatrix(*this) *= aOther;
214 /* Helper function to fill in an nscoord with the specified nsCSSValue. */
215 static nscoord CalcLength(const nsCSSValue &aValue,
216 nsStyleContext* aContext,
217 nsPresContext* aPresContext,
218 PRBool &aCanStoreInRuleTree)
220 if (aValue.GetUnit() == eCSSUnit_Pixel) {
221 // Handle this here (even though nsRuleNode::CalcLength handles it
222 // fine) so that callers are allowed to pass a null style context
223 // and pres context to SetToTransformFunction if they know (as
224 // nsStyleAnimation does) that all lengths within the transform
225 // function have already been computed to pixels and percents.
226 return nsPresContext::CSSPixelsToAppUnits(aValue.GetFloatValue());
228 return nsRuleNode::CalcLength(aValue, aContext, aPresContext,
229 aCanStoreInRuleTree);
232 static void ProcessTranslatePart(nscoord& aOffset, float& aPercent,
233 const nsCSSValue& aValue,
234 nsStyleContext* aContext,
235 nsPresContext* aPresContext,
236 PRBool& aCanStoreInRuleTree)
238 if (aValue.GetUnit() == eCSSUnit_Percent) {
239 aPercent = aValue.GetPercentValue();
240 } else if (aValue.IsCalcUnit()) {
241 nsRuleNode::ComputedCalc result =
242 nsRuleNode::SpecifiedCalcToComputedCalc(aValue, aContext, aPresContext,
243 aCanStoreInRuleTree);
244 aPercent = result.mPercent;
245 aOffset = result.mLength;
246 } else {
247 aOffset = CalcLength(aValue, aContext, aPresContext,
248 aCanStoreInRuleTree);
252 /* Helper function to process a matrix entry. */
253 static void ProcessMatrix(float aMain[4], nscoord aDelta[2],
254 float aX[2], float aY[2],
255 const nsCSSValue::Array* aData,
256 nsStyleContext* aContext,
257 nsPresContext* aPresContext,
258 PRBool& aCanStoreInRuleTree)
260 NS_PRECONDITION(aData->Count() == 7, "Invalid array!");
262 /* Take the first four elements out of the array as floats and store
263 * them in aMain.
265 for (PRUint16 index = 1; index <= 4; ++index)
266 aMain[index - 1] = aData->Item(index).GetFloatValue();
268 /* The last two elements have their length parts stored in aDelta
269 * and their percent parts stored in aX[0] and aY[1].
271 ProcessTranslatePart(aDelta[0], aX[0], aData->Item(5),
272 aContext, aPresContext, aCanStoreInRuleTree);
273 ProcessTranslatePart(aDelta[1], aY[1], aData->Item(6),
274 aContext, aPresContext, aCanStoreInRuleTree);
277 /* Helper function to process a translatex function. */
278 static void ProcessTranslateX(nscoord aDelta[2], float aX[2],
279 const nsCSSValue::Array* aData,
280 nsStyleContext* aContext,
281 nsPresContext* aPresContext,
282 PRBool& aCanStoreInRuleTree)
284 NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
286 /* There are two cases. If we have a number, we want our matrix to look
287 * like this:
289 * | 1 0 dx|
290 * | 0 1 0|
291 * | 0 0 1|
292 * So E = value
294 * Otherwise, we might have a percentage, so we want to set the dX component
295 * to the percent.
297 ProcessTranslatePart(aDelta[0], aX[0], aData->Item(1),
298 aContext, aPresContext, aCanStoreInRuleTree);
301 /* Helper function to process a translatey function. */
302 static void ProcessTranslateY(nscoord aDelta[2], float aY[2],
303 const nsCSSValue::Array* aData,
304 nsStyleContext* aContext,
305 nsPresContext* aPresContext,
306 PRBool& aCanStoreInRuleTree)
308 NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
310 /* There are two cases. If we have a number, we want our matrix to look
311 * like this:
313 * | 1 0 0|
314 * | 0 1 dy|
315 * | 0 0 1|
316 * So E = value
318 * Otherwise, we might have a percentage, so we want to set the dY component
319 * to the percent.
321 ProcessTranslatePart(aDelta[1], aY[1], aData->Item(1),
322 aContext, aPresContext, aCanStoreInRuleTree);
325 /* Helper function to process a translate function. */
326 static void ProcessTranslate(nscoord aDelta[2], float aX[2], float aY[2],
327 const nsCSSValue::Array* aData,
328 nsStyleContext* aContext,
329 nsPresContext* aPresContext,
330 PRBool& aCanStoreInRuleTree)
332 NS_PRECONDITION(aData->Count() == 2 || aData->Count() == 3, "Invalid array!");
334 /* There are several cases to consider.
335 * First, we might have one value, or we might have two. If we have
336 * two, we need to consider both dX and dY components.
337 * Next, the values might be lengths, or they might be percents. If they're
338 * percents, store them in the dX and dY components. Otherwise, store them in
339 * the main matrix.
342 ProcessTranslatePart(aDelta[0], aX[0], aData->Item(1),
343 aContext, aPresContext, aCanStoreInRuleTree);
345 /* If we read in a Y component, set it appropriately */
346 if (aData->Count() == 3) {
347 ProcessTranslatePart(aDelta[1], aY[1], aData->Item(2),
348 aContext, aPresContext, aCanStoreInRuleTree);
352 /* Helper function to set up a scale matrix. */
353 static void ProcessScaleHelper(float aXScale, float aYScale, float aMain[4])
355 /* We want our matrix to look like this:
356 * | dx 0 0|
357 * | 0 dy 0|
358 * | 0 0 1|
359 * So A = value
361 aMain[0] = aXScale;
362 aMain[3] = aYScale;
365 /* Process a scalex function. */
366 static void ProcessScaleX(float aMain[4], const nsCSSValue::Array* aData)
368 NS_PRECONDITION(aData->Count() == 2, "Bad array!");
369 ProcessScaleHelper(aData->Item(1).GetFloatValue(), 1.0f, aMain);
372 /* Process a scaley function. */
373 static void ProcessScaleY(float aMain[4], const nsCSSValue::Array* aData)
375 NS_PRECONDITION(aData->Count() == 2, "Bad array!");
376 ProcessScaleHelper(1.0f, aData->Item(1).GetFloatValue(), aMain);
379 /* Process a scale function. */
380 static void ProcessScale(float aMain[4], const nsCSSValue::Array* aData)
382 NS_PRECONDITION(aData->Count() == 2 || aData->Count() == 3, "Bad array!");
383 /* We either have one element or two. If we have one, it's for both X and Y.
384 * Otherwise it's one for each.
386 const nsCSSValue& scaleX = aData->Item(1);
387 const nsCSSValue& scaleY = (aData->Count() == 2 ? scaleX :
388 aData->Item(2));
390 ProcessScaleHelper(scaleX.GetFloatValue(),
391 scaleY.GetFloatValue(), aMain);
394 /* Helper function that, given a set of angles, constructs the appropriate
395 * skew matrix.
397 static void ProcessSkewHelper(double aXAngle, double aYAngle, float aMain[4])
399 /* We want our matrix to look like this:
400 * | 1 tan(ThetaX) 0|
401 * | tan(ThetaY) 1 0|
402 * | 0 0 1|
403 * However, to avoid infinite values, we'll use the SafeTangent function
404 * instead of the C standard tan function.
406 aMain[2] = SafeTangent(aXAngle);
407 aMain[1] = SafeTangent(aYAngle);
410 /* Function that converts a skewx transform into a matrix. */
411 static void ProcessSkewX(float aMain[4], const nsCSSValue::Array* aData)
413 NS_ASSERTION(aData->Count() == 2, "Bad array!");
414 ProcessSkewHelper(aData->Item(1).GetAngleValueInRadians(), 0.0, aMain);
417 /* Function that converts a skewy transform into a matrix. */
418 static void ProcessSkewY(float aMain[4], const nsCSSValue::Array* aData)
420 NS_ASSERTION(aData->Count() == 2, "Bad array!");
421 ProcessSkewHelper(0.0, aData->Item(1).GetAngleValueInRadians(), aMain);
424 /* Function that converts a skew transform into a matrix. */
425 static void ProcessSkew(float aMain[4], const nsCSSValue::Array* aData)
427 NS_ASSERTION(aData->Count() == 2 || aData->Count() == 3, "Bad array!");
429 double xSkew = aData->Item(1).GetAngleValueInRadians();
430 double ySkew = (aData->Count() == 2
431 ? 0.0 : aData->Item(2).GetAngleValueInRadians());
433 ProcessSkewHelper(xSkew, ySkew, aMain);
436 /* Function that converts a rotate transform into a matrix. */
437 static void ProcessRotate(float aMain[4], const nsCSSValue::Array* aData)
439 NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
441 /* We want our matrix to look like this:
442 * | cos(theta) -sin(theta) 0|
443 * | sin(theta) cos(theta) 0|
444 * | 0 0 1|
445 * (see http://www.w3.org/TR/SVG/coords.html#RotationDefined)
447 double theta = aData->Item(1).GetAngleValueInRadians();
448 float cosTheta = FlushToZero(cos(theta));
449 float sinTheta = FlushToZero(sin(theta));
451 aMain[0] = cosTheta;
452 aMain[1] = sinTheta;
453 aMain[2] = -sinTheta;
454 aMain[3] = cosTheta;
458 * Return the transform function, as an nsCSSKeyword, for the given
459 * nsCSSValue::Array from a transform list.
461 /* static */ nsCSSKeyword
462 nsStyleTransformMatrix::TransformFunctionOf(const nsCSSValue::Array* aData)
464 nsAutoString keyword;
465 aData->Item(0).GetStringValue(keyword);
466 return nsCSSKeywords::LookupKeyword(keyword);
470 * SetToTransformFunction is essentially a giant switch statement that fans
471 * out to many smaller helper functions.
473 void
474 nsStyleTransformMatrix::SetToTransformFunction(const nsCSSValue::Array * aData,
475 nsStyleContext* aContext,
476 nsPresContext* aPresContext,
477 PRBool& aCanStoreInRuleTree)
479 NS_PRECONDITION(aData, "Why did you want to get data from a null array?");
480 // It's OK if aContext and aPresContext are null if the caller already
481 // knows that all length units have been converted to pixels (as
482 // nsStyleAnimation does).
484 /* Reset the matrix to the identity so that each subfunction can just
485 * worry about its own components.
487 SetToIdentity();
489 /* Get the keyword for the transform. */
490 switch (TransformFunctionOf(aData)) {
491 case eCSSKeyword_translatex:
492 ProcessTranslateX(mDelta, mX, aData, aContext, aPresContext,
493 aCanStoreInRuleTree);
494 break;
495 case eCSSKeyword_translatey:
496 ProcessTranslateY(mDelta, mY, aData, aContext, aPresContext,
497 aCanStoreInRuleTree);
498 break;
499 case eCSSKeyword_translate:
500 ProcessTranslate(mDelta, mX, mY, aData, aContext, aPresContext,
501 aCanStoreInRuleTree);
502 break;
503 case eCSSKeyword_scalex:
504 ProcessScaleX(mMain, aData);
505 break;
506 case eCSSKeyword_scaley:
507 ProcessScaleY(mMain, aData);
508 break;
509 case eCSSKeyword_scale:
510 ProcessScale(mMain, aData);
511 break;
512 case eCSSKeyword_skewx:
513 ProcessSkewX(mMain, aData);
514 break;
515 case eCSSKeyword_skewy:
516 ProcessSkewY(mMain, aData);
517 break;
518 case eCSSKeyword_skew:
519 ProcessSkew(mMain, aData);
520 break;
521 case eCSSKeyword_rotate:
522 ProcessRotate(mMain, aData);
523 break;
524 case eCSSKeyword_matrix:
525 ProcessMatrix(mMain, mDelta, mX, mY, aData, aContext, aPresContext,
526 aCanStoreInRuleTree);
527 break;
528 default:
529 NS_NOTREACHED("Unknown transform function!");
533 /* Given a -moz-transform token stream, accumulates them into an
534 * nsStyleTransformMatrix
536 * @param aList The nsCSSValueList of arrays to read into transform functions.
537 * @param aContext The style context to use for unit conversion.
538 * @param aPresContext The presentation context to use for unit conversion
539 * @param aCanStoreInRuleTree This is set to PR_FALSE if the value cannot be stored in the rule tree.
540 * @return An nsStyleTransformMatrix corresponding to the net transform.
542 /* static */ nsStyleTransformMatrix
543 nsStyleTransformMatrix::ReadTransforms(const nsCSSValueList* aList,
544 nsStyleContext* aContext,
545 nsPresContext* aPresContext,
546 PRBool &aCanStoreInRuleTree)
548 nsStyleTransformMatrix result;
550 for (const nsCSSValueList* curr = aList; curr != nsnull; curr = curr->mNext) {
551 const nsCSSValue &currElem = curr->mValue;
552 NS_ASSERTION(currElem.GetUnit() == eCSSUnit_Function,
553 "Stream should consist solely of functions!");
554 NS_ASSERTION(currElem.GetArrayValue()->Count() >= 1,
555 "Incoming function is too short!");
557 /* Read in a single transform matrix, then accumulate it with the total. */
558 nsStyleTransformMatrix currMatrix;
559 currMatrix.SetToTransformFunction(currElem.GetArrayValue(), aContext,
560 aPresContext, aCanStoreInRuleTree);
561 result *= currMatrix;
563 return result;
566 /* Does an element-by-element comparison and returns whether or not the
567 * matrices are equal.
569 PRBool
570 nsStyleTransformMatrix::operator ==(const nsStyleTransformMatrix &aOther) const
572 for (PRInt32 index = 0; index < 4; ++index)
573 if (mMain[index] != aOther.mMain[index])
574 return PR_FALSE;
576 for (PRInt32 index = 0; index < 2; ++index)
577 if (mDelta[index] != aOther.mDelta[index] ||
578 mX[index] != aOther.mX[index] ||
579 mY[index] != aOther.mY[index])
580 return PR_FALSE;
582 return PR_TRUE;