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