DOMSVGPoint.h (6730B)
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_DOMSVGPOINT_H_ 8 #define DOM_SVG_DOMSVGPOINT_H_ 9 10 #include "DOMSVGPointList.h" 11 #include "SVGPoint.h" 12 #include "mozilla/dom/SVGSVGElement.h" 13 #include "mozilla/gfx/2D.h" 14 #include "nsCycleCollectionParticipant.h" 15 #include "nsDebug.h" 16 #include "nsWrapperCache.h" 17 18 #define MOZ_SVG_LIST_INDEX_BIT_COUNT 29 19 20 namespace mozilla::dom { 21 struct DOMMatrix2DInit; 22 23 /** 24 * Class DOMSVGPoint 25 * 26 * This class creates the DOM objects that wrap internal SVGPoint objects that 27 * are in an SVGPointList. It is also used to create the objects returned by 28 * SVGSVGElement.createSVGPoint() and other functions that return DOM SVGPoint 29 * objects. 30 * 31 * See the architecture comment in DOMSVGPointList.h for an overview of the 32 * important points regarding these DOM wrapper structures. 33 * 34 * See the architecture comment in DOMSVGLength.h (yes, LENGTH) for an overview 35 * of the important points regarding how this specific class works. 36 */ 37 class DOMSVGPoint final : public nsWrapperCache { 38 template <class T> 39 friend class AutoChangePointListNotifier; 40 41 using Point = gfx::Point; 42 43 public: 44 /** 45 * Generic ctor for DOMSVGPoint objects that are created for an attribute. 46 */ 47 DOMSVGPoint(DOMSVGPointList* aList, uint32_t aListIndex, bool aIsAnimValItem) 48 : mVal(nullptr), 49 mOwner(aList), 50 mListIndex(aListIndex), 51 mIsAnimValItem(aIsAnimValItem), 52 mIsTranslatePoint(false), 53 mIsInTearoffTable(false) { 54 // These shifts are in sync with the members. 55 MOZ_ASSERT(aList && aListIndex <= MaxListIndex(), "bad arg"); 56 57 MOZ_ASSERT(IndexIsValid(), "Bad index for DOMSVGPoint!"); 58 } 59 60 // Constructor for unowned points and SVGSVGElement.createSVGPoint 61 explicit DOMSVGPoint(const Point& aPt) 62 : mListIndex(0), 63 mIsAnimValItem(false), 64 mIsTranslatePoint(false), 65 mIsInTearoffTable(false) { 66 // In this case we own mVal 67 mVal = new SVGPoint(aPt.x, aPt.y); 68 } 69 70 private: 71 // The translate of an SVGSVGElement 72 DOMSVGPoint(SVGPoint* aPt, SVGSVGElement* aSVGSVGElement) 73 : mVal(aPt), 74 mOwner(ToSupports(aSVGSVGElement)), 75 mListIndex(0), 76 mIsAnimValItem(false), 77 mIsTranslatePoint(true), 78 mIsInTearoffTable(false) {} 79 80 virtual ~DOMSVGPoint() { CleanupWeakRefs(); } 81 82 public: 83 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(DOMSVGPoint) 84 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(DOMSVGPoint) 85 86 static already_AddRefed<DOMSVGPoint> GetTranslateTearOff( 87 SVGPoint* aVal, SVGSVGElement* aSVGSVGElement); 88 89 bool IsInList() const { return HasOwner() && !IsTranslatePoint(); } 90 91 /** 92 * "Owner" here means that the instance has an 93 * internal counterpart from which it gets its values. (A better name may 94 * be HasWrappee().) 95 */ 96 bool HasOwner() const { return !!mOwner; } 97 98 bool IsTranslatePoint() const { return mIsTranslatePoint; } 99 100 void DidChangeTranslate(); 101 102 /** 103 * This method is called to notify this DOM object that it is being inserted 104 * into a list, and give it the information it needs as a result. 105 * 106 * This object MUST NOT already belong to a list when this method is called. 107 * That's not to say that script can't move these DOM objects between 108 * lists - it can - it's just that the logic to handle that (and send out 109 * the necessary notifications) is located elsewhere (in DOMSVGPointList).) 110 */ 111 void InsertingIntoList(DOMSVGPointList* aList, uint32_t aListIndex, 112 bool aIsAnimValItem); 113 114 static uint32_t MaxListIndex() { 115 return (1U << MOZ_SVG_LIST_INDEX_BIT_COUNT) - 1; 116 } 117 118 /// This method is called to notify this object that its list index changed. 119 void UpdateListIndex(uint32_t aListIndex) { mListIndex = aListIndex; } 120 121 /** 122 * This method is called to notify this DOM object that it is about to be 123 * removed from its current DOM list so that it can first make a copy of its 124 * internal counterpart's values. (If it didn't do this, then it would 125 * "lose" its value on being removed.) 126 */ 127 void RemovingFromList(); 128 129 SVGPoint ToSVGPoint() { return InternalItem(); } 130 131 // WebIDL 132 float X(); 133 void SetX(float aX, ErrorResult& rv); 134 float Y(); 135 void SetY(float aY, ErrorResult& rv); 136 already_AddRefed<DOMSVGPoint> MatrixTransform(const DOMMatrix2DInit& aMatrix, 137 ErrorResult& aRv); 138 139 nsISupports* GetParentObject() { return Element(); } 140 141 /** 142 * Returns true if our attribute is animating (in which case our animVal is 143 * not simply a mirror of our baseVal). 144 */ 145 bool AttrIsAnimating() const; 146 147 JSObject* WrapObject(JSContext* cx, 148 JS::Handle<JSObject*> aGivenProto) override; 149 150 DOMSVGPoint* Copy() { return new DOMSVGPoint(InternalItem()); } 151 152 private: 153 #ifdef DEBUG 154 bool IndexIsValid(); 155 #endif 156 157 SVGElement* Element(); 158 159 /** 160 * Clears soon-to-be-invalid weak references in external objects that were 161 * set up during the creation of this object. This should be called during 162 * destruction and during cycle collection. 163 */ 164 void CleanupWeakRefs(); 165 166 /** 167 * Get a reference to the internal SVGPoint list item that this DOM wrapper 168 * object currently wraps. 169 */ 170 SVGPoint& InternalItem(); 171 172 SVGPoint* mVal; // If mIsTranslatePoint is true, the element owns 173 // the value. Otherwise we do. 174 RefPtr<nsISupports> mOwner; // If mIsTranslatePoint is true, this is an 175 // SVGSVGElement, if we're unowned it's null, or 176 // we're in a list and it's a DOMSVGPointList 177 178 // Bounds for the following are checked in the ctor, so be sure to update 179 // that if you change the capacity of any of the following. 180 181 uint32_t mListIndex : MOZ_SVG_LIST_INDEX_BIT_COUNT; 182 uint32_t mIsAnimValItem : 1; // True if We're the animated value of a list 183 uint32_t mIsTranslatePoint : 1; // true iff our owner is a SVGSVGElement 184 185 // Tracks whether we're in the tearoff table. Initialized to false in the 186 // ctor, but then immediately set to true if/when we're added to the table 187 // (not all instances are). Updated to false when we're removed from the 188 // table (at which point we're being destructed or soon-to-be destructed). 189 uint32_t mIsInTearoffTable : 1; 190 }; 191 192 } // namespace mozilla::dom 193 194 #undef MOZ_SVG_LIST_INDEX_BIT_COUNT 195 196 #endif // DOM_SVG_DOMSVGPOINT_H_