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 IBM Corporation.
18 : * Portions created by the Initial Developer are Copyright (C) 2006
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 : #ifndef __NS_SVGLENGTH2_H__
38 : #define __NS_SVGLENGTH2_H__
39 :
40 : #include "nsAutoPtr.h"
41 : #include "nsCoord.h"
42 : #include "nsCycleCollectionParticipant.h"
43 : #include "nsDOMError.h"
44 : #include "nsError.h"
45 : #include "nsIDOMSVGAnimatedLength.h"
46 : #include "nsIDOMSVGLength.h"
47 : #include "nsISMILAttr.h"
48 : #include "nsMathUtils.h"
49 : #include "nsSVGElement.h"
50 : #include "nsSVGUtils.h"
51 :
52 : class nsIFrame;
53 : class nsISMILAnimationElement;
54 : class nsSMILValue;
55 : class nsSVGSVGElement;
56 :
57 : class nsSVGLength2
58 : {
59 :
60 : public:
61 0 : void Init(PRUint8 aCtxType = nsSVGUtils::XY,
62 : PRUint8 aAttrEnum = 0xff,
63 : float aValue = 0,
64 : PRUint8 aUnitType = nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER) {
65 0 : mAnimVal = mBaseVal = aValue;
66 0 : mSpecifiedUnitType = aUnitType;
67 0 : mAttrEnum = aAttrEnum;
68 0 : mCtxType = aCtxType;
69 0 : mIsAnimated = false;
70 0 : mIsBaseSet = false;
71 0 : }
72 :
73 0 : nsSVGLength2& operator=(const nsSVGLength2& aLength) {
74 0 : mBaseVal = aLength.mBaseVal;
75 0 : mAnimVal = aLength.mAnimVal;
76 0 : mSpecifiedUnitType = aLength.mSpecifiedUnitType;
77 0 : mIsAnimated = aLength.mIsAnimated;
78 0 : mIsBaseSet = aLength.mIsBaseSet;
79 0 : return *this;
80 : }
81 :
82 : nsresult SetBaseValueString(const nsAString& aValue,
83 : nsSVGElement *aSVGElement,
84 : bool aDoSetAttr);
85 : void GetBaseValueString(nsAString& aValue) const;
86 : void GetAnimValueString(nsAString& aValue) const;
87 :
88 0 : float GetBaseValue(nsSVGElement* aSVGElement) const
89 0 : { return mBaseVal / GetUnitScaleFactor(aSVGElement, mSpecifiedUnitType); }
90 0 : float GetAnimValue(nsSVGElement* aSVGElement) const
91 0 : { return mAnimVal / GetUnitScaleFactor(aSVGElement, mSpecifiedUnitType); }
92 0 : float GetAnimValue(nsIFrame* aFrame) const
93 0 : { return mAnimVal / GetUnitScaleFactor(aFrame, mSpecifiedUnitType); }
94 :
95 0 : PRUint8 GetCtxType() const { return mCtxType; }
96 0 : PRUint8 GetSpecifiedUnitType() const { return mSpecifiedUnitType; }
97 0 : bool IsPercentage() const
98 0 : { return mSpecifiedUnitType == nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE; }
99 0 : float GetAnimValInSpecifiedUnits() const { return mAnimVal; }
100 : float GetBaseValInSpecifiedUnits() const { return mBaseVal; }
101 :
102 : float GetBaseValue(nsSVGSVGElement* aCtx) const
103 : { return mBaseVal / GetUnitScaleFactor(aCtx, mSpecifiedUnitType); }
104 0 : float GetAnimValue(nsSVGSVGElement* aCtx) const
105 0 : { return mAnimVal / GetUnitScaleFactor(aCtx, mSpecifiedUnitType); }
106 :
107 0 : bool HasBaseVal() const {
108 0 : return mIsBaseSet;
109 : }
110 : // Returns true if the animated value of this length has been explicitly
111 : // set (either by animation, or by taking on the base value which has been
112 : // explicitly set by markup or a DOM call), false otherwise.
113 : // If this returns false, the animated value is still valid, that is,
114 : // useable, and represents the default base value of the attribute.
115 0 : bool IsExplicitlySet() const
116 0 : { return mIsAnimated || mIsBaseSet; }
117 :
118 : nsresult ToDOMAnimatedLength(nsIDOMSVGAnimatedLength **aResult,
119 : nsSVGElement* aSVGElement);
120 : // Returns a new nsISMILAttr object that the caller must delete
121 : nsISMILAttr* ToSMILAttr(nsSVGElement* aSVGElement);
122 :
123 : private:
124 :
125 : float mAnimVal;
126 : float mBaseVal;
127 : PRUint8 mSpecifiedUnitType;
128 : PRUint8 mAttrEnum; // element specified tracking for attribute
129 : PRUint8 mCtxType; // X, Y or Unspecified
130 : bool mIsAnimated:1;
131 : bool mIsBaseSet:1;
132 :
133 0 : static float GetMMPerPixel() { return MM_PER_INCH_FLOAT / 96; }
134 : float GetAxisLength(nsIFrame *aNonSVGFrame) const;
135 0 : static float GetEmLength(nsIFrame *aFrame)
136 0 : { return nsSVGUtils::GetFontSize(aFrame); }
137 0 : static float GetExLength(nsIFrame *aFrame)
138 0 : { return nsSVGUtils::GetFontXHeight(aFrame); }
139 : float GetUnitScaleFactor(nsIFrame *aFrame, PRUint8 aUnitType) const;
140 :
141 : float GetMMPerPixel(nsSVGSVGElement *aCtx) const;
142 : float GetAxisLength(nsSVGSVGElement *aCtx) const;
143 0 : static float GetEmLength(nsSVGElement *aSVGElement)
144 0 : { return nsSVGUtils::GetFontSize(aSVGElement); }
145 0 : static float GetExLength(nsSVGElement *aSVGElement)
146 0 : { return nsSVGUtils::GetFontXHeight(aSVGElement); }
147 : float GetUnitScaleFactor(nsSVGElement *aSVGElement, PRUint8 aUnitType) const;
148 : float GetUnitScaleFactor(nsSVGSVGElement *aCtx, PRUint8 aUnitType) const;
149 :
150 : // SetBaseValue and SetAnimValue set the value in user units
151 : void SetBaseValue(float aValue, nsSVGElement *aSVGElement, bool aDoSetAttr);
152 : void SetBaseValueInSpecifiedUnits(float aValue, nsSVGElement *aSVGElement,
153 : bool aDoSetAttr);
154 : void SetAnimValue(float aValue, nsSVGElement *aSVGElement);
155 : void SetAnimValueInSpecifiedUnits(float aValue, nsSVGElement *aSVGElement);
156 : nsresult NewValueSpecifiedUnits(PRUint16 aUnitType, float aValue,
157 : nsSVGElement *aSVGElement);
158 : nsresult ConvertToSpecifiedUnits(PRUint16 aUnitType, nsSVGElement *aSVGElement);
159 : nsresult ToDOMBaseVal(nsIDOMSVGLength **aResult, nsSVGElement* aSVGElement);
160 : nsresult ToDOMAnimVal(nsIDOMSVGLength **aResult, nsSVGElement* aSVGElement);
161 :
162 : struct DOMBaseVal : public nsIDOMSVGLength
163 : {
164 0 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
165 1464 : NS_DECL_CYCLE_COLLECTION_CLASS(DOMBaseVal)
166 :
167 0 : DOMBaseVal(nsSVGLength2* aVal, nsSVGElement *aSVGElement)
168 0 : : mVal(aVal), mSVGElement(aSVGElement) {}
169 : virtual ~DOMBaseVal();
170 :
171 : nsSVGLength2* mVal; // kept alive because it belongs to mSVGElement
172 : nsRefPtr<nsSVGElement> mSVGElement;
173 :
174 0 : NS_IMETHOD GetUnitType(PRUint16* aResult)
175 0 : { *aResult = mVal->mSpecifiedUnitType; return NS_OK; }
176 :
177 0 : NS_IMETHOD GetValue(float* aResult)
178 0 : { *aResult = mVal->GetBaseValue(mSVGElement); return NS_OK; }
179 0 : NS_IMETHOD SetValue(float aValue)
180 : {
181 0 : if (!NS_finite(aValue)) {
182 0 : return NS_ERROR_ILLEGAL_VALUE;
183 : }
184 0 : mVal->SetBaseValue(aValue, mSVGElement, true);
185 0 : return NS_OK;
186 : }
187 :
188 0 : NS_IMETHOD GetValueInSpecifiedUnits(float* aResult)
189 0 : { *aResult = mVal->mBaseVal; return NS_OK; }
190 0 : NS_IMETHOD SetValueInSpecifiedUnits(float aValue)
191 : {
192 0 : if (!NS_finite(aValue)) {
193 0 : return NS_ERROR_ILLEGAL_VALUE;
194 : }
195 0 : mVal->SetBaseValueInSpecifiedUnits(aValue, mSVGElement, true);
196 0 : return NS_OK;
197 : }
198 :
199 0 : NS_IMETHOD SetValueAsString(const nsAString& aValue)
200 0 : { return mVal->SetBaseValueString(aValue, mSVGElement, true); }
201 0 : NS_IMETHOD GetValueAsString(nsAString& aValue)
202 0 : { mVal->GetBaseValueString(aValue); return NS_OK; }
203 :
204 0 : NS_IMETHOD NewValueSpecifiedUnits(PRUint16 unitType,
205 : float valueInSpecifiedUnits)
206 : {
207 : return mVal->NewValueSpecifiedUnits(unitType, valueInSpecifiedUnits,
208 0 : mSVGElement); }
209 :
210 0 : NS_IMETHOD ConvertToSpecifiedUnits(PRUint16 unitType)
211 0 : { return mVal->ConvertToSpecifiedUnits(unitType, mSVGElement); }
212 : };
213 :
214 : struct DOMAnimVal : public nsIDOMSVGLength
215 : {
216 0 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
217 1464 : NS_DECL_CYCLE_COLLECTION_CLASS(DOMAnimVal)
218 :
219 0 : DOMAnimVal(nsSVGLength2* aVal, nsSVGElement *aSVGElement)
220 0 : : mVal(aVal), mSVGElement(aSVGElement) {}
221 : virtual ~DOMAnimVal();
222 :
223 : nsSVGLength2* mVal; // kept alive because it belongs to mSVGElement
224 : nsRefPtr<nsSVGElement> mSVGElement;
225 :
226 : // Script may have modified animation parameters or timeline -- DOM getters
227 : // need to flush any resample requests to reflect these modifications.
228 0 : NS_IMETHOD GetUnitType(PRUint16* aResult)
229 : {
230 0 : mSVGElement->FlushAnimations();
231 0 : *aResult = mVal->mSpecifiedUnitType;
232 0 : return NS_OK;
233 : }
234 :
235 0 : NS_IMETHOD GetValue(float* aResult)
236 : {
237 0 : mSVGElement->FlushAnimations();
238 0 : *aResult = mVal->GetAnimValue(mSVGElement);
239 0 : return NS_OK;
240 : }
241 0 : NS_IMETHOD SetValue(float aValue)
242 0 : { return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR; }
243 :
244 0 : NS_IMETHOD GetValueInSpecifiedUnits(float* aResult)
245 : {
246 0 : mSVGElement->FlushAnimations();
247 0 : *aResult = mVal->mAnimVal;
248 0 : return NS_OK;
249 : }
250 0 : NS_IMETHOD SetValueInSpecifiedUnits(float aValue)
251 0 : { return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR; }
252 :
253 0 : NS_IMETHOD SetValueAsString(const nsAString& aValue)
254 0 : { return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR; }
255 0 : NS_IMETHOD GetValueAsString(nsAString& aValue)
256 : {
257 0 : mSVGElement->FlushAnimations();
258 0 : mVal->GetAnimValueString(aValue);
259 0 : return NS_OK;
260 : }
261 :
262 0 : NS_IMETHOD NewValueSpecifiedUnits(PRUint16 unitType,
263 : float valueInSpecifiedUnits)
264 0 : { return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR; }
265 :
266 0 : NS_IMETHOD ConvertToSpecifiedUnits(PRUint16 unitType)
267 0 : { return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR; }
268 : };
269 :
270 : public:
271 : struct DOMAnimatedLength : public nsIDOMSVGAnimatedLength
272 : {
273 0 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
274 1464 : NS_DECL_CYCLE_COLLECTION_CLASS(DOMAnimatedLength)
275 :
276 0 : DOMAnimatedLength(nsSVGLength2* aVal, nsSVGElement *aSVGElement)
277 0 : : mVal(aVal), mSVGElement(aSVGElement) {}
278 : virtual ~DOMAnimatedLength();
279 :
280 : nsSVGLength2* mVal; // kept alive because it belongs to content
281 : nsRefPtr<nsSVGElement> mSVGElement;
282 :
283 0 : NS_IMETHOD GetBaseVal(nsIDOMSVGLength **aBaseVal)
284 0 : { return mVal->ToDOMBaseVal(aBaseVal, mSVGElement); }
285 :
286 0 : NS_IMETHOD GetAnimVal(nsIDOMSVGLength **aAnimVal)
287 0 : { return mVal->ToDOMAnimVal(aAnimVal, mSVGElement); }
288 : };
289 :
290 : struct SMILLength : public nsISMILAttr
291 0 : {
292 : public:
293 0 : SMILLength(nsSVGLength2* aVal, nsSVGElement *aSVGElement)
294 0 : : mVal(aVal), mSVGElement(aSVGElement) {}
295 :
296 : // These will stay alive because a nsISMILAttr only lives as long
297 : // as the Compositing step, and DOM elements don't get a chance to
298 : // die during that.
299 : nsSVGLength2* mVal;
300 : nsSVGElement* mSVGElement;
301 :
302 : // nsISMILAttr methods
303 : virtual nsresult ValueFromString(const nsAString& aStr,
304 : const nsISMILAnimationElement* aSrcElement,
305 : nsSMILValue &aValue,
306 : bool& aPreventCachingOfSandwich) const;
307 : virtual nsSMILValue GetBaseValue() const;
308 : virtual void ClearAnimValue();
309 : virtual nsresult SetAnimValue(const nsSMILValue& aValue);
310 : };
311 : };
312 :
313 : #endif // __NS_SVGLENGTH2_H__
|