1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is the Mozilla SVG project.
16 : *
17 : * The Initial Developer of the Original Code is the Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2010
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either of the GNU General Public License Version 2 or later (the "GPL"),
25 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #include "SVGPointListSMILType.h"
38 : #include "nsSMILValue.h"
39 : #include "SVGPointList.h"
40 : #include "nsMathUtils.h"
41 : #include <math.h>
42 :
43 : namespace mozilla {
44 :
45 1464 : /*static*/ SVGPointListSMILType SVGPointListSMILType::sSingleton;
46 :
47 : //----------------------------------------------------------------------
48 : // nsISMILType implementation
49 :
50 : void
51 0 : SVGPointListSMILType::Init(nsSMILValue &aValue) const
52 : {
53 0 : NS_ABORT_IF_FALSE(aValue.IsNull(), "Unexpected value type");
54 :
55 0 : SVGPointListAndInfo* pointList = new SVGPointListAndInfo();
56 :
57 0 : aValue.mU.mPtr = pointList;
58 0 : aValue.mType = this;
59 0 : }
60 :
61 : void
62 0 : SVGPointListSMILType::Destroy(nsSMILValue& aValue) const
63 : {
64 0 : NS_PRECONDITION(aValue.mType == this, "Unexpected SMIL value type");
65 0 : delete static_cast<SVGPointListAndInfo*>(aValue.mU.mPtr);
66 0 : aValue.mU.mPtr = nsnull;
67 0 : aValue.mType = &nsSMILNullType::sSingleton;
68 0 : }
69 :
70 : nsresult
71 0 : SVGPointListSMILType::Assign(nsSMILValue& aDest,
72 : const nsSMILValue& aSrc) const
73 : {
74 0 : NS_PRECONDITION(aDest.mType == aSrc.mType, "Incompatible SMIL types");
75 0 : NS_PRECONDITION(aDest.mType == this, "Unexpected SMIL value");
76 :
77 : const SVGPointListAndInfo* src =
78 0 : static_cast<const SVGPointListAndInfo*>(aSrc.mU.mPtr);
79 : SVGPointListAndInfo* dest =
80 0 : static_cast<SVGPointListAndInfo*>(aDest.mU.mPtr);
81 :
82 0 : return dest->CopyFrom(*src);
83 : }
84 :
85 : bool
86 0 : SVGPointListSMILType::IsEqual(const nsSMILValue& aLeft,
87 : const nsSMILValue& aRight) const
88 : {
89 0 : NS_PRECONDITION(aLeft.mType == aRight.mType, "Incompatible SMIL types");
90 0 : NS_PRECONDITION(aLeft.mType == this, "Unexpected type for SMIL value");
91 :
92 : return *static_cast<const SVGPointListAndInfo*>(aLeft.mU.mPtr) ==
93 0 : *static_cast<const SVGPointListAndInfo*>(aRight.mU.mPtr);
94 : }
95 :
96 : nsresult
97 0 : SVGPointListSMILType::Add(nsSMILValue& aDest,
98 : const nsSMILValue& aValueToAdd,
99 : PRUint32 aCount) const
100 : {
101 0 : NS_PRECONDITION(aDest.mType == this, "Unexpected SMIL type");
102 0 : NS_PRECONDITION(aValueToAdd.mType == this, "Incompatible SMIL type");
103 :
104 : SVGPointListAndInfo& dest =
105 0 : *static_cast<SVGPointListAndInfo*>(aDest.mU.mPtr);
106 : const SVGPointListAndInfo& valueToAdd =
107 0 : *static_cast<const SVGPointListAndInfo*>(aValueToAdd.mU.mPtr);
108 :
109 0 : NS_ABORT_IF_FALSE(dest.Element() || valueToAdd.Element(),
110 : "Target element propagation failure");
111 :
112 0 : if (!valueToAdd.Element()) {
113 0 : NS_ABORT_IF_FALSE(valueToAdd.Length() == 0,
114 : "Not identity value - target element propagation failure");
115 0 : return NS_OK;
116 : }
117 0 : if (!dest.Element()) {
118 0 : NS_ABORT_IF_FALSE(dest.Length() == 0,
119 : "Not identity value - target element propagation failure");
120 0 : if (!dest.SetLength(valueToAdd.Length())) {
121 0 : return NS_ERROR_OUT_OF_MEMORY;
122 : }
123 0 : for (PRUint32 i = 0; i < dest.Length(); ++i) {
124 0 : dest[i] = aCount * valueToAdd[i];
125 : }
126 0 : dest.SetInfo(valueToAdd.Element()); // propagate target element info!
127 0 : return NS_OK;
128 : }
129 0 : NS_ABORT_IF_FALSE(dest.Element() == valueToAdd.Element(),
130 : "adding values from different elements...?");
131 0 : if (dest.Length() != valueToAdd.Length()) {
132 : // For now we only support animation between lists with the same number of
133 : // items. nsSVGUtils::ReportToConsole
134 0 : return NS_ERROR_FAILURE;
135 : }
136 0 : for (PRUint32 i = 0; i < dest.Length(); ++i) {
137 0 : dest[i] += aCount * valueToAdd[i];
138 : }
139 0 : dest.SetInfo(valueToAdd.Element()); // propagate target element info!
140 0 : return NS_OK;
141 : }
142 :
143 : nsresult
144 0 : SVGPointListSMILType::ComputeDistance(const nsSMILValue& aFrom,
145 : const nsSMILValue& aTo,
146 : double& aDistance) const
147 : {
148 0 : NS_PRECONDITION(aFrom.mType == this, "Unexpected SMIL type");
149 0 : NS_PRECONDITION(aTo.mType == this, "Incompatible SMIL type");
150 :
151 : const SVGPointListAndInfo& from =
152 0 : *static_cast<const SVGPointListAndInfo*>(aFrom.mU.mPtr);
153 : const SVGPointListAndInfo& to =
154 0 : *static_cast<const SVGPointListAndInfo*>(aTo.mU.mPtr);
155 :
156 0 : if (from.Length() != to.Length()) {
157 : // Lists in the 'values' attribute must have the same length.
158 : // nsSVGUtils::ReportToConsole
159 0 : return NS_ERROR_FAILURE;
160 : }
161 :
162 : // We return the root of the sum of the squares of the distances between the
163 : // points at each corresponding index.
164 :
165 0 : double total = 0.0;
166 :
167 0 : for (PRUint32 i = 0; i < to.Length(); ++i) {
168 0 : double dx = to[i].mX - from[i].mX;
169 0 : double dy = to[i].mY - from[i].mY;
170 0 : total += dx * dx + dy * dy;
171 : }
172 0 : double distance = sqrt(total);
173 0 : if (!NS_finite(distance)) {
174 0 : return NS_ERROR_FAILURE;
175 : }
176 0 : aDistance = distance;
177 :
178 0 : return NS_OK;
179 : }
180 :
181 : nsresult
182 0 : SVGPointListSMILType::Interpolate(const nsSMILValue& aStartVal,
183 : const nsSMILValue& aEndVal,
184 : double aUnitDistance,
185 : nsSMILValue& aResult) const
186 : {
187 0 : NS_PRECONDITION(aStartVal.mType == aEndVal.mType,
188 : "Trying to interpolate different types");
189 0 : NS_PRECONDITION(aStartVal.mType == this,
190 : "Unexpected types for interpolation");
191 0 : NS_PRECONDITION(aResult.mType == this, "Unexpected result type");
192 :
193 : const SVGPointListAndInfo& start =
194 0 : *static_cast<const SVGPointListAndInfo*>(aStartVal.mU.mPtr);
195 : const SVGPointListAndInfo& end =
196 0 : *static_cast<const SVGPointListAndInfo*>(aEndVal.mU.mPtr);
197 : SVGPointListAndInfo& result =
198 0 : *static_cast<SVGPointListAndInfo*>(aResult.mU.mPtr);
199 :
200 0 : NS_ABORT_IF_FALSE(end.Element(), "Can't propagate target element");
201 0 : NS_ABORT_IF_FALSE(start.Element() == end.Element() || !start.Element(),
202 : "Different target elements");
203 :
204 0 : if (start.Element() && // 'start' is not an "identity" value
205 0 : start.Length() != end.Length()) {
206 : // For now we only support animation between lists of the same length.
207 : // nsSVGUtils::ReportToConsole
208 0 : return NS_ERROR_FAILURE;
209 : }
210 0 : if (!result.SetLength(end.Length())) {
211 0 : return NS_ERROR_OUT_OF_MEMORY;
212 : }
213 :
214 0 : result.SetInfo(end.Element()); // propagate target element info!
215 :
216 0 : if (start.Length() != end.Length()) {
217 0 : NS_ABORT_IF_FALSE(start.Length() == 0, "Not an identity value");
218 0 : for (PRUint32 i = 0; i < end.Length(); ++i) {
219 0 : result[i] = aUnitDistance * end[i];
220 : }
221 0 : return NS_OK;
222 : }
223 0 : for (PRUint32 i = 0; i < end.Length(); ++i) {
224 0 : result[i] = start[i] + (end[i] - start[i]) * aUnitDistance;
225 : }
226 0 : return NS_OK;
227 : }
228 :
229 4392 : } // namespace mozilla
|