Bug 1805294 [wpt PR 37463] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / dom / svg / SVGAnimatedNumberPair.h
blob1642d6dc10470bf2050599b5480740f949e734d5
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/. */
7 #ifndef DOM_SVG_SVGANIMATEDNUMBERPAIR_H_
8 #define DOM_SVG_SVGANIMATEDNUMBERPAIR_H_
10 #include "DOMSVGAnimatedNumber.h"
11 #include "nsCycleCollectionParticipant.h"
12 #include "nsError.h"
13 #include "nsMathUtils.h"
14 #include "mozilla/Attributes.h"
15 #include "mozilla/FloatingPoint.h"
16 #include "mozilla/SMILAttr.h"
17 #include "mozilla/UniquePtr.h"
19 namespace mozilla {
21 class SMILValue;
23 namespace dom {
24 class SVGAnimationElement;
25 class SVGElement;
26 } // namespace dom
28 class SVGAnimatedNumberPair {
29 public:
30 friend class AutoChangeNumberPairNotifier;
31 using SVGElement = dom::SVGElement;
33 enum PairIndex { eFirst, eSecond };
35 void Init(uint8_t aAttrEnum = 0xff, float aValue1 = 0, float aValue2 = 0) {
36 mAnimVal[0] = mBaseVal[0] = aValue1;
37 mAnimVal[1] = mBaseVal[1] = aValue2;
38 mAttrEnum = aAttrEnum;
39 mIsAnimated = false;
40 mIsBaseSet = false;
43 nsresult SetBaseValueString(const nsAString& aValue, SVGElement* aSVGElement);
44 void GetBaseValueString(nsAString& aValue) const;
46 void SetBaseValue(float aValue, PairIndex aPairIndex,
47 SVGElement* aSVGElement);
48 void SetBaseValues(float aValue1, float aValue2, SVGElement* aSVGElement);
49 float GetBaseValue(PairIndex aIndex) const {
50 return mBaseVal[aIndex == eFirst ? 0 : 1];
52 void SetAnimValue(const float aValue[2], SVGElement* aSVGElement);
53 float GetAnimValue(PairIndex aIndex) const {
54 return mAnimVal[aIndex == eFirst ? 0 : 1];
57 // Returns true if the animated value of this number has been explicitly
58 // set (either by animation, or by taking on the base value which has been
59 // explicitly set by markup or a DOM call), false otherwise.
60 // If this returns false, the animated value is still valid, that is,
61 // usable, and represents the default base value of the attribute.
62 bool IsExplicitlySet() const { return mIsAnimated || mIsBaseSet; }
64 already_AddRefed<dom::DOMSVGAnimatedNumber> ToDOMAnimatedNumber(
65 PairIndex aIndex, SVGElement* aSVGElement);
66 UniquePtr<SMILAttr> ToSMILAttr(SVGElement* aSVGElement);
68 private:
69 float mAnimVal[2];
70 float mBaseVal[2];
71 uint8_t mAttrEnum; // element specified tracking for attribute
72 bool mIsAnimated;
73 bool mIsBaseSet;
75 public:
76 // DOM wrapper class for the (DOM)SVGAnimatedNumber interface where the
77 // wrapped class is SVGAnimatedNumberPair.
78 struct DOMAnimatedNumber final : public dom::DOMSVGAnimatedNumber {
79 DOMAnimatedNumber(SVGAnimatedNumberPair* aVal, PairIndex aIndex,
80 SVGElement* aSVGElement)
81 : dom::DOMSVGAnimatedNumber(aSVGElement), mVal(aVal), mIndex(aIndex) {}
82 virtual ~DOMAnimatedNumber();
84 SVGAnimatedNumberPair* mVal; // kept alive because it belongs to content
85 PairIndex mIndex; // are we the first or second number
87 float BaseVal() override { return mVal->GetBaseValue(mIndex); }
88 void SetBaseVal(float aValue) override {
89 MOZ_ASSERT(IsFinite(aValue));
90 mVal->SetBaseValue(aValue, mIndex, mSVGElement);
93 // Script may have modified animation parameters or timeline -- DOM getters
94 // need to flush any resample requests to reflect these modifications.
95 float AnimVal() override {
96 mSVGElement->FlushAnimations();
97 return mVal->GetAnimValue(mIndex);
101 struct SMILNumberPair : public SMILAttr {
102 public:
103 SMILNumberPair(SVGAnimatedNumberPair* aVal, SVGElement* aSVGElement)
104 : mVal(aVal), mSVGElement(aSVGElement) {}
106 // These will stay alive because a SMILAttr only lives as long
107 // as the Compositing step, and DOM elements don't get a chance to
108 // die during that.
109 SVGAnimatedNumberPair* mVal;
110 SVGElement* mSVGElement;
112 // SMILAttr methods
113 nsresult ValueFromString(const nsAString& aStr,
114 const dom::SVGAnimationElement* aSrcElement,
115 SMILValue& aValue,
116 bool& aPreventCachingOfSandwich) const override;
117 SMILValue GetBaseValue() const override;
118 void ClearAnimValue() override;
119 nsresult SetAnimValue(const SMILValue& aValue) override;
123 } // namespace mozilla
125 #endif // DOM_SVG_SVGANIMATEDNUMBERPAIR_H_