SVGPreserveAspectRatio.h (3950B)
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_SVGPRESERVEASPECTRATIO_H_ 8 #define DOM_SVG_SVGPRESERVEASPECTRATIO_H_ 9 10 #include "mozilla/HashFunctions.h" // for HashGeneric 11 #include "mozilla/dom/SVGElement.h" 12 #include "mozilla/dom/SVGPreserveAspectRatioBinding.h" 13 #include "nsCycleCollectionParticipant.h" 14 #include "nsWrapperCache.h" 15 16 namespace mozilla { 17 class ErrorResult; 18 19 // These constants represent the range of valid enum values for the <align> 20 // parameter. They exclude the sentinel _UNKNOWN value. 21 const uint16_t SVG_ALIGN_MIN_VALID = 22 dom::SVGPreserveAspectRatio_Binding::SVG_PRESERVEASPECTRATIO_NONE; 23 const uint16_t SVG_ALIGN_MAX_VALID = 24 dom::SVGPreserveAspectRatio_Binding::SVG_PRESERVEASPECTRATIO_XMAXYMAX; 25 26 // These constants represent the range of valid enum values for the 27 // <meetOrSlice> parameter. They exclude the sentinel _UNKNOWN value. 28 const uint16_t SVG_MEETORSLICE_MIN_VALID = 29 dom::SVGPreserveAspectRatio_Binding::SVG_MEETORSLICE_MEET; 30 const uint16_t SVG_MEETORSLICE_MAX_VALID = 31 dom::SVGPreserveAspectRatio_Binding::SVG_MEETORSLICE_SLICE; 32 33 class SVGAnimatedPreserveAspectRatio; 34 35 class SVGPreserveAspectRatio final { 36 friend class SVGAnimatedPreserveAspectRatio; 37 38 public: 39 explicit SVGPreserveAspectRatio() 40 : mAlign(dom::SVGPreserveAspectRatio_Binding:: 41 SVG_PRESERVEASPECTRATIO_UNKNOWN), 42 mMeetOrSlice( 43 dom::SVGPreserveAspectRatio_Binding::SVG_MEETORSLICE_UNKNOWN) {} 44 45 SVGPreserveAspectRatio(uint8_t aAlign, uint8_t aMeetOrSlice) 46 : mAlign(aAlign), mMeetOrSlice(aMeetOrSlice) {} 47 48 static nsresult FromString(const nsAString& aString, 49 SVGPreserveAspectRatio* aValue); 50 void ToString(nsAString& aValueAsString) const; 51 52 bool operator==(const SVGPreserveAspectRatio& aOther) const; 53 54 bool SetAlign(uint16_t aAlign) { 55 if (aAlign < SVG_ALIGN_MIN_VALID || aAlign > SVG_ALIGN_MAX_VALID) 56 return false; 57 mAlign = static_cast<uint8_t>(aAlign); 58 return true; 59 } 60 61 auto GetAlign() const { return mAlign; } 62 63 bool SetMeetOrSlice(uint16_t aMeetOrSlice) { 64 if (aMeetOrSlice < SVG_MEETORSLICE_MIN_VALID || 65 aMeetOrSlice > SVG_MEETORSLICE_MAX_VALID) 66 return false; 67 mMeetOrSlice = static_cast<uint8_t>(aMeetOrSlice); 68 return true; 69 } 70 71 auto GetMeetOrSlice() const { return mMeetOrSlice; } 72 73 PLDHashNumber Hash() const { return HashGeneric(mAlign, mMeetOrSlice); } 74 75 private: 76 // We can't use enum types here because some compilers fail to pack them. 77 uint8_t mAlign; 78 uint8_t mMeetOrSlice; 79 }; 80 81 namespace dom { 82 83 class DOMSVGPreserveAspectRatio final : public nsWrapperCache { 84 public: 85 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(DOMSVGPreserveAspectRatio) 86 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(DOMSVGPreserveAspectRatio) 87 88 DOMSVGPreserveAspectRatio(SVGAnimatedPreserveAspectRatio* aVal, 89 SVGElement* aSVGElement, bool aIsBaseValue) 90 : mVal(aVal), mSVGElement(aSVGElement), mIsBaseValue(aIsBaseValue) {} 91 92 // WebIDL 93 SVGElement* GetParentObject() const { return mSVGElement; } 94 JSObject* WrapObject(JSContext* aCx, 95 JS::Handle<JSObject*> aGivenProto) override; 96 97 uint16_t Align(); 98 void SetAlign(uint16_t aAlign, ErrorResult& aRv); 99 uint16_t MeetOrSlice(); 100 void SetMeetOrSlice(uint16_t aMeetOrSlice, ErrorResult& aRv); 101 102 protected: 103 ~DOMSVGPreserveAspectRatio(); 104 105 SVGAnimatedPreserveAspectRatio* 106 mVal; // kept alive because it belongs to mSVGElement 107 RefPtr<SVGElement> mSVGElement; 108 const bool mIsBaseValue; 109 }; 110 111 } // namespace dom 112 } // namespace mozilla 113 114 #endif // DOM_SVG_SVGPRESERVEASPECTRATIO_H_