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 : #ifndef MOZILLA_DOMSVGPATHSEG_H__
38 : #define MOZILLA_DOMSVGPATHSEG_H__
39 :
40 : #include "DOMSVGPathSegList.h"
41 : #include "nsAutoPtr.h"
42 : #include "nsCycleCollectionParticipant.h"
43 : #include "nsIDOMSVGPathSeg.h"
44 :
45 : class nsSVGElement;
46 :
47 : // We make DOMSVGPathSeg a pseudo-interface to allow us to QI to it in order to
48 : // check that the objects that scripts pass to DOMSVGPathSegList methods are
49 : // our *native* path seg objects.
50 : //
51 : // {494A7566-DC26-40C8-9122-52ABD76870C4}
52 : #define MOZILLA_DOMSVGPATHSEG_IID \
53 : { 0x494A7566, 0xDC26, 0x40C8, { 0x91, 0x22, 0x52, 0xAB, 0xD7, 0x68, 0x70, 0xC4 } }
54 :
55 : #define MOZ_SVG_LIST_INDEX_BIT_COUNT 31
56 :
57 : namespace mozilla {
58 :
59 : /**
60 : * Class DOMSVGPathSeg
61 : *
62 : * This class is the base class of the classes that create the DOM objects that
63 : * wrap the internal path segments that are encoded in an SVGPathData. Its
64 : * sub-classes are also used to create the objects returned by
65 : * SVGPathElement.createSVGPathSegXxx().
66 : *
67 : * See the architecture comment in DOMSVGPathSegList.h for an overview of the
68 : * important points regarding these DOM wrapper structures.
69 : *
70 : * See the architecture comment in DOMSVGLength.h (yes, LENGTH) for an overview
71 : * of the important points regarding how this specific class works.
72 : *
73 : * The main differences between this class and DOMSVGLength is that we have
74 : * sub-classes (it does not), and the "internal counterpart" that we provide a
75 : * DOM wrapper for is a list of floats, not an instance of an internal class.
76 : */
77 : class DOMSVGPathSeg : public nsIDOMSVGPathSeg
78 : {
79 : public:
80 : NS_DECLARE_STATIC_IID_ACCESSOR(MOZILLA_DOMSVGPATHSEG_IID)
81 0 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
82 1464 : NS_DECL_CYCLE_COLLECTION_CLASS(DOMSVGPathSeg)
83 : NS_DECL_NSIDOMSVGPATHSEG
84 :
85 : /**
86 : * Unlike the other list classes, we hide our ctor (because no one should be
87 : * creating instances of this class directly). This factory method in exposed
88 : * instead to take care of creating instances of the correct sub-class.
89 : */
90 : static DOMSVGPathSeg *CreateFor(DOMSVGPathSegList *aList,
91 : PRUint32 aListIndex,
92 : bool aIsAnimValItem);
93 :
94 : /**
95 : * Create an unowned copy of this object. The caller is responsible for the
96 : * first AddRef()!
97 : */
98 : virtual DOMSVGPathSeg* Clone() = 0;
99 :
100 0 : bool IsInList() const {
101 0 : return !!mList;
102 : }
103 :
104 : /**
105 : * In future, if this class is used for non-list segments, this will be
106 : * different to IsInList().
107 : */
108 0 : bool HasOwner() const {
109 0 : return !!mList;
110 : }
111 :
112 : /**
113 : * This method is called to notify this DOM object that it is being inserted
114 : * into a list, and give it the information it needs as a result.
115 : *
116 : * This object MUST NOT already belong to a list when this method is called.
117 : * That's not to say that script can't move these DOM objects between
118 : * lists - it can - it's just that the logic to handle that (and send out
119 : * the necessary notifications) is located elsewhere (in DOMSVGPathSegList).)
120 : */
121 : void InsertingIntoList(DOMSVGPathSegList *aList,
122 : PRUint32 aListIndex,
123 : bool aIsAnimValItem);
124 :
125 0 : static PRUint32 MaxListIndex() {
126 0 : return (1U << MOZ_SVG_LIST_INDEX_BIT_COUNT) - 1;
127 : }
128 :
129 : /// This method is called to notify this object that its list index changed.
130 0 : void UpdateListIndex(PRUint32 aListIndex) {
131 0 : mListIndex = aListIndex;
132 0 : }
133 :
134 : /**
135 : * This method is called to notify this DOM object that it is about to be
136 : * removed from its current DOM list so that it can first make a copy of its
137 : * internal counterpart's values. (If it didn't do this, then it would
138 : * "lose" its value on being removed.)
139 : */
140 : void RemovingFromList();
141 :
142 : /**
143 : * This method converts the segment to a string of floats as found in
144 : * SVGPathData (i.e. the first float contains the type of the segment,
145 : * encoded into a float, followed by its arguments in the same order as they
146 : * are given in the <path> element's 'd' attribute).
147 : */
148 : void ToSVGPathSegEncodedData(float *aData);
149 :
150 : /**
151 : * The type of this path segment.
152 : */
153 : virtual PRUint32 Type() const = 0;
154 :
155 : protected:
156 :
157 : /**
158 : * Generic ctor for DOMSVGPathSeg objects that are created for an attribute.
159 : */
160 : DOMSVGPathSeg(DOMSVGPathSegList *aList,
161 : PRUint32 aListIndex,
162 : bool aIsAnimValItem);
163 :
164 : /**
165 : * Ctor for creating the objects returned by
166 : * SVGPathElement.createSVGPathSegXxx(), which do not initially belong to an
167 : * attribute.
168 : */
169 : DOMSVGPathSeg();
170 :
171 0 : virtual ~DOMSVGPathSeg() {
172 : // Our mList's weak ref to us must be nulled out when we die. If GC has
173 : // unlinked us using the cycle collector code, then that has already
174 : // happened, and mList is null.
175 0 : if (mList) {
176 0 : mList->ItemAt(mListIndex) = nsnull;
177 : }
178 0 : }
179 :
180 0 : nsSVGElement* Element() {
181 0 : return mList->Element();
182 : }
183 :
184 : /**
185 : * Get a reference to the internal SVGPathSeg list item that this DOM wrapper
186 : * object currently wraps.
187 : *
188 : * To simplify the code we just have this one method for obtaining both
189 : * baseVal and animVal internal items. This means that animVal items don't
190 : * get const protection, but then our setter methods guard against changing
191 : * animVal items.
192 : */
193 : float* InternalItem();
194 :
195 : virtual float* PtrToMemberArgs() = 0;
196 :
197 : #ifdef DEBUG
198 : bool IndexIsValid();
199 : #endif
200 :
201 : nsRefPtr<DOMSVGPathSegList> mList;
202 :
203 : // Bounds for the following are checked in the ctor, so be sure to update
204 : // that if you change the capacity of any of the following.
205 :
206 : PRUint32 mListIndex:MOZ_SVG_LIST_INDEX_BIT_COUNT;
207 : PRUint32 mIsAnimValItem:1; // PRUint32 because MSVC won't pack otherwise
208 : };
209 :
210 : NS_DEFINE_STATIC_IID_ACCESSOR(DOMSVGPathSeg, MOZILLA_DOMSVGPATHSEG_IID)
211 :
212 : } // namespace mozilla
213 :
214 : nsIDOMSVGPathSeg*
215 : NS_NewSVGPathSegClosePath();
216 :
217 : nsIDOMSVGPathSeg*
218 : NS_NewSVGPathSegMovetoAbs(float x, float y);
219 :
220 : nsIDOMSVGPathSeg*
221 : NS_NewSVGPathSegMovetoRel(float x, float y);
222 :
223 : nsIDOMSVGPathSeg*
224 : NS_NewSVGPathSegLinetoAbs(float x, float y);
225 :
226 : nsIDOMSVGPathSeg*
227 : NS_NewSVGPathSegLinetoRel(float x, float y);
228 :
229 : nsIDOMSVGPathSeg*
230 : NS_NewSVGPathSegCurvetoCubicAbs(float x, float y,
231 : float x1, float y1,
232 : float x2, float y2);
233 :
234 : nsIDOMSVGPathSeg*
235 : NS_NewSVGPathSegCurvetoCubicRel(float x, float y,
236 : float x1, float y1,
237 : float x2, float y2);
238 :
239 : nsIDOMSVGPathSeg*
240 : NS_NewSVGPathSegCurvetoQuadraticAbs(float x, float y,
241 : float x1, float y1);
242 :
243 : nsIDOMSVGPathSeg*
244 : NS_NewSVGPathSegCurvetoQuadraticRel(float x, float y,
245 : float x1, float y1);
246 :
247 : nsIDOMSVGPathSeg*
248 : NS_NewSVGPathSegArcAbs(float x, float y,
249 : float r1, float r2, float angle,
250 : bool largeArcFlag, bool sweepFlag);
251 :
252 : nsIDOMSVGPathSeg*
253 : NS_NewSVGPathSegArcRel(float x, float y,
254 : float r1, float r2, float angle,
255 : bool largeArcFlag, bool sweepFlag);
256 :
257 : nsIDOMSVGPathSeg*
258 : NS_NewSVGPathSegLinetoHorizontalAbs(float x);
259 :
260 : nsIDOMSVGPathSeg*
261 : NS_NewSVGPathSegLinetoHorizontalRel(float x);
262 :
263 : nsIDOMSVGPathSeg*
264 : NS_NewSVGPathSegLinetoVerticalAbs(float y);
265 :
266 : nsIDOMSVGPathSeg*
267 : NS_NewSVGPathSegLinetoVerticalRel(float y);
268 :
269 : nsIDOMSVGPathSeg*
270 : NS_NewSVGPathSegCurvetoCubicSmoothAbs(float x, float y,
271 : float x2, float y2);
272 :
273 : nsIDOMSVGPathSeg*
274 : NS_NewSVGPathSegCurvetoCubicSmoothRel(float x, float y,
275 : float x2, float y2);
276 :
277 : nsIDOMSVGPathSeg*
278 : NS_NewSVGPathSegCurvetoQuadraticSmoothAbs(float x, float y);
279 :
280 : nsIDOMSVGPathSeg*
281 : NS_NewSVGPathSegCurvetoQuadraticSmoothRel(float x, float y);
282 :
283 : #undef MOZ_SVG_LIST_INDEX_BIT_COUNT
284 :
285 : #endif // MOZILLA_DOMSVGPATHSEG_H__
|