1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "SVGOrientSMILType.h"
9 #include "mozilla/SMILValue.h"
10 #include "mozilla/dom/SVGMarkerElement.h"
12 #include "SVGAnimatedOrient.h"
17 using namespace dom::SVGAngle_Binding
;
18 using namespace dom::SVGMarkerElement_Binding
;
21 SVGOrientSMILType
SVGOrientSMILType::sSingleton
;
23 void SVGOrientSMILType::Init(SMILValue
& aValue
) const {
24 MOZ_ASSERT(aValue
.IsNull(), "Unexpected value type");
26 aValue
.mU
.mOrient
.mAngle
= 0.0f
;
27 aValue
.mU
.mOrient
.mUnit
= SVG_ANGLETYPE_UNSPECIFIED
;
28 aValue
.mU
.mOrient
.mOrientType
= SVG_MARKER_ORIENT_ANGLE
;
32 void SVGOrientSMILType::Destroy(SMILValue
& aValue
) const {
33 MOZ_ASSERT(aValue
.mType
== this, "Unexpected SMIL value.");
34 aValue
.mU
.mPtr
= nullptr;
35 aValue
.mType
= SMILNullType::Singleton();
38 nsresult
SVGOrientSMILType::Assign(SMILValue
& aDest
,
39 const SMILValue
& aSrc
) const {
40 MOZ_ASSERT(aDest
.mType
== aSrc
.mType
, "Incompatible SMIL types.");
41 MOZ_ASSERT(aDest
.mType
== this, "Unexpected SMIL value.");
43 aDest
.mU
.mOrient
.mAngle
= aSrc
.mU
.mOrient
.mAngle
;
44 aDest
.mU
.mOrient
.mUnit
= aSrc
.mU
.mOrient
.mUnit
;
45 aDest
.mU
.mOrient
.mOrientType
= aSrc
.mU
.mOrient
.mOrientType
;
49 bool SVGOrientSMILType::IsEqual(const SMILValue
& aLeft
,
50 const SMILValue
& aRight
) const {
51 MOZ_ASSERT(aLeft
.mType
== aRight
.mType
, "Incompatible SMIL types");
52 MOZ_ASSERT(aLeft
.mType
== this, "Unexpected type for SMIL value");
54 return aLeft
.mU
.mOrient
.mAngle
== aRight
.mU
.mOrient
.mAngle
&&
55 aLeft
.mU
.mOrient
.mUnit
== aRight
.mU
.mOrient
.mUnit
&&
56 aLeft
.mU
.mOrient
.mOrientType
== aRight
.mU
.mOrient
.mOrientType
;
59 static float ValueInDegrees(const SMILValue
& aValue
) {
60 MOZ_ASSERT(aValue
.mU
.mOrient
.mOrientType
== SVG_MARKER_ORIENT_ANGLE
);
62 return aValue
.mU
.mOrient
.mAngle
== 0.0f
64 : aValue
.mU
.mOrient
.mAngle
* SVGAnimatedOrient::GetDegreesPerUnit(
65 aValue
.mU
.mOrient
.mUnit
);
68 nsresult
SVGOrientSMILType::Add(SMILValue
& aDest
, const SMILValue
& aValueToAdd
,
69 uint32_t aCount
) const {
70 MOZ_ASSERT(aValueToAdd
.mType
== aDest
.mType
, "Trying to add invalid types");
71 MOZ_ASSERT(aValueToAdd
.mType
== this, "Unexpected source type");
73 if (aDest
.mU
.mOrient
.mOrientType
!= SVG_MARKER_ORIENT_ANGLE
||
74 aValueToAdd
.mU
.mOrient
.mOrientType
!= SVG_MARKER_ORIENT_ANGLE
) {
75 // TODO: it would be nice to be able to add to auto angles
76 return NS_ERROR_FAILURE
;
79 // We may be dealing with two different angle units, so we normalize to
80 // degrees for the add:
81 float currentAngle
= ValueInDegrees(aDest
);
82 float angleToAdd
= ValueInDegrees(aValueToAdd
) * aCount
;
84 // And then we give the resulting animated value the same units as the value
85 // that we're animating to/by (i.e. the same as aValueToAdd):
86 aDest
.mU
.mOrient
.mAngle
=
87 (currentAngle
+ angleToAdd
) /
88 SVGAnimatedOrient::GetDegreesPerUnit(aValueToAdd
.mU
.mOrient
.mUnit
);
89 aDest
.mU
.mOrient
.mUnit
= aValueToAdd
.mU
.mOrient
.mUnit
;
94 nsresult
SVGOrientSMILType::ComputeDistance(const SMILValue
& aFrom
,
96 double& aDistance
) const {
97 MOZ_ASSERT(aFrom
.mType
== aTo
.mType
, "Trying to compare different types");
98 MOZ_ASSERT(aFrom
.mType
== this, "Unexpected source type");
100 if (aFrom
.mU
.mOrient
.mOrientType
!= SVG_MARKER_ORIENT_ANGLE
||
101 aTo
.mU
.mOrient
.mOrientType
!= SVG_MARKER_ORIENT_ANGLE
) {
102 // TODO: it would be nice to be able to compute distance with auto angles
103 return NS_ERROR_FAILURE
;
106 // Normalize both to degrees in case they're different angle units:
107 aDistance
= fabs(ValueInDegrees(aTo
) - ValueInDegrees(aFrom
));
112 nsresult
SVGOrientSMILType::Interpolate(const SMILValue
& aStartVal
,
113 const SMILValue
& aEndVal
,
114 double aUnitDistance
,
115 SMILValue
& aResult
) const {
116 MOZ_ASSERT(aStartVal
.mType
== aEndVal
.mType
,
117 "Trying to interpolate different types");
118 MOZ_ASSERT(aStartVal
.mType
== this, "Unexpected types for interpolation.");
119 MOZ_ASSERT(aResult
.mType
== this, "Unexpected result type.");
121 if (aStartVal
.mU
.mOrient
.mOrientType
!= SVG_MARKER_ORIENT_ANGLE
||
122 aEndVal
.mU
.mOrient
.mOrientType
!= SVG_MARKER_ORIENT_ANGLE
) {
123 // TODO: it would be nice to be able to handle auto angles too.
124 return NS_ERROR_FAILURE
;
127 float start
= ValueInDegrees(aStartVal
);
128 float end
= ValueInDegrees(aEndVal
);
129 float result
= (start
+ (end
- start
) * aUnitDistance
);
131 // we use the unit of the nearest value for the result:
132 if (aUnitDistance
> 0.5) {
133 aResult
.mU
.mOrient
.mAngle
=
134 result
/ SVGAnimatedOrient::GetDegreesPerUnit(aEndVal
.mU
.mOrient
.mUnit
);
135 aResult
.mU
.mOrient
.mUnit
= aEndVal
.mU
.mOrient
.mUnit
;
137 aResult
.mU
.mOrient
.mAngle
= result
/ SVGAnimatedOrient::GetDegreesPerUnit(
138 aStartVal
.mU
.mOrient
.mUnit
);
139 aResult
.mU
.mOrient
.mUnit
= aStartVal
.mU
.mOrient
.mUnit
;
145 } // namespace mozilla