SVGAnimatedPreserveAspectRatio.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_SVGANIMATEDPRESERVEASPECTRATIO_H__
  6. #define MOZILLA_SVGANIMATEDPRESERVEASPECTRATIO_H__
  7. #include "nsCycleCollectionParticipant.h"
  8. #include "nsError.h"
  9. #include "nsISMILAttr.h"
  10. #include "nsSVGElement.h"
  11. #include "SVGPreserveAspectRatio.h"
  12. #include "mozilla/Attributes.h"
  13. class nsSMILValue;
  14. namespace mozilla {
  15. namespace dom {
  16. class DOMSVGAnimatedPreserveAspectRatio;
  17. class SVGAnimationElement;
  18. } // namespace dom
  19. class SVGAnimatedPreserveAspectRatio final
  20. {
  21. public:
  22. void Init() {
  23. mBaseVal.mAlign = SVG_PRESERVEASPECTRATIO_XMIDYMID;
  24. mBaseVal.mMeetOrSlice = SVG_MEETORSLICE_MEET;
  25. mAnimVal = mBaseVal;
  26. mIsAnimated = false;
  27. mIsBaseSet = false;
  28. }
  29. nsresult SetBaseValueString(const nsAString& aValue,
  30. nsSVGElement *aSVGElement,
  31. bool aDoSetAttr);
  32. void GetBaseValueString(nsAString& aValue) const;
  33. void SetBaseValue(const SVGPreserveAspectRatio &aValue,
  34. nsSVGElement *aSVGElement);
  35. nsresult SetBaseAlign(uint16_t aAlign, nsSVGElement *aSVGElement) {
  36. if (aAlign < SVG_ALIGN_MIN_VALID || aAlign > SVG_ALIGN_MAX_VALID) {
  37. return NS_ERROR_FAILURE;
  38. }
  39. SetBaseValue(SVGPreserveAspectRatio(
  40. static_cast<SVGAlign>(aAlign), mBaseVal.GetMeetOrSlice()),
  41. aSVGElement);
  42. return NS_OK;
  43. }
  44. nsresult SetBaseMeetOrSlice(uint16_t aMeetOrSlice, nsSVGElement *aSVGElement) {
  45. if (aMeetOrSlice < SVG_MEETORSLICE_MIN_VALID ||
  46. aMeetOrSlice > SVG_MEETORSLICE_MAX_VALID) {
  47. return NS_ERROR_FAILURE;
  48. }
  49. SetBaseValue(SVGPreserveAspectRatio(
  50. mBaseVal.GetAlign(), static_cast<SVGMeetOrSlice>(aMeetOrSlice)),
  51. aSVGElement);
  52. return NS_OK;
  53. }
  54. void SetAnimValue(uint64_t aPackedValue, nsSVGElement *aSVGElement);
  55. const SVGPreserveAspectRatio &GetBaseValue() const
  56. { return mBaseVal; }
  57. const SVGPreserveAspectRatio &GetAnimValue() const
  58. { return mAnimVal; }
  59. bool IsAnimated() const
  60. { return mIsAnimated; }
  61. bool IsExplicitlySet() const
  62. { return mIsAnimated || mIsBaseSet; }
  63. already_AddRefed<mozilla::dom::DOMSVGAnimatedPreserveAspectRatio>
  64. ToDOMAnimatedPreserveAspectRatio(nsSVGElement* aSVGElement);
  65. // Returns a new nsISMILAttr object that the caller must delete
  66. nsISMILAttr* ToSMILAttr(nsSVGElement* aSVGElement);
  67. private:
  68. SVGPreserveAspectRatio mAnimVal;
  69. SVGPreserveAspectRatio mBaseVal;
  70. bool mIsAnimated;
  71. bool mIsBaseSet;
  72. public:
  73. struct SMILPreserveAspectRatio final : public nsISMILAttr
  74. {
  75. public:
  76. SMILPreserveAspectRatio(SVGAnimatedPreserveAspectRatio* aVal,
  77. nsSVGElement* aSVGElement)
  78. : mVal(aVal), mSVGElement(aSVGElement) {}
  79. // These will stay alive because a nsISMILAttr only lives as long
  80. // as the Compositing step, and DOM elements don't get a chance to
  81. // die during that.
  82. SVGAnimatedPreserveAspectRatio* mVal;
  83. nsSVGElement* mSVGElement;
  84. // nsISMILAttr methods
  85. virtual nsresult ValueFromString(const nsAString& aStr,
  86. const dom::SVGAnimationElement* aSrcElement,
  87. nsSMILValue& aValue,
  88. bool& aPreventCachingOfSandwich) const override;
  89. virtual nsSMILValue GetBaseValue() const override;
  90. virtual void ClearAnimValue() override;
  91. virtual nsresult SetAnimValue(const nsSMILValue& aValue) override;
  92. };
  93. };
  94. namespace dom {
  95. class DOMSVGAnimatedPreserveAspectRatio final : public nsISupports,
  96. public nsWrapperCache
  97. {
  98. ~DOMSVGAnimatedPreserveAspectRatio();
  99. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  100. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMSVGAnimatedPreserveAspectRatio)
  101. DOMSVGAnimatedPreserveAspectRatio(SVGAnimatedPreserveAspectRatio* aVal,
  102. nsSVGElement *aSVGElement)
  103. : mVal(aVal), mSVGElement(aSVGElement)
  104. {
  105. }
  106. // WebIDL
  107. nsSVGElement* GetParentObject() const { return mSVGElement; }
  108. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  109. // These aren't weak refs because new objects are returned each time
  110. already_AddRefed<DOMSVGPreserveAspectRatio> BaseVal();
  111. already_AddRefed<DOMSVGPreserveAspectRatio> AnimVal();
  112. protected:
  113. // kept alive because it belongs to content:
  114. SVGAnimatedPreserveAspectRatio* mVal;
  115. RefPtr<nsSVGElement> mSVGElement;
  116. };
  117. } // namespace dom
  118. } // namespace mozilla
  119. #endif // MOZILLA_SVGANIMATEDPRESERVEASPECTRATIO_H__