SVGTransform.h 5.5 KB

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