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_DOMSVGTRANSFORM_H_
8 #define DOM_SVG_DOMSVGTRANSFORM_H_
10 #include "DOMSVGTransformList.h"
11 #include "gfxMatrix.h"
12 #include "nsCycleCollectionParticipant.h"
15 #include "SVGTransform.h"
16 #include "nsWrapperCache.h"
17 #include "mozilla/Attributes.h"
18 #include "mozilla/UniquePtr.h"
20 #define MOZ_SVG_LIST_INDEX_BIT_COUNT 31 // supports > 2 billion list items
22 namespace mozilla::dom
{
24 struct DOMMatrix2DInit
;
29 * DOM wrapper for an SVG transform. See DOMSVGLength.h.
31 class DOMSVGTransform final
: public nsWrapperCache
{
33 friend class AutoChangeTransformListNotifier
;
36 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(DOMSVGTransform
)
37 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(DOMSVGTransform
)
40 * Generic ctor for DOMSVGTransform objects that are created for an attribute.
42 DOMSVGTransform(DOMSVGTransformList
* aList
, uint32_t aListIndex
,
46 * Ctors for creating the objects returned by:
47 * SVGSVGElement.createSVGTransform(),
48 * SVGSVGElement.createSVGTransformFromMatrix(in DOMMatrix2DInit matrix),
49 * SVGTransformList.createSVGTransformFromMatrix(in DOMMatrix2DInit matrix)
50 * which do not initially belong to an attribute.
53 explicit DOMSVGTransform(const gfxMatrix
& aMatrix
);
54 DOMSVGTransform(const DOMMatrix2DInit
& aMatrix
, ErrorResult
& rv
);
57 * Ctor for creating an unowned copy. Used with Clone().
59 explicit DOMSVGTransform(const SVGTransform
& aTransform
);
62 * Create an unowned copy of an owned transform. The caller is responsible for
65 DOMSVGTransform
* Clone() {
66 NS_ASSERTION(mList
, "unexpected caller");
67 return new DOMSVGTransform(InternalItem());
70 bool IsInList() const { return !!mList
; }
73 * Returns true if our attribute is animating (in which case our animVal is
74 * not simply a mirror of our baseVal).
76 bool IsAnimating() const { return mList
&& mList
->IsAnimating(); }
79 * In future, if this class is used for non-list transforms, this will be
80 * different to IsInList().
82 bool HasOwner() const { return !!mList
; }
85 * This method is called to notify this DOM object that it is being inserted
86 * into a list, and give it the information it needs as a result.
88 * This object MUST NOT already belong to a list when this method is called.
89 * That's not to say that script can't move these DOM objects between
90 * lists - it can - it's just that the logic to handle that (and send out
91 * the necessary notifications) is located elsewhere (in
92 * DOMSVGTransformList).)
94 void InsertingIntoList(DOMSVGTransformList
* aList
, uint32_t aListIndex
,
97 static uint32_t MaxListIndex() {
98 return (1U << MOZ_SVG_LIST_INDEX_BIT_COUNT
) - 1;
101 /// This method is called to notify this object that its list index changed.
102 void UpdateListIndex(uint32_t aListIndex
) { mListIndex
= aListIndex
; }
105 * This method is called to notify this DOM object that it is about to be
106 * removed from its current DOM list so that it can first make a copy of its
107 * internal counterpart's values. (If it didn't do this, then it would
108 * "lose" its value on being removed.)
110 void RemovingFromList();
112 SVGTransform
ToSVGTransform() const { return Transform(); }
115 DOMSVGTransformList
* GetParentObject() const { return mList
; }
116 JSObject
* WrapObject(JSContext
* aCx
,
117 JS::Handle
<JSObject
*> aGivenProto
) override
;
118 uint16_t Type() const;
119 dom::SVGMatrix
* GetMatrix();
121 void SetMatrix(const DOMMatrix2DInit
& matrix
, ErrorResult
& rv
);
122 void SetTranslate(float tx
, float ty
, ErrorResult
& rv
);
123 void SetScale(float sx
, float sy
, ErrorResult
& rv
);
124 void SetRotate(float angle
, float cx
, float cy
, ErrorResult
& rv
);
125 void SetSkewX(float angle
, ErrorResult
& rv
);
126 void SetSkewY(float angle
, ErrorResult
& rv
);
131 // Interface for SVGMatrix's use
132 friend class dom::SVGMatrix
;
133 bool IsAnimVal() const { return mIsAnimValItem
; }
134 const gfxMatrix
& Matrixgfx() const { return Transform().GetMatrix(); }
135 void SetMatrix(const gfxMatrix
& aMatrix
);
138 SVGElement
* Element() { return mList
->Element(); }
141 * Get a reference to the internal SVGTransform list item that this DOM
142 * wrapper object currently wraps.
144 SVGTransform
& InternalItem();
145 const SVGTransform
& InternalItem() const;
151 const SVGTransform
& Transform() const {
152 return HasOwner() ? InternalItem() : *mTransform
;
154 SVGTransform
& Transform() {
155 return HasOwner() ? InternalItem() : *mTransform
;
158 RefPtr
<DOMSVGTransformList
> mList
;
160 // Bounds for the following are checked in the ctor, so be sure to update
161 // that if you change the capacity of any of the following.
163 uint32_t mListIndex
: MOZ_SVG_LIST_INDEX_BIT_COUNT
;
164 uint32_t mIsAnimValItem
: 1;
166 // Usually this class acts as a wrapper for an SVGTransform object which is
167 // part of a list and is accessed by going via the owning Element.
169 // However, in some circumstances, objects of this class may not be associated
170 // with any particular list and thus, no internal SVGTransform object. In
171 // that case we allocate an SVGTransform object on the heap to store the
173 UniquePtr
<SVGTransform
> mTransform
;
176 } // namespace mozilla::dom
178 #undef MOZ_SVG_LIST_INDEX_BIT_COUNT
180 #endif // DOM_SVG_DOMSVGTRANSFORM_H_