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 Mozilla SVG Project code.
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 the GNU General Public License Version 2 or later (the "GPL"), or
25 : * 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 "SVGAnimatedLengthList.h"
38 : #include "DOMSVGAnimatedLengthList.h"
39 : #include "nsSVGElement.h"
40 : #include "nsSVGAttrTearoffTable.h"
41 : #include "nsSMILValue.h"
42 : #include "SVGLengthListSMILType.h"
43 :
44 : namespace mozilla {
45 :
46 : nsresult
47 0 : SVGAnimatedLengthList::SetBaseValueString(const nsAString& aValue)
48 : {
49 0 : SVGLengthList newBaseValue;
50 0 : nsresult rv = newBaseValue.SetValueFromString(aValue);
51 0 : if (NS_FAILED(rv)) {
52 0 : return rv;
53 : }
54 :
55 : DOMSVGAnimatedLengthList *domWrapper =
56 0 : DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
57 0 : if (domWrapper) {
58 : // We must send this notification *before* changing mBaseVal! If the length
59 : // of our baseVal is being reduced, our baseVal's DOM wrapper list may have
60 : // to remove DOM items from itself, and any removed DOM items need to copy
61 : // their internal counterpart values *before* we change them.
62 : //
63 0 : domWrapper->InternalBaseValListWillChangeTo(newBaseValue);
64 : }
65 :
66 : // We don't need to call DidChange* here - we're only called by
67 : // nsSVGElement::ParseAttribute under nsGenericElement::SetAttr,
68 : // which takes care of notifying.
69 :
70 0 : rv = mBaseVal.CopyFrom(newBaseValue);
71 0 : if (NS_FAILED(rv) && domWrapper) {
72 : // Attempting to increase mBaseVal's length failed - reduce domWrapper
73 : // back to the same length:
74 0 : domWrapper->InternalBaseValListWillChangeTo(mBaseVal);
75 : }
76 0 : return rv;
77 : }
78 :
79 : void
80 0 : SVGAnimatedLengthList::ClearBaseValue(PRUint32 aAttrEnum)
81 : {
82 : DOMSVGAnimatedLengthList *domWrapper =
83 0 : DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
84 0 : if (domWrapper) {
85 : // We must send this notification *before* changing mBaseVal! (See above.)
86 0 : domWrapper->InternalBaseValListWillChangeTo(SVGLengthList());
87 : }
88 0 : mBaseVal.Clear();
89 : // Caller notifies
90 0 : }
91 :
92 : nsresult
93 0 : SVGAnimatedLengthList::SetAnimValue(const SVGLengthList& aNewAnimValue,
94 : nsSVGElement *aElement,
95 : PRUint32 aAttrEnum)
96 : {
97 : DOMSVGAnimatedLengthList *domWrapper =
98 0 : DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
99 0 : if (domWrapper) {
100 : // A new animation may totally change the number of items in the animVal
101 : // list, replacing what was essentially a mirror of the baseVal list, or
102 : // else replacing and overriding an existing animation. When this happens
103 : // we must try and keep our animVal's DOM wrapper in sync (see the comment
104 : // in DOMSVGAnimatedLengthList::InternalBaseValListWillChangeTo).
105 : //
106 : // It's not possible for us to reliably distinguish between calls to this
107 : // method that are setting a new sample for an existing animation, and
108 : // calls that are setting the first sample of an animation that will
109 : // override an existing animation. Happily it's cheap to just blindly
110 : // notify our animVal's DOM wrapper of its internal counterpart's new value
111 : // each time this method is called, so that's what we do.
112 : //
113 : // Note that we must send this notification *before* setting or changing
114 : // mAnimVal! (See the comment in SetBaseValueString above.)
115 : //
116 0 : domWrapper->InternalAnimValListWillChangeTo(aNewAnimValue);
117 : }
118 0 : if (!mAnimVal) {
119 0 : mAnimVal = new SVGLengthList();
120 : }
121 0 : nsresult rv = mAnimVal->CopyFrom(aNewAnimValue);
122 0 : if (NS_FAILED(rv)) {
123 : // OOM. We clear the animation, and, importantly, ClearAnimValue() ensures
124 : // that mAnimVal and its DOM wrapper (if any) will have the same length!
125 0 : ClearAnimValue(aElement, aAttrEnum);
126 0 : return rv;
127 : }
128 0 : aElement->DidAnimateLengthList(aAttrEnum);
129 0 : return NS_OK;
130 : }
131 :
132 : void
133 0 : SVGAnimatedLengthList::ClearAnimValue(nsSVGElement *aElement,
134 : PRUint32 aAttrEnum)
135 : {
136 : DOMSVGAnimatedLengthList *domWrapper =
137 0 : DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
138 0 : if (domWrapper) {
139 : // When all animation ends, animVal simply mirrors baseVal, which may have
140 : // a different number of items to the last active animated value. We must
141 : // keep the length of our animVal's DOM wrapper list in sync, and again we
142 : // must do that before touching mAnimVal. See comments above.
143 : //
144 0 : domWrapper->InternalAnimValListWillChangeTo(mBaseVal);
145 : }
146 0 : mAnimVal = nsnull;
147 0 : aElement->DidAnimateLengthList(aAttrEnum);
148 0 : }
149 :
150 : nsISMILAttr*
151 0 : SVGAnimatedLengthList::ToSMILAttr(nsSVGElement *aSVGElement,
152 : PRUint8 aAttrEnum,
153 : PRUint8 aAxis,
154 : bool aCanZeroPadList)
155 : {
156 0 : return new SMILAnimatedLengthList(this, aSVGElement, aAttrEnum, aAxis, aCanZeroPadList);
157 : }
158 :
159 : nsresult
160 0 : SVGAnimatedLengthList::
161 : SMILAnimatedLengthList::ValueFromString(const nsAString& aStr,
162 : const nsISMILAnimationElement* /*aSrcElement*/,
163 : nsSMILValue& aValue,
164 : bool& aPreventCachingOfSandwich) const
165 : {
166 0 : nsSMILValue val(&SVGLengthListSMILType::sSingleton);
167 0 : SVGLengthListAndInfo *llai = static_cast<SVGLengthListAndInfo*>(val.mU.mPtr);
168 0 : nsresult rv = llai->SetValueFromString(aStr);
169 0 : if (NS_SUCCEEDED(rv)) {
170 0 : llai->SetInfo(mElement, mAxis, mCanZeroPadList);
171 0 : aValue.Swap(val);
172 :
173 : // If any of the lengths in the list depend on their context, then we must
174 : // prevent caching of the entire animation sandwich. This is because the
175 : // units of a length at a given index can change from sandwich layer to
176 : // layer, and indeed even be different within a single sandwich layer. If
177 : // any length in the result of an animation sandwich is the result of the
178 : // addition of lengths where one or more of those lengths is context
179 : // dependent, then naturally the resultant length is also context
180 : // dependent, regardless of whether its actual unit is context dependent or
181 : // not. Unfortunately normal invalidation mechanisms won't cause us to
182 : // recalculate the result of the sandwich if the context changes, so we
183 : // take the (substantial) performance hit of preventing caching of the
184 : // sandwich layer, causing the animation sandwich to be recalculated every
185 : // single sample.
186 :
187 0 : aPreventCachingOfSandwich = false;
188 0 : for (PRUint32 i = 0; i < llai->Length(); ++i) {
189 0 : PRUint8 unit = (*llai)[i].GetUnit();
190 0 : if (unit == nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE ||
191 : unit == nsIDOMSVGLength::SVG_LENGTHTYPE_EMS ||
192 : unit == nsIDOMSVGLength::SVG_LENGTHTYPE_EXS) {
193 0 : aPreventCachingOfSandwich = true;
194 0 : break;
195 : }
196 : }
197 : }
198 0 : return rv;
199 : }
200 :
201 : nsSMILValue
202 0 : SVGAnimatedLengthList::SMILAnimatedLengthList::GetBaseValue() const
203 : {
204 : // To benefit from Return Value Optimization and avoid copy constructor calls
205 : // due to our use of return-by-value, we must return the exact same object
206 : // from ALL return points. This function must only return THIS variable:
207 0 : nsSMILValue val;
208 :
209 0 : nsSMILValue tmp(&SVGLengthListSMILType::sSingleton);
210 0 : SVGLengthListAndInfo *llai = static_cast<SVGLengthListAndInfo*>(tmp.mU.mPtr);
211 0 : nsresult rv = llai->CopyFrom(mVal->mBaseVal);
212 0 : if (NS_SUCCEEDED(rv)) {
213 0 : llai->SetInfo(mElement, mAxis, mCanZeroPadList);
214 0 : val.Swap(tmp);
215 : }
216 : return val;
217 : }
218 :
219 : nsresult
220 0 : SVGAnimatedLengthList::SMILAnimatedLengthList::SetAnimValue(const nsSMILValue& aValue)
221 : {
222 0 : NS_ASSERTION(aValue.mType == &SVGLengthListSMILType::sSingleton,
223 : "Unexpected type to assign animated value");
224 0 : if (aValue.mType == &SVGLengthListSMILType::sSingleton) {
225 : mVal->SetAnimValue(*static_cast<SVGLengthListAndInfo*>(aValue.mU.mPtr),
226 : mElement,
227 0 : mAttrEnum);
228 : }
229 0 : return NS_OK;
230 : }
231 :
232 : void
233 0 : SVGAnimatedLengthList::SMILAnimatedLengthList::ClearAnimValue()
234 : {
235 0 : if (mVal->mAnimVal) {
236 0 : mVal->ClearAnimValue(mElement, mAttrEnum);
237 : }
238 0 : }
239 :
240 : } // namespace mozilla
|