Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / layout / style / nsStyleTransformMatrix.cpp
blobb9b11fc528b14b91207827b8a05b8b2ea0fcd535
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 <math.h>
50 /* Arguably, this loses precision, but it doesn't hurt! */
51 const float kPi = 3.1415926535897932384626433832795f;
52 const float kTwoPi = 6.283185307179586476925286766559f;
53 const float kEpsilon = 0.0001f;
55 /* Computes tan(theta). For values of theta such that
56 * tan(theta) is undefined or arbitrarily large, SafeTangent
57 * returns a managably large or small value of the correct sign.
59 static float SafeTangent(float aTheta)
61 /* We'll do this by computing sin and cos theta. If cos(theta) is
62 * is too close to zero, we'll set it to some arbitrary epsilon value
63 * that avoid float overflow or undefined result.
65 float sinTheta = sin(aTheta);
66 float cosTheta = cos(aTheta);
68 /* Bound cos(theta) to be in the range [-1, -epsilon) U (epsilon, 1] */
69 if (cosTheta >= 0 && cosTheta < kEpsilon)
70 cosTheta = kEpsilon;
71 else if (cosTheta < 0 && cosTheta >= -kEpsilon)
72 cosTheta = -kEpsilon;
74 return sinTheta / cosTheta;
77 /* Helper function to constrain an angle to a value in the range [-pi, pi),
78 * which reduces accumulated floating point errors from trigonometric functions
79 * by keeping the error terms small.
81 static inline float ConstrainFloatValue(float aValue)
83 /* Get in range [0, 2pi) */
84 aValue = fmod(aValue, kTwoPi);
85 return aValue >= kPi ? aValue - kTwoPi : aValue;
88 /* Converts an nsCSSValue containing an angle into an equivalent measure
89 * of radians. The value is guaranteed to be in the range (-pi, pi) to
90 * minimize error.
92 static float CSSToRadians(const nsCSSValue &aValue)
94 NS_PRECONDITION(aValue.IsAngularUnit(),
95 "Expected an angle, but didn't find one!");
97 switch (aValue.GetUnit()) {
98 case eCSSUnit_Degree:
99 /* 360deg = 2pi rad, so deg = pi / 180 rad */
100 return
101 ConstrainFloatValue(aValue.GetFloatValue() * kPi / 180.0f);
102 case eCSSUnit_Grad:
103 /* 400grad = 2pi rad, so grad = pi / 200 rad */
104 return
105 ConstrainFloatValue(aValue.GetFloatValue() * kPi / 200.0f);
106 case eCSSUnit_Radian:
107 /* Yay identity transforms! */
108 return ConstrainFloatValue(aValue.GetFloatValue());
109 default:
110 NS_NOTREACHED("Unexpected angular unit!");
111 return 0.0f;
115 /* Constructor sets the data to the identity matrix. */
116 nsStyleTransformMatrix::nsStyleTransformMatrix()
118 SetToIdentity();
121 /* SetToIdentity just fills in the appropriate values. */
122 void nsStyleTransformMatrix::SetToIdentity()
124 /* Set the main matrix to the identity. */
125 mMain[0] = 1.0f;
126 mMain[1] = 0.0f;
127 mMain[2] = 0.0f;
128 mMain[3] = 1.0f;
129 mDelta[0] = 0;
130 mDelta[1] = 0;
132 /* Both translation matrices are zero. */
133 mX[0] = 0.0f;
134 mX[1] = 0.0f;
135 mY[0] = 0.0f;
136 mY[1] = 0.0f;
139 /* Adds the constant translation to the scale factor translation components. */
140 nscoord nsStyleTransformMatrix::GetXTranslation(const nsRect& aBounds) const
142 return NSToCoordRound(aBounds.width * mX[0] + aBounds.height * mY[0]) +
143 mDelta[0];
145 nscoord nsStyleTransformMatrix::GetYTranslation(const nsRect& aBounds) const
147 return NSToCoordRound(aBounds.width * mX[1] + aBounds.height * mY[1]) +
148 mDelta[1];
151 /* GetThebesMatrix converts the stored matrix in a few steps. */
152 gfxMatrix nsStyleTransformMatrix::GetThebesMatrix(const nsRect& aBounds,
153 float aScale) const
155 /* Compute the graphics matrix. We take the stored main elements, along with
156 * the delta, and add in the matrices:
158 * | 0 0 dx1|
159 * | 0 0 dx2| * width
160 * | 0 0 0|
162 * | 0 0 dy1|
163 * | 0 0 dy2| * height
164 * | 0 0 0|
166 return gfxMatrix(mMain[0], mMain[1], mMain[2], mMain[3],
167 NSAppUnitsToFloatPixels(GetXTranslation(aBounds), aScale),
168 NSAppUnitsToFloatPixels(GetYTranslation(aBounds), aScale));
171 /* Performs the matrix multiplication necessary to multiply the two matrices,
172 * then hands back a reference to ourself.
174 nsStyleTransformMatrix&
175 nsStyleTransformMatrix::operator *= (const nsStyleTransformMatrix &aOther)
177 /* We'll buffer all of our results into a temporary storage location
178 * during this operation since we don't want to overwrite the values of
179 * the old matrix with the values of the new.
181 float newMatrix[4];
182 nscoord newDelta[2];
183 float newX[2];
184 float newY[2];
186 /* [this] [aOther]
187 * |a1 c1 e1| |a0 c0 e0| |a0a1 + b0c1 c0a1 + d0c1 e0a1 + f0c1 + e1|
188 * |b1 d1 f1|x|b0 d0 f0| = |a0b1 + b0d1 c0b1 + d0d1 e0b1 + f0d1 + f1|
189 * |0 0 1 | | 0 0 1| | 0 0 1|
191 newMatrix[0] = aOther.mMain[0] * mMain[0] + aOther.mMain[1] * mMain[2];
192 newMatrix[1] = aOther.mMain[0] * mMain[1] + aOther.mMain[1] * mMain[3];
193 newMatrix[2] = aOther.mMain[2] * mMain[0] + aOther.mMain[3] * mMain[2];
194 newMatrix[3] = aOther.mMain[2] * mMain[1] + aOther.mMain[3] * mMain[3];
195 newDelta[0] = NSToCoordRound(aOther.mDelta[0] * mMain[0] +
196 aOther.mDelta[1] * mMain[2]) + mDelta[0];
197 newDelta[1] = NSToCoordRound(aOther.mDelta[0] * mMain[1] +
198 aOther.mDelta[1] * mMain[3]) + mDelta[1];
200 /* For consistent terminology, let u0, u1, v0, and v1 be the four transform
201 * coordinates from our matrix, and let x0, x1, y0, and y1 be the four
202 * transform coordinates from the other matrix. Then the new transform
203 * coordinates are:
205 * u0' = a1u0 + c1u1 + x0
206 * u1' = b1u0 + d1u1 + x1
207 * v0' = a1v0 + c1v1 + y0
208 * v1' = b1v0 + d1v1 + y1
210 newX[0] = mMain[0] * aOther.mX[0] + mMain[2] * aOther.mX[1] + mX[0];
211 newX[1] = mMain[1] * aOther.mX[0] + mMain[3] * aOther.mX[1] + mX[1];
212 newY[0] = mMain[0] * aOther.mY[0] + mMain[2] * aOther.mY[1] + mY[0];
213 newY[1] = mMain[1] * aOther.mY[0] + mMain[3] * aOther.mY[1] + mY[1];
215 /* Now, write everything back in. */
216 for (PRInt32 index = 0; index < 4; ++index)
217 mMain[index] = newMatrix[index];
218 for (PRInt32 index = 0; index < 2; ++index) {
219 mDelta[index] = newDelta[index];
220 mX[index] = newX[index];
221 mY[index] = newY[index];
224 /* As promised, return a reference to ourselves. */
225 return *this;
228 /* op* is implemented in terms of op*=. */
229 const nsStyleTransformMatrix
230 nsStyleTransformMatrix::operator *(const nsStyleTransformMatrix &aOther) const
232 return nsStyleTransformMatrix(*this) *= aOther;
235 /* Helper function to fill in an nscoord with the specified nsCSSValue. */
236 static void SetCoordToValue(const nsCSSValue &aValue,
237 nsStyleContext* aContext,
238 nsPresContext* aPresContext,
239 PRBool &aInherited, nscoord &aOut)
241 aOut = nsRuleNode::CalcLength(aValue, aContext, aPresContext, aInherited);
244 /* Helper function to process a matrix entry. */
245 static void ProcessMatrix(float aMain[4], nscoord aDelta[2],
246 float aX[2], float aY[2],
247 const nsCSSValue::Array* aData,
248 nsStyleContext* aContext,
249 nsPresContext* aPresContext,
250 PRBool& aInherited)
252 NS_PRECONDITION(aData->Count() == 7, "Invalid array!");
254 /* Take the first four elements out of the array as floats and store
255 * them in aMain.
257 for (PRUint16 index = 1; index <= 4; ++index)
258 aMain[index - 1] = aData->Item(index).GetFloatValue();
260 /* For the fifth element, if it's a percentage, store it in aX[0].
261 * Otherwise, it's a length that needs to go in aDelta[0]
263 if (aData->Item(5).GetUnit() == eCSSUnit_Percent)
264 aX[0] = aData->Item(5).GetPercentValue();
265 else
266 SetCoordToValue(aData->Item(5), aContext, aPresContext, aInherited,
267 aDelta[0]);
269 /* For the final element, if it's a percentage, store it in aY[1].
270 * Otherwise, it's a length that needs to go in aDelta[1].
272 if (aData->Item(6).GetUnit() == eCSSUnit_Percent)
273 aY[1] = aData->Item(6).GetPercentValue();
274 else
275 SetCoordToValue(aData->Item(6), aContext, aPresContext, aInherited,
276 aDelta[1]);
279 /* Helper function to process a translatex function. */
280 static void ProcessTranslateX(nscoord aDelta[2], float aX[2],
281 const nsCSSValue::Array* aData,
282 nsStyleContext* aContext,
283 nsPresContext* aPresContext,
284 PRBool& aInherited)
286 NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
288 /* There are two cases. If we have a number, we want our matrix to look
289 * like this:
291 * | 1 0 dx|
292 * | 0 1 0|
293 * | 0 0 1|
294 * So E = value
296 * Otherwise, we might have a percentage, so we want to set the dX component
297 * to the percent.
299 if (aData->Item(1).GetUnit() != eCSSUnit_Percent)
300 SetCoordToValue(aData->Item(1), aContext, aPresContext, aInherited,
301 aDelta[0]);
302 else
303 aX[0] = aData->Item(1).GetPercentValue();
306 /* Helper function to process a translatey function. */
307 static void ProcessTranslateY(nscoord aDelta[2], float aY[2],
308 const nsCSSValue::Array* aData,
309 nsStyleContext* aContext,
310 nsPresContext* aPresContext,
311 PRBool& aInherited)
313 NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
315 /* There are two cases. If we have a number, we want our matrix to look
316 * like this:
318 * | 1 0 0|
319 * | 0 1 dy|
320 * | 0 0 1|
321 * So E = value
323 * Otherwise, we might have a percentage, so we want to set the dY component
324 * to the percent.
326 if (aData->Item(1).GetUnit() != eCSSUnit_Percent)
327 SetCoordToValue(aData->Item(1), aContext, aPresContext, aInherited,
328 aDelta[1]);
329 else
330 aY[1] = aData->Item(1).GetPercentValue();
333 /* Helper function to process a translate function. */
334 static void ProcessTranslate(nscoord aDelta[2], float aX[2], float aY[2],
335 const nsCSSValue::Array* aData,
336 nsStyleContext* aContext,
337 nsPresContext* aPresContext,
338 PRBool& aInherited)
340 NS_PRECONDITION(aData->Count() == 2 || aData->Count() == 3, "Invalid array!");
342 /* There are several cases to consider.
343 * First, we might have one value, or we might have two. If we have
344 * two, we need to consider both dX and dY components.
345 * Next, the values might be lengths, or they might be percents. If they're
346 * percents, store them in the dX and dY components. Otherwise, store them in
347 * the main matrix.
350 const nsCSSValue &dx = aData->Item(1);
351 if (dx.GetUnit() == eCSSUnit_Percent)
352 aX[0] = dx.GetPercentValue();
353 else
354 SetCoordToValue(dx, aContext, aPresContext, aInherited, aDelta[0]);
356 /* If we read in a Y component, set it appropriately */
357 if (aData->Count() == 3) {
358 const nsCSSValue &dy = aData->Item(2);
359 if (dy.GetUnit() == eCSSUnit_Percent)
360 aY[1] = dy.GetPercentValue();
361 else
362 SetCoordToValue(dy, aContext, aPresContext, aInherited, aDelta[1]);
366 /* Helper function to set up a scale matrix. */
367 static void ProcessScaleHelper(float aXScale, float aYScale, float aMain[4])
369 /* We want our matrix to look like this:
370 * | dx 0 0|
371 * | 0 dy 0|
372 * | 0 0 1|
373 * So A = value
375 aMain[0] = aXScale;
376 aMain[3] = aYScale;
379 /* Process a scalex function. */
380 static void ProcessScaleX(float aMain[4], const nsCSSValue::Array* aData)
382 NS_PRECONDITION(aData->Count() == 2, "Bad array!");
383 ProcessScaleHelper(aData->Item(1).GetFloatValue(), 1.0f, aMain);
386 /* Process a scaley function. */
387 static void ProcessScaleY(float aMain[4], const nsCSSValue::Array* aData)
389 NS_PRECONDITION(aData->Count() == 2, "Bad array!");
390 ProcessScaleHelper(1.0f, aData->Item(1).GetFloatValue(), aMain);
393 /* Process a scale function. */
394 static void ProcessScale(float aMain[4], const nsCSSValue::Array* aData)
396 NS_PRECONDITION(aData->Count() == 2 || aData->Count() == 3, "Bad array!");
397 /* We either have one element or two. If we have one, it's for both X and Y.
398 * Otherwise it's one for each.
400 const nsCSSValue& scaleX = aData->Item(1);
401 const nsCSSValue& scaleY = (aData->Count() == 2 ? scaleX :
402 aData->Item(2));
404 ProcessScaleHelper(scaleX.GetFloatValue(),
405 scaleY.GetFloatValue(), aMain);
408 /* Helper function that, given a set of angles, constructs the appropriate
409 * skew matrix.
411 static void ProcessSkewHelper(float aXAngle, float aYAngle, float aMain[4])
413 /* We want our matrix to look like this:
414 * | 1 tan(ThetaX) 0|
415 * | tan(ThetaY) 1 0|
416 * | 0 0 1|
417 * However, to avoid infinte values, we'll use the SafeTangent function
418 * instead of the C standard tan function.
420 aMain[2] = SafeTangent(aXAngle);
421 aMain[1] = SafeTangent(aYAngle);
424 /* Function that converts a skewx transform into a matrix. */
425 static void ProcessSkewX(float aMain[4], const nsCSSValue::Array* aData)
427 NS_ASSERTION(aData->Count() == 2, "Bad array!");
428 ProcessSkewHelper(CSSToRadians(aData->Item(1)), 0.0f, aMain);
431 /* Function that converts a skewy transform into a matrix. */
432 static void ProcessSkewY(float aMain[4], const nsCSSValue::Array* aData)
434 NS_ASSERTION(aData->Count() == 2, "Bad array!");
435 ProcessSkewHelper(0.0f, CSSToRadians(aData->Item(1)), aMain);
438 /* Function that converts a skew transform into a matrix. */
439 static void ProcessSkew(float aMain[4], const nsCSSValue::Array* aData)
441 NS_ASSERTION(aData->Count() == 2 || aData->Count() == 3, "Bad array!");
443 float xSkew = CSSToRadians(aData->Item(1));
444 float ySkew = (aData->Count() == 2 ? 0.0f : CSSToRadians(aData->Item(2)));
446 ProcessSkewHelper(xSkew, ySkew, aMain);
449 /* Function that converts a rotate transform into a matrix. */
450 static void ProcessRotate(float aMain[4], const nsCSSValue::Array* aData)
452 NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
454 /* We want our matrix to look like this:
455 * | cos(theta) -sin(theta) 0|
456 * | sin(theta) cos(theta) 0|
457 * | 0 0 1|
458 * (see http://www.w3.org/TR/SVG/coords.html#RotationDefined)
460 float theta = CSSToRadians(aData->Item(1));
461 float cosTheta = cos(theta);
462 float sinTheta = sin(theta);
464 aMain[0] = cosTheta;
465 aMain[1] = sinTheta;
466 aMain[2] = -sinTheta;
467 aMain[3] = cosTheta;
471 * SetToTransformFunction is essentially a giant switch statement that fans
472 * out to many smaller helper functions.
474 void
475 nsStyleTransformMatrix::SetToTransformFunction(const nsCSSValue::Array * aData,
476 nsStyleContext* aContext,
477 nsPresContext* aPresContext,
478 PRBool& aInherited)
480 NS_PRECONDITION(aData, "Why did you want to get data from a null array?");
481 NS_PRECONDITION(aContext, "Need a context for unit conversion!");
482 NS_PRECONDITION(aPresContext, "Need a context for unit conversion!");
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 nsAutoString keyword;
491 aData->Item(0).GetStringValue(keyword);
492 switch (nsCSSKeywords::LookupKeyword(keyword)) {
493 case eCSSKeyword_translatex:
494 ProcessTranslateX(mDelta, mX, aData, aContext, aPresContext, aInherited);
495 break;
496 case eCSSKeyword_translatey:
497 ProcessTranslateY(mDelta, mY, aData, aContext, aPresContext, aInherited);
498 break;
499 case eCSSKeyword_translate:
500 ProcessTranslate(mDelta, mX, mY, aData, aContext, aPresContext,
501 aInherited);
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 aInherited);
527 break;
528 default:
529 NS_NOTREACHED("Unknown transform function!");
533 /* Does an element-by-element comparison and returns whether or not the
534 * matrices are equal.
536 PRBool
537 nsStyleTransformMatrix::operator ==(const nsStyleTransformMatrix &aOther) const
539 for (PRInt32 index = 0; index < 4; ++index)
540 if (mMain[index] != aOther.mMain[index])
541 return PR_FALSE;
543 for (PRInt32 index = 0; index < 2; ++index)
544 if (mDelta[index] != aOther.mDelta[index] ||
545 mX[index] != aOther.mX[index] ||
546 mY[index] != aOther.mY[index])
547 return PR_FALSE;
549 return PR_TRUE;