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 MOZILLA_DOMSVGPATHSEG_H__
8 #define MOZILLA_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
23 #define CHECK_ARG_COUNT_IN_SYNC(segType) \
25 ArrayLength(mArgs) == \
26 SVGPathSegUtils::ArgCountForType(uint32_t(segType)) || \
27 uint32_t(segType) == dom::SVGPathSeg_Binding::PATHSEG_CLOSEPATH, \
28 "Arg count/array size out of sync")
30 #define IMPL_SVGPATHSEG_SUBCLASS_COMMON(segName, segType) \
31 explicit DOMSVGPathSeg##segName(const float* aArgs) : DOMSVGPathSeg() { \
32 CHECK_ARG_COUNT_IN_SYNC(segType); \
35 SVGPathSegUtils::ArgCountForType(uint32_t(segType)) * sizeof(float)); \
37 DOMSVGPathSeg##segName(DOMSVGPathSegList* aList, uint32_t aListIndex, \
38 bool aIsAnimValItem) \
39 : DOMSVGPathSeg(aList, aListIndex, aIsAnimValItem) { \
40 CHECK_ARG_COUNT_IN_SYNC(segType); \
42 /* From DOMSVGPathSeg: */ \
43 virtual uint32_t Type() const override { return segType; } \
44 virtual DOMSVGPathSeg* Clone() override { \
45 /* InternalItem() + 1, because we're skipping the encoded seg type */ \
46 float* args = IsInList() ? InternalItem() + 1 : mArgs; \
47 return new DOMSVGPathSeg##segName(args); \
49 virtual float* PtrToMemberArgs() override { return mArgs; } \
51 virtual JSObject* WrapObject(JSContext* aCx, \
52 JS::Handle<JSObject*> aGivenProto) override { \
53 return dom::SVGPathSeg##segName##_Binding::Wrap(aCx, this, aGivenProto); \
59 * This class is the base class of the classes that create the DOM objects that
60 * wrap the internal path segments that are encoded in an SVGPathData. Its
61 * sub-classes are also used to create the objects returned by
62 * SVGPathElement.createSVGPathSegXxx().
64 * See the architecture comment in DOMSVGPathSegList.h for an overview of the
65 * important points regarding these DOM wrapper structures.
67 * See the architecture comment in DOMSVGLength.h (yes, LENGTH) for an overview
68 * of the important points regarding how this specific class works.
70 * The main differences between this class and DOMSVGLength is that we have
71 * sub-classes (it does not), and the "internal counterpart" that we provide a
72 * DOM wrapper for is a list of floats, not an instance of an internal class.
74 class DOMSVGPathSeg
: public nsWrapperCache
{
75 friend class AutoChangePathSegNotifier
;
78 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(DOMSVGPathSeg
)
79 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(DOMSVGPathSeg
)
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
,
90 * Create an unowned copy of this object. The caller is responsible for the
93 virtual DOMSVGPathSeg
* Clone() = 0;
95 bool IsInList() const { return !!mList
; }
98 * In future, if this class is used for non-list segments, this will be
99 * different to IsInList().
101 bool HasOwner() const { return !!mList
; }
104 * This method is called to notify this DOM object that it is being inserted
105 * into a list, and give it the information it needs as a result.
107 * This object MUST NOT already belong to a list when this method is called.
108 * That's not to say that script can't move these DOM objects between
109 * lists - it can - it's just that the logic to handle that (and send out
110 * the necessary notifications) is located elsewhere (in DOMSVGPathSegList).)
112 void InsertingIntoList(DOMSVGPathSegList
* aList
, uint32_t aListIndex
,
113 bool aIsAnimValItem
);
115 static uint32_t MaxListIndex() {
116 return (1U << MOZ_SVG_LIST_INDEX_BIT_COUNT
) - 1;
119 /// This method is called to notify this object that its list index changed.
120 void UpdateListIndex(uint32_t aListIndex
) { mListIndex
= aListIndex
; }
123 * This method is called to notify this DOM object that it is about to be
124 * removed from its current DOM list so that it can first make a copy of its
125 * internal counterpart's values. (If it didn't do this, then it would
126 * "lose" its value on being removed.)
128 void RemovingFromList();
131 * This method converts the segment to a string of floats as found in
132 * SVGPathData (i.e. the first float contains the type of the segment,
133 * encoded into a float, followed by its arguments in the same order as they
134 * are given in the <path> element's 'd' attribute).
136 void ToSVGPathSegEncodedData(float* aRaw
);
139 * The type of this path segment.
141 virtual uint32_t Type() const = 0;
144 DOMSVGPathSegList
* GetParentObject() { return mList
; }
145 uint16_t PathSegType() const { return Type(); }
146 void GetPathSegTypeAsLetter(nsAString
& aPathSegTypeAsLetter
) {
147 aPathSegTypeAsLetter
= SVGPathSegUtils::GetPathSegTypeAsLetter(Type());
149 virtual JSObject
* WrapObject(JSContext
* aCx
,
150 JS::Handle
<JSObject
*> aGivenProto
) override
= 0;
154 * Generic ctor for DOMSVGPathSeg objects that are created for an attribute.
156 DOMSVGPathSeg(DOMSVGPathSegList
* aList
, uint32_t aListIndex
,
157 bool aIsAnimValItem
);
160 * Ctor for creating the objects returned by
161 * SVGPathElement.createSVGPathSegXxx(), which do not initially belong to an
166 virtual ~DOMSVGPathSeg() {
167 // Our mList's weak ref to us must be nulled out when we die. If GC has
168 // unlinked us using the cycle collector code, then that has already
169 // happened, and mList is null.
171 mList
->ItemAt(mListIndex
) = nullptr;
175 dom::SVGElement
* Element() { return mList
->Element(); }
178 * Get a reference to the internal SVGPathSeg list item that this DOM wrapper
179 * object currently wraps.
181 * To simplify the code we just have this one method for obtaining both
182 * baseVal and animVal internal items. This means that animVal items don't
183 * get const protection, but then our setter methods guard against changing
186 float* InternalItem();
188 virtual float* PtrToMemberArgs() = 0;
194 RefPtr
<DOMSVGPathSegList
> mList
;
196 // Bounds for the following are checked in the ctor, so be sure to update
197 // that if you change the capacity of any of the following.
199 uint32_t mListIndex
: MOZ_SVG_LIST_INDEX_BIT_COUNT
;
200 uint32_t mIsAnimValItem
: 1; // uint32_t because MSVC won't pack otherwise
203 class DOMSVGPathSegClosePath
: public DOMSVGPathSeg
{
205 DOMSVGPathSegClosePath() : DOMSVGPathSeg() {}
207 IMPL_SVGPATHSEG_SUBCLASS_COMMON(ClosePath
,
208 dom::SVGPathSeg_Binding::PATHSEG_CLOSEPATH
)
211 // To allow IMPL_SVGPATHSEG_SUBCLASS_COMMON above to compile we need an
212 // mArgs, but since C++ doesn't allow zero-sized arrays we need to give it
213 // one (unused) element.
217 class DOMSVGPathSegMovetoAbs
: public DOMSVGPathSeg
{
219 DOMSVGPathSegMovetoAbs(float x
, float y
) : DOMSVGPathSeg() {
224 IMPL_SVGPATHSEG_SUBCLASS_COMMON(MovetoAbs
,
225 dom::SVGPathSeg_Binding::PATHSEG_MOVETO_ABS
)
228 void SetX(float aX
, ErrorResult
& rv
);
230 void SetY(float aY
, ErrorResult
& rv
);
236 class DOMSVGPathSegMovetoRel
: public DOMSVGPathSeg
{
238 DOMSVGPathSegMovetoRel(float x
, float y
) : DOMSVGPathSeg() {
243 IMPL_SVGPATHSEG_SUBCLASS_COMMON(MovetoRel
,
244 dom::SVGPathSeg_Binding::PATHSEG_MOVETO_REL
)
247 void SetX(float aX
, ErrorResult
& rv
);
249 void SetY(float aY
, ErrorResult
& rv
);
255 class DOMSVGPathSegLinetoAbs
: public DOMSVGPathSeg
{
257 DOMSVGPathSegLinetoAbs(float x
, float y
) : DOMSVGPathSeg() {
262 IMPL_SVGPATHSEG_SUBCLASS_COMMON(LinetoAbs
,
263 dom::SVGPathSeg_Binding::PATHSEG_LINETO_ABS
)
266 void SetX(float aX
, ErrorResult
& rv
);
268 void SetY(float aY
, ErrorResult
& rv
);
274 class DOMSVGPathSegLinetoRel
: public DOMSVGPathSeg
{
276 DOMSVGPathSegLinetoRel(float x
, float y
) : DOMSVGPathSeg() {
281 IMPL_SVGPATHSEG_SUBCLASS_COMMON(LinetoRel
,
282 dom::SVGPathSeg_Binding::PATHSEG_LINETO_REL
)
285 void SetX(float aX
, ErrorResult
& rv
);
287 void SetY(float aY
, ErrorResult
& rv
);
293 class DOMSVGPathSegCurvetoCubicAbs
: public DOMSVGPathSeg
{
295 DOMSVGPathSegCurvetoCubicAbs(float x1
, float y1
, float x2
, float y2
, float x
,
307 void SetX(float aX
, ErrorResult
& rv
);
309 void SetY(float aY
, ErrorResult
& rv
);
311 void SetX1(float aX1
, ErrorResult
& rv
);
313 void SetY1(float aY1
, ErrorResult
& rv
);
315 void SetX2(float aX2
, ErrorResult
& rv
);
317 void SetY2(float aY2
, ErrorResult
& rv
);
319 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
320 CurvetoCubicAbs
, dom::SVGPathSeg_Binding::PATHSEG_CURVETO_CUBIC_ABS
)
326 class DOMSVGPathSegCurvetoCubicRel
: public DOMSVGPathSeg
{
328 DOMSVGPathSegCurvetoCubicRel(float x1
, float y1
, float x2
, float y2
, float x
,
339 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
340 CurvetoCubicRel
, dom::SVGPathSeg_Binding::PATHSEG_CURVETO_CUBIC_REL
)
343 void SetX(float aX
, ErrorResult
& rv
);
345 void SetY(float aY
, ErrorResult
& rv
);
347 void SetX1(float aX1
, ErrorResult
& rv
);
349 void SetY1(float aY1
, ErrorResult
& rv
);
351 void SetX2(float aX2
, ErrorResult
& rv
);
353 void SetY2(float aY2
, ErrorResult
& rv
);
359 class DOMSVGPathSegCurvetoQuadraticAbs
: public DOMSVGPathSeg
{
361 DOMSVGPathSegCurvetoQuadraticAbs(float x1
, float y1
, float x
, float y
)
369 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
371 dom::SVGPathSeg_Binding::PATHSEG_CURVETO_QUADRATIC_ABS
)
374 void SetX(float aX
, ErrorResult
& rv
);
376 void SetY(float aY
, ErrorResult
& rv
);
378 void SetX1(float aX1
, ErrorResult
& rv
);
380 void SetY1(float aY1
, ErrorResult
& rv
);
386 class DOMSVGPathSegCurvetoQuadraticRel
: public DOMSVGPathSeg
{
388 DOMSVGPathSegCurvetoQuadraticRel(float x1
, float y1
, float x
, float y
)
396 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
398 dom::SVGPathSeg_Binding::PATHSEG_CURVETO_QUADRATIC_REL
)
401 void SetX(float aX
, ErrorResult
& rv
);
403 void SetY(float aY
, ErrorResult
& rv
);
405 void SetX1(float aX1
, ErrorResult
& rv
);
407 void SetY1(float aY1
, ErrorResult
& rv
);
413 class DOMSVGPathSegArcAbs
: public DOMSVGPathSeg
{
415 DOMSVGPathSegArcAbs(float r1
, float r2
, float angle
, bool largeArcFlag
,
416 bool sweepFlag
, float x
, float y
)
421 mArgs
[3] = largeArcFlag
;
422 mArgs
[4] = sweepFlag
;
427 IMPL_SVGPATHSEG_SUBCLASS_COMMON(ArcAbs
,
428 dom::SVGPathSeg_Binding::PATHSEG_ARC_ABS
)
431 void SetX(float aX
, ErrorResult
& rv
);
433 void SetY(float aY
, ErrorResult
& rv
);
435 void SetR1(float aR1
, ErrorResult
& rv
);
437 void SetR2(float aR2
, ErrorResult
& rv
);
439 void SetAngle(float aAngle
, ErrorResult
& rv
);
441 void SetLargeArcFlag(bool aLargeArcFlag
, ErrorResult
& rv
);
443 void SetSweepFlag(bool aSweepFlag
, ErrorResult
& rv
);
449 class DOMSVGPathSegArcRel
: public DOMSVGPathSeg
{
451 DOMSVGPathSegArcRel(float r1
, float r2
, float angle
, bool largeArcFlag
,
452 bool sweepFlag
, float x
, float y
)
457 mArgs
[3] = largeArcFlag
;
458 mArgs
[4] = sweepFlag
;
463 IMPL_SVGPATHSEG_SUBCLASS_COMMON(ArcRel
,
464 dom::SVGPathSeg_Binding::PATHSEG_ARC_REL
)
467 void SetX(float aX
, ErrorResult
& rv
);
469 void SetY(float aY
, ErrorResult
& rv
);
471 void SetR1(float aR1
, ErrorResult
& rv
);
473 void SetR2(float aR2
, ErrorResult
& rv
);
475 void SetAngle(float aAngle
, ErrorResult
& rv
);
477 void SetLargeArcFlag(bool aLargeArcFlag
, ErrorResult
& rv
);
479 void SetSweepFlag(bool aSweepFlag
, ErrorResult
& rv
);
485 class DOMSVGPathSegLinetoHorizontalAbs
: public DOMSVGPathSeg
{
487 explicit DOMSVGPathSegLinetoHorizontalAbs(float x
) : DOMSVGPathSeg() {
491 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
493 dom::SVGPathSeg_Binding::PATHSEG_LINETO_HORIZONTAL_ABS
)
496 void SetX(float aX
, ErrorResult
& rv
);
502 class DOMSVGPathSegLinetoHorizontalRel
: public DOMSVGPathSeg
{
504 explicit DOMSVGPathSegLinetoHorizontalRel(float x
) : DOMSVGPathSeg() {
508 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
510 dom::SVGPathSeg_Binding::PATHSEG_LINETO_HORIZONTAL_REL
)
513 void SetX(float aX
, ErrorResult
& rv
);
519 class DOMSVGPathSegLinetoVerticalAbs
: public DOMSVGPathSeg
{
521 explicit DOMSVGPathSegLinetoVerticalAbs(float y
) : DOMSVGPathSeg() {
525 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
526 LinetoVerticalAbs
, dom::SVGPathSeg_Binding::PATHSEG_LINETO_VERTICAL_ABS
)
529 void SetY(float aY
, ErrorResult
& rv
);
535 class DOMSVGPathSegLinetoVerticalRel
: public DOMSVGPathSeg
{
537 explicit DOMSVGPathSegLinetoVerticalRel(float y
) : DOMSVGPathSeg() {
541 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
542 LinetoVerticalRel
, dom::SVGPathSeg_Binding::PATHSEG_LINETO_VERTICAL_REL
)
545 void SetY(float aY
, ErrorResult
& rv
);
551 class DOMSVGPathSegCurvetoCubicSmoothAbs
: public DOMSVGPathSeg
{
553 DOMSVGPathSegCurvetoCubicSmoothAbs(float x2
, float y2
, float x
, float y
)
561 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
562 CurvetoCubicSmoothAbs
,
563 dom::SVGPathSeg_Binding::PATHSEG_CURVETO_CUBIC_SMOOTH_ABS
)
566 void SetX(float aX
, ErrorResult
& rv
);
568 void SetY(float aY
, ErrorResult
& rv
);
570 void SetX2(float aX2
, ErrorResult
& rv
);
572 void SetY2(float aY2
, ErrorResult
& rv
);
578 class DOMSVGPathSegCurvetoCubicSmoothRel
: public DOMSVGPathSeg
{
580 DOMSVGPathSegCurvetoCubicSmoothRel(float x2
, float y2
, float x
, float y
)
588 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
589 CurvetoCubicSmoothRel
,
590 dom::SVGPathSeg_Binding::PATHSEG_CURVETO_CUBIC_SMOOTH_REL
)
593 void SetX(float aX
, ErrorResult
& rv
);
595 void SetY(float aY
, ErrorResult
& rv
);
597 void SetX2(float aX2
, ErrorResult
& rv
);
599 void SetY2(float aY2
, ErrorResult
& rv
);
605 class DOMSVGPathSegCurvetoQuadraticSmoothAbs
: public DOMSVGPathSeg
{
607 DOMSVGPathSegCurvetoQuadraticSmoothAbs(float x
, float y
) : DOMSVGPathSeg() {
612 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
613 CurvetoQuadraticSmoothAbs
,
614 dom::SVGPathSeg_Binding::PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS
)
617 void SetX(float aX
, ErrorResult
& rv
);
619 void SetY(float aY
, ErrorResult
& rv
);
625 class DOMSVGPathSegCurvetoQuadraticSmoothRel
: public DOMSVGPathSeg
{
627 DOMSVGPathSegCurvetoQuadraticSmoothRel(float x
, float y
) : DOMSVGPathSeg() {
632 IMPL_SVGPATHSEG_SUBCLASS_COMMON(
633 CurvetoQuadraticSmoothRel
,
634 dom::SVGPathSeg_Binding::PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL
)
637 void SetX(float aX
, ErrorResult
& rv
);
639 void SetY(float aY
, ErrorResult
& rv
);
646 } // namespace mozilla
648 #undef MOZ_SVG_LIST_INDEX_BIT_COUNT
650 #endif // MOZILLA_DOMSVGPATHSEG_H__