2011-09-25 14:04:27 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 04:12:37 -07: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/. */
|
2011-09-25 14:04:27 -07:00
|
|
|
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2013-04-02 12:17:41 -07:00
|
|
|
#include "nsSVGTransform.h"
|
2013-08-13 23:56:21 -07:00
|
|
|
#include "nsContentUtils.h" // for NS_ENSURE_FINITE
|
2011-09-25 14:04:27 -07:00
|
|
|
#include "nsTextFormatter.h"
|
|
|
|
|
|
|
|
namespace {
|
2013-12-25 00:33:48 -08:00
|
|
|
const double kRadPerDegree = 2.0 * M_PI / 360.0;
|
2011-09-25 14:04:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
void
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform::GetValueAsString(nsAString& aValue) const
|
2011-09-25 14:04:27 -07:00
|
|
|
{
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t buf[256];
|
2011-09-25 14:04:27 -07:00
|
|
|
|
|
|
|
switch (mType) {
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_TRANSLATE:
|
2012-03-18 11:24:35 -07:00
|
|
|
// The spec say that if Y is not provided, it is assumed to be zero.
|
2014-06-17 10:35:51 -07:00
|
|
|
if (mMatrix._32 != 0)
|
2014-01-04 07:02:17 -08:00
|
|
|
nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
|
2013-12-12 17:50:01 -08:00
|
|
|
MOZ_UTF16("translate(%g, %g)"),
|
2014-06-17 10:35:51 -07:00
|
|
|
mMatrix._31, mMatrix._32);
|
2011-09-25 14:04:27 -07:00
|
|
|
else
|
2014-01-04 07:02:17 -08:00
|
|
|
nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
|
2013-12-12 17:50:01 -08:00
|
|
|
MOZ_UTF16("translate(%g)"),
|
2014-06-17 10:35:51 -07:00
|
|
|
mMatrix._31);
|
2011-09-25 14:04:27 -07:00
|
|
|
break;
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_ROTATE:
|
2011-09-25 14:04:27 -07:00
|
|
|
if (mOriginX != 0.0f || mOriginY != 0.0f)
|
2014-01-04 07:02:17 -08:00
|
|
|
nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
|
2013-12-12 17:50:01 -08:00
|
|
|
MOZ_UTF16("rotate(%g, %g, %g)"),
|
2011-09-25 14:04:27 -07:00
|
|
|
mAngle, mOriginX, mOriginY);
|
|
|
|
else
|
2014-01-04 07:02:17 -08:00
|
|
|
nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
|
2013-12-12 17:50:01 -08:00
|
|
|
MOZ_UTF16("rotate(%g)"), mAngle);
|
2011-09-25 14:04:27 -07:00
|
|
|
break;
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_SCALE:
|
2014-06-17 10:35:51 -07:00
|
|
|
if (mMatrix._11 != mMatrix._22)
|
2014-01-04 07:02:17 -08:00
|
|
|
nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
|
2014-06-17 10:35:51 -07:00
|
|
|
MOZ_UTF16("scale(%g, %g)"), mMatrix._11, mMatrix._22);
|
2011-09-25 14:04:27 -07:00
|
|
|
else
|
2014-01-04 07:02:17 -08:00
|
|
|
nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
|
2014-06-17 10:35:51 -07:00
|
|
|
MOZ_UTF16("scale(%g)"), mMatrix._11);
|
2011-09-25 14:04:27 -07:00
|
|
|
break;
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_SKEWX:
|
2014-01-04 07:02:17 -08:00
|
|
|
nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
|
2013-12-12 17:50:01 -08:00
|
|
|
MOZ_UTF16("skewX(%g)"), mAngle);
|
2011-09-25 14:04:27 -07:00
|
|
|
break;
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_SKEWY:
|
2014-01-04 07:02:17 -08:00
|
|
|
nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
|
2013-12-12 17:50:01 -08:00
|
|
|
MOZ_UTF16("skewY(%g)"), mAngle);
|
2011-09-25 14:04:27 -07:00
|
|
|
break;
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_MATRIX:
|
2014-01-04 07:02:17 -08:00
|
|
|
nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
|
2013-12-12 17:50:01 -08:00
|
|
|
MOZ_UTF16("matrix(%g, %g, %g, %g, %g, %g)"),
|
2014-06-17 10:35:51 -07:00
|
|
|
mMatrix._11, mMatrix._12,
|
|
|
|
mMatrix._21, mMatrix._22,
|
|
|
|
mMatrix._31, mMatrix._32);
|
2011-09-25 14:04:27 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buf[0] = '\0';
|
|
|
|
NS_ERROR("unknown transformation type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
aValue.Assign(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform::SetMatrix(const gfxMatrix& aMatrix)
|
2011-09-25 14:04:27 -07:00
|
|
|
{
|
2012-12-22 20:54:24 -08:00
|
|
|
mType = SVG_TRANSFORM_MATRIX;
|
2011-09-25 14:04:27 -07:00
|
|
|
mMatrix = aMatrix;
|
|
|
|
// We set the other members here too, since operator== requires it and
|
|
|
|
// the DOM requires it for mAngle.
|
|
|
|
mAngle = 0.f;
|
|
|
|
mOriginX = 0.f;
|
|
|
|
mOriginY = 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform::SetTranslate(float aTx, float aTy)
|
2011-09-25 14:04:27 -07:00
|
|
|
{
|
2012-12-22 20:54:24 -08:00
|
|
|
mType = SVG_TRANSFORM_TRANSLATE;
|
2011-09-25 14:04:27 -07:00
|
|
|
mMatrix.Reset();
|
2014-06-17 10:35:51 -07:00
|
|
|
mMatrix._31 = aTx;
|
|
|
|
mMatrix._32 = aTy;
|
2011-09-25 14:04:27 -07:00
|
|
|
mAngle = 0.f;
|
|
|
|
mOriginX = 0.f;
|
|
|
|
mOriginY = 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform::SetScale(float aSx, float aSy)
|
2011-09-25 14:04:27 -07:00
|
|
|
{
|
2012-12-22 20:54:24 -08:00
|
|
|
mType = SVG_TRANSFORM_SCALE;
|
2011-09-25 14:04:27 -07:00
|
|
|
mMatrix.Reset();
|
2014-06-17 10:35:51 -07:00
|
|
|
mMatrix._11 = aSx;
|
|
|
|
mMatrix._22 = aSy;
|
2011-09-25 14:04:27 -07:00
|
|
|
mAngle = 0.f;
|
|
|
|
mOriginX = 0.f;
|
|
|
|
mOriginY = 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform::SetRotate(float aAngle, float aCx, float aCy)
|
2011-09-25 14:04:27 -07:00
|
|
|
{
|
2012-12-22 20:54:24 -08:00
|
|
|
mType = SVG_TRANSFORM_ROTATE;
|
2011-09-25 14:04:27 -07:00
|
|
|
mMatrix.Reset();
|
2014-10-03 01:50:44 -07:00
|
|
|
mMatrix.Translate(aCx, aCy);
|
2013-12-25 00:33:48 -08:00
|
|
|
mMatrix.Rotate(aAngle*kRadPerDegree);
|
2014-10-03 01:50:44 -07:00
|
|
|
mMatrix.Translate(-aCx, -aCy);
|
2011-09-25 14:04:27 -07:00
|
|
|
mAngle = aAngle;
|
|
|
|
mOriginX = aCx;
|
|
|
|
mOriginY = aCy;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform::SetSkewX(float aAngle)
|
2011-09-25 14:04:27 -07:00
|
|
|
{
|
2013-12-25 00:33:48 -08:00
|
|
|
double ta = tan(aAngle*kRadPerDegree);
|
2012-06-13 18:12:37 -07:00
|
|
|
NS_ENSURE_FINITE(ta, NS_ERROR_RANGE_ERR);
|
2011-09-25 14:04:27 -07:00
|
|
|
|
2012-12-22 20:54:24 -08:00
|
|
|
mType = SVG_TRANSFORM_SKEWX;
|
2011-09-25 14:04:27 -07:00
|
|
|
mMatrix.Reset();
|
2014-06-17 10:35:51 -07:00
|
|
|
mMatrix._21 = ta;
|
2011-09-25 14:04:27 -07:00
|
|
|
mAngle = aAngle;
|
|
|
|
mOriginX = 0.f;
|
|
|
|
mOriginY = 0.f;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform::SetSkewY(float aAngle)
|
2011-09-25 14:04:27 -07:00
|
|
|
{
|
2013-12-25 00:33:48 -08:00
|
|
|
double ta = tan(aAngle*kRadPerDegree);
|
2012-06-13 18:12:37 -07:00
|
|
|
NS_ENSURE_FINITE(ta, NS_ERROR_RANGE_ERR);
|
2011-09-25 14:04:27 -07:00
|
|
|
|
2012-12-22 20:54:24 -08:00
|
|
|
mType = SVG_TRANSFORM_SKEWY;
|
2011-09-25 14:04:27 -07:00
|
|
|
mMatrix.Reset();
|
2014-06-17 10:35:51 -07:00
|
|
|
mMatrix._12 = ta;
|
2011-09-25 14:04:27 -07:00
|
|
|
mAngle = aAngle;
|
|
|
|
mOriginX = 0.f;
|
|
|
|
mOriginY = 0.f;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-02 12:17:41 -07:00
|
|
|
SVGTransformSMILData::SVGTransformSMILData(const nsSVGTransform& aTransform)
|
2011-09-25 14:04:31 -07:00
|
|
|
: mTransformType(aTransform.Type())
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(
|
2012-12-22 20:54:24 -08:00
|
|
|
mTransformType >= SVG_TRANSFORM_MATRIX &&
|
|
|
|
mTransformType <= SVG_TRANSFORM_SKEWY,
|
2011-09-25 14:04:31 -07:00
|
|
|
"Unexpected transform type");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < NUM_STORED_PARAMS; ++i) {
|
2011-09-25 14:04:31 -07:00
|
|
|
mParams[i] = 0.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mTransformType) {
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_MATRIX: {
|
2014-03-17 19:41:35 -07:00
|
|
|
const gfxMatrix& mx = aTransform.GetMatrix();
|
2014-06-17 10:35:51 -07:00
|
|
|
mParams[0] = static_cast<float>(mx._11);
|
|
|
|
mParams[1] = static_cast<float>(mx._12);
|
|
|
|
mParams[2] = static_cast<float>(mx._21);
|
|
|
|
mParams[3] = static_cast<float>(mx._22);
|
|
|
|
mParams[4] = static_cast<float>(mx._31);
|
|
|
|
mParams[5] = static_cast<float>(mx._32);
|
2011-09-25 14:04:31 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_TRANSLATE: {
|
2014-03-17 19:41:35 -07:00
|
|
|
const gfxMatrix& mx = aTransform.GetMatrix();
|
2014-06-17 10:35:51 -07:00
|
|
|
mParams[0] = static_cast<float>(mx._31);
|
|
|
|
mParams[1] = static_cast<float>(mx._32);
|
2011-09-25 14:04:31 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_SCALE: {
|
2014-03-17 19:41:35 -07:00
|
|
|
const gfxMatrix& mx = aTransform.GetMatrix();
|
2014-06-17 10:35:51 -07:00
|
|
|
mParams[0] = static_cast<float>(mx._11);
|
|
|
|
mParams[1] = static_cast<float>(mx._22);
|
2011-09-25 14:04:31 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_ROTATE:
|
2011-09-25 14:04:31 -07:00
|
|
|
mParams[0] = aTransform.Angle();
|
|
|
|
aTransform.GetRotationOrigin(mParams[1], mParams[2]);
|
|
|
|
break;
|
|
|
|
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_SKEWX:
|
|
|
|
case SVG_TRANSFORM_SKEWY:
|
2011-09-25 14:04:31 -07:00
|
|
|
mParams[0] = aTransform.Angle();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unexpected transform type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform
|
2011-09-25 14:04:31 -07:00
|
|
|
SVGTransformSMILData::ToSVGTransform() const
|
|
|
|
{
|
2013-04-02 12:17:41 -07:00
|
|
|
nsSVGTransform result;
|
2011-09-25 14:04:31 -07:00
|
|
|
|
|
|
|
switch (mTransformType) {
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_MATRIX:
|
2011-09-25 14:04:31 -07:00
|
|
|
result.SetMatrix(gfxMatrix(mParams[0], mParams[1],
|
|
|
|
mParams[2], mParams[3],
|
|
|
|
mParams[4], mParams[5]));
|
|
|
|
break;
|
|
|
|
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_TRANSLATE:
|
2011-09-25 14:04:31 -07:00
|
|
|
result.SetTranslate(mParams[0], mParams[1]);
|
|
|
|
break;
|
|
|
|
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_SCALE:
|
2011-09-25 14:04:31 -07:00
|
|
|
result.SetScale(mParams[0], mParams[1]);
|
|
|
|
break;
|
|
|
|
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_ROTATE:
|
2011-09-25 14:04:31 -07:00
|
|
|
result.SetRotate(mParams[0], mParams[1], mParams[2]);
|
|
|
|
break;
|
|
|
|
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_SKEWX:
|
2011-09-25 14:04:31 -07:00
|
|
|
result.SetSkewX(mParams[0]);
|
|
|
|
break;
|
|
|
|
|
2012-12-22 20:54:24 -08:00
|
|
|
case SVG_TRANSFORM_SKEWY:
|
2011-09-25 14:04:31 -07:00
|
|
|
result.SetSkewY(mParams[0]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unexpected transform type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-09-25 14:04:27 -07:00
|
|
|
} // namespace mozilla
|