/src/mozilla-central/dom/svg/nsSVGViewBox.h
Line | Count | Source (jump to first uncovered line) |
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 | | #ifndef __NS_SVGVIEWBOX_H__ |
8 | | #define __NS_SVGVIEWBOX_H__ |
9 | | |
10 | | #include "nsAutoPtr.h" |
11 | | #include "nsCycleCollectionParticipant.h" |
12 | | #include "nsError.h" |
13 | | #include "mozilla/dom/SVGAnimatedRect.h" |
14 | | #include "mozilla/dom/SVGIRect.h" |
15 | | #include "nsISMILAttr.h" |
16 | | #include "nsSVGElement.h" |
17 | | #include "mozilla/Attributes.h" |
18 | | #include "mozilla/UniquePtr.h" |
19 | | #include "nsSVGAttrTearoffTable.h" |
20 | | |
21 | | class nsSMILValue; |
22 | | |
23 | | namespace mozilla { |
24 | | namespace dom { |
25 | | class SVGAnimationElement; |
26 | | } // namespace dom |
27 | | } // namespace mozilla |
28 | | |
29 | | struct nsSVGViewBoxRect |
30 | | { |
31 | | float x, y; |
32 | | float width, height; |
33 | | bool none; |
34 | | |
35 | | nsSVGViewBoxRect() |
36 | | : x(0.0) |
37 | | , y(0.0) |
38 | | , width(0.0) |
39 | | , height(0.0) |
40 | | , none(true) |
41 | 0 | {} |
42 | | nsSVGViewBoxRect(float aX, float aY, float aWidth, float aHeight) : |
43 | 0 | x(aX), y(aY), width(aWidth), height(aHeight), none(false) {} |
44 | | nsSVGViewBoxRect(const nsSVGViewBoxRect& rhs) : |
45 | 0 | x(rhs.x), y(rhs.y), width(rhs.width), height(rhs.height), none(rhs.none) {} |
46 | | bool operator==(const nsSVGViewBoxRect& aOther) const; |
47 | | |
48 | | static nsresult FromString(const nsAString& aStr, nsSVGViewBoxRect *aViewBox); |
49 | | }; |
50 | | |
51 | | class nsSVGViewBox |
52 | | { |
53 | | public: |
54 | | |
55 | | void Init(); |
56 | | |
57 | | /** |
58 | | * Returns true if the corresponding "viewBox" attribute defined a rectangle |
59 | | * with finite values and nonnegative width/height. |
60 | | * Returns false if the viewBox was set to an invalid |
61 | | * string, or if any of the four rect values were too big to store in a |
62 | | * float, or the width/height are negative. |
63 | | */ |
64 | | bool HasRect() const; |
65 | | |
66 | | /** |
67 | | * Returns true if the corresponding "viewBox" attribute either defined a |
68 | | * rectangle with finite values or the special "none" value. |
69 | | */ |
70 | | bool IsExplicitlySet() const |
71 | 0 | { |
72 | 0 | if (mAnimVal || mHasBaseVal) { |
73 | 0 | const nsSVGViewBoxRect& rect = GetAnimValue(); |
74 | 0 | return rect.none || (rect.width >= 0 && rect.height >= 0); |
75 | 0 | } |
76 | 0 | return false; |
77 | 0 | } |
78 | | |
79 | | const nsSVGViewBoxRect& GetBaseValue() const |
80 | 0 | { return mBaseVal; } |
81 | | void SetBaseValue(const nsSVGViewBoxRect& aRect, |
82 | | nsSVGElement *aSVGElement); |
83 | | const nsSVGViewBoxRect& GetAnimValue() const |
84 | 0 | { return mAnimVal ? *mAnimVal : mBaseVal; } |
85 | | void SetAnimValue(const nsSVGViewBoxRect& aRect, |
86 | | nsSVGElement *aSVGElement); |
87 | | |
88 | | nsresult SetBaseValueString(const nsAString& aValue, |
89 | | nsSVGElement *aSVGElement, |
90 | | bool aDoSetAttr); |
91 | | void GetBaseValueString(nsAString& aValue) const; |
92 | | |
93 | | already_AddRefed<mozilla::dom::SVGAnimatedRect> |
94 | | ToSVGAnimatedRect(nsSVGElement *aSVGElement); |
95 | | |
96 | | already_AddRefed<mozilla::dom::SVGIRect> |
97 | | ToDOMBaseVal(nsSVGElement* aSVGElement); |
98 | | |
99 | | already_AddRefed<mozilla::dom::SVGIRect> |
100 | | ToDOMAnimVal(nsSVGElement* aSVGElement); |
101 | | |
102 | | mozilla::UniquePtr<nsISMILAttr> ToSMILAttr(nsSVGElement* aSVGElement); |
103 | | |
104 | | private: |
105 | | nsSVGViewBoxRect mBaseVal; |
106 | | nsAutoPtr<nsSVGViewBoxRect> mAnimVal; |
107 | | bool mHasBaseVal; |
108 | | |
109 | | public: |
110 | | struct DOMBaseVal final : public mozilla::dom::SVGIRect |
111 | | { |
112 | | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
113 | | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMBaseVal) |
114 | | |
115 | | DOMBaseVal(nsSVGViewBox *aVal, nsSVGElement *aSVGElement) |
116 | | : mozilla::dom::SVGIRect() |
117 | | , mVal(aVal) |
118 | | , mSVGElement(aSVGElement) |
119 | 0 | {} |
120 | | |
121 | | nsSVGViewBox* mVal; // kept alive because it belongs to content |
122 | | RefPtr<nsSVGElement> mSVGElement; |
123 | | |
124 | | float X() const final |
125 | 0 | { |
126 | 0 | return mVal->GetBaseValue().x; |
127 | 0 | } |
128 | | |
129 | | float Y() const final |
130 | 0 | { |
131 | 0 | return mVal->GetBaseValue().y; |
132 | 0 | } |
133 | | |
134 | | float Width() const final |
135 | 0 | { |
136 | 0 | return mVal->GetBaseValue().width; |
137 | 0 | } |
138 | | |
139 | | float Height() const final |
140 | 0 | { |
141 | 0 | return mVal->GetBaseValue().height; |
142 | 0 | } |
143 | | |
144 | | void SetX(float aX, mozilla::ErrorResult& aRv) final; |
145 | | void SetY(float aY, mozilla::ErrorResult& aRv) final; |
146 | | void SetWidth(float aWidth, mozilla::ErrorResult& aRv) final; |
147 | | void SetHeight(float aHeight, mozilla::ErrorResult& aRv) final; |
148 | | |
149 | | virtual nsIContent* GetParentObject() const override |
150 | 0 | { |
151 | 0 | return mSVGElement; |
152 | 0 | } |
153 | | |
154 | | private: |
155 | | virtual ~DOMBaseVal(); |
156 | | }; |
157 | | |
158 | | struct DOMAnimVal final : public mozilla::dom::SVGIRect |
159 | | { |
160 | | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
161 | | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMAnimVal) |
162 | | |
163 | | DOMAnimVal(nsSVGViewBox *aVal, nsSVGElement *aSVGElement) |
164 | | : mozilla::dom::SVGIRect() |
165 | | , mVal(aVal) |
166 | | , mSVGElement(aSVGElement) |
167 | 0 | {} |
168 | | |
169 | | nsSVGViewBox* mVal; // kept alive because it belongs to content |
170 | | RefPtr<nsSVGElement> mSVGElement; |
171 | | |
172 | | // Script may have modified animation parameters or timeline -- DOM getters |
173 | | // need to flush any resample requests to reflect these modifications. |
174 | | float X() const final |
175 | 0 | { |
176 | 0 | mSVGElement->FlushAnimations(); |
177 | 0 | return mVal->GetAnimValue().x; |
178 | 0 | } |
179 | | |
180 | | float Y() const final |
181 | 0 | { |
182 | 0 | mSVGElement->FlushAnimations(); |
183 | 0 | return mVal->GetAnimValue().y; |
184 | 0 | } |
185 | | |
186 | | float Width() const final |
187 | 0 | { |
188 | 0 | mSVGElement->FlushAnimations(); |
189 | 0 | return mVal->GetAnimValue().width; |
190 | 0 | } |
191 | | |
192 | | float Height() const final |
193 | 0 | { |
194 | 0 | mSVGElement->FlushAnimations(); |
195 | 0 | return mVal->GetAnimValue().height; |
196 | 0 | } |
197 | | |
198 | | void SetX(float aX, mozilla::ErrorResult& aRv) final |
199 | 0 | { |
200 | 0 | aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR); |
201 | 0 | } |
202 | | |
203 | | void SetY(float aY, mozilla::ErrorResult& aRv) final |
204 | 0 | { |
205 | 0 | aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR); |
206 | 0 | } |
207 | | |
208 | | void SetWidth(float aWidth, mozilla::ErrorResult& aRv) final |
209 | 0 | { |
210 | 0 | aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR); |
211 | 0 | } |
212 | | |
213 | | void SetHeight(float aHeight, mozilla::ErrorResult& aRv) final |
214 | 0 | { |
215 | 0 | aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR); |
216 | 0 | } |
217 | | |
218 | | virtual nsIContent* GetParentObject() const override |
219 | 0 | { |
220 | 0 | return mSVGElement; |
221 | 0 | } |
222 | | |
223 | | private: |
224 | | virtual ~DOMAnimVal(); |
225 | | |
226 | | }; |
227 | | |
228 | | struct SMILViewBox : public nsISMILAttr |
229 | | { |
230 | | public: |
231 | | SMILViewBox(nsSVGViewBox* aVal, nsSVGElement* aSVGElement) |
232 | 0 | : mVal(aVal), mSVGElement(aSVGElement) {} |
233 | | |
234 | | // These will stay alive because a nsISMILAttr only lives as long |
235 | | // as the Compositing step, and DOM elements don't get a chance to |
236 | | // die during that. |
237 | | nsSVGViewBox* mVal; |
238 | | nsSVGElement* mSVGElement; |
239 | | |
240 | | // nsISMILAttr methods |
241 | | virtual nsresult ValueFromString(const nsAString& aStr, |
242 | | const mozilla::dom::SVGAnimationElement* aSrcElement, |
243 | | nsSMILValue& aValue, |
244 | | bool& aPreventCachingOfSandwich) const override; |
245 | | virtual nsSMILValue GetBaseValue() const override; |
246 | | virtual void ClearAnimValue() override; |
247 | | virtual nsresult SetAnimValue(const nsSMILValue& aValue) override; |
248 | | }; |
249 | | |
250 | | static nsSVGAttrTearoffTable<nsSVGViewBox, mozilla::dom::SVGAnimatedRect> |
251 | | sSVGAnimatedRectTearoffTable; |
252 | | }; |
253 | | |
254 | | #endif // __NS_SVGVIEWBOX_H__ |