Line data Source code
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/. */
6 :
7 : #include "SVGOrientSMILType.h"
8 : #include "nsSMILValue.h"
9 : #include "nsSVGAngle.h"
10 : #include "nsDebug.h"
11 : #include "mozilla/dom/SVGMarkerElement.h"
12 : #include <math.h>
13 :
14 : namespace mozilla {
15 :
16 : /*static*/ SVGOrientSMILType SVGOrientSMILType::sSingleton;
17 :
18 : void
19 0 : SVGOrientSMILType::Init(nsSMILValue& aValue) const
20 : {
21 0 : MOZ_ASSERT(aValue.IsNull(), "Unexpected value type");
22 :
23 0 : aValue.mU.mOrient.mAngle = 0.0f;
24 0 : aValue.mU.mOrient.mUnit = SVG_ANGLETYPE_UNSPECIFIED;
25 0 : aValue.mU.mOrient.mOrientType = dom::SVG_MARKER_ORIENT_ANGLE;
26 0 : aValue.mType = this;
27 0 : }
28 :
29 : void
30 0 : SVGOrientSMILType::Destroy(nsSMILValue& aValue) const
31 : {
32 0 : NS_PRECONDITION(aValue.mType == this, "Unexpected SMIL value.");
33 0 : aValue.mU.mPtr = nullptr;
34 0 : aValue.mType = nsSMILNullType::Singleton();
35 0 : }
36 :
37 : nsresult
38 0 : SVGOrientSMILType::Assign(nsSMILValue& aDest, const nsSMILValue& aSrc) const
39 : {
40 0 : NS_PRECONDITION(aDest.mType == aSrc.mType, "Incompatible SMIL types.");
41 0 : NS_PRECONDITION(aDest.mType == this, "Unexpected SMIL value.");
42 :
43 0 : aDest.mU.mOrient.mAngle = aSrc.mU.mOrient.mAngle;
44 0 : aDest.mU.mOrient.mUnit = aSrc.mU.mOrient.mUnit;
45 0 : aDest.mU.mOrient.mOrientType = aSrc.mU.mOrient.mOrientType;
46 0 : return NS_OK;
47 : }
48 :
49 : bool
50 0 : SVGOrientSMILType::IsEqual(const nsSMILValue& aLeft,
51 : const nsSMILValue& aRight) const
52 : {
53 0 : NS_PRECONDITION(aLeft.mType == aRight.mType, "Incompatible SMIL types");
54 0 : NS_PRECONDITION(aLeft.mType == this, "Unexpected type for SMIL value");
55 :
56 : return
57 0 : aLeft.mU.mOrient.mAngle == aRight.mU.mOrient.mAngle &&
58 0 : aLeft.mU.mOrient.mUnit == aRight.mU.mOrient.mUnit &&
59 0 : aLeft.mU.mOrient.mOrientType == aRight.mU.mOrient.mOrientType;
60 : }
61 :
62 : nsresult
63 0 : SVGOrientSMILType::Add(nsSMILValue& aDest, const nsSMILValue& aValueToAdd,
64 : uint32_t aCount) const
65 : {
66 0 : NS_PRECONDITION(aValueToAdd.mType == aDest.mType,
67 : "Trying to add invalid types");
68 0 : NS_PRECONDITION(aValueToAdd.mType == this, "Unexpected source type");
69 :
70 0 : if (aDest.mU.mOrient.mOrientType != dom::SVG_MARKER_ORIENT_ANGLE ||
71 0 : aValueToAdd.mU.mOrient.mOrientType != dom::SVG_MARKER_ORIENT_ANGLE) {
72 : // TODO: it would be nice to be able to add to auto angles
73 0 : return NS_ERROR_FAILURE;
74 : }
75 :
76 : // We may be dealing with two different angle units, so we normalize to
77 : // degrees for the add:
78 0 : float currentAngle = aDest.mU.mOrient.mAngle *
79 0 : nsSVGAngle::GetDegreesPerUnit(aDest.mU.mOrient.mUnit);
80 0 : float angleToAdd = aValueToAdd.mU.mOrient.mAngle *
81 0 : nsSVGAngle::GetDegreesPerUnit(aValueToAdd.mU.mOrient.mUnit) *
82 0 : aCount;
83 :
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 0 : aDest.mU.mOrient.mAngle = (currentAngle + angleToAdd) /
87 0 : nsSVGAngle::GetDegreesPerUnit(aValueToAdd.mU.mOrient.mUnit);
88 0 : aDest.mU.mOrient.mUnit = aValueToAdd.mU.mOrient.mUnit;
89 :
90 0 : return NS_OK;
91 : }
92 :
93 : nsresult
94 0 : SVGOrientSMILType::ComputeDistance(const nsSMILValue& aFrom,
95 : const nsSMILValue& aTo,
96 : double& aDistance) const
97 : {
98 0 : NS_PRECONDITION(aFrom.mType == aTo.mType,"Trying to compare different types");
99 0 : NS_PRECONDITION(aFrom.mType == this, "Unexpected source type");
100 :
101 0 : if (aFrom.mU.mOrient.mOrientType != dom::SVG_MARKER_ORIENT_ANGLE ||
102 0 : aTo.mU.mOrient.mOrientType != dom::SVG_MARKER_ORIENT_ANGLE) {
103 : // TODO: it would be nice to be able to compute distance with auto angles
104 0 : return NS_ERROR_FAILURE;
105 : }
106 :
107 : // Normalize both to degrees in case they're different angle units:
108 0 : double from = aFrom.mU.mOrient.mAngle *
109 0 : nsSVGAngle::GetDegreesPerUnit(aFrom.mU.mOrient.mUnit);
110 0 : double to = aTo.mU.mOrient.mAngle *
111 0 : nsSVGAngle::GetDegreesPerUnit(aTo.mU.mOrient.mUnit);
112 :
113 0 : aDistance = fabs(to - from);
114 :
115 0 : return NS_OK;
116 : }
117 :
118 : nsresult
119 0 : SVGOrientSMILType::Interpolate(const nsSMILValue& aStartVal,
120 : const nsSMILValue& aEndVal,
121 : double aUnitDistance,
122 : nsSMILValue& aResult) const
123 : {
124 0 : NS_PRECONDITION(aStartVal.mType == aEndVal.mType,
125 : "Trying to interpolate different types");
126 0 : NS_PRECONDITION(aStartVal.mType == this,
127 : "Unexpected types for interpolation.");
128 0 : NS_PRECONDITION(aResult.mType == this, "Unexpected result type.");
129 :
130 0 : if (aStartVal.mU.mOrient.mOrientType != dom::SVG_MARKER_ORIENT_ANGLE ||
131 0 : aEndVal.mU.mOrient.mOrientType != dom::SVG_MARKER_ORIENT_ANGLE) {
132 : // TODO: it would be nice to be able to handle auto angles too.
133 0 : return NS_ERROR_FAILURE;
134 : }
135 :
136 0 : float start = aStartVal.mU.mOrient.mAngle *
137 0 : nsSVGAngle::GetDegreesPerUnit(aStartVal.mU.mOrient.mUnit);
138 0 : float end = aEndVal.mU.mOrient.mAngle *
139 0 : nsSVGAngle::GetDegreesPerUnit(aEndVal.mU.mOrient.mUnit);
140 0 : float result = (start + (end - start) * aUnitDistance);
141 :
142 : // Again, we use the unit of the to/by value for the result:
143 0 : aResult.mU.mOrient.mAngle = result /
144 0 : nsSVGAngle::GetDegreesPerUnit(aEndVal.mU.mOrient.mUnit);
145 0 : aResult.mU.mOrient.mUnit = aEndVal.mU.mOrient.mUnit;
146 :
147 0 : return NS_OK;
148 : }
149 :
150 : } // namespace mozilla
|