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