nsSVGNumber2.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 __NS_SVGNUMBER2_H__
  6. #define __NS_SVGNUMBER2_H__
  7. #include "nsCycleCollectionParticipant.h"
  8. #include "nsError.h"
  9. #include "nsISMILAttr.h"
  10. #include "nsMathUtils.h"
  11. #include "nsSVGElement.h"
  12. #include "mozilla/Attributes.h"
  13. #include "mozilla/FloatingPoint.h"
  14. #include "mozilla/dom/SVGAnimatedNumber.h"
  15. class nsSMILValue;
  16. namespace mozilla {
  17. namespace dom {
  18. class SVGAnimationElement;
  19. } // namespace dom
  20. } // namespace mozilla
  21. class nsSVGNumber2
  22. {
  23. public:
  24. void Init(uint8_t aAttrEnum = 0xff, float aValue = 0) {
  25. mAnimVal = mBaseVal = aValue;
  26. mAttrEnum = aAttrEnum;
  27. mIsAnimated = false;
  28. mIsBaseSet = false;
  29. }
  30. nsresult SetBaseValueString(const nsAString& aValue,
  31. nsSVGElement *aSVGElement);
  32. void GetBaseValueString(nsAString& aValue);
  33. void SetBaseValue(float aValue, nsSVGElement *aSVGElement);
  34. float GetBaseValue() const
  35. { return mBaseVal; }
  36. void SetAnimValue(float aValue, nsSVGElement *aSVGElement);
  37. float GetAnimValue() const
  38. { return mAnimVal; }
  39. // Returns true if the animated value of this number has been explicitly
  40. // set (either by animation, or by taking on the base value which has been
  41. // explicitly set by markup or a DOM call), false otherwise.
  42. // If this returns false, the animated value is still valid, that is,
  43. // useable, and represents the default base value of the attribute.
  44. bool IsExplicitlySet() const
  45. { return mIsAnimated || mIsBaseSet; }
  46. already_AddRefed<mozilla::dom::SVGAnimatedNumber>
  47. ToDOMAnimatedNumber(nsSVGElement* aSVGElement);
  48. // Returns a new nsISMILAttr object that the caller must delete
  49. nsISMILAttr* ToSMILAttr(nsSVGElement* aSVGElement);
  50. private:
  51. float mAnimVal;
  52. float mBaseVal;
  53. uint8_t mAttrEnum; // element specified tracking for attribute
  54. bool mIsAnimated;
  55. bool mIsBaseSet;
  56. public:
  57. struct DOMAnimatedNumber final : public mozilla::dom::SVGAnimatedNumber
  58. {
  59. DOMAnimatedNumber(nsSVGNumber2* aVal, nsSVGElement* aSVGElement)
  60. : mozilla::dom::SVGAnimatedNumber(aSVGElement)
  61. , mVal(aVal)
  62. {}
  63. virtual ~DOMAnimatedNumber();
  64. nsSVGNumber2* mVal; // kept alive because it belongs to content
  65. virtual float BaseVal() override
  66. {
  67. return mVal->GetBaseValue();
  68. }
  69. virtual void SetBaseVal(float aValue) override
  70. {
  71. MOZ_ASSERT(mozilla::IsFinite(aValue));
  72. mVal->SetBaseValue(aValue, mSVGElement);
  73. }
  74. // Script may have modified animation parameters or timeline -- DOM getters
  75. // need to flush any resample requests to reflect these modifications.
  76. virtual float AnimVal() override
  77. {
  78. mSVGElement->FlushAnimations();
  79. return mVal->GetAnimValue();
  80. }
  81. };
  82. struct SMILNumber : public nsISMILAttr
  83. {
  84. public:
  85. SMILNumber(nsSVGNumber2* aVal, nsSVGElement* aSVGElement)
  86. : mVal(aVal), mSVGElement(aSVGElement) {}
  87. // These will stay alive because a nsISMILAttr only lives as long
  88. // as the Compositing step, and DOM elements don't get a chance to
  89. // die during that.
  90. nsSVGNumber2* mVal;
  91. nsSVGElement* mSVGElement;
  92. // nsISMILAttr methods
  93. virtual nsresult ValueFromString(const nsAString& aStr,
  94. const mozilla::dom::SVGAnimationElement* aSrcElement,
  95. nsSMILValue& aValue,
  96. bool& aPreventCachingOfSandwich) const override;
  97. virtual nsSMILValue GetBaseValue() const override;
  98. virtual void ClearAnimValue() override;
  99. virtual nsresult SetAnimValue(const nsSMILValue& aValue) override;
  100. };
  101. };
  102. #endif //__NS_SVGNUMBER2_H__