2008-09-13 09:42:11 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-09-13 09:42:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A class used for intermediate representations of the -moz-transform property.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsStyleTransformMatrix.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCSSValue.h"
|
|
|
|
#include "nsStyleContext.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsRuleNode.h"
|
|
|
|
#include "nsCSSKeywords.h"
|
2009-11-02 19:36:43 +00:00
|
|
|
#include "nsMathUtils.h"
|
2010-07-03 04:18:56 +00:00
|
|
|
#include "CSSCalc.h"
|
2011-07-22 22:28:07 +00:00
|
|
|
#include "nsStyleAnimation.h"
|
2010-07-03 04:18:56 +00:00
|
|
|
|
|
|
|
namespace css = mozilla::css;
|
2008-09-13 09:42:11 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
namespace nsStyleTransformMatrix {
|
|
|
|
|
2009-11-02 19:36:43 +00:00
|
|
|
/* Note on floating point precision: The transform matrix is an array
|
|
|
|
* of single precision 'float's, and so are most of the input values
|
|
|
|
* we get from the style system, but intermediate calculations
|
|
|
|
* involving angles need to be done in 'double'.
|
|
|
|
*/
|
2008-09-13 09:42:11 +00:00
|
|
|
|
2009-11-03 05:00:46 +00:00
|
|
|
/* Force small values to zero. We do this to avoid having sin(360deg)
|
|
|
|
* evaluate to a tiny but nonzero value.
|
|
|
|
*/
|
|
|
|
static double FlushToZero(double aVal)
|
|
|
|
{
|
|
|
|
if (-FLT_EPSILON < aVal && aVal < FLT_EPSILON)
|
|
|
|
return 0.0f;
|
|
|
|
else
|
|
|
|
return aVal;
|
|
|
|
}
|
|
|
|
|
2008-09-13 09:42:11 +00:00
|
|
|
/* Helper function to fill in an nscoord with the specified nsCSSValue. */
|
2010-07-03 04:18:56 +00:00
|
|
|
static nscoord CalcLength(const nsCSSValue &aValue,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool &aCanStoreInRuleTree)
|
2008-09-13 09:42:11 +00:00
|
|
|
{
|
2011-09-26 21:54:30 +00:00
|
|
|
if (aValue.GetUnit() == eCSSUnit_Pixel ||
|
|
|
|
aValue.GetUnit() == eCSSUnit_Number) {
|
2010-07-03 04:18:56 +00:00
|
|
|
// Handle this here (even though nsRuleNode::CalcLength handles it
|
|
|
|
// fine) so that callers are allowed to pass a null style context
|
|
|
|
// and pres context to SetToTransformFunction if they know (as
|
|
|
|
// nsStyleAnimation does) that all lengths within the transform
|
|
|
|
// function have already been computed to pixels and percents.
|
2011-09-26 21:54:30 +00:00
|
|
|
//
|
|
|
|
// Raw numbers are treated as being pixels.
|
2010-07-03 04:18:56 +00:00
|
|
|
return nsPresContext::CSSPixelsToAppUnits(aValue.GetFloatValue());
|
|
|
|
}
|
2010-07-03 04:18:56 +00:00
|
|
|
return nsRuleNode::CalcLength(aValue, aContext, aPresContext,
|
2009-02-04 21:24:18 +00:00
|
|
|
aCanStoreInRuleTree);
|
2008-09-13 09:42:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
static float
|
|
|
|
ProcessTranslatePart(const nsCSSValue& aValue,
|
2011-07-22 22:28:07 +00:00
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool& aCanStoreInRuleTree,
|
2011-07-22 22:28:07 +00:00
|
|
|
nscoord aSize, float aAppUnitsPerMatrixUnit)
|
2010-07-03 04:18:56 +00:00
|
|
|
{
|
2011-07-22 22:28:07 +00:00
|
|
|
nscoord offset = 0;
|
|
|
|
float percent = 0.0f;
|
|
|
|
|
2010-07-03 04:18:56 +00:00
|
|
|
if (aValue.GetUnit() == eCSSUnit_Percent) {
|
2011-07-22 22:28:07 +00:00
|
|
|
percent = aValue.GetPercentValue();
|
2010-07-03 04:18:56 +00:00
|
|
|
} else if (aValue.IsCalcUnit()) {
|
2010-09-11 16:27:13 +00:00
|
|
|
nsRuleNode::ComputedCalc result =
|
|
|
|
nsRuleNode::SpecifiedCalcToComputedCalc(aValue, aContext, aPresContext,
|
|
|
|
aCanStoreInRuleTree);
|
2011-07-22 22:28:07 +00:00
|
|
|
percent = result.mPercent;
|
|
|
|
offset = result.mLength;
|
2010-07-03 04:18:56 +00:00
|
|
|
} else {
|
2011-07-22 22:28:07 +00:00
|
|
|
offset = CalcLength(aValue, aContext, aPresContext,
|
2010-07-03 04:18:56 +00:00
|
|
|
aCanStoreInRuleTree);
|
2010-07-03 04:18:56 +00:00
|
|
|
}
|
2011-07-22 22:28:07 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
return (percent * NSAppUnitsToFloatPixels(aSize, aAppUnitsPerMatrixUnit)) +
|
|
|
|
NSAppUnitsToFloatPixels(offset, aAppUnitsPerMatrixUnit);
|
2010-07-03 04:18:56 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
/**
|
|
|
|
* Helper functions to process all the transformation function types.
|
|
|
|
*
|
|
|
|
* These take a matrix parameter to accumulate the current matrix.
|
|
|
|
*/
|
|
|
|
|
2010-08-06 04:59:18 +00:00
|
|
|
/* Helper function to process a matrix entry. */
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessMatrix(gfx3DMatrix& aMatrix,
|
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool& aCanStoreInRuleTree,
|
2011-09-26 21:54:45 +00:00
|
|
|
nsRect& aBounds, float aAppUnitsPerMatrixUnit)
|
2010-08-06 04:59:18 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 7, "Invalid array!");
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
gfxMatrix result;
|
2011-07-22 22:28:07 +00:00
|
|
|
|
2010-08-06 04:59:18 +00:00
|
|
|
/* Take the first four elements out of the array as floats and store
|
2011-07-22 22:28:07 +00:00
|
|
|
* them.
|
2010-08-06 04:59:18 +00:00
|
|
|
*/
|
2011-09-26 21:54:45 +00:00
|
|
|
result.xx = aData->Item(1).GetFloatValue();
|
|
|
|
result.yx = aData->Item(2).GetFloatValue();
|
|
|
|
result.xy = aData->Item(3).GetFloatValue();
|
|
|
|
result.yy = aData->Item(4).GetFloatValue();
|
2010-08-06 04:59:18 +00:00
|
|
|
|
|
|
|
/* The last two elements have their length parts stored in aDelta
|
|
|
|
* and their percent parts stored in aX[0] and aY[1].
|
|
|
|
*/
|
2011-09-26 21:54:45 +00:00
|
|
|
result.x0 = ProcessTranslatePart(aData->Item(5),
|
|
|
|
aContext, aPresContext, aCanStoreInRuleTree,
|
|
|
|
aBounds.Width(), aAppUnitsPerMatrixUnit);
|
|
|
|
result.y0 = ProcessTranslatePart(aData->Item(6),
|
|
|
|
aContext, aPresContext, aCanStoreInRuleTree,
|
|
|
|
aBounds.Height(), aAppUnitsPerMatrixUnit);
|
|
|
|
|
|
|
|
aMatrix.PreMultiply(result);
|
2011-07-22 22:28:07 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessMatrix3D(gfx3DMatrix& aMatrix,
|
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool& aCanStoreInRuleTree,
|
2011-09-26 21:54:45 +00:00
|
|
|
nsRect& aBounds, float aAppUnitsPerMatrixUnit)
|
2011-08-03 03:04:19 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 17, "Invalid array!");
|
|
|
|
|
|
|
|
gfx3DMatrix temp;
|
|
|
|
|
|
|
|
temp._11 = aData->Item(1).GetFloatValue();
|
|
|
|
temp._12 = aData->Item(2).GetFloatValue();
|
|
|
|
temp._13 = aData->Item(3).GetFloatValue();
|
|
|
|
temp._14 = aData->Item(4).GetFloatValue();
|
|
|
|
temp._21 = aData->Item(5).GetFloatValue();
|
|
|
|
temp._22 = aData->Item(6).GetFloatValue();
|
|
|
|
temp._23 = aData->Item(7).GetFloatValue();
|
|
|
|
temp._24 = aData->Item(8).GetFloatValue();
|
|
|
|
temp._31 = aData->Item(9).GetFloatValue();
|
|
|
|
temp._32 = aData->Item(10).GetFloatValue();
|
|
|
|
temp._33 = aData->Item(11).GetFloatValue();
|
|
|
|
temp._34 = aData->Item(12).GetFloatValue();
|
|
|
|
temp._44 = aData->Item(16).GetFloatValue();
|
2011-09-26 21:54:30 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
temp._41 = ProcessTranslatePart(aData->Item(13),
|
|
|
|
aContext, aPresContext, aCanStoreInRuleTree,
|
|
|
|
aBounds.Width(), aAppUnitsPerMatrixUnit);
|
|
|
|
temp._42 = ProcessTranslatePart(aData->Item(14),
|
|
|
|
aContext, aPresContext, aCanStoreInRuleTree,
|
|
|
|
aBounds.Height(), aAppUnitsPerMatrixUnit);
|
|
|
|
temp._43 = ProcessTranslatePart(aData->Item(15),
|
|
|
|
aContext, aPresContext, aCanStoreInRuleTree,
|
|
|
|
aBounds.Height(), aAppUnitsPerMatrixUnit);
|
|
|
|
|
|
|
|
aMatrix.PreMultiply(temp);
|
2011-08-03 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 22:28:07 +00:00
|
|
|
/* Helper function to process two matrices that we need to interpolate between */
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessInterpolateMatrix(gfx3DMatrix& aMatrix,
|
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool& aCanStoreInRuleTree,
|
2011-09-26 21:54:45 +00:00
|
|
|
nsRect& aBounds, float aAppUnitsPerMatrixUnit)
|
2011-07-22 22:28:07 +00:00
|
|
|
{
|
2011-09-26 21:53:33 +00:00
|
|
|
NS_PRECONDITION(aData->Count() == 4, "Invalid array!");
|
|
|
|
|
|
|
|
gfx3DMatrix matrix1, matrix2;
|
|
|
|
if (aData->Item(1).GetUnit() == eCSSUnit_List) {
|
2011-09-26 21:54:45 +00:00
|
|
|
matrix1 = nsStyleTransformMatrix::ReadTransforms(aData->Item(1).GetListValue(),
|
2011-09-26 21:53:33 +00:00
|
|
|
aContext, aPresContext,
|
|
|
|
aCanStoreInRuleTree,
|
|
|
|
aBounds, aAppUnitsPerMatrixUnit);
|
|
|
|
}
|
|
|
|
if (aData->Item(2).GetUnit() == eCSSUnit_List) {
|
|
|
|
matrix2 = ReadTransforms(aData->Item(2).GetListValue(),
|
|
|
|
aContext, aPresContext,
|
|
|
|
aCanStoreInRuleTree,
|
|
|
|
aBounds, aAppUnitsPerMatrixUnit);
|
|
|
|
}
|
|
|
|
double progress = aData->Item(3).GetPercentValue();
|
2011-07-22 22:28:07 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
aMatrix = nsStyleAnimation::InterpolateTransformMatrix(matrix1, matrix2, progress) * aMatrix;
|
2010-08-06 04:59:18 +00:00
|
|
|
}
|
|
|
|
|
2008-09-13 09:42:11 +00:00
|
|
|
/* Helper function to process a translatex function. */
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessTranslateX(gfx3DMatrix& aMatrix,
|
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool& aCanStoreInRuleTree,
|
2011-09-26 21:54:45 +00:00
|
|
|
nsRect& aBounds, float aAppUnitsPerMatrixUnit)
|
2008-09-13 09:42:11 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
gfxPoint3D temp;
|
2011-07-22 22:28:07 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
temp.x = ProcessTranslatePart(aData->Item(1),
|
|
|
|
aContext, aPresContext, aCanStoreInRuleTree,
|
|
|
|
aBounds.Width(), aAppUnitsPerMatrixUnit);
|
|
|
|
aMatrix.Translate(temp);
|
2008-09-13 09:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function to process a translatey function. */
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessTranslateY(gfx3DMatrix& aMatrix,
|
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool& aCanStoreInRuleTree,
|
2011-09-26 21:54:45 +00:00
|
|
|
nsRect& aBounds, float aAppUnitsPerMatrixUnit)
|
2008-09-13 09:42:11 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
gfxPoint3D temp;
|
2011-07-22 22:28:07 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
temp.y = ProcessTranslatePart(aData->Item(1),
|
|
|
|
aContext, aPresContext, aCanStoreInRuleTree,
|
|
|
|
aBounds.Height(), aAppUnitsPerMatrixUnit);
|
|
|
|
aMatrix.Translate(temp);
|
2008-09-13 09:42:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessTranslateZ(gfx3DMatrix& aMatrix,
|
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
2011-08-03 03:04:19 +00:00
|
|
|
nsPresContext* aPresContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool& aCanStoreInRuleTree,
|
2011-08-03 03:04:19 +00:00
|
|
|
float aAppUnitsPerMatrixUnit)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
gfxPoint3D temp;
|
2011-08-03 03:04:19 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
temp.z = ProcessTranslatePart(aData->Item(1),
|
|
|
|
aContext, aPresContext, aCanStoreInRuleTree,
|
|
|
|
0, aAppUnitsPerMatrixUnit);
|
|
|
|
aMatrix.Translate(temp);
|
2011-08-03 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2008-09-20 16:55:27 +00:00
|
|
|
/* Helper function to process a translate function. */
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessTranslate(gfx3DMatrix& aMatrix,
|
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool& aCanStoreInRuleTree,
|
2011-09-26 21:54:45 +00:00
|
|
|
nsRect& aBounds, float aAppUnitsPerMatrixUnit)
|
2008-09-13 09:42:11 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2 || aData->Count() == 3, "Invalid array!");
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
gfxPoint3D temp;
|
2011-07-22 22:28:07 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
temp.x = ProcessTranslatePart(aData->Item(1),
|
|
|
|
aContext, aPresContext, aCanStoreInRuleTree,
|
|
|
|
aBounds.Width(), aAppUnitsPerMatrixUnit);
|
2008-09-13 09:42:11 +00:00
|
|
|
|
2008-09-20 16:52:12 +00:00
|
|
|
/* If we read in a Y component, set it appropriately */
|
|
|
|
if (aData->Count() == 3) {
|
2011-09-26 21:54:45 +00:00
|
|
|
temp.y = ProcessTranslatePart(aData->Item(2),
|
|
|
|
aContext, aPresContext, aCanStoreInRuleTree,
|
|
|
|
aBounds.Height(), aAppUnitsPerMatrixUnit);
|
2008-09-20 16:52:12 +00:00
|
|
|
}
|
2011-09-26 21:54:45 +00:00
|
|
|
aMatrix.Translate(temp);
|
2008-09-13 09:42:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessTranslate3D(gfx3DMatrix& aMatrix,
|
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool& aCanStoreInRuleTree,
|
2011-09-26 21:54:45 +00:00
|
|
|
nsRect& aBounds, float aAppUnitsPerMatrixUnit)
|
2011-08-03 03:04:19 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 4, "Invalid array!");
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
gfxPoint3D temp;
|
2011-08-03 03:04:19 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
temp.x = ProcessTranslatePart(aData->Item(1),
|
|
|
|
aContext, aPresContext, aCanStoreInRuleTree,
|
|
|
|
aBounds.Width(), aAppUnitsPerMatrixUnit);
|
2011-08-03 03:04:19 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
temp.y = ProcessTranslatePart(aData->Item(2),
|
|
|
|
aContext, aPresContext, aCanStoreInRuleTree,
|
|
|
|
aBounds.Height(), aAppUnitsPerMatrixUnit);
|
2011-08-03 03:04:19 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
temp.z = ProcessTranslatePart(aData->Item(3),
|
|
|
|
aContext, aPresContext, aCanStoreInRuleTree,
|
|
|
|
0, aAppUnitsPerMatrixUnit);
|
2011-08-03 03:04:19 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
aMatrix.Translate(temp);
|
2011-08-03 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2008-09-13 09:42:11 +00:00
|
|
|
/* Helper function to set up a scale matrix. */
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessScaleHelper(gfx3DMatrix& aMatrix,
|
|
|
|
float aXScale,
|
|
|
|
float aYScale,
|
|
|
|
float aZScale)
|
2008-09-13 09:42:11 +00:00
|
|
|
{
|
2011-09-26 21:54:45 +00:00
|
|
|
aMatrix.Scale(aXScale, aYScale, aZScale);
|
2008-09-13 09:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process a scalex function. */
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessScaleX(gfx3DMatrix& aMatrix, const nsCSSValue::Array* aData)
|
2008-09-13 09:42:11 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Bad array!");
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessScaleHelper(aMatrix, aData->Item(1).GetFloatValue(), 1.0f, 1.0f);
|
2008-09-13 09:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process a scaley function. */
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessScaleY(gfx3DMatrix& aMatrix, const nsCSSValue::Array* aData)
|
2008-09-13 09:42:11 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Bad array!");
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessScaleHelper(aMatrix, 1.0f, aData->Item(1).GetFloatValue(), 1.0f);
|
2008-09-13 09:42:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessScaleZ(gfx3DMatrix& aMatrix, const nsCSSValue::Array* aData)
|
2011-08-03 03:04:19 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Bad array!");
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessScaleHelper(aMatrix, 1.0f, 1.0f, aData->Item(1).GetFloatValue());
|
2011-08-03 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessScale3D(gfx3DMatrix& aMatrix, const nsCSSValue::Array* aData)
|
2011-08-03 03:04:19 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 4, "Bad array!");
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessScaleHelper(aMatrix,
|
|
|
|
aData->Item(1).GetFloatValue(),
|
|
|
|
aData->Item(2).GetFloatValue(),
|
|
|
|
aData->Item(3).GetFloatValue());
|
2011-08-03 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2008-09-13 09:42:11 +00:00
|
|
|
/* Process a scale function. */
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessScale(gfx3DMatrix& aMatrix, const nsCSSValue::Array* aData)
|
2008-09-13 09:42:11 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2 || aData->Count() == 3, "Bad array!");
|
|
|
|
/* We either have one element or two. If we have one, it's for both X and Y.
|
|
|
|
* Otherwise it's one for each.
|
|
|
|
*/
|
|
|
|
const nsCSSValue& scaleX = aData->Item(1);
|
|
|
|
const nsCSSValue& scaleY = (aData->Count() == 2 ? scaleX :
|
2008-10-20 12:07:09 +00:00
|
|
|
aData->Item(2));
|
2008-09-13 09:42:11 +00:00
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessScaleHelper(aMatrix,
|
|
|
|
scaleX.GetFloatValue(),
|
|
|
|
scaleY.GetFloatValue(),
|
|
|
|
1.0f);
|
2008-09-13 09:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function that, given a set of angles, constructs the appropriate
|
|
|
|
* skew matrix.
|
|
|
|
*/
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessSkewHelper(gfx3DMatrix& aMatrix, double aXAngle, double aYAngle)
|
2008-09-13 09:42:11 +00:00
|
|
|
{
|
2011-09-26 21:54:45 +00:00
|
|
|
aMatrix.SkewXY(aXAngle, aYAngle);
|
2008-09-13 09:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Function that converts a skewx transform into a matrix. */
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessSkewX(gfx3DMatrix& aMatrix, const nsCSSValue::Array* aData)
|
2008-09-13 09:42:11 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aData->Count() == 2, "Bad array!");
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessSkewHelper(aMatrix, aData->Item(1).GetAngleValueInRadians(), 0.0);
|
2008-09-13 09:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Function that converts a skewy transform into a matrix. */
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessSkewY(gfx3DMatrix& aMatrix, const nsCSSValue::Array* aData)
|
2008-09-13 09:42:11 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aData->Count() == 2, "Bad array!");
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessSkewHelper(aMatrix, 0.0, aData->Item(1).GetAngleValueInRadians());
|
2008-09-13 09:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Function that converts a rotate transform into a matrix. */
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessRotateZ(gfx3DMatrix& aMatrix, const nsCSSValue::Array* aData)
|
2008-09-13 09:42:11 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
2009-11-02 19:36:43 +00:00
|
|
|
double theta = aData->Item(1).GetAngleValueInRadians();
|
2011-09-26 21:54:45 +00:00
|
|
|
aMatrix.RotateZ(theta);
|
2008-09-13 09:42:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessRotateX(gfx3DMatrix& aMatrix, const nsCSSValue::Array* aData)
|
2011-08-03 03:04:19 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
|
|
|
double theta = aData->Item(1).GetAngleValueInRadians();
|
2011-09-26 21:54:45 +00:00
|
|
|
aMatrix.RotateX(theta);
|
2011-08-03 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessRotateY(gfx3DMatrix& aMatrix, const nsCSSValue::Array* aData)
|
2011-08-03 03:04:19 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
|
|
|
double theta = aData->Item(1).GetAngleValueInRadians();
|
2011-09-26 21:54:45 +00:00
|
|
|
aMatrix.RotateY(theta);
|
2011-08-03 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessRotate3D(gfx3DMatrix& aMatrix, const nsCSSValue::Array* aData)
|
2011-08-03 03:04:19 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 5, "Invalid array!");
|
|
|
|
|
|
|
|
/* We want our matrix to look like this:
|
|
|
|
* | 1 + (1-cos(angle))*(x*x-1) -z*sin(angle)+(1-cos(angle))*x*y y*sin(angle)+(1-cos(angle))*x*z 0 |
|
|
|
|
* | z*sin(angle)+(1-cos(angle))*x*y 1 + (1-cos(angle))*(y*y-1) -x*sin(angle)+(1-cos(angle))*y*z 0 |
|
|
|
|
* | -y*sin(angle)+(1-cos(angle))*x*z x*sin(angle)+(1-cos(angle))*y*z 1 + (1-cos(angle))*(z*z-1) 0 |
|
|
|
|
* | 0 0 0 1 |
|
|
|
|
* (see http://www.w3.org/TR/css3-3d-transforms/#transform-functions)
|
|
|
|
*/
|
2011-12-04 22:53:38 +00:00
|
|
|
|
|
|
|
/* The current spec specifies a matrix that rotates in the wrong direction. For now we just negate
|
|
|
|
* the angle provided to get the correct rotation direction until the spec is updated.
|
|
|
|
* See bug 704468.
|
|
|
|
*/
|
|
|
|
double theta = -aData->Item(4).GetAngleValueInRadians();
|
2011-08-03 03:04:19 +00:00
|
|
|
float cosTheta = FlushToZero(cos(theta));
|
|
|
|
float sinTheta = FlushToZero(sin(theta));
|
|
|
|
|
|
|
|
float x = aData->Item(1).GetFloatValue();
|
|
|
|
float y = aData->Item(2).GetFloatValue();
|
|
|
|
float z = aData->Item(3).GetFloatValue();
|
|
|
|
|
|
|
|
/* Normalize [x,y,z] */
|
|
|
|
float length = sqrt(x*x + y*y + z*z);
|
|
|
|
if (length == 0.0) {
|
2011-09-26 21:54:45 +00:00
|
|
|
return;
|
2011-08-03 03:04:19 +00:00
|
|
|
}
|
|
|
|
x /= length;
|
|
|
|
y /= length;
|
|
|
|
z /= length;
|
|
|
|
|
|
|
|
gfx3DMatrix temp;
|
|
|
|
|
|
|
|
/* Create our matrix */
|
|
|
|
temp._11 = 1 + (1 - cosTheta) * (x * x - 1);
|
|
|
|
temp._12 = -z * sinTheta + (1 - cosTheta) * x * y;
|
|
|
|
temp._13 = y * sinTheta + (1 - cosTheta) * x * z;
|
|
|
|
temp._14 = 0.0f;
|
|
|
|
temp._21 = z * sinTheta + (1 - cosTheta) * x * y;
|
|
|
|
temp._22 = 1 + (1 - cosTheta) * (y * y - 1);
|
|
|
|
temp._23 = -x * sinTheta + (1 - cosTheta) * y * z;
|
|
|
|
temp._24 = 0.0f;
|
|
|
|
temp._31 = -y * sinTheta + (1 - cosTheta) * x * z;
|
|
|
|
temp._32 = x * sinTheta + (1 - cosTheta) * y * z;
|
|
|
|
temp._33 = 1 + (1 - cosTheta) * (z * z - 1);
|
|
|
|
temp._34 = 0.0f;
|
|
|
|
temp._41 = 0.0f;
|
|
|
|
temp._42 = 0.0f;
|
|
|
|
temp._43 = 0.0f;
|
|
|
|
temp._44 = 1.0f;
|
2011-09-26 21:54:45 +00:00
|
|
|
|
|
|
|
aMatrix = temp * aMatrix;
|
2011-08-03 03:04:19 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
ProcessPerspective(gfx3DMatrix& aMatrix,
|
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext *aContext,
|
|
|
|
nsPresContext *aPresContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool &aCanStoreInRuleTree,
|
2011-09-26 21:54:45 +00:00
|
|
|
float aAppUnitsPerMatrixUnit)
|
2011-08-03 03:04:22 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
float depth = ProcessTranslatePart(aData->Item(1), aContext,
|
|
|
|
aPresContext, aCanStoreInRuleTree,
|
|
|
|
0, aAppUnitsPerMatrixUnit);
|
|
|
|
aMatrix.Perspective(depth);
|
2011-08-03 03:04:22 +00:00
|
|
|
}
|
|
|
|
|
2010-07-03 04:18:56 +00:00
|
|
|
|
2008-09-13 09:42:11 +00:00
|
|
|
/**
|
|
|
|
* SetToTransformFunction is essentially a giant switch statement that fans
|
|
|
|
* out to many smaller helper functions.
|
|
|
|
*/
|
2011-09-26 21:54:45 +00:00
|
|
|
static void
|
|
|
|
MatrixForTransformFunction(gfx3DMatrix& aMatrix,
|
|
|
|
const nsCSSValue::Array * aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool& aCanStoreInRuleTree,
|
2011-09-26 21:54:45 +00:00
|
|
|
nsRect& aBounds,
|
|
|
|
float aAppUnitsPerMatrixUnit)
|
2008-09-13 09:42:11 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData, "Why did you want to get data from a null array?");
|
2010-07-03 04:18:56 +00:00
|
|
|
// It's OK if aContext and aPresContext are null if the caller already
|
|
|
|
// knows that all length units have been converted to pixels (as
|
|
|
|
// nsStyleAnimation does).
|
|
|
|
|
2008-09-13 09:42:11 +00:00
|
|
|
|
|
|
|
/* Get the keyword for the transform. */
|
2010-07-03 04:18:56 +00:00
|
|
|
switch (TransformFunctionOf(aData)) {
|
2008-09-13 09:42:11 +00:00
|
|
|
case eCSSKeyword_translatex:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessTranslateX(aMatrix, aData, aContext, aPresContext,
|
|
|
|
aCanStoreInRuleTree, aBounds, aAppUnitsPerMatrixUnit);
|
|
|
|
break;
|
2008-09-13 09:42:11 +00:00
|
|
|
case eCSSKeyword_translatey:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessTranslateY(aMatrix, aData, aContext, aPresContext,
|
|
|
|
aCanStoreInRuleTree, aBounds, aAppUnitsPerMatrixUnit);
|
|
|
|
break;
|
2011-08-03 03:04:19 +00:00
|
|
|
case eCSSKeyword_translatez:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessTranslateZ(aMatrix, aData, aContext, aPresContext,
|
|
|
|
aCanStoreInRuleTree, aAppUnitsPerMatrixUnit);
|
|
|
|
break;
|
2008-09-13 09:42:11 +00:00
|
|
|
case eCSSKeyword_translate:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessTranslate(aMatrix, aData, aContext, aPresContext,
|
|
|
|
aCanStoreInRuleTree, aBounds, aAppUnitsPerMatrixUnit);
|
|
|
|
break;
|
2011-08-03 03:04:19 +00:00
|
|
|
case eCSSKeyword_translate3d:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessTranslate3D(aMatrix, aData, aContext, aPresContext,
|
|
|
|
aCanStoreInRuleTree, aBounds, aAppUnitsPerMatrixUnit);
|
|
|
|
break;
|
2008-09-13 09:42:11 +00:00
|
|
|
case eCSSKeyword_scalex:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessScaleX(aMatrix, aData);
|
|
|
|
break;
|
2008-09-13 09:42:11 +00:00
|
|
|
case eCSSKeyword_scaley:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessScaleY(aMatrix, aData);
|
|
|
|
break;
|
2011-08-03 03:04:19 +00:00
|
|
|
case eCSSKeyword_scalez:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessScaleZ(aMatrix, aData);
|
|
|
|
break;
|
2008-09-13 09:42:11 +00:00
|
|
|
case eCSSKeyword_scale:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessScale(aMatrix, aData);
|
|
|
|
break;
|
2011-08-03 03:04:19 +00:00
|
|
|
case eCSSKeyword_scale3d:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessScale3D(aMatrix, aData);
|
|
|
|
break;
|
2008-09-13 09:42:11 +00:00
|
|
|
case eCSSKeyword_skewx:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessSkewX(aMatrix, aData);
|
|
|
|
break;
|
2008-09-13 09:42:11 +00:00
|
|
|
case eCSSKeyword_skewy:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessSkewY(aMatrix, aData);
|
|
|
|
break;
|
2011-08-03 03:04:19 +00:00
|
|
|
case eCSSKeyword_rotatex:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessRotateX(aMatrix, aData);
|
|
|
|
break;
|
2011-08-03 03:04:19 +00:00
|
|
|
case eCSSKeyword_rotatey:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessRotateY(aMatrix, aData);
|
|
|
|
break;
|
2011-08-03 03:04:19 +00:00
|
|
|
case eCSSKeyword_rotatez:
|
2008-09-13 09:42:11 +00:00
|
|
|
case eCSSKeyword_rotate:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessRotateZ(aMatrix, aData);
|
|
|
|
break;
|
2011-08-03 03:04:19 +00:00
|
|
|
case eCSSKeyword_rotate3d:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessRotate3D(aMatrix, aData);
|
|
|
|
break;
|
2008-09-13 09:42:11 +00:00
|
|
|
case eCSSKeyword_matrix:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessMatrix(aMatrix, aData, aContext, aPresContext,
|
|
|
|
aCanStoreInRuleTree, aBounds, aAppUnitsPerMatrixUnit);
|
|
|
|
break;
|
2011-08-03 03:04:19 +00:00
|
|
|
case eCSSKeyword_matrix3d:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessMatrix3D(aMatrix, aData, aContext, aPresContext,
|
|
|
|
aCanStoreInRuleTree, aBounds, aAppUnitsPerMatrixUnit);
|
|
|
|
break;
|
2011-07-22 22:28:07 +00:00
|
|
|
case eCSSKeyword_interpolatematrix:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessInterpolateMatrix(aMatrix, aData, aContext, aPresContext,
|
|
|
|
aCanStoreInRuleTree, aBounds, aAppUnitsPerMatrixUnit);
|
|
|
|
break;
|
2011-08-03 03:04:22 +00:00
|
|
|
case eCSSKeyword_perspective:
|
2011-09-26 21:54:45 +00:00
|
|
|
ProcessPerspective(aMatrix, aData, aContext, aPresContext,
|
|
|
|
aCanStoreInRuleTree, aAppUnitsPerMatrixUnit);
|
|
|
|
break;
|
2008-09-13 09:42:11 +00:00
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown transform function!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
/**
|
|
|
|
* Return the transform function, as an nsCSSKeyword, for the given
|
|
|
|
* nsCSSValue::Array from a transform list.
|
|
|
|
*/
|
|
|
|
nsCSSKeyword
|
|
|
|
TransformFunctionOf(const nsCSSValue::Array* aData)
|
|
|
|
{
|
|
|
|
nsAutoString keyword;
|
|
|
|
aData->Item(0).GetStringValue(keyword);
|
|
|
|
return nsCSSKeywords::LookupKeyword(keyword);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx3DMatrix
|
|
|
|
ReadTransforms(const nsCSSValueList* aList,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool &aCanStoreInRuleTree,
|
2011-09-26 21:54:45 +00:00
|
|
|
nsRect& aBounds,
|
|
|
|
float aAppUnitsPerMatrixUnit)
|
2010-07-03 04:18:56 +00:00
|
|
|
{
|
2011-07-22 22:28:07 +00:00
|
|
|
gfx3DMatrix result;
|
2010-07-03 04:18:56 +00:00
|
|
|
|
|
|
|
for (const nsCSSValueList* curr = aList; curr != nsnull; curr = curr->mNext) {
|
|
|
|
const nsCSSValue &currElem = curr->mValue;
|
|
|
|
NS_ASSERTION(currElem.GetUnit() == eCSSUnit_Function,
|
|
|
|
"Stream should consist solely of functions!");
|
|
|
|
NS_ASSERTION(currElem.GetArrayValue()->Count() >= 1,
|
|
|
|
"Incoming function is too short!");
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
/* Read in a single transform matrix. */
|
|
|
|
MatrixForTransformFunction(result, currElem.GetArrayValue(), aContext,
|
|
|
|
aPresContext, aCanStoreInRuleTree,
|
|
|
|
aBounds, aAppUnitsPerMatrixUnit);
|
2010-07-03 04:18:56 +00:00
|
|
|
}
|
2011-07-22 22:28:07 +00:00
|
|
|
|
2011-07-22 22:28:51 +00:00
|
|
|
return result;
|
2010-07-03 04:18:56 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 21:54:45 +00:00
|
|
|
} // namespace nsStyleTransformMatrix
|