gecko/dom/svg/nsSVGTransform.h
2015-05-03 15:32:37 -04:00

179 lines
5.3 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#ifndef MOZILLA_SVGTRANSFORM_H__
#define MOZILLA_SVGTRANSFORM_H__
#include "gfxMatrix.h"
#include "nsDebug.h"
namespace mozilla {
// Transform Types
static const unsigned short SVG_TRANSFORM_UNKNOWN = 0;
static const unsigned short SVG_TRANSFORM_MATRIX = 1;
static const unsigned short SVG_TRANSFORM_TRANSLATE = 2;
static const unsigned short SVG_TRANSFORM_SCALE = 3;
static const unsigned short SVG_TRANSFORM_ROTATE = 4;
static const unsigned short SVG_TRANSFORM_SKEWX = 5;
static const unsigned short SVG_TRANSFORM_SKEWY = 6;
/*
* The DOM wrapper class for this class is DOMSVGTransformMatrix.
*/
class nsSVGTransform
{
public:
// Default ctor initialises to matrix type with identity matrix
nsSVGTransform()
: mMatrix() // Initialises to identity
, mAngle(0.f)
, mOriginX(0.f)
, mOriginY(0.f)
, mType(SVG_TRANSFORM_MATRIX)
{ }
explicit nsSVGTransform(const gfxMatrix& aMatrix)
: mMatrix(aMatrix)
, mAngle(0.f)
, mOriginX(0.f)
, mOriginY(0.f)
, mType(SVG_TRANSFORM_MATRIX)
{ }
bool operator==(const nsSVGTransform& rhs) const {
return mType == rhs.mType &&
MatricesEqual(mMatrix, rhs.mMatrix) &&
mAngle == rhs.mAngle &&
mOriginX == rhs.mOriginX &&
mOriginY == rhs.mOriginY;
}
void GetValueAsString(nsAString& aValue) const;
float Angle() const {
return mAngle;
}
void GetRotationOrigin(float& aOriginX, float& aOriginY) const {
aOriginX = mOriginX;
aOriginY = mOriginY;
}
uint16_t Type() const {
return mType;
}
const gfxMatrix& GetMatrix() const { return mMatrix; }
void SetMatrix(const gfxMatrix& aMatrix);
void SetTranslate(float aTx, float aTy);
void SetScale(float aSx, float aSy);
void SetRotate(float aAngle, float aCx, float aCy);
nsresult SetSkewX(float aAngle);
nsresult SetSkewY(float aAngle);
static bool MatricesEqual(const gfxMatrix& a, const gfxMatrix& b)
{
return a._11 == b._11 &&
a._12 == b._12 &&
a._21 == b._21 &&
a._22 == b._22 &&
a._31 == b._31 &&
a._32 == b._32;
}
protected:
gfxMatrix mMatrix;
float mAngle, mOriginX, mOriginY;
uint16_t mType;
};
/*
* A slightly more light-weight version of nsSVGTransform for SMIL animation.
*
* Storing the parameters in an array (rather than a matrix) also allows simpler
* (transform type-agnostic) interpolation and addition.
*
* The meaning of the mParams array depends on the transform type as follows:
*
* Type | mParams[0], mParams[1], mParams[2], ...
* --------------------+-----------------------------------------
* translate | tx, ty
* scale | sx, sy
* rotate | rotation-angle (in degrees), cx, cy
* skewX | skew-angle (in degrees)
* skewY | skew-angle (in degrees)
* matrix | a, b, c, d, e, f
*
* The matrix type is never generated by animation code (it is only produced
* when the user inserts one via the DOM) and often requires special handling
* when we do encounter it. Therefore many users of this class are only
* interested in the first three parameters and so we provide a special
* constructor for setting those parameters only.
*/
class SVGTransformSMILData
{
public:
// Number of float-params required in constructor, if constructing one of the
// 'simple' transform types (all but matrix type)
static const uint32_t NUM_SIMPLE_PARAMS = 3;
// Number of float-params required in constructor for matrix type.
// This is also the number of params we actually store, regardless of type.
static const uint32_t NUM_STORED_PARAMS = 6;
explicit SVGTransformSMILData(uint16_t aType)
: mTransformType(aType)
{
MOZ_ASSERT(aType >= SVG_TRANSFORM_MATRIX && aType <= SVG_TRANSFORM_SKEWY,
"Unexpected transform type");
for (uint32_t i = 0; i < NUM_STORED_PARAMS; ++i) {
mParams[i] = 0.f;
}
}
SVGTransformSMILData(uint16_t aType, float (&aParams)[NUM_SIMPLE_PARAMS])
: mTransformType(aType)
{
MOZ_ASSERT(aType >= SVG_TRANSFORM_TRANSLATE && aType <= SVG_TRANSFORM_SKEWY,
"Expected 'simple' transform type");
for (uint32_t i = 0; i < NUM_SIMPLE_PARAMS; ++i) {
mParams[i] = aParams[i];
}
for (uint32_t i = NUM_SIMPLE_PARAMS; i < NUM_STORED_PARAMS; ++i) {
mParams[i] = 0.f;
}
}
// Conversion to/from a fully-fledged nsSVGTransform
explicit SVGTransformSMILData(const nsSVGTransform& aTransform);
nsSVGTransform ToSVGTransform() const;
bool operator==(const SVGTransformSMILData& aOther) const
{
if (mTransformType != aOther.mTransformType)
return false;
for (uint32_t i = 0; i < NUM_STORED_PARAMS; ++i) {
if (mParams[i] != aOther.mParams[i]) {
return false;
}
}
return true;
}
bool operator!=(const SVGTransformSMILData& aOther) const
{
return !(*this == aOther);
}
uint16_t mTransformType;
float mParams[NUM_STORED_PARAMS];
};
} // namespace mozilla
#endif // MOZILLA_SVGTRANSFORM_H__