nsISVGPoint.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #pragma once
  6. #include "nsCycleCollectionParticipant.h"
  7. #include "nsWrapperCache.h"
  8. #include "mozilla/dom/SVGPointBinding.h"
  9. #include "DOMSVGPointList.h"
  10. // {d6b6c440-af8d-40ee-856b-02a317cab275}
  11. #define MOZILLA_NSISVGPOINT_IID \
  12. { 0xd6b6c440, 0xaf8d, 0x40ee, \
  13. { 0x85, 0x6b, 0x02, 0xa3, 0x17, 0xca, 0xb2, 0x75 } }
  14. #define MOZ_SVG_LIST_INDEX_BIT_COUNT 29
  15. namespace mozilla {
  16. namespace dom {
  17. class SVGMatrix;
  18. } // namespace dom
  19. /**
  20. * Class nsISVGPoint
  21. *
  22. * This class creates the DOM objects that wrap internal SVGPoint objects.
  23. * An nsISVGPoint can be either a DOMSVGPoint or a DOMSVGTranslatePoint
  24. */
  25. class nsISVGPoint : public nsISupports,
  26. public nsWrapperCache
  27. {
  28. public:
  29. NS_DECLARE_STATIC_IID_ACCESSOR(MOZILLA_NSISVGPOINT_IID)
  30. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  31. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsISVGPoint)
  32. /**
  33. * Generic ctor for DOMSVGPoint objects that are created for an attribute.
  34. */
  35. explicit nsISVGPoint()
  36. : mList(nullptr)
  37. , mListIndex(0)
  38. , mIsReadonly(false)
  39. , mIsAnimValItem(false)
  40. , mIsTranslatePoint(false)
  41. {
  42. }
  43. explicit nsISVGPoint(SVGPoint* aPt, bool aIsTranslatePoint)
  44. : mList(nullptr)
  45. , mListIndex(0)
  46. , mIsReadonly(false)
  47. , mIsAnimValItem(false)
  48. , mIsTranslatePoint(aIsTranslatePoint)
  49. {
  50. mPt.mX = aPt->GetX();
  51. mPt.mY = aPt->GetY();
  52. }
  53. protected:
  54. virtual ~nsISVGPoint()
  55. {
  56. // Our mList's weak ref to us must be nulled out when we die. If GC has
  57. // unlinked us using the cycle collector code, then that has already
  58. // happened, and mList is null.
  59. if (mList) {
  60. mList->mItems[mListIndex] = nullptr;
  61. }
  62. }
  63. public:
  64. /**
  65. * Creates an unowned copy of this object's point as a DOMSVGPoint.
  66. */
  67. virtual DOMSVGPoint* Copy() = 0;
  68. SVGPoint ToSVGPoint() const {
  69. return HasOwner() ? const_cast<nsISVGPoint*>(this)->InternalItem() : mPt;
  70. }
  71. bool IsInList() const {
  72. return !!mList;
  73. }
  74. /**
  75. * In future, if this class is used for non-list points, this will be
  76. * different to IsInList(). "Owner" here means that the instance has an
  77. * internal counterpart from which it gets its values. (A better name may
  78. * be HasWrappee().)
  79. */
  80. bool HasOwner() const {
  81. return !!mList;
  82. }
  83. bool IsTranslatePoint() const {
  84. return mIsTranslatePoint;
  85. }
  86. /**
  87. * This method is called to notify this DOM object that it is being inserted
  88. * into a list, and give it the information it needs as a result.
  89. *
  90. * This object MUST NOT already belong to a list when this method is called.
  91. * That's not to say that script can't move these DOM objects between
  92. * lists - it can - it's just that the logic to handle that (and send out
  93. * the necessary notifications) is located elsewhere (in DOMSVGPointList).)
  94. */
  95. void InsertingIntoList(DOMSVGPointList *aList,
  96. uint32_t aListIndex,
  97. bool aIsAnimValItem);
  98. static uint32_t MaxListIndex() {
  99. return (1U << MOZ_SVG_LIST_INDEX_BIT_COUNT) - 1;
  100. }
  101. /// This method is called to notify this object that its list index changed.
  102. void UpdateListIndex(uint32_t aListIndex) {
  103. mListIndex = aListIndex;
  104. }
  105. /**
  106. * This method is called to notify this DOM object that it is about to be
  107. * removed from its current DOM list so that it can first make a copy of its
  108. * internal counterpart's values. (If it didn't do this, then it would
  109. * "lose" its value on being removed.)
  110. */
  111. void RemovingFromList();
  112. bool IsReadonly() const {
  113. return mIsReadonly;
  114. }
  115. void SetReadonly(bool aReadonly) {
  116. mIsReadonly = aReadonly;
  117. }
  118. // WebIDL
  119. virtual float X() = 0;
  120. virtual void SetX(float aX, ErrorResult& rv) = 0;
  121. virtual float Y() = 0;
  122. virtual void SetY(float aY, ErrorResult& rv) = 0;
  123. virtual already_AddRefed<nsISVGPoint> MatrixTransform(dom::SVGMatrix& matrix) = 0;
  124. virtual JSObject* WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto) override
  125. { return dom::SVGPointBinding::Wrap(cx, this, aGivenProto); }
  126. virtual nsISupports* GetParentObject() = 0;
  127. protected:
  128. #ifdef DEBUG
  129. bool IndexIsValid();
  130. #endif
  131. RefPtr<DOMSVGPointList> mList;
  132. // Bounds for the following are checked in the ctor, so be sure to update
  133. // that if you change the capacity of any of the following.
  134. uint32_t mListIndex:MOZ_SVG_LIST_INDEX_BIT_COUNT;
  135. uint32_t mIsReadonly:1; // These flags are uint32_t because MSVC won't
  136. uint32_t mIsAnimValItem:1; // pack otherwise.
  137. uint32_t mIsTranslatePoint:1;
  138. /**
  139. * Get a reference to the internal SVGPoint list item that this DOM wrapper
  140. * object currently wraps.
  141. *
  142. * To simplify the code we just have this one method for obtaining both
  143. * baseVal and animVal internal items. This means that animVal items don't
  144. * get const protection, but then our setter methods guard against changing
  145. * animVal items.
  146. */
  147. SVGPoint& InternalItem();
  148. // The following member is only used when we're not in a list:
  149. SVGPoint mPt;
  150. };
  151. NS_DEFINE_STATIC_IID_ACCESSOR(nsISVGPoint, MOZILLA_NSISVGPOINT_IID)
  152. } // namespace mozilla
  153. #undef MOZ_SVG_LIST_INDEX_BIT_COUNT