no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / svg / DOMSVGPathSeg.h
blob7447526aaeed9d6f30eacb8d6e16c566841bd45a
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/. */
7 #ifndef DOM_SVG_DOMSVGPATHSEG_H_
8 #define DOM_SVG_DOMSVGPATHSEG_H_
10 #include "DOMSVGPathSegList.h"
11 #include "nsCycleCollectionParticipant.h"
12 #include "nsWrapperCache.h"
13 #include "SVGPathSegUtils.h"
14 #include "mozilla/dom/SVGPathSegBinding.h"
16 #define MOZ_SVG_LIST_INDEX_BIT_COUNT 31
18 namespace mozilla::dom {
20 class SVGElement;
22 #define CHECK_ARG_COUNT_IN_SYNC(segType) \
23 MOZ_ASSERT( \
24 ArrayLength(mArgs) == \
25 SVGPathSegUtils::ArgCountForType(uint32_t(segType)) || \
26 uint32_t(segType) == dom::SVGPathSeg_Binding::PATHSEG_CLOSEPATH, \
27 "Arg count/array size out of sync")
29 #define IMPL_SVGPATHSEG_SUBCLASS_COMMON(segName, segType) \
30 explicit DOMSVGPathSeg##segName(const float* aArgs) : DOMSVGPathSeg() { \
31 CHECK_ARG_COUNT_IN_SYNC(segType); \
32 memcpy( \
33 mArgs, aArgs, \
34 SVGPathSegUtils::ArgCountForType(uint32_t(segType)) * sizeof(float)); \
35 } \
36 DOMSVGPathSeg##segName(DOMSVGPathSegList* aList, uint32_t aListIndex, \
37 bool aIsAnimValItem) \
38 : DOMSVGPathSeg(aList, aListIndex, aIsAnimValItem) { \
39 CHECK_ARG_COUNT_IN_SYNC(segType); \
40 } \
41 /* From DOMSVGPathSeg: */ \
42 uint32_t Type() const override { return segType; } \
43 DOMSVGPathSeg* Clone() override { \
44 /* InternalItem() + 1, because we're skipping the encoded seg type */ \
45 float* args = IsInList() ? InternalItem() + 1 : mArgs; \
46 return new DOMSVGPathSeg##segName(args); \
47 } \
48 float* PtrToMemberArgs() override { return mArgs; } \
50 JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) \
51 override { \
52 return dom::SVGPathSeg##segName##_Binding::Wrap(aCx, this, aGivenProto); \
55 /**
56 * Class DOMSVGPathSeg
58 * This class is the base class of the classes that create the DOM objects that
59 * wrap the internal path segments that are encoded in an SVGPathData. Its
60 * sub-classes are also used to create the objects returned by
61 * SVGPathElement.createSVGPathSegXxx().
63 * See the architecture comment in DOMSVGPathSegList.h for an overview of the
64 * important points regarding these DOM wrapper structures.
66 * See the architecture comment in DOMSVGLength.h (yes, LENGTH) for an overview
67 * of the important points regarding how this specific class works.
69 * The main differences between this class and DOMSVGLength is that we have
70 * sub-classes (it does not), and the "internal counterpart" that we provide a
71 * DOM wrapper for is a list of floats, not an instance of an internal class.
73 class DOMSVGPathSeg : public nsWrapperCache {
74 template <class T>
75 friend class AutoChangePathSegListNotifier;
77 public:
78 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(DOMSVGPathSeg)
79 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(DOMSVGPathSeg)
81 /**
82 * Unlike the other list classes, we hide our ctor (because no one should be
83 * creating instances of this class directly). This factory method in exposed
84 * instead to take care of creating instances of the correct sub-class.
86 static DOMSVGPathSeg* CreateFor(DOMSVGPathSegList* aList, uint32_t aListIndex,
87 bool aIsAnimValItem);
89 /**
90 * Create an unowned copy of this object. The caller is responsible for the
91 * first AddRef()!
93 virtual DOMSVGPathSeg* Clone() = 0;
95 bool IsInList() const { return !!mList; }
97 /**
98 * Returns true if our attribute is animating (in which case our animVal is
99 * not simply a mirror of our baseVal).
101 bool AttrIsAnimating() const { return mList && mList->AttrIsAnimating(); }
104 * In future, if this class is used for non-list segments, this will be
105 * different to IsInList().
107 bool HasOwner() const { return !!mList; }
110 * This method is called to notify this DOM object that it is being inserted
111 * into a list, and give it the information it needs as a result.
113 * This object MUST NOT already belong to a list when this method is called.
114 * That's not to say that script can't move these DOM objects between
115 * lists - it can - it's just that the logic to handle that (and send out
116 * the necessary notifications) is located elsewhere (in DOMSVGPathSegList).)
118 void InsertingIntoList(DOMSVGPathSegList* aList, uint32_t aListIndex,
119 bool aIsAnimValItem);
121 static uint32_t MaxListIndex() {
122 return (1U << MOZ_SVG_LIST_INDEX_BIT_COUNT) - 1;
125 /// This method is called to notify this object that its list index changed.
126 void UpdateListIndex(uint32_t aListIndex) { mListIndex = aListIndex; }
129 * This method is called to notify this DOM object that it is about to be
130 * removed from its current DOM list so that it can first make a copy of its
131 * internal counterpart's values. (If it didn't do this, then it would
132 * "lose" its value on being removed.)
134 void RemovingFromList();
137 * This method converts the segment to a string of floats as found in
138 * SVGPathData (i.e. the first float contains the type of the segment,
139 * encoded into a float, followed by its arguments in the same order as they
140 * are given in the <path> element's 'd' attribute).
142 void ToSVGPathSegEncodedData(float* aRaw);
145 * The type of this path segment.
147 virtual uint32_t Type() const = 0;
149 // WebIDL
150 DOMSVGPathSegList* GetParentObject() { return mList; }
151 uint16_t PathSegType() const { return Type(); }
152 void GetPathSegTypeAsLetter(nsAString& aPathSegTypeAsLetter) {
153 aPathSegTypeAsLetter = SVGPathSegUtils::GetPathSegTypeAsLetter(Type());
155 virtual JSObject* WrapObject(JSContext* aCx,
156 JS::Handle<JSObject*> aGivenProto) override = 0;
158 protected:
160 * Generic ctor for DOMSVGPathSeg objects that are created for an attribute.
162 DOMSVGPathSeg(DOMSVGPathSegList* aList, uint32_t aListIndex,
163 bool aIsAnimValItem);
166 * Ctor for creating the objects returned by
167 * SVGPathElement.createSVGPathSegXxx(), which do not initially belong to an
168 * attribute.
170 DOMSVGPathSeg();
172 virtual ~DOMSVGPathSeg() {
173 // Our mList's weak ref to us must be nulled out when we die. If GC has
174 // unlinked us using the cycle collector code, then that has already
175 // happened, and mList is null.
176 if (mList) {
177 mList->ItemAt(mListIndex) = nullptr;
181 dom::SVGElement* Element() { return mList->Element(); }
184 * Get a reference to the internal SVGPathSeg list item that this DOM wrapper
185 * object currently wraps.
187 * To simplify the code we just have this one method for obtaining both
188 * baseVal and animVal internal items. This means that animVal items don't
189 * get const protection, but then our setter methods guard against changing
190 * animVal items.
192 float* InternalItem();
194 virtual float* PtrToMemberArgs() = 0;
196 #ifdef DEBUG
197 bool IndexIsValid();
198 #endif
200 RefPtr<DOMSVGPathSegList> mList;
202 // Bounds for the following are checked in the ctor, so be sure to update
203 // that if you change the capacity of any of the following.
205 uint32_t mListIndex : MOZ_SVG_LIST_INDEX_BIT_COUNT;
206 uint32_t mIsAnimValItem : 1; // uint32_t because MSVC won't pack otherwise
209 class DOMSVGPathSegClosePath : public DOMSVGPathSeg {
210 public:
211 DOMSVGPathSegClosePath() {}
213 IMPL_SVGPATHSEG_SUBCLASS_COMMON(ClosePath,
214 dom::SVGPathSeg_Binding::PATHSEG_CLOSEPATH)
216 protected:
217 // To allow IMPL_SVGPATHSEG_SUBCLASS_COMMON above to compile we need an
218 // mArgs, but since C++ doesn't allow zero-sized arrays we need to give it
219 // one (unused) element.
220 float mArgs[1];
223 class DOMSVGPathSegMovetoAbs : public DOMSVGPathSeg {
224 public:
225 DOMSVGPathSegMovetoAbs(float x, float y) {
226 mArgs[0] = x;
227 mArgs[1] = y;
230 IMPL_SVGPATHSEG_SUBCLASS_COMMON(MovetoAbs,
231 dom::SVGPathSeg_Binding::PATHSEG_MOVETO_ABS)
233 float X();
234 void SetX(float aX, ErrorResult& rv);
235 float Y();
236 void SetY(float aY, ErrorResult& rv);
238 protected:
239 float mArgs[2];
242 class DOMSVGPathSegMovetoRel : public DOMSVGPathSeg {
243 public:
244 DOMSVGPathSegMovetoRel(float x, float y) {
245 mArgs[0] = x;
246 mArgs[1] = y;
249 IMPL_SVGPATHSEG_SUBCLASS_COMMON(MovetoRel,
250 dom::SVGPathSeg_Binding::PATHSEG_MOVETO_REL)
252 float X();
253 void SetX(float aX, ErrorResult& rv);
254 float Y();
255 void SetY(float aY, ErrorResult& rv);
257 protected:
258 float mArgs[2];
261 class DOMSVGPathSegLinetoAbs : public DOMSVGPathSeg {
262 public:
263 DOMSVGPathSegLinetoAbs(float x, float y) {
264 mArgs[0] = x;
265 mArgs[1] = y;
268 IMPL_SVGPATHSEG_SUBCLASS_COMMON(LinetoAbs,
269 dom::SVGPathSeg_Binding::PATHSEG_LINETO_ABS)
271 float X();
272 void SetX(float aX, ErrorResult& rv);
273 float Y();
274 void SetY(float aY, ErrorResult& rv);
276 protected:
277 float mArgs[2];
280 class DOMSVGPathSegLinetoRel : public DOMSVGPathSeg {
281 public:
282 DOMSVGPathSegLinetoRel(float x, float y) {
283 mArgs[0] = x;
284 mArgs[1] = y;
287 IMPL_SVGPATHSEG_SUBCLASS_COMMON(LinetoRel,
288 dom::SVGPathSeg_Binding::PATHSEG_LINETO_REL)
290 float X();
291 void SetX(float aX, ErrorResult& rv);
292 float Y();
293 void SetY(float aY, ErrorResult& rv);
295 protected:
296 float mArgs[2];
299 class DOMSVGPathSegCurvetoCubicAbs : public DOMSVGPathSeg {
300 public:
301 DOMSVGPathSegCurvetoCubicAbs(float x1, float y1, float x2, float y2, float x,
302 float y) {
303 mArgs[0] = x1;
304 mArgs[1] = y1;
305 mArgs[2] = x2;
306 mArgs[3] = y2;
307 mArgs[4] = x;
308 mArgs[5] = y;
311 float X();
312 void SetX(float aX, ErrorResult& rv);
313 float Y();
314 void SetY(float aY, ErrorResult& rv);
315 float X1();
316 void SetX1(float aX1, ErrorResult& rv);
317 float Y1();
318 void SetY1(float aY1, ErrorResult& rv);
319 float X2();
320 void SetX2(float aX2, ErrorResult& rv);
321 float Y2();
322 void SetY2(float aY2, ErrorResult& rv);
324 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
325 CurvetoCubicAbs, dom::SVGPathSeg_Binding::PATHSEG_CURVETO_CUBIC_ABS)
327 protected:
328 float mArgs[6];
331 class DOMSVGPathSegCurvetoCubicRel : public DOMSVGPathSeg {
332 public:
333 DOMSVGPathSegCurvetoCubicRel(float x1, float y1, float x2, float y2, float x,
334 float y) {
335 mArgs[0] = x1;
336 mArgs[1] = y1;
337 mArgs[2] = x2;
338 mArgs[3] = y2;
339 mArgs[4] = x;
340 mArgs[5] = y;
343 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
344 CurvetoCubicRel, dom::SVGPathSeg_Binding::PATHSEG_CURVETO_CUBIC_REL)
346 float X();
347 void SetX(float aX, ErrorResult& rv);
348 float Y();
349 void SetY(float aY, ErrorResult& rv);
350 float X1();
351 void SetX1(float aX1, ErrorResult& rv);
352 float Y1();
353 void SetY1(float aY1, ErrorResult& rv);
354 float X2();
355 void SetX2(float aX2, ErrorResult& rv);
356 float Y2();
357 void SetY2(float aY2, ErrorResult& rv);
359 protected:
360 float mArgs[6];
363 class DOMSVGPathSegCurvetoQuadraticAbs : public DOMSVGPathSeg {
364 public:
365 DOMSVGPathSegCurvetoQuadraticAbs(float x1, float y1, float x, float y) {
366 mArgs[0] = x1;
367 mArgs[1] = y1;
368 mArgs[2] = x;
369 mArgs[3] = y;
372 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
373 CurvetoQuadraticAbs,
374 dom::SVGPathSeg_Binding::PATHSEG_CURVETO_QUADRATIC_ABS)
376 float X();
377 void SetX(float aX, ErrorResult& rv);
378 float Y();
379 void SetY(float aY, ErrorResult& rv);
380 float X1();
381 void SetX1(float aX1, ErrorResult& rv);
382 float Y1();
383 void SetY1(float aY1, ErrorResult& rv);
385 protected:
386 float mArgs[4];
389 class DOMSVGPathSegCurvetoQuadraticRel : public DOMSVGPathSeg {
390 public:
391 DOMSVGPathSegCurvetoQuadraticRel(float x1, float y1, float x, float y) {
392 mArgs[0] = x1;
393 mArgs[1] = y1;
394 mArgs[2] = x;
395 mArgs[3] = y;
398 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
399 CurvetoQuadraticRel,
400 dom::SVGPathSeg_Binding::PATHSEG_CURVETO_QUADRATIC_REL)
402 float X();
403 void SetX(float aX, ErrorResult& rv);
404 float Y();
405 void SetY(float aY, ErrorResult& rv);
406 float X1();
407 void SetX1(float aX1, ErrorResult& rv);
408 float Y1();
409 void SetY1(float aY1, ErrorResult& rv);
411 protected:
412 float mArgs[4];
415 class DOMSVGPathSegArcAbs : public DOMSVGPathSeg {
416 public:
417 DOMSVGPathSegArcAbs(float r1, float r2, float angle, bool largeArcFlag,
418 bool sweepFlag, float x, float y) {
419 mArgs[0] = r1;
420 mArgs[1] = r2;
421 mArgs[2] = angle;
422 mArgs[3] = largeArcFlag;
423 mArgs[4] = sweepFlag;
424 mArgs[5] = x;
425 mArgs[6] = y;
428 IMPL_SVGPATHSEG_SUBCLASS_COMMON(ArcAbs,
429 dom::SVGPathSeg_Binding::PATHSEG_ARC_ABS)
431 float X();
432 void SetX(float aX, ErrorResult& rv);
433 float Y();
434 void SetY(float aY, ErrorResult& rv);
435 float R1();
436 void SetR1(float aR1, ErrorResult& rv);
437 float R2();
438 void SetR2(float aR2, ErrorResult& rv);
439 float Angle();
440 void SetAngle(float aAngle, ErrorResult& rv);
441 bool LargeArcFlag();
442 void SetLargeArcFlag(bool aLargeArcFlag, ErrorResult& rv);
443 bool SweepFlag();
444 void SetSweepFlag(bool aSweepFlag, ErrorResult& rv);
446 protected:
447 float mArgs[7];
450 class DOMSVGPathSegArcRel : public DOMSVGPathSeg {
451 public:
452 DOMSVGPathSegArcRel(float r1, float r2, float angle, bool largeArcFlag,
453 bool sweepFlag, float x, float y) {
454 mArgs[0] = r1;
455 mArgs[1] = r2;
456 mArgs[2] = angle;
457 mArgs[3] = largeArcFlag;
458 mArgs[4] = sweepFlag;
459 mArgs[5] = x;
460 mArgs[6] = y;
463 IMPL_SVGPATHSEG_SUBCLASS_COMMON(ArcRel,
464 dom::SVGPathSeg_Binding::PATHSEG_ARC_REL)
466 float X();
467 void SetX(float aX, ErrorResult& rv);
468 float Y();
469 void SetY(float aY, ErrorResult& rv);
470 float R1();
471 void SetR1(float aR1, ErrorResult& rv);
472 float R2();
473 void SetR2(float aR2, ErrorResult& rv);
474 float Angle();
475 void SetAngle(float aAngle, ErrorResult& rv);
476 bool LargeArcFlag();
477 void SetLargeArcFlag(bool aLargeArcFlag, ErrorResult& rv);
478 bool SweepFlag();
479 void SetSweepFlag(bool aSweepFlag, ErrorResult& rv);
481 protected:
482 float mArgs[7];
485 class DOMSVGPathSegLinetoHorizontalAbs : public DOMSVGPathSeg {
486 public:
487 explicit DOMSVGPathSegLinetoHorizontalAbs(float x) { mArgs[0] = x; }
489 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
490 LinetoHorizontalAbs,
491 dom::SVGPathSeg_Binding::PATHSEG_LINETO_HORIZONTAL_ABS)
493 float X();
494 void SetX(float aX, ErrorResult& rv);
496 protected:
497 float mArgs[1];
500 class DOMSVGPathSegLinetoHorizontalRel : public DOMSVGPathSeg {
501 public:
502 explicit DOMSVGPathSegLinetoHorizontalRel(float x) { mArgs[0] = x; }
504 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
505 LinetoHorizontalRel,
506 dom::SVGPathSeg_Binding::PATHSEG_LINETO_HORIZONTAL_REL)
508 float X();
509 void SetX(float aX, ErrorResult& rv);
511 protected:
512 float mArgs[1];
515 class DOMSVGPathSegLinetoVerticalAbs : public DOMSVGPathSeg {
516 public:
517 explicit DOMSVGPathSegLinetoVerticalAbs(float y) { mArgs[0] = y; }
519 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
520 LinetoVerticalAbs, dom::SVGPathSeg_Binding::PATHSEG_LINETO_VERTICAL_ABS)
522 float Y();
523 void SetY(float aY, ErrorResult& rv);
525 protected:
526 float mArgs[1];
529 class DOMSVGPathSegLinetoVerticalRel : public DOMSVGPathSeg {
530 public:
531 explicit DOMSVGPathSegLinetoVerticalRel(float y) { mArgs[0] = y; }
533 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
534 LinetoVerticalRel, dom::SVGPathSeg_Binding::PATHSEG_LINETO_VERTICAL_REL)
536 float Y();
537 void SetY(float aY, ErrorResult& rv);
539 protected:
540 float mArgs[1];
543 class DOMSVGPathSegCurvetoCubicSmoothAbs : public DOMSVGPathSeg {
544 public:
545 DOMSVGPathSegCurvetoCubicSmoothAbs(float x2, float y2, float x, float y) {
546 mArgs[0] = x2;
547 mArgs[1] = y2;
548 mArgs[2] = x;
549 mArgs[3] = y;
552 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
553 CurvetoCubicSmoothAbs,
554 dom::SVGPathSeg_Binding::PATHSEG_CURVETO_CUBIC_SMOOTH_ABS)
556 float X();
557 void SetX(float aX, ErrorResult& rv);
558 float Y();
559 void SetY(float aY, ErrorResult& rv);
560 float X2();
561 void SetX2(float aX2, ErrorResult& rv);
562 float Y2();
563 void SetY2(float aY2, ErrorResult& rv);
565 protected:
566 float mArgs[4];
569 class DOMSVGPathSegCurvetoCubicSmoothRel : public DOMSVGPathSeg {
570 public:
571 DOMSVGPathSegCurvetoCubicSmoothRel(float x2, float y2, float x, float y) {
572 mArgs[0] = x2;
573 mArgs[1] = y2;
574 mArgs[2] = x;
575 mArgs[3] = y;
578 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
579 CurvetoCubicSmoothRel,
580 dom::SVGPathSeg_Binding::PATHSEG_CURVETO_CUBIC_SMOOTH_REL)
582 float X();
583 void SetX(float aX, ErrorResult& rv);
584 float Y();
585 void SetY(float aY, ErrorResult& rv);
586 float X2();
587 void SetX2(float aX2, ErrorResult& rv);
588 float Y2();
589 void SetY2(float aY2, ErrorResult& rv);
591 protected:
592 float mArgs[4];
595 class DOMSVGPathSegCurvetoQuadraticSmoothAbs : public DOMSVGPathSeg {
596 public:
597 DOMSVGPathSegCurvetoQuadraticSmoothAbs(float x, float y) {
598 mArgs[0] = x;
599 mArgs[1] = y;
602 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
603 CurvetoQuadraticSmoothAbs,
604 dom::SVGPathSeg_Binding::PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS)
606 float X();
607 void SetX(float aX, ErrorResult& rv);
608 float Y();
609 void SetY(float aY, ErrorResult& rv);
611 protected:
612 float mArgs[2];
615 class DOMSVGPathSegCurvetoQuadraticSmoothRel : public DOMSVGPathSeg {
616 public:
617 DOMSVGPathSegCurvetoQuadraticSmoothRel(float x, float y) {
618 mArgs[0] = x;
619 mArgs[1] = y;
622 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
623 CurvetoQuadraticSmoothRel,
624 dom::SVGPathSeg_Binding::PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL)
626 float X();
627 void SetX(float aX, ErrorResult& rv);
628 float Y();
629 void SetY(float aY, ErrorResult& rv);
631 protected:
632 float mArgs[2];
635 } // namespace mozilla::dom
637 #undef MOZ_SVG_LIST_INDEX_BIT_COUNT
639 #endif // DOM_SVG_DOMSVGPATHSEG_H_