DOMSVGTransform.h (5975B)
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 DOM_SVG_DOMSVGTRANSFORM_H_ 8 #define DOM_SVG_DOMSVGTRANSFORM_H_ 9 10 #include "DOMSVGTransformList.h" 11 #include "SVGTransform.h" 12 #include "gfxMatrix.h" 13 #include "mozilla/UniquePtr.h" 14 #include "nsCycleCollectionParticipant.h" 15 #include "nsDebug.h" 16 #include "nsID.h" 17 #include "nsWrapperCache.h" 18 19 #define MOZ_SVG_LIST_INDEX_BIT_COUNT 31 // supports > 2 billion list items 20 21 namespace mozilla::dom { 22 23 struct DOMMatrix2DInit; 24 class SVGElement; 25 class SVGMatrix; 26 27 /** 28 * DOM wrapper for an SVG transform. See DOMSVGLength.h. 29 */ 30 class DOMSVGTransform final : public nsWrapperCache { 31 template <class T> 32 friend class AutoChangeTransformListNotifier; 33 34 public: 35 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(DOMSVGTransform) 36 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(DOMSVGTransform) 37 38 /** 39 * Generic ctor for DOMSVGTransform objects that are created for an attribute. 40 */ 41 DOMSVGTransform(DOMSVGTransformList* aList, uint32_t aListIndex, 42 bool aIsAnimValItem); 43 44 /** 45 * Ctors for creating the objects returned by: 46 * SVGSVGElement.createSVGTransform(), 47 * SVGSVGElement.createSVGTransformFromMatrix(in DOMMatrix2DInit matrix), 48 * SVGTransformList.createSVGTransformFromMatrix(in DOMMatrix2DInit matrix) 49 * which do not initially belong to an attribute. 50 */ 51 DOMSVGTransform(); 52 explicit DOMSVGTransform(const gfxMatrix& aMatrix); 53 DOMSVGTransform(const DOMMatrix2DInit& aMatrix, ErrorResult& aRv); 54 55 /** 56 * Ctor for creating an unowned copy. Used with Clone(). 57 */ 58 explicit DOMSVGTransform(const SVGTransform& aTransform); 59 60 /** 61 * Create an unowned copy of an owned transform. The caller is responsible for 62 * the first AddRef(). 63 */ 64 DOMSVGTransform* Clone() { 65 NS_ASSERTION(mList, "unexpected caller"); 66 return new DOMSVGTransform(InternalItem()); 67 } 68 69 bool IsInList() const { return !!mList; } 70 71 /** 72 * Returns true if our attribute is animating (in which case our animVal is 73 * not simply a mirror of our baseVal). 74 */ 75 bool IsAnimating() const { return mList && mList->IsAnimating(); } 76 77 /** 78 * In future, if this class is used for non-list transforms, this will be 79 * different to IsInList(). 80 */ 81 bool HasOwner() const { return !!mList; } 82 83 /** 84 * This method is called to notify this DOM object that it is being inserted 85 * into a list, and give it the information it needs as a result. 86 * 87 * This object MUST NOT already belong to a list when this method is called. 88 * That's not to say that script can't move these DOM objects between 89 * lists - it can - it's just that the logic to handle that (and send out 90 * the necessary notifications) is located elsewhere (in 91 * DOMSVGTransformList).) 92 */ 93 void InsertingIntoList(DOMSVGTransformList* aList, uint32_t aListIndex, 94 bool aIsAnimValItem); 95 96 static uint32_t MaxListIndex() { 97 return (1U << MOZ_SVG_LIST_INDEX_BIT_COUNT) - 1; 98 } 99 100 /// This method is called to notify this object that its list index changed. 101 void UpdateListIndex(uint32_t aListIndex) { mListIndex = aListIndex; } 102 103 /** 104 * This method is called to notify this DOM object that it is about to be 105 * removed from its current DOM list so that it can first make a copy of its 106 * internal counterpart's values. (If it didn't do this, then it would 107 * "lose" its value on being removed.) 108 */ 109 void RemovingFromList(); 110 111 SVGTransform ToSVGTransform() const { return Transform(); } 112 113 // WebIDL 114 DOMSVGTransformList* GetParentObject() const { return mList; } 115 JSObject* WrapObject(JSContext* aCx, 116 JS::Handle<JSObject*> aGivenProto) override; 117 uint16_t Type() const; 118 dom::SVGMatrix* GetMatrix(); 119 float Angle() const; 120 void SetMatrix(const DOMMatrix2DInit& matrix, ErrorResult& aRv); 121 void SetTranslate(float tx, float ty, ErrorResult& aRv); 122 void SetScale(float sx, float sy, ErrorResult& aRv); 123 void SetRotate(float angle, float cx, float cy, ErrorResult& aRv); 124 void SetSkewX(float angle, ErrorResult& aRv); 125 void SetSkewY(float angle, ErrorResult& aRv); 126 127 protected: 128 ~DOMSVGTransform(); 129 130 // Interface for SVGMatrix's use 131 friend class dom::SVGMatrix; 132 bool IsAnimVal() const { return mIsAnimValItem; } 133 const gfxMatrix& Matrixgfx() const { return Transform().GetMatrix(); } 134 void SetMatrix(const gfxMatrix& aMatrix); 135 136 private: 137 SVGElement* Element() { return mList->Element(); } 138 139 /** 140 * Get a reference to the internal SVGTransform list item that this DOM 141 * wrapper object currently wraps. 142 */ 143 SVGTransform& InternalItem(); 144 const SVGTransform& InternalItem() const; 145 146 #ifdef DEBUG 147 bool IndexIsValid(); 148 #endif 149 150 const SVGTransform& Transform() const { 151 return HasOwner() ? InternalItem() : *mTransform; 152 } 153 SVGTransform& Transform() { 154 return HasOwner() ? InternalItem() : *mTransform; 155 } 156 157 RefPtr<DOMSVGTransformList> mList; 158 159 // Bounds for the following are checked in the ctor, so be sure to update 160 // that if you change the capacity of any of the following. 161 162 uint32_t mListIndex : MOZ_SVG_LIST_INDEX_BIT_COUNT; 163 uint32_t mIsAnimValItem : 1; 164 165 // Usually this class acts as a wrapper for an SVGTransform object which is 166 // part of a list and is accessed by going via the owning Element. 167 // 168 // However, in some circumstances, objects of this class may not be associated 169 // with any particular list and thus, no internal SVGTransform object. In 170 // that case we allocate an SVGTransform object on the heap to store the 171 // data. 172 UniquePtr<SVGTransform> mTransform; 173 }; 174 175 } // namespace mozilla::dom 176 177 #undef MOZ_SVG_LIST_INDEX_BIT_COUNT 178 179 #endif // DOM_SVG_DOMSVGTRANSFORM_H_