2008-09-13 13:42:11 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04: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 13:42:11 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A class used for intermediate representations of the -moz-transform property.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsStyleTransformMatrix.h"
|
|
|
|
#include "nsCSSValue.h"
|
2015-05-25 00:40:37 +03:00
|
|
|
#include "nsLayoutUtils.h"
|
2008-09-13 13:42:11 +04:00
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsRuleNode.h"
|
2015-05-25 00:40:37 +03:00
|
|
|
#include "nsSVGUtils.h"
|
2008-09-13 13:42:11 +04:00
|
|
|
#include "nsCSSKeywords.h"
|
2014-06-24 10:29:54 +04:00
|
|
|
#include "mozilla/StyleAnimationValue.h"
|
2013-09-16 05:06:52 +04:00
|
|
|
#include "gfxMatrix.h"
|
2016-10-04 10:00:31 +03:00
|
|
|
#include "gfxQuaternion.h"
|
2010-07-03 08:18:56 +04:00
|
|
|
|
2013-11-15 06:42:57 +04:00
|
|
|
using namespace mozilla;
|
2014-08-29 22:47:30 +04:00
|
|
|
using namespace mozilla::gfx;
|
2008-09-13 13:42:11 +04:00
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
namespace nsStyleTransformMatrix {
|
|
|
|
|
2009-11-02 22:36:43 +03: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 13:42:11 +04:00
|
|
|
|
2015-04-28 21:55:42 +03:00
|
|
|
|
|
|
|
// Define UNIFIED_CONTINUATIONS here and in nsDisplayList.cpp
|
|
|
|
// to have the transform property try
|
|
|
|
// to transform content with continuations as one unified block instead of
|
|
|
|
// several smaller ones. This is currently disabled because it doesn't work
|
|
|
|
// correctly, since when the frames are initially being reflowed, their
|
|
|
|
// continuations all compute their bounding rects independently of each other
|
|
|
|
// and consequently get the wrong value.
|
|
|
|
//#define UNIFIED_CONTINUATIONS
|
|
|
|
|
|
|
|
void
|
|
|
|
TransformReferenceBox::EnsureDimensionsAreCached()
|
|
|
|
{
|
|
|
|
if (mIsCached) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mFrame);
|
|
|
|
|
|
|
|
mIsCached = true;
|
|
|
|
|
|
|
|
if (mFrame->GetStateBits() & NS_FRAME_SVG_LAYOUT) {
|
2015-09-16 00:10:48 +03:00
|
|
|
if (!nsLayoutUtils::SVGTransformBoxEnabled()) {
|
2015-05-25 00:40:37 +03:00
|
|
|
mX = -mFrame->GetPosition().x;
|
|
|
|
mY = -mFrame->GetPosition().y;
|
2015-09-16 00:10:48 +03:00
|
|
|
Size contextSize = nsSVGUtils::GetContextSize(mFrame);
|
|
|
|
mWidth = nsPresContext::CSSPixelsToAppUnits(contextSize.width);
|
|
|
|
mHeight = nsPresContext::CSSPixelsToAppUnits(contextSize.height);
|
2015-05-25 00:40:37 +03:00
|
|
|
} else
|
2017-03-16 10:18:10 +03:00
|
|
|
if (mFrame->StyleDisplay()->mTransformBox == StyleGeometryBox::FillBox) {
|
2015-05-25 00:40:37 +03:00
|
|
|
// Percentages in transforms resolve against the SVG bbox, and the
|
|
|
|
// transform is relative to the top-left of the SVG bbox.
|
|
|
|
nsRect bboxInAppUnits =
|
2017-03-15 07:21:07 +03:00
|
|
|
nsLayoutUtils::ComputeGeometryBox(const_cast<nsIFrame*>(mFrame),
|
2017-03-16 10:18:10 +03:00
|
|
|
StyleGeometryBox::FillBox);
|
2015-05-25 00:40:37 +03:00
|
|
|
// The mRect of an SVG nsIFrame is its user space bounds *including*
|
|
|
|
// stroke and markers, whereas bboxInAppUnits is its user space bounds
|
|
|
|
// including fill only. We need to note the offset of the reference box
|
|
|
|
// from the frame's mRect in mX/mY.
|
|
|
|
mX = bboxInAppUnits.x - mFrame->GetPosition().x;
|
|
|
|
mY = bboxInAppUnits.y - mFrame->GetPosition().y;
|
|
|
|
mWidth = bboxInAppUnits.width;
|
|
|
|
mHeight = bboxInAppUnits.height;
|
|
|
|
} else {
|
|
|
|
// The value 'border-box' is treated as 'view-box' for SVG content.
|
|
|
|
MOZ_ASSERT(mFrame->StyleDisplay()->mTransformBox ==
|
2017-03-16 10:18:10 +03:00
|
|
|
StyleGeometryBox::ViewBox ||
|
2015-05-25 00:40:37 +03:00
|
|
|
mFrame->StyleDisplay()->mTransformBox ==
|
2017-03-16 10:18:10 +03:00
|
|
|
StyleGeometryBox::BorderBox,
|
2015-05-25 00:40:37 +03:00
|
|
|
"Unexpected value for 'transform-box'");
|
|
|
|
// Percentages in transforms resolve against the width/height of the
|
2015-09-16 00:10:48 +03:00
|
|
|
// nearest viewport (or its viewBox if one is applied), and the
|
2015-05-25 00:40:37 +03:00
|
|
|
// transform is relative to {0,0} in current user space.
|
|
|
|
mX = -mFrame->GetPosition().x;
|
|
|
|
mY = -mFrame->GetPosition().y;
|
|
|
|
Size contextSize = nsSVGUtils::GetContextSize(mFrame);
|
|
|
|
mWidth = nsPresContext::CSSPixelsToAppUnits(contextSize.width);
|
|
|
|
mHeight = nsPresContext::CSSPixelsToAppUnits(contextSize.height);
|
|
|
|
}
|
2015-04-28 21:55:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If UNIFIED_CONTINUATIONS is not defined, this is simply the frame's
|
|
|
|
// bounding rectangle, translated to the origin. Otherwise, it is the
|
|
|
|
// smallest rectangle containing a frame and all of its continuations. For
|
|
|
|
// example, if there is a <span> element with several continuations split
|
|
|
|
// over several lines, this function will return the rectangle containing all
|
|
|
|
// of those continuations.
|
|
|
|
|
|
|
|
nsRect rect;
|
|
|
|
|
|
|
|
#ifndef UNIFIED_CONTINUATIONS
|
|
|
|
rect = mFrame->GetRect();
|
|
|
|
#else
|
|
|
|
// Iterate the continuation list, unioning together the bounding rects:
|
|
|
|
for (const nsIFrame *currFrame = mFrame->FirstContinuation();
|
|
|
|
currFrame != nullptr;
|
|
|
|
currFrame = currFrame->GetNextContinuation())
|
|
|
|
{
|
|
|
|
// Get the frame rect in local coordinates, then translate back to the
|
|
|
|
// original coordinates:
|
|
|
|
rect.UnionRect(result, nsRect(currFrame->GetOffsetTo(mFrame),
|
|
|
|
currFrame->GetSize()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-25 00:40:37 +03:00
|
|
|
mX = 0;
|
|
|
|
mY = 0;
|
2015-04-28 21:55:42 +03:00
|
|
|
mWidth = rect.Width();
|
|
|
|
mHeight = rect.Height();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TransformReferenceBox::Init(const nsSize& aDimensions)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mFrame && !mIsCached);
|
|
|
|
|
2015-05-25 00:40:37 +03:00
|
|
|
mX = 0;
|
|
|
|
mY = 0;
|
2015-04-28 21:55:42 +03:00
|
|
|
mWidth = aDimensions.width;
|
|
|
|
mHeight = aDimensions.height;
|
|
|
|
mIsCached = true;
|
|
|
|
}
|
|
|
|
|
2012-07-31 21:28:21 +04:00
|
|
|
float
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessTranslatePart(const nsCSSValue& aValue,
|
2011-07-23 02:28:07 +04:00
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
RuleNodeCacheConditions& aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
TransformReferenceBox* aRefBox,
|
|
|
|
TransformReferenceBox::DimensionGetter aDimensionGetter)
|
2010-07-03 08:18:56 +04:00
|
|
|
{
|
2011-07-23 02:28:07 +04:00
|
|
|
nscoord offset = 0;
|
|
|
|
float percent = 0.0f;
|
|
|
|
|
2010-07-03 08:18:56 +04:00
|
|
|
if (aValue.GetUnit() == eCSSUnit_Percent) {
|
2011-07-23 02:28:07 +04:00
|
|
|
percent = aValue.GetPercentValue();
|
2012-03-06 22:47:13 +04:00
|
|
|
} else if (aValue.GetUnit() == eCSSUnit_Pixel ||
|
|
|
|
aValue.GetUnit() == eCSSUnit_Number) {
|
|
|
|
// 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
|
2014-06-24 10:29:54 +04:00
|
|
|
// StyleAnimationValue does) that all lengths within the transform
|
2012-03-06 22:47:13 +04:00
|
|
|
// function have already been computed to pixels and percents.
|
|
|
|
//
|
|
|
|
// Raw numbers are treated as being pixels.
|
|
|
|
//
|
|
|
|
// Don't convert to aValue to AppUnits here to avoid precision issues.
|
2014-05-08 09:35:00 +04:00
|
|
|
return aValue.GetFloatValue();
|
2010-07-03 08:18:56 +04:00
|
|
|
} else if (aValue.IsCalcUnit()) {
|
2010-09-11 20:27:13 +04:00
|
|
|
nsRuleNode::ComputedCalc result =
|
|
|
|
nsRuleNode::SpecifiedCalcToComputedCalc(aValue, aContext, aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
aConditions);
|
2011-07-23 02:28:07 +04:00
|
|
|
percent = result.mPercent;
|
|
|
|
offset = result.mLength;
|
2010-07-03 08:18:56 +04:00
|
|
|
} else {
|
2012-03-06 22:47:13 +04:00
|
|
|
offset = nsRuleNode::CalcLength(aValue, aContext, aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
aConditions);
|
2010-07-03 08:18:56 +04:00
|
|
|
}
|
2011-07-23 02:28:07 +04:00
|
|
|
|
2015-04-28 21:55:42 +03:00
|
|
|
float translation = NSAppUnitsToFloatPixels(offset,
|
|
|
|
nsPresContext::AppUnitsPerCSSPixel());
|
|
|
|
// We want to avoid calling aDimensionGetter if there's no percentage to be
|
|
|
|
// resolved (for performance reasons - see TransformReferenceBox).
|
2016-10-05 10:36:16 +03:00
|
|
|
if (percent != 0.0f && aRefBox && !aRefBox->IsEmpty()) {
|
2015-04-28 21:55:42 +03:00
|
|
|
translation += percent *
|
|
|
|
NSAppUnitsToFloatPixels((aRefBox->*aDimensionGetter)(),
|
|
|
|
nsPresContext::AppUnitsPerCSSPixel());
|
|
|
|
}
|
|
|
|
return translation;
|
2010-07-03 08:18:56 +04:00
|
|
|
}
|
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
/**
|
|
|
|
* Helper functions to process all the transformation function types.
|
|
|
|
*
|
|
|
|
* These take a matrix parameter to accumulate the current matrix.
|
|
|
|
*/
|
|
|
|
|
2010-08-06 08:59:18 +04:00
|
|
|
/* Helper function to process a matrix entry. */
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessMatrix(Matrix4x4& aMatrix,
|
2011-09-27 01:54:45 +04:00
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
RuleNodeCacheConditions& aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
TransformReferenceBox& aRefBox)
|
2010-08-06 08:59:18 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 7, "Invalid array!");
|
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
gfxMatrix result;
|
2011-07-23 02:28:07 +04:00
|
|
|
|
2010-08-06 08:59:18 +04:00
|
|
|
/* Take the first four elements out of the array as floats and store
|
2011-07-23 02:28:07 +04:00
|
|
|
* them.
|
2010-08-06 08:59:18 +04:00
|
|
|
*/
|
2014-06-17 21:35:51 +04:00
|
|
|
result._11 = aData->Item(1).GetFloatValue();
|
|
|
|
result._12 = aData->Item(2).GetFloatValue();
|
|
|
|
result._21 = aData->Item(3).GetFloatValue();
|
|
|
|
result._22 = aData->Item(4).GetFloatValue();
|
2010-08-06 08:59:18 +04:00
|
|
|
|
|
|
|
/* The last two elements have their length parts stored in aDelta
|
|
|
|
* and their percent parts stored in aX[0] and aY[1].
|
|
|
|
*/
|
2014-06-17 21:35:51 +04:00
|
|
|
result._31 = ProcessTranslatePart(aData->Item(5),
|
2015-06-23 04:48:18 +03:00
|
|
|
aContext, aPresContext, aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
&aRefBox, &TransformReferenceBox::Width);
|
2014-06-17 21:35:51 +04:00
|
|
|
result._32 = ProcessTranslatePart(aData->Item(6),
|
2015-06-23 04:48:18 +03:00
|
|
|
aContext, aPresContext, aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
&aRefBox, &TransformReferenceBox::Height);
|
2011-09-27 01:54:45 +04:00
|
|
|
|
2015-07-11 03:05:47 +03:00
|
|
|
aMatrix = result * aMatrix;
|
2011-07-23 02:28:07 +04:00
|
|
|
}
|
|
|
|
|
2016-10-04 10:00:31 +03:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessMatrix3D(Matrix4x4& aMatrix,
|
2011-09-27 01:54:45 +04:00
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
RuleNodeCacheConditions& aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
TransformReferenceBox& aRefBox)
|
2011-08-03 07:04:19 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 17, "Invalid array!");
|
|
|
|
|
2015-07-11 03:05:47 +03:00
|
|
|
Matrix4x4 temp;
|
2011-08-03 07:04:19 +04:00
|
|
|
|
|
|
|
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-27 01:54:30 +04:00
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
temp._41 = ProcessTranslatePart(aData->Item(13),
|
2015-06-23 04:48:18 +03:00
|
|
|
aContext, aPresContext, aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
&aRefBox, &TransformReferenceBox::Width);
|
2011-09-27 01:54:45 +04:00
|
|
|
temp._42 = ProcessTranslatePart(aData->Item(14),
|
2015-06-23 04:48:18 +03:00
|
|
|
aContext, aPresContext, aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
&aRefBox, &TransformReferenceBox::Height);
|
2011-09-27 01:54:45 +04:00
|
|
|
temp._43 = ProcessTranslatePart(aData->Item(15),
|
2015-06-23 04:48:18 +03:00
|
|
|
aContext, aPresContext, aConditions,
|
2015-05-25 13:13:42 +03:00
|
|
|
nullptr);
|
2011-09-27 01:54:45 +04:00
|
|
|
|
2015-07-11 03:05:47 +03:00
|
|
|
aMatrix = temp * aMatrix;
|
2011-08-03 07:04:19 +04:00
|
|
|
}
|
|
|
|
|
2016-11-16 14:32:33 +03:00
|
|
|
// For accumulation for transform functions, |aOne| corresponds to |aB| and
|
|
|
|
// |aTwo| corresponds to |aA| for StyleAnimationValue::Accumulate().
|
|
|
|
class Accumulate {
|
|
|
|
public:
|
|
|
|
template<typename T>
|
|
|
|
static T operate(const T& aOne, const T& aTwo, double aCoeff)
|
|
|
|
{
|
|
|
|
return aOne + aTwo * aCoeff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Point4D operateForPerspective(const Point4D& aOne,
|
|
|
|
const Point4D& aTwo,
|
|
|
|
double aCoeff)
|
|
|
|
{
|
|
|
|
return (aOne - Point4D(0, 0, 0, 1)) +
|
|
|
|
(aTwo - Point4D(0, 0, 0, 1)) * aCoeff +
|
|
|
|
Point4D(0, 0, 0, 1);
|
|
|
|
}
|
|
|
|
static Point3D operateForScale(const Point3D& aOne,
|
|
|
|
const Point3D& aTwo,
|
|
|
|
double aCoeff)
|
|
|
|
{
|
|
|
|
// For scale, the identify element is 1, see AddTransformScale in
|
|
|
|
// StyleAnimationValue.cpp.
|
|
|
|
return (aOne - Point3D(1, 1, 1)) +
|
|
|
|
(aTwo - Point3D(1, 1, 1)) * aCoeff +
|
|
|
|
Point3D(1, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Matrix4x4 operateForRotate(const gfxQuaternion& aOne,
|
|
|
|
const gfxQuaternion& aTwo,
|
|
|
|
double aCoeff)
|
|
|
|
{
|
|
|
|
if (aCoeff == 0.0) {
|
|
|
|
return aOne.ToMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
double theta = acos(mozilla::clamped(aTwo.w, -1.0, 1.0));
|
|
|
|
double scale = (theta != 0.0) ? 1.0 / sin(theta) : 0.0;
|
|
|
|
theta *= aCoeff;
|
|
|
|
scale *= sin(theta);
|
|
|
|
|
|
|
|
gfxQuaternion result = gfxQuaternion(scale * aTwo.x,
|
|
|
|
scale * aTwo.y,
|
|
|
|
scale * aTwo.z,
|
|
|
|
cos(theta)) * aOne;
|
|
|
|
return result.ToMatrix();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-16 14:32:32 +03:00
|
|
|
class Interpolate {
|
|
|
|
public:
|
|
|
|
template<typename T>
|
|
|
|
static T operate(const T& aOne, const T& aTwo, double aCoeff)
|
|
|
|
{
|
|
|
|
return aOne + (aTwo - aOne) * aCoeff;
|
|
|
|
}
|
|
|
|
|
2016-11-16 14:32:33 +03:00
|
|
|
static Point4D operateForPerspective(const Point4D& aOne,
|
|
|
|
const Point4D& aTwo,
|
|
|
|
double aCoeff)
|
|
|
|
{
|
|
|
|
return aOne + (aTwo - aOne) * aCoeff;
|
|
|
|
}
|
|
|
|
|
2016-11-16 14:32:32 +03:00
|
|
|
static Point3D operateForScale(const Point3D& aOne,
|
|
|
|
const Point3D& aTwo,
|
|
|
|
double aCoeff)
|
|
|
|
{
|
|
|
|
return aOne + (aTwo - aOne) * aCoeff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Matrix4x4 operateForRotate(const gfxQuaternion& aOne,
|
|
|
|
const gfxQuaternion& aTwo,
|
|
|
|
double aCoeff)
|
|
|
|
{
|
|
|
|
return aOne.Slerp(aTwo, aCoeff).ToMatrix();
|
|
|
|
}
|
|
|
|
};
|
2016-11-16 14:32:32 +03:00
|
|
|
|
|
|
|
/**
|
2016-11-16 14:32:32 +03:00
|
|
|
* Calculate 2 matrices by decomposing them with Operator.
|
2016-11-16 14:32:32 +03:00
|
|
|
*
|
|
|
|
* @param aMatrix1 First matrix, using CSS pixel units.
|
|
|
|
* @param aMatrix2 Second matrix, using CSS pixel units.
|
2016-11-16 14:32:32 +03:00
|
|
|
* @param aProgress Coefficient for the Operator.
|
2016-11-16 14:32:32 +03:00
|
|
|
*/
|
2016-11-16 14:32:32 +03:00
|
|
|
template <typename Operator>
|
2016-11-16 14:32:32 +03:00
|
|
|
static Matrix4x4
|
2016-11-16 14:32:32 +03:00
|
|
|
OperateTransformMatrix(const Matrix4x4 &aMatrix1,
|
|
|
|
const Matrix4x4 &aMatrix2,
|
|
|
|
double aProgress)
|
2016-11-16 14:32:32 +03:00
|
|
|
{
|
|
|
|
// Decompose both matrices
|
|
|
|
|
|
|
|
// TODO: What do we do if one of these returns false (singular matrix)
|
|
|
|
Point3D scale1(1, 1, 1), translate1;
|
|
|
|
Point4D perspective1(0, 0, 0, 1);
|
|
|
|
gfxQuaternion rotate1;
|
|
|
|
nsStyleTransformMatrix::ShearArray shear1{0.0f, 0.0f, 0.0f};
|
|
|
|
|
|
|
|
Point3D scale2(1, 1, 1), translate2;
|
|
|
|
Point4D perspective2(0, 0, 0, 1);
|
|
|
|
gfxQuaternion rotate2;
|
|
|
|
nsStyleTransformMatrix::ShearArray shear2{0.0f, 0.0f, 0.0f};
|
|
|
|
|
|
|
|
Matrix matrix2d1, matrix2d2;
|
|
|
|
if (aMatrix1.Is2D(&matrix2d1) && aMatrix2.Is2D(&matrix2d2)) {
|
|
|
|
Decompose2DMatrix(matrix2d1, scale1, shear1, rotate1, translate1);
|
|
|
|
Decompose2DMatrix(matrix2d2, scale2, shear2, rotate2, translate2);
|
|
|
|
} else {
|
|
|
|
Decompose3DMatrix(aMatrix1, scale1, shear1,
|
|
|
|
rotate1, translate1, perspective1);
|
|
|
|
Decompose3DMatrix(aMatrix2, scale2, shear2,
|
|
|
|
rotate2, translate2, perspective2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix4x4 result;
|
|
|
|
|
2016-11-16 14:32:33 +03:00
|
|
|
// Operate each of the pieces in response to |Operator|.
|
2016-11-16 14:32:32 +03:00
|
|
|
Point4D perspective =
|
2016-11-16 14:32:33 +03:00
|
|
|
Operator::operateForPerspective(perspective1, perspective2, aProgress);
|
2016-11-16 14:32:32 +03:00
|
|
|
result.SetTransposedVector(3, perspective);
|
|
|
|
|
|
|
|
Point3D translate =
|
2016-11-16 14:32:32 +03:00
|
|
|
Operator::operate(translate1, translate2, aProgress);
|
2016-11-16 14:32:32 +03:00
|
|
|
result.PreTranslate(translate.x, translate.y, translate.z);
|
|
|
|
|
2016-11-16 14:32:32 +03:00
|
|
|
Matrix4x4 rotate = Operator::operateForRotate(rotate1, rotate2, aProgress);
|
2016-11-16 14:32:32 +03:00
|
|
|
if (!rotate.IsIdentity()) {
|
|
|
|
result = rotate * result;
|
|
|
|
}
|
|
|
|
|
2016-11-16 14:32:33 +03:00
|
|
|
// TODO: Would it be better to operate these as angles?
|
2016-11-16 14:32:32 +03:00
|
|
|
// How do we convert back to angles?
|
|
|
|
float yzshear =
|
2016-11-16 14:32:32 +03:00
|
|
|
Operator::operate(shear1[ShearType::YZSHEAR],
|
|
|
|
shear2[ShearType::YZSHEAR],
|
|
|
|
aProgress);
|
2016-11-16 14:32:32 +03:00
|
|
|
if (yzshear != 0.0) {
|
|
|
|
result.SkewYZ(yzshear);
|
|
|
|
}
|
|
|
|
|
|
|
|
float xzshear =
|
2016-11-16 14:32:32 +03:00
|
|
|
Operator::operate(shear1[ShearType::XZSHEAR],
|
|
|
|
shear2[ShearType::XZSHEAR],
|
|
|
|
aProgress);
|
2016-11-16 14:32:32 +03:00
|
|
|
if (xzshear != 0.0) {
|
|
|
|
result.SkewXZ(xzshear);
|
|
|
|
}
|
|
|
|
|
|
|
|
float xyshear =
|
2016-11-16 14:32:32 +03:00
|
|
|
Operator::operate(shear1[ShearType::XYSHEAR],
|
|
|
|
shear2[ShearType::XYSHEAR],
|
|
|
|
aProgress);
|
2016-11-16 14:32:32 +03:00
|
|
|
if (xyshear != 0.0) {
|
|
|
|
result.SkewXY(xyshear);
|
|
|
|
}
|
|
|
|
|
|
|
|
Point3D scale =
|
2016-11-16 14:32:32 +03:00
|
|
|
Operator::operateForScale(scale1, scale2, aProgress);
|
2016-11-16 14:32:32 +03:00
|
|
|
if (scale != Point3D(1.0, 1.0, 1.0)) {
|
|
|
|
result.PreScale(scale.x, scale.y, scale.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-16 14:32:32 +03:00
|
|
|
template <typename Operator>
|
|
|
|
static void
|
|
|
|
ProcessMatrixOperator(Matrix4x4& aMatrix,
|
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
RuleNodeCacheConditions& aConditions,
|
|
|
|
TransformReferenceBox& aRefBox,
|
|
|
|
bool* aContains3dTransform)
|
2011-07-23 02:28:07 +04:00
|
|
|
{
|
2011-09-27 01:53:33 +04:00
|
|
|
NS_PRECONDITION(aData->Count() == 4, "Invalid array!");
|
|
|
|
|
2015-07-11 03:05:47 +03:00
|
|
|
Matrix4x4 matrix1, matrix2;
|
2011-09-27 01:53:33 +04:00
|
|
|
if (aData->Item(1).GetUnit() == eCSSUnit_List) {
|
2011-09-27 01:54:45 +04:00
|
|
|
matrix1 = nsStyleTransformMatrix::ReadTransforms(aData->Item(1).GetListValue(),
|
2011-09-27 01:53:33 +04:00
|
|
|
aContext, aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
aConditions,
|
2016-02-02 03:45:09 +03:00
|
|
|
aRefBox, nsPresContext::AppUnitsPerCSSPixel(),
|
|
|
|
aContains3dTransform);
|
2011-09-27 01:53:33 +04:00
|
|
|
}
|
|
|
|
if (aData->Item(2).GetUnit() == eCSSUnit_List) {
|
|
|
|
matrix2 = ReadTransforms(aData->Item(2).GetListValue(),
|
|
|
|
aContext, aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
aConditions,
|
2016-02-02 03:45:09 +03:00
|
|
|
aRefBox, nsPresContext::AppUnitsPerCSSPixel(),
|
|
|
|
aContains3dTransform);
|
2011-09-27 01:53:33 +04:00
|
|
|
}
|
|
|
|
double progress = aData->Item(3).GetPercentValue();
|
2011-07-23 02:28:07 +04:00
|
|
|
|
2016-11-16 14:32:32 +03:00
|
|
|
aMatrix =
|
2016-11-16 14:32:32 +03:00
|
|
|
OperateTransformMatrix<Operator>(matrix1, matrix2, progress) * aMatrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function to process two matrices that we need to interpolate between */
|
|
|
|
void
|
|
|
|
ProcessInterpolateMatrix(Matrix4x4& aMatrix,
|
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
RuleNodeCacheConditions& aConditions,
|
|
|
|
TransformReferenceBox& aRefBox,
|
|
|
|
bool* aContains3dTransform)
|
|
|
|
{
|
|
|
|
ProcessMatrixOperator<Interpolate>(aMatrix, aData, aContext, aPresContext,
|
|
|
|
aConditions, aRefBox,
|
|
|
|
aContains3dTransform);
|
2010-08-06 08:59:18 +04:00
|
|
|
}
|
|
|
|
|
2016-11-16 14:32:33 +03:00
|
|
|
void
|
|
|
|
ProcessAccumulateMatrix(Matrix4x4& aMatrix,
|
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
RuleNodeCacheConditions& aConditions,
|
|
|
|
TransformReferenceBox& aRefBox,
|
|
|
|
bool* aContains3dTransform)
|
|
|
|
{
|
|
|
|
ProcessMatrixOperator<Accumulate>(aMatrix, aData, aContext, aPresContext,
|
|
|
|
aConditions, aRefBox,
|
|
|
|
aContains3dTransform);
|
|
|
|
}
|
|
|
|
|
2008-09-13 13:42:11 +04:00
|
|
|
/* Helper function to process a translatex function. */
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessTranslateX(Matrix4x4& aMatrix,
|
2011-09-27 01:54:45 +04:00
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
RuleNodeCacheConditions& aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
TransformReferenceBox& aRefBox)
|
2008-09-13 13:42:11 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
|
|
|
|
2014-08-29 22:47:30 +04:00
|
|
|
Point3D temp;
|
2011-07-23 02:28:07 +04:00
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
temp.x = ProcessTranslatePart(aData->Item(1),
|
2015-06-23 04:48:18 +03:00
|
|
|
aContext, aPresContext, aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
&aRefBox, &TransformReferenceBox::Width);
|
2015-07-11 03:05:47 +03:00
|
|
|
aMatrix.PreTranslate(temp);
|
2008-09-13 13:42:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function to process a translatey function. */
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessTranslateY(Matrix4x4& aMatrix,
|
2011-09-27 01:54:45 +04:00
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
RuleNodeCacheConditions& aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
TransformReferenceBox& aRefBox)
|
2008-09-13 13:42:11 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
|
|
|
|
2014-08-29 22:47:30 +04:00
|
|
|
Point3D temp;
|
2011-07-23 02:28:07 +04:00
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
temp.y = ProcessTranslatePart(aData->Item(1),
|
2015-06-23 04:48:18 +03:00
|
|
|
aContext, aPresContext, aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
&aRefBox, &TransformReferenceBox::Height);
|
2015-07-11 03:05:47 +03:00
|
|
|
aMatrix.PreTranslate(temp);
|
2008-09-13 13:42:11 +04:00
|
|
|
}
|
|
|
|
|
2016-10-04 10:00:31 +03:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessTranslateZ(Matrix4x4& aMatrix,
|
2011-09-27 01:54:45 +04:00
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
2014-05-08 09:35:00 +04:00
|
|
|
nsPresContext* aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
RuleNodeCacheConditions& aConditions)
|
2011-08-03 07:04:19 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
|
|
|
|
2014-08-29 22:47:30 +04:00
|
|
|
Point3D temp;
|
2011-08-03 07:04:19 +04:00
|
|
|
|
2014-05-08 09:35:00 +04:00
|
|
|
temp.z = ProcessTranslatePart(aData->Item(1), aContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
aPresContext, aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
nullptr);
|
2015-07-11 03:05:47 +03:00
|
|
|
aMatrix.PreTranslate(temp);
|
2011-08-03 07:04:19 +04:00
|
|
|
}
|
|
|
|
|
2008-09-20 20:55:27 +04:00
|
|
|
/* Helper function to process a translate function. */
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessTranslate(Matrix4x4& aMatrix,
|
2011-09-27 01:54:45 +04:00
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
RuleNodeCacheConditions& aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
TransformReferenceBox& aRefBox)
|
2008-09-13 13:42:11 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2 || aData->Count() == 3, "Invalid array!");
|
|
|
|
|
2014-08-29 22:47:30 +04:00
|
|
|
Point3D temp;
|
2011-07-23 02:28:07 +04:00
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
temp.x = ProcessTranslatePart(aData->Item(1),
|
2015-06-23 04:48:18 +03:00
|
|
|
aContext, aPresContext, aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
&aRefBox, &TransformReferenceBox::Width);
|
2008-09-13 13:42:11 +04:00
|
|
|
|
2008-09-20 20:52:12 +04:00
|
|
|
/* If we read in a Y component, set it appropriately */
|
|
|
|
if (aData->Count() == 3) {
|
2011-09-27 01:54:45 +04:00
|
|
|
temp.y = ProcessTranslatePart(aData->Item(2),
|
2015-06-23 04:48:18 +03:00
|
|
|
aContext, aPresContext, aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
&aRefBox, &TransformReferenceBox::Height);
|
2008-09-20 20:52:12 +04:00
|
|
|
}
|
2015-07-11 03:05:47 +03:00
|
|
|
aMatrix.PreTranslate(temp);
|
2008-09-13 13:42:11 +04:00
|
|
|
}
|
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessTranslate3D(Matrix4x4& aMatrix,
|
2011-09-27 01:54:45 +04:00
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
RuleNodeCacheConditions& aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
TransformReferenceBox& aRefBox)
|
2011-08-03 07:04:19 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 4, "Invalid array!");
|
|
|
|
|
2014-08-29 22:47:30 +04:00
|
|
|
Point3D temp;
|
2011-08-03 07:04:19 +04:00
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
temp.x = ProcessTranslatePart(aData->Item(1),
|
2015-06-23 04:48:18 +03:00
|
|
|
aContext, aPresContext, aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
&aRefBox, &TransformReferenceBox::Width);
|
2011-08-03 07:04:19 +04:00
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
temp.y = ProcessTranslatePart(aData->Item(2),
|
2015-06-23 04:48:18 +03:00
|
|
|
aContext, aPresContext, aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
&aRefBox, &TransformReferenceBox::Height);
|
2011-08-03 07:04:19 +04:00
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
temp.z = ProcessTranslatePart(aData->Item(3),
|
2015-06-23 04:48:18 +03:00
|
|
|
aContext, aPresContext, aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
nullptr);
|
2011-08-03 07:04:19 +04:00
|
|
|
|
2015-07-11 03:05:47 +03:00
|
|
|
aMatrix.PreTranslate(temp);
|
2011-08-03 07:04:19 +04:00
|
|
|
}
|
|
|
|
|
2008-09-13 13:42:11 +04:00
|
|
|
/* Helper function to set up a scale matrix. */
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessScaleHelper(Matrix4x4& aMatrix,
|
2016-10-04 10:00:31 +03:00
|
|
|
float aXScale,
|
|
|
|
float aYScale,
|
2011-09-27 01:54:45 +04:00
|
|
|
float aZScale)
|
2008-09-13 13:42:11 +04:00
|
|
|
{
|
2015-07-11 03:05:47 +03:00
|
|
|
aMatrix.PreScale(aXScale, aYScale, aZScale);
|
2008-09-13 13:42:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process a scalex function. */
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessScaleX(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
|
2008-09-13 13:42:11 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Bad array!");
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessScaleHelper(aMatrix, aData->Item(1).GetFloatValue(), 1.0f, 1.0f);
|
2008-09-13 13:42:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process a scaley function. */
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessScaleY(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
|
2008-09-13 13:42:11 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Bad array!");
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessScaleHelper(aMatrix, 1.0f, aData->Item(1).GetFloatValue(), 1.0f);
|
2008-09-13 13:42:11 +04:00
|
|
|
}
|
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessScaleZ(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
|
2011-08-03 07:04:19 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Bad array!");
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessScaleHelper(aMatrix, 1.0f, 1.0f, aData->Item(1).GetFloatValue());
|
2011-08-03 07:04:19 +04:00
|
|
|
}
|
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessScale3D(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
|
2011-08-03 07:04:19 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 4, "Bad array!");
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessScaleHelper(aMatrix,
|
|
|
|
aData->Item(1).GetFloatValue(),
|
|
|
|
aData->Item(2).GetFloatValue(),
|
|
|
|
aData->Item(3).GetFloatValue());
|
2011-08-03 07:04:19 +04:00
|
|
|
}
|
|
|
|
|
2008-09-13 13:42:11 +04:00
|
|
|
/* Process a scale function. */
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessScale(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
|
2008-09-13 13:42:11 +04: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 16:07:09 +04:00
|
|
|
aData->Item(2));
|
2008-09-13 13:42:11 +04:00
|
|
|
|
2016-10-04 10:00:31 +03:00
|
|
|
ProcessScaleHelper(aMatrix,
|
2011-09-27 01:54:45 +04:00
|
|
|
scaleX.GetFloatValue(),
|
|
|
|
scaleY.GetFloatValue(),
|
|
|
|
1.0f);
|
2008-09-13 13:42:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function that, given a set of angles, constructs the appropriate
|
|
|
|
* skew matrix.
|
|
|
|
*/
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessSkewHelper(Matrix4x4& aMatrix, double aXAngle, double aYAngle)
|
2008-09-13 13:42:11 +04:00
|
|
|
{
|
2011-09-27 01:54:45 +04:00
|
|
|
aMatrix.SkewXY(aXAngle, aYAngle);
|
2008-09-13 13:42:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Function that converts a skewx transform into a matrix. */
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessSkewX(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
|
2008-09-13 13:42:11 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aData->Count() == 2, "Bad array!");
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessSkewHelper(aMatrix, aData->Item(1).GetAngleValueInRadians(), 0.0);
|
2008-09-13 13:42:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Function that converts a skewy transform into a matrix. */
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessSkewY(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
|
2008-09-13 13:42:11 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aData->Count() == 2, "Bad array!");
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessSkewHelper(aMatrix, 0.0, aData->Item(1).GetAngleValueInRadians());
|
2008-09-13 13:42:11 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 17:48:02 +04:00
|
|
|
/* Function that converts a skew transform into a matrix. */
|
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessSkew(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
|
2012-07-30 17:48:02 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aData->Count() == 2 || aData->Count() == 3, "Bad array!");
|
|
|
|
|
|
|
|
double xSkew = aData->Item(1).GetAngleValueInRadians();
|
|
|
|
double ySkew = (aData->Count() == 2
|
|
|
|
? 0.0 : aData->Item(2).GetAngleValueInRadians());
|
|
|
|
|
|
|
|
ProcessSkewHelper(aMatrix, xSkew, ySkew);
|
|
|
|
}
|
|
|
|
|
2008-09-13 13:42:11 +04:00
|
|
|
/* Function that converts a rotate transform into a matrix. */
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessRotateZ(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
|
2008-09-13 13:42:11 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
2009-11-02 22:36:43 +03:00
|
|
|
double theta = aData->Item(1).GetAngleValueInRadians();
|
2011-09-27 01:54:45 +04:00
|
|
|
aMatrix.RotateZ(theta);
|
2008-09-13 13:42:11 +04:00
|
|
|
}
|
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessRotateX(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
|
2011-08-03 07:04:19 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
|
|
|
double theta = aData->Item(1).GetAngleValueInRadians();
|
2011-09-27 01:54:45 +04:00
|
|
|
aMatrix.RotateX(theta);
|
2011-08-03 07:04:19 +04:00
|
|
|
}
|
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessRotateY(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
|
2011-08-03 07:04:19 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
|
|
|
double theta = aData->Item(1).GetAngleValueInRadians();
|
2011-09-27 01:54:45 +04:00
|
|
|
aMatrix.RotateY(theta);
|
2011-08-03 07:04:19 +04:00
|
|
|
}
|
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessRotate3D(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
|
2011-08-03 07:04:19 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 5, "Invalid array!");
|
|
|
|
|
2016-02-04 11:30:00 +03:00
|
|
|
double theta = aData->Item(4).GetAngleValueInRadians();
|
|
|
|
float x = aData->Item(1).GetFloatValue();
|
|
|
|
float y = aData->Item(2).GetFloatValue();
|
|
|
|
float z = aData->Item(3).GetFloatValue();
|
2011-08-03 07:04:19 +04:00
|
|
|
|
2015-07-11 03:05:47 +03:00
|
|
|
Matrix4x4 temp;
|
2016-02-04 11:30:00 +03:00
|
|
|
temp.SetRotateAxisAngle(x, y, z, theta);
|
2011-09-27 01:54:45 +04:00
|
|
|
|
|
|
|
aMatrix = temp * aMatrix;
|
2011-08-03 07:04:19 +04:00
|
|
|
}
|
|
|
|
|
2016-10-04 10:00:31 +03:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
ProcessPerspective(Matrix4x4& aMatrix,
|
2011-09-27 01:54:45 +04:00
|
|
|
const nsCSSValue::Array* aData,
|
|
|
|
nsStyleContext *aContext,
|
|
|
|
nsPresContext *aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
RuleNodeCacheConditions& aConditions)
|
2011-08-03 07:04:22 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData->Count() == 2, "Invalid array!");
|
|
|
|
|
2017-01-20 04:45:33 +03:00
|
|
|
float depth = ProcessTranslatePart(aData->Item(1), aContext,
|
|
|
|
aPresContext, aConditions, nullptr);
|
|
|
|
ApplyPerspectiveToMatrix(aMatrix, depth);
|
2011-08-03 07:04:22 +04:00
|
|
|
}
|
|
|
|
|
2010-07-03 08:18:56 +04:00
|
|
|
|
2008-09-13 13:42:11 +04:00
|
|
|
/**
|
|
|
|
* SetToTransformFunction is essentially a giant switch statement that fans
|
|
|
|
* out to many smaller helper functions.
|
|
|
|
*/
|
2011-09-27 01:54:45 +04:00
|
|
|
static void
|
2015-07-11 03:05:47 +03:00
|
|
|
MatrixForTransformFunction(Matrix4x4& aMatrix,
|
2011-09-27 01:54:45 +04:00
|
|
|
const nsCSSValue::Array * aData,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
RuleNodeCacheConditions& aConditions,
|
2016-02-02 03:45:09 +03:00
|
|
|
TransformReferenceBox& aRefBox,
|
|
|
|
bool* aContains3dTransform)
|
2008-09-13 13:42:11 +04:00
|
|
|
{
|
2016-02-02 03:45:09 +03:00
|
|
|
MOZ_ASSERT(aContains3dTransform);
|
2008-09-13 13:42:11 +04:00
|
|
|
NS_PRECONDITION(aData, "Why did you want to get data from a null array?");
|
2010-07-03 08:18:56 +04: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
|
2014-06-24 10:29:54 +04:00
|
|
|
// StyleAnimationValue does).
|
2010-07-03 08:18:56 +04:00
|
|
|
|
2008-09-13 13:42:11 +04:00
|
|
|
|
|
|
|
/* Get the keyword for the transform. */
|
2010-07-03 08:18:56 +04:00
|
|
|
switch (TransformFunctionOf(aData)) {
|
2008-09-13 13:42:11 +04:00
|
|
|
case eCSSKeyword_translatex:
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessTranslateX(aMatrix, aData, aContext, aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
aConditions, aRefBox);
|
2011-09-27 01:54:45 +04:00
|
|
|
break;
|
2008-09-13 13:42:11 +04:00
|
|
|
case eCSSKeyword_translatey:
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessTranslateY(aMatrix, aData, aContext, aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
aConditions, aRefBox);
|
2011-09-27 01:54:45 +04:00
|
|
|
break;
|
2011-08-03 07:04:19 +04:00
|
|
|
case eCSSKeyword_translatez:
|
2016-02-02 03:45:09 +03:00
|
|
|
*aContains3dTransform = true;
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessTranslateZ(aMatrix, aData, aContext, aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
aConditions);
|
2011-09-27 01:54:45 +04:00
|
|
|
break;
|
2008-09-13 13:42:11 +04:00
|
|
|
case eCSSKeyword_translate:
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessTranslate(aMatrix, aData, aContext, aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
aConditions, aRefBox);
|
2011-09-27 01:54:45 +04:00
|
|
|
break;
|
2011-08-03 07:04:19 +04:00
|
|
|
case eCSSKeyword_translate3d:
|
2016-02-02 03:45:09 +03:00
|
|
|
*aContains3dTransform = true;
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessTranslate3D(aMatrix, aData, aContext, aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
aConditions, aRefBox);
|
2011-09-27 01:54:45 +04:00
|
|
|
break;
|
2008-09-13 13:42:11 +04:00
|
|
|
case eCSSKeyword_scalex:
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessScaleX(aMatrix, aData);
|
|
|
|
break;
|
2008-09-13 13:42:11 +04:00
|
|
|
case eCSSKeyword_scaley:
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessScaleY(aMatrix, aData);
|
|
|
|
break;
|
2011-08-03 07:04:19 +04:00
|
|
|
case eCSSKeyword_scalez:
|
2016-02-02 03:45:09 +03:00
|
|
|
*aContains3dTransform = true;
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessScaleZ(aMatrix, aData);
|
|
|
|
break;
|
2008-09-13 13:42:11 +04:00
|
|
|
case eCSSKeyword_scale:
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessScale(aMatrix, aData);
|
|
|
|
break;
|
2011-08-03 07:04:19 +04:00
|
|
|
case eCSSKeyword_scale3d:
|
2016-02-02 03:45:09 +03:00
|
|
|
*aContains3dTransform = true;
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessScale3D(aMatrix, aData);
|
|
|
|
break;
|
2008-09-13 13:42:11 +04:00
|
|
|
case eCSSKeyword_skewx:
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessSkewX(aMatrix, aData);
|
|
|
|
break;
|
2008-09-13 13:42:11 +04:00
|
|
|
case eCSSKeyword_skewy:
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessSkewY(aMatrix, aData);
|
|
|
|
break;
|
2012-07-30 17:48:02 +04:00
|
|
|
case eCSSKeyword_skew:
|
|
|
|
ProcessSkew(aMatrix, aData);
|
|
|
|
break;
|
2011-08-03 07:04:19 +04:00
|
|
|
case eCSSKeyword_rotatex:
|
2016-02-02 03:45:09 +03:00
|
|
|
*aContains3dTransform = true;
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessRotateX(aMatrix, aData);
|
|
|
|
break;
|
2011-08-03 07:04:19 +04:00
|
|
|
case eCSSKeyword_rotatey:
|
2016-02-02 03:45:09 +03:00
|
|
|
*aContains3dTransform = true;
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessRotateY(aMatrix, aData);
|
|
|
|
break;
|
2011-08-03 07:04:19 +04:00
|
|
|
case eCSSKeyword_rotatez:
|
2016-02-02 03:45:09 +03:00
|
|
|
*aContains3dTransform = true;
|
2016-02-11 10:16:29 +03:00
|
|
|
MOZ_FALLTHROUGH;
|
2008-09-13 13:42:11 +04:00
|
|
|
case eCSSKeyword_rotate:
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessRotateZ(aMatrix, aData);
|
|
|
|
break;
|
2011-08-03 07:04:19 +04:00
|
|
|
case eCSSKeyword_rotate3d:
|
2016-02-02 03:45:09 +03:00
|
|
|
*aContains3dTransform = true;
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessRotate3D(aMatrix, aData);
|
|
|
|
break;
|
2008-09-13 13:42:11 +04:00
|
|
|
case eCSSKeyword_matrix:
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessMatrix(aMatrix, aData, aContext, aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
aConditions, aRefBox);
|
2011-09-27 01:54:45 +04:00
|
|
|
break;
|
2011-08-03 07:04:19 +04:00
|
|
|
case eCSSKeyword_matrix3d:
|
2016-02-02 03:45:09 +03:00
|
|
|
*aContains3dTransform = true;
|
2011-09-27 01:54:45 +04:00
|
|
|
ProcessMatrix3D(aMatrix, aData, aContext, aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
aConditions, aRefBox);
|
2011-09-27 01:54:45 +04:00
|
|
|
break;
|
2011-07-23 02:28:07 +04:00
|
|
|
case eCSSKeyword_interpolatematrix:
|
2016-11-16 14:32:32 +03:00
|
|
|
ProcessMatrixOperator<Interpolate>(aMatrix, aData, aContext, aPresContext,
|
|
|
|
aConditions, aRefBox,
|
|
|
|
aContains3dTransform);
|
2011-09-27 01:54:45 +04:00
|
|
|
break;
|
2016-11-16 14:32:33 +03:00
|
|
|
case eCSSKeyword_accumulatematrix:
|
|
|
|
ProcessMatrixOperator<Accumulate>(aMatrix, aData, aContext, aPresContext,
|
|
|
|
aConditions, aRefBox,
|
|
|
|
aContains3dTransform);
|
|
|
|
break;
|
2011-08-03 07:04:22 +04:00
|
|
|
case eCSSKeyword_perspective:
|
2016-02-02 03:45:09 +03:00
|
|
|
*aContains3dTransform = true;
|
2016-10-04 10:00:31 +03:00
|
|
|
ProcessPerspective(aMatrix, aData, aContext, aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
aConditions);
|
2011-09-27 01:54:45 +04:00
|
|
|
break;
|
2008-09-13 13:42:11 +04:00
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown transform function!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
/**
|
|
|
|
* Return the transform function, as an nsCSSKeyword, for the given
|
|
|
|
* nsCSSValue::Array from a transform list.
|
|
|
|
*/
|
|
|
|
nsCSSKeyword
|
|
|
|
TransformFunctionOf(const nsCSSValue::Array* aData)
|
|
|
|
{
|
2013-05-23 05:08:29 +04:00
|
|
|
MOZ_ASSERT(aData->Item(0).GetUnit() == eCSSUnit_Enumerated);
|
|
|
|
return aData->Item(0).GetKeywordValue();
|
2011-09-27 01:54:45 +04:00
|
|
|
}
|
|
|
|
|
2016-10-03 12:43:20 +03:00
|
|
|
void
|
|
|
|
SetIdentityMatrix(nsCSSValue::Array* aMatrix)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aMatrix, "aMatrix should be non-null");
|
|
|
|
|
|
|
|
nsCSSKeyword tfunc = TransformFunctionOf(aMatrix);
|
|
|
|
MOZ_ASSERT(tfunc == eCSSKeyword_matrix ||
|
|
|
|
tfunc == eCSSKeyword_matrix3d,
|
|
|
|
"Only accept matrix and matrix3d");
|
|
|
|
|
|
|
|
if (tfunc == eCSSKeyword_matrix) {
|
|
|
|
MOZ_ASSERT(aMatrix->Count() == 7, "Invalid matrix");
|
|
|
|
Matrix m;
|
|
|
|
for (size_t i = 0; i < 6; ++i) {
|
|
|
|
aMatrix->Item(i + 1).SetFloatValue(m.components[i], eCSSUnit_Number);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aMatrix->Count() == 17, "Invalid matrix3d");
|
|
|
|
Matrix4x4 m;
|
|
|
|
for (size_t i = 0; i < 16; ++i) {
|
|
|
|
aMatrix->Item(i + 1).SetFloatValue(m.components[i], eCSSUnit_Number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-11 03:05:47 +03:00
|
|
|
Matrix4x4
|
2011-09-27 01:54:45 +04:00
|
|
|
ReadTransforms(const nsCSSValueList* aList,
|
|
|
|
nsStyleContext* aContext,
|
|
|
|
nsPresContext* aPresContext,
|
2015-06-23 04:48:18 +03:00
|
|
|
RuleNodeCacheConditions& aConditions,
|
2015-04-28 21:55:42 +03:00
|
|
|
TransformReferenceBox& aRefBox,
|
2016-02-02 03:45:09 +03:00
|
|
|
float aAppUnitsPerMatrixUnit,
|
|
|
|
bool* aContains3dTransform)
|
2010-07-03 08:18:56 +04:00
|
|
|
{
|
2015-07-11 03:05:47 +03:00
|
|
|
Matrix4x4 result;
|
2010-07-03 08:18:56 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
for (const nsCSSValueList* curr = aList; curr != nullptr; curr = curr->mNext) {
|
2010-07-03 08:18:56 +04:00
|
|
|
const nsCSSValue &currElem = curr->mValue;
|
2015-04-17 04:13:15 +03:00
|
|
|
if (currElem.GetUnit() != eCSSUnit_Function) {
|
|
|
|
NS_ASSERTION(currElem.GetUnit() == eCSSUnit_None &&
|
|
|
|
!aList->mNext,
|
|
|
|
"stream should either be a list of functions or a "
|
|
|
|
"lone None");
|
|
|
|
continue;
|
|
|
|
}
|
2010-07-03 08:18:56 +04:00
|
|
|
NS_ASSERTION(currElem.GetArrayValue()->Count() >= 1,
|
|
|
|
"Incoming function is too short!");
|
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
/* Read in a single transform matrix. */
|
|
|
|
MatrixForTransformFunction(result, currElem.GetArrayValue(), aContext,
|
2016-02-02 03:45:09 +03:00
|
|
|
aPresContext, aConditions, aRefBox,
|
|
|
|
aContains3dTransform);
|
2010-07-03 08:18:56 +04:00
|
|
|
}
|
2014-05-08 09:35:00 +04:00
|
|
|
|
|
|
|
float scale = float(nsPresContext::AppUnitsPerCSSPixel()) / aAppUnitsPerMatrixUnit;
|
2015-07-11 03:05:47 +03:00
|
|
|
result.PreScale(1/scale, 1/scale, 1/scale);
|
|
|
|
result.PostScale(scale, scale, scale);
|
2016-02-02 03:45:09 +03:00
|
|
|
|
2011-07-23 02:28:51 +04:00
|
|
|
return result;
|
2010-07-03 08:18:56 +04:00
|
|
|
}
|
|
|
|
|
2016-10-04 10:00:31 +03:00
|
|
|
/*
|
|
|
|
* The relevant section of the transitions specification:
|
|
|
|
* http://dev.w3.org/csswg/css3-transitions/#animation-of-property-types-
|
|
|
|
* defers all of the details to the 2-D and 3-D transforms specifications.
|
|
|
|
* For the 2-D transforms specification (all that's relevant for us, right
|
|
|
|
* now), the relevant section is:
|
|
|
|
* http://dev.w3.org/csswg/css3-2d-transforms/#animation
|
|
|
|
* This, in turn, refers to the unmatrix program in Graphics Gems,
|
|
|
|
* available from http://tog.acm.org/resources/GraphicsGems/ , and in
|
|
|
|
* particular as the file GraphicsGems/gemsii/unmatrix.c
|
|
|
|
* in http://tog.acm.org/resources/GraphicsGems/AllGems.tar.gz
|
|
|
|
*
|
|
|
|
* The unmatrix reference is for general 3-D transform matrices (any of the
|
|
|
|
* 16 components can have any value).
|
|
|
|
*
|
|
|
|
* For CSS 2-D transforms, we have a 2-D matrix with the bottom row constant:
|
|
|
|
*
|
|
|
|
* [ A C E ]
|
|
|
|
* [ B D F ]
|
|
|
|
* [ 0 0 1 ]
|
|
|
|
*
|
|
|
|
* For that case, I believe the algorithm in unmatrix reduces to:
|
|
|
|
*
|
|
|
|
* (1) If A * D - B * C == 0, the matrix is singular. Fail.
|
|
|
|
*
|
|
|
|
* (2) Set translation components (Tx and Ty) to the translation parts of
|
|
|
|
* the matrix (E and F) and then ignore them for the rest of the time.
|
|
|
|
* (For us, E and F each actually consist of three constants: a
|
|
|
|
* length, a multiplier for the width, and a multiplier for the
|
|
|
|
* height. This actually requires its own decomposition, but I'll
|
|
|
|
* keep that separate.)
|
|
|
|
*
|
|
|
|
* (3) Let the X scale (Sx) be sqrt(A^2 + B^2). Then divide both A and B
|
|
|
|
* by it.
|
|
|
|
*
|
|
|
|
* (4) Let the XY shear (K) be A * C + B * D. From C, subtract A times
|
|
|
|
* the XY shear. From D, subtract B times the XY shear.
|
|
|
|
*
|
|
|
|
* (5) Let the Y scale (Sy) be sqrt(C^2 + D^2). Divide C, D, and the XY
|
|
|
|
* shear (K) by it.
|
|
|
|
*
|
|
|
|
* (6) At this point, A * D - B * C is either 1 or -1. If it is -1,
|
|
|
|
* negate the XY shear (K), the X scale (Sx), and A, B, C, and D.
|
|
|
|
* (Alternatively, we could negate the XY shear (K) and the Y scale
|
|
|
|
* (Sy).)
|
|
|
|
*
|
|
|
|
* (7) Let the rotation be R = atan2(B, A).
|
|
|
|
*
|
|
|
|
* Then the resulting decomposed transformation is:
|
|
|
|
*
|
|
|
|
* translate(Tx, Ty) rotate(R) skewX(atan(K)) scale(Sx, Sy)
|
|
|
|
*
|
|
|
|
* An interesting result of this is that all of the simple transform
|
|
|
|
* functions (i.e., all functions other than matrix()), in isolation,
|
|
|
|
* decompose back to themselves except for:
|
|
|
|
* 'skewY(φ)', which is 'matrix(1, tan(φ), 0, 1, 0, 0)', which decomposes
|
|
|
|
* to 'rotate(φ) skewX(φ) scale(sec(φ), cos(φ))' since (ignoring the
|
|
|
|
* alternate sign possibilities that would get fixed in step 6):
|
|
|
|
* In step 3, the X scale factor is sqrt(1+tan²(φ)) = sqrt(sec²(φ)) = sec(φ).
|
|
|
|
* Thus, after step 3, A = 1/sec(φ) = cos(φ) and B = tan(φ) / sec(φ) = sin(φ).
|
|
|
|
* In step 4, the XY shear is sin(φ).
|
|
|
|
* Thus, after step 4, C = -cos(φ)sin(φ) and D = 1 - sin²(φ) = cos²(φ).
|
|
|
|
* Thus, in step 5, the Y scale is sqrt(cos²(φ)(sin²(φ) + cos²(φ)) = cos(φ).
|
|
|
|
* Thus, after step 5, C = -sin(φ), D = cos(φ), and the XY shear is tan(φ).
|
|
|
|
* Thus, in step 6, A * D - B * C = cos²(φ) + sin²(φ) = 1.
|
|
|
|
* In step 7, the rotation is thus φ.
|
|
|
|
*
|
|
|
|
* skew(θ, φ), which is matrix(1, tan(φ), tan(θ), 1, 0, 0), which decomposes
|
|
|
|
* to 'rotate(φ) skewX(θ + φ) scale(sec(φ), cos(φ))' since (ignoring
|
|
|
|
* the alternate sign possibilities that would get fixed in step 6):
|
|
|
|
* In step 3, the X scale factor is sqrt(1+tan²(φ)) = sqrt(sec²(φ)) = sec(φ).
|
|
|
|
* Thus, after step 3, A = 1/sec(φ) = cos(φ) and B = tan(φ) / sec(φ) = sin(φ).
|
|
|
|
* In step 4, the XY shear is cos(φ)tan(θ) + sin(φ).
|
|
|
|
* Thus, after step 4,
|
|
|
|
* C = tan(θ) - cos(φ)(cos(φ)tan(θ) + sin(φ)) = tan(θ)sin²(φ) - cos(φ)sin(φ)
|
|
|
|
* D = 1 - sin(φ)(cos(φ)tan(θ) + sin(φ)) = cos²(φ) - sin(φ)cos(φ)tan(θ)
|
|
|
|
* Thus, in step 5, the Y scale is sqrt(C² + D²) =
|
|
|
|
* sqrt(tan²(θ)(sin⁴(φ) + sin²(φ)cos²(φ)) -
|
|
|
|
* 2 tan(θ)(sin³(φ)cos(φ) + sin(φ)cos³(φ)) +
|
|
|
|
* (sin²(φ)cos²(φ) + cos⁴(φ))) =
|
|
|
|
* sqrt(tan²(θ)sin²(φ) - 2 tan(θ)sin(φ)cos(φ) + cos²(φ)) =
|
|
|
|
* cos(φ) - tan(θ)sin(φ) (taking the negative of the obvious solution so
|
|
|
|
* we avoid flipping in step 6).
|
|
|
|
* After step 5, C = -sin(φ) and D = cos(φ), and the XY shear is
|
|
|
|
* (cos(φ)tan(θ) + sin(φ)) / (cos(φ) - tan(θ)sin(φ)) =
|
|
|
|
* (dividing both numerator and denominator by cos(φ))
|
|
|
|
* (tan(θ) + tan(φ)) / (1 - tan(θ)tan(φ)) = tan(θ + φ).
|
|
|
|
* (See http://en.wikipedia.org/wiki/List_of_trigonometric_identities .)
|
|
|
|
* Thus, in step 6, A * D - B * C = cos²(φ) + sin²(φ) = 1.
|
|
|
|
* In step 7, the rotation is thus φ.
|
|
|
|
*
|
|
|
|
* To check this result, we can multiply things back together:
|
|
|
|
*
|
|
|
|
* [ cos(φ) -sin(φ) ] [ 1 tan(θ + φ) ] [ sec(φ) 0 ]
|
|
|
|
* [ sin(φ) cos(φ) ] [ 0 1 ] [ 0 cos(φ) ]
|
|
|
|
*
|
|
|
|
* [ cos(φ) cos(φ)tan(θ + φ) - sin(φ) ] [ sec(φ) 0 ]
|
|
|
|
* [ sin(φ) sin(φ)tan(θ + φ) + cos(φ) ] [ 0 cos(φ) ]
|
|
|
|
*
|
|
|
|
* but since tan(θ + φ) = (tan(θ) + tan(φ)) / (1 - tan(θ)tan(φ)),
|
|
|
|
* cos(φ)tan(θ + φ) - sin(φ)
|
|
|
|
* = cos(φ)(tan(θ) + tan(φ)) - sin(φ) + sin(φ)tan(θ)tan(φ)
|
|
|
|
* = cos(φ)tan(θ) + sin(φ) - sin(φ) + sin(φ)tan(θ)tan(φ)
|
|
|
|
* = cos(φ)tan(θ) + sin(φ)tan(θ)tan(φ)
|
|
|
|
* = tan(θ) (cos(φ) + sin(φ)tan(φ))
|
|
|
|
* = tan(θ) sec(φ) (cos²(φ) + sin²(φ))
|
|
|
|
* = tan(θ) sec(φ)
|
|
|
|
* and
|
|
|
|
* sin(φ)tan(θ + φ) + cos(φ)
|
|
|
|
* = sin(φ)(tan(θ) + tan(φ)) + cos(φ) - cos(φ)tan(θ)tan(φ)
|
|
|
|
* = tan(θ) (sin(φ) - sin(φ)) + sin(φ)tan(φ) + cos(φ)
|
|
|
|
* = sec(φ) (sin²(φ) + cos²(φ))
|
|
|
|
* = sec(φ)
|
|
|
|
* so the above is:
|
|
|
|
* [ cos(φ) tan(θ) sec(φ) ] [ sec(φ) 0 ]
|
|
|
|
* [ sin(φ) sec(φ) ] [ 0 cos(φ) ]
|
|
|
|
*
|
|
|
|
* [ 1 tan(θ) ]
|
|
|
|
* [ tan(φ) 1 ]
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decompose2DMatrix implements the above decomposition algorithm.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool
|
|
|
|
Decompose2DMatrix(const Matrix& aMatrix,
|
|
|
|
Point3D& aScale,
|
2016-10-12 07:36:58 +03:00
|
|
|
ShearArray& aShear,
|
2016-10-04 10:00:31 +03:00
|
|
|
gfxQuaternion& aRotate,
|
|
|
|
Point3D& aTranslate)
|
|
|
|
{
|
|
|
|
float A = aMatrix._11,
|
|
|
|
B = aMatrix._12,
|
|
|
|
C = aMatrix._21,
|
|
|
|
D = aMatrix._22;
|
|
|
|
if (A * D == B * C) {
|
|
|
|
// singular matrix
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float scaleX = sqrt(A * A + B * B);
|
|
|
|
A /= scaleX;
|
|
|
|
B /= scaleX;
|
|
|
|
|
|
|
|
float XYshear = A * C + B * D;
|
|
|
|
C -= A * XYshear;
|
|
|
|
D -= B * XYshear;
|
|
|
|
|
|
|
|
float scaleY = sqrt(C * C + D * D);
|
|
|
|
C /= scaleY;
|
|
|
|
D /= scaleY;
|
|
|
|
XYshear /= scaleY;
|
|
|
|
|
|
|
|
// A*D - B*C should now be 1 or -1
|
|
|
|
NS_ASSERTION(0.99 < Abs(A*D - B*C) && Abs(A*D - B*C) < 1.01,
|
|
|
|
"determinant should now be 1 or -1");
|
|
|
|
if (A * D < B * C) {
|
|
|
|
A = -A;
|
|
|
|
B = -B;
|
|
|
|
C = -C;
|
|
|
|
D = -D;
|
|
|
|
XYshear = -XYshear;
|
|
|
|
scaleX = -scaleX;
|
|
|
|
}
|
|
|
|
|
|
|
|
float rotate = atan2f(B, A);
|
|
|
|
aRotate = gfxQuaternion(0, 0, sin(rotate/2), cos(rotate/2));
|
2016-10-12 07:36:58 +03:00
|
|
|
aShear[ShearType::XYSHEAR] = XYshear;
|
2016-10-04 10:00:31 +03:00
|
|
|
aScale.x = scaleX;
|
|
|
|
aScale.y = scaleY;
|
|
|
|
aTranslate.x = aMatrix._31;
|
|
|
|
aTranslate.y = aMatrix._32;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of the unmatrix algorithm, specified by:
|
|
|
|
*
|
|
|
|
* http://dev.w3.org/csswg/css3-2d-transforms/#unmatrix
|
|
|
|
*
|
|
|
|
* This, in turn, refers to the unmatrix program in Graphics Gems,
|
|
|
|
* available from http://tog.acm.org/resources/GraphicsGems/ , and in
|
|
|
|
* particular as the file GraphicsGems/gemsii/unmatrix.c
|
|
|
|
* in http://tog.acm.org/resources/GraphicsGems/AllGems.tar.gz
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
Decompose3DMatrix(const Matrix4x4& aMatrix,
|
|
|
|
Point3D& aScale,
|
2016-10-12 07:36:58 +03:00
|
|
|
ShearArray& aShear,
|
2016-10-04 10:00:31 +03:00
|
|
|
gfxQuaternion& aRotate,
|
|
|
|
Point3D& aTranslate,
|
|
|
|
Point4D& aPerspective)
|
|
|
|
{
|
|
|
|
Matrix4x4 local = aMatrix;
|
|
|
|
|
|
|
|
if (local[3][3] == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* Normalize the matrix */
|
|
|
|
local.Normalize();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* perspective is used to solve for perspective, but it also provides
|
|
|
|
* an easy way to test for singularity of the upper 3x3 component.
|
|
|
|
*/
|
|
|
|
Matrix4x4 perspective = local;
|
|
|
|
Point4D empty(0, 0, 0, 1);
|
|
|
|
perspective.SetTransposedVector(3, empty);
|
|
|
|
|
|
|
|
if (perspective.Determinant() == 0.0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First, isolate perspective. */
|
|
|
|
if (local[0][3] != 0 || local[1][3] != 0 ||
|
|
|
|
local[2][3] != 0) {
|
|
|
|
/* aPerspective is the right hand side of the equation. */
|
|
|
|
aPerspective = local.TransposedVector(3);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Solve the equation by inverting perspective and multiplying
|
|
|
|
* aPerspective by the inverse.
|
|
|
|
*/
|
|
|
|
perspective.Invert();
|
|
|
|
aPerspective = perspective.TransposeTransform4D(aPerspective);
|
|
|
|
|
|
|
|
/* Clear the perspective partition */
|
|
|
|
local.SetTransposedVector(3, empty);
|
|
|
|
} else {
|
|
|
|
aPerspective = Point4D(0, 0, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next take care of translation */
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
aTranslate[i] = local[3][i];
|
|
|
|
local[3][i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now get scale and shear. */
|
|
|
|
|
|
|
|
/* Compute X scale factor and normalize first row. */
|
|
|
|
aScale.x = local[0].Length();
|
|
|
|
local[0] /= aScale.x;
|
|
|
|
|
|
|
|
/* Compute XY shear factor and make 2nd local orthogonal to 1st. */
|
2016-10-12 07:36:58 +03:00
|
|
|
aShear[ShearType::XYSHEAR] = local[0].DotProduct(local[1]);
|
|
|
|
local[1] -= local[0] * aShear[ShearType::XYSHEAR];
|
2016-10-04 10:00:31 +03:00
|
|
|
|
|
|
|
/* Now, compute Y scale and normalize 2nd local. */
|
|
|
|
aScale.y = local[1].Length();
|
|
|
|
local[1] /= aScale.y;
|
2016-10-12 07:36:58 +03:00
|
|
|
aShear[ShearType::XYSHEAR] /= aScale.y;
|
2016-10-04 10:00:31 +03:00
|
|
|
|
|
|
|
/* Compute XZ and YZ shears, make 3rd local orthogonal */
|
2016-10-12 07:36:58 +03:00
|
|
|
aShear[ShearType::XZSHEAR] = local[0].DotProduct(local[2]);
|
|
|
|
local[2] -= local[0] * aShear[ShearType::XZSHEAR];
|
|
|
|
aShear[ShearType::YZSHEAR] = local[1].DotProduct(local[2]);
|
|
|
|
local[2] -= local[1] * aShear[ShearType::YZSHEAR];
|
2016-10-04 10:00:31 +03:00
|
|
|
|
|
|
|
/* Next, get Z scale and normalize 3rd local. */
|
|
|
|
aScale.z = local[2].Length();
|
|
|
|
local[2] /= aScale.z;
|
|
|
|
|
2016-10-12 07:36:58 +03:00
|
|
|
aShear[ShearType::XZSHEAR] /= aScale.z;
|
|
|
|
aShear[ShearType::YZSHEAR] /= aScale.z;
|
2016-10-04 10:00:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* At this point, the matrix (in locals) is orthonormal.
|
|
|
|
* Check for a coordinate system flip. If the determinant
|
|
|
|
* is -1, then negate the matrix and the scaling factors.
|
|
|
|
*/
|
|
|
|
if (local[0].DotProduct(local[1].CrossProduct(local[2])) < 0) {
|
|
|
|
aScale *= -1;
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
local[i] *= -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, get the rotations out */
|
|
|
|
aRotate = gfxQuaternion(local);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-04 13:32:20 +03:00
|
|
|
Matrix
|
|
|
|
CSSValueArrayTo2DMatrix(nsCSSValue::Array* aArray)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aArray &&
|
|
|
|
TransformFunctionOf(aArray) == eCSSKeyword_matrix &&
|
|
|
|
aArray->Count() == 7);
|
|
|
|
Matrix m(aArray->Item(1).GetFloatValue(),
|
|
|
|
aArray->Item(2).GetFloatValue(),
|
|
|
|
aArray->Item(3).GetFloatValue(),
|
|
|
|
aArray->Item(4).GetFloatValue(),
|
|
|
|
aArray->Item(5).GetFloatValue(),
|
|
|
|
aArray->Item(6).GetFloatValue());
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix4x4
|
|
|
|
CSSValueArrayTo3DMatrix(nsCSSValue::Array* aArray)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aArray &&
|
|
|
|
TransformFunctionOf(aArray) == eCSSKeyword_matrix3d &&
|
|
|
|
aArray->Count() == 17);
|
|
|
|
gfx::Float array[16];
|
|
|
|
for (size_t i = 0; i < 16; ++i) {
|
|
|
|
array[i] = aArray->Item(i+1).GetFloatValue();
|
|
|
|
}
|
|
|
|
Matrix4x4 m(array);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2017-02-06 11:32:21 +03:00
|
|
|
gfxSize
|
|
|
|
GetScaleValue(const nsCSSValueSharedList* aList,
|
|
|
|
const nsIFrame* aForFrame)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aList && aList->mHead);
|
|
|
|
MOZ_ASSERT(aForFrame);
|
|
|
|
|
|
|
|
RuleNodeCacheConditions dontCare;
|
|
|
|
bool dontCareBool;
|
|
|
|
TransformReferenceBox refBox(aForFrame);
|
|
|
|
Matrix4x4 transform = ReadTransforms(
|
|
|
|
aList->mHead,
|
|
|
|
aForFrame->StyleContext(),
|
|
|
|
aForFrame->PresContext(), dontCare, refBox,
|
|
|
|
aForFrame->PresContext()->AppUnitsPerDevPixel(),
|
|
|
|
&dontCareBool);
|
|
|
|
Matrix transform2d;
|
|
|
|
bool canDraw2D = transform.CanDraw2D(&transform2d);
|
|
|
|
if (!canDraw2D) {
|
|
|
|
return gfxSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ThebesMatrix(transform2d).ScaleFactors(true);
|
|
|
|
}
|
|
|
|
|
2011-09-27 01:54:45 +04:00
|
|
|
} // namespace nsStyleTransformMatrix
|