Bug 1805294 [wpt PR 37463] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / dom / svg / SVGSVGElement.h
blob86a172d39a12a661baf64ff3a4d38733a9a19d57
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_SVGSVGELEMENT_H_
8 #define DOM_SVG_SVGSVGELEMENT_H_
10 #include "SVGAnimatedEnumeration.h"
11 #include "SVGViewportElement.h"
13 nsresult NS_NewSVGSVGElement(
14 nsIContent** aResult, already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
15 mozilla::dom::FromParser aFromParser);
17 // {4b83982c-e5e9-4ca1-abd4-14d27e8b3531}
18 #define MOZILLA_SVGSVGELEMENT_IID \
19 { \
20 0x4b83982c, 0xe5e9, 0x4ca1, { \
21 0xab, 0xd4, 0x14, 0xd2, 0x7e, 0x8b, 0x35, 0x31 \
22 } \
25 namespace mozilla {
26 class AutoSVGViewHandler;
27 class SMILTimeContainer;
28 class SVGFragmentIdentifier;
29 class EventChainPreVisitor;
31 namespace dom {
32 struct DOMMatrix2DInit;
33 class DOMSVGAngle;
34 class DOMSVGLength;
35 class DOMSVGNumber;
36 class DOMSVGPoint;
37 class SVGMatrix;
38 class SVGRect;
39 class SVGSVGElement;
41 // Stores svgView arguments of SVG fragment identifiers.
42 class SVGView {
43 public:
44 SVGView();
46 SVGAnimatedEnumeration mZoomAndPan;
47 SVGAnimatedViewBox mViewBox;
48 SVGAnimatedPreserveAspectRatio mPreserveAspectRatio;
49 UniquePtr<SVGAnimatedTransformList> mTransforms;
52 using SVGSVGElementBase = SVGViewportElement;
54 class SVGSVGElement final : public SVGSVGElementBase {
55 friend class mozilla::SVGFragmentIdentifier;
56 friend class mozilla::SVGOuterSVGFrame;
57 friend class mozilla::AutoSVGViewHandler;
58 friend class mozilla::AutoPreserveAspectRatioOverride;
59 friend class mozilla::dom::SVGView;
61 protected:
62 SVGSVGElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
63 FromParser aFromParser);
64 JSObject* WrapNode(JSContext* aCx,
65 JS::Handle<JSObject*> aGivenProto) override;
67 friend nsresult(::NS_NewSVGSVGElement(
68 nsIContent** aResult,
69 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
70 mozilla::dom::FromParser aFromParser));
72 ~SVGSVGElement() = default;
74 public:
75 // interfaces:
76 NS_DECLARE_STATIC_IID_ACCESSOR(MOZILLA_SVGSVGELEMENT_IID)
77 NS_DECL_ISUPPORTS_INHERITED
78 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SVGSVGElement, SVGSVGElementBase)
81 * Send appropriate events and updates if our root translate
82 * has changed.
84 MOZ_CAN_RUN_SCRIPT
85 void DidChangeTranslate();
87 // nsIContent interface
88 void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
89 bool IsEventAttributeNameInternal(nsAtom* aName) override;
91 // nsINode methods:
92 nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
94 // WebIDL
95 already_AddRefed<DOMSVGAnimatedLength> X();
96 already_AddRefed<DOMSVGAnimatedLength> Y();
97 already_AddRefed<DOMSVGAnimatedLength> Width();
98 already_AddRefed<DOMSVGAnimatedLength> Height();
99 bool UseCurrentView() const;
100 float CurrentScale() const;
101 void SetCurrentScale(float aCurrentScale);
102 already_AddRefed<DOMSVGPoint> CurrentTranslate();
103 uint32_t SuspendRedraw(uint32_t max_wait_milliseconds);
104 void UnsuspendRedraw(uint32_t suspend_handle_id);
105 void UnsuspendRedrawAll();
106 void ForceRedraw();
107 void PauseAnimations();
108 void UnpauseAnimations();
109 bool AnimationsPaused();
110 float GetCurrentTimeAsFloat();
111 void SetCurrentTime(float seconds);
112 void DeselectAll();
113 already_AddRefed<DOMSVGNumber> CreateSVGNumber();
114 already_AddRefed<DOMSVGLength> CreateSVGLength();
115 already_AddRefed<DOMSVGAngle> CreateSVGAngle();
116 already_AddRefed<DOMSVGPoint> CreateSVGPoint();
117 already_AddRefed<SVGMatrix> CreateSVGMatrix();
118 already_AddRefed<SVGRect> CreateSVGRect();
119 already_AddRefed<DOMSVGTransform> CreateSVGTransform();
120 already_AddRefed<DOMSVGTransform> CreateSVGTransformFromMatrix(
121 const DOMMatrix2DInit& matrix, ErrorResult& rv);
122 using nsINode::GetElementById; // This does what we want
123 uint16_t ZoomAndPan() const;
124 void SetZoomAndPan(uint16_t aZoomAndPan, ErrorResult& rv);
126 // SVGElement overrides
128 nsresult BindToTree(BindContext&, nsINode& aParent) override;
129 void UnbindFromTree(bool aNullParent) override;
130 SVGAnimatedTransformList* GetAnimatedTransformList(
131 uint32_t aFlags = 0) override;
133 // SVGSVGElement methods:
135 // Returns true IFF our attributes are currently overridden by a <view>
136 // element and that element's ID matches the passed-in string.
137 bool IsOverriddenBy(const nsAString& aViewID) const {
138 return mCurrentViewID && mCurrentViewID->Equals(aViewID);
141 SMILTimeContainer* GetTimedDocumentRoot();
143 // public helpers:
145 const SVGPoint& GetCurrentTranslate() const { return mCurrentTranslate; }
146 bool IsScaledOrTranslated() const {
147 return mCurrentTranslate != SVGPoint() || mCurrentScale != 1.0f;
151 * Returns -1 if the width/height is a percentage, else returns the user unit
152 * length clamped to fit in a int32_t.
153 * XXX see bug 1112533 comment 3 - we should fix drawImage so that we can
154 * change these methods to make zero the error flag for percentages.
156 int32_t GetIntrinsicWidth();
157 int32_t GetIntrinsicHeight();
159 // This services any pending notifications for the transform on on this root
160 // <svg> node needing to be recalculated. (Only applicable in
161 // SVG-as-an-image documents.)
162 virtual void FlushImageTransformInvalidation();
164 private:
165 // SVGViewportElement methods:
167 virtual SVGViewElement* GetCurrentViewElement() const;
168 SVGPreserveAspectRatio GetPreserveAspectRatioWithOverride() const override;
170 // implementation helpers:
173 * While binding to the tree we need to determine if we will be the outermost
174 * <svg> element _before_ the children are bound (as they want to know what
175 * timed document root to register with) and therefore _before_ our parent is
176 * set (both actions are performed by Element::BindToTree) so we
177 * can't use GetOwnerSVGElement() as it relies on GetParent(). This code is
178 * basically a simplified version of GetOwnerSVGElement that uses the parent
179 * parameters passed in instead.
181 * FIXME(bug 1596690): GetOwnerSVGElement() uses the flattened tree parent
182 * rather than the DOM tree parent nowadays.
184 bool WillBeOutermostSVG(nsINode& aParent) const;
186 // invalidate viewbox -> viewport xform & inform frames
187 void InvalidateTransformNotifyFrame();
189 // Methods for <image> elements to override my "PreserveAspectRatio" value.
190 // These are private so that only our friends
191 // (AutoPreserveAspectRatioOverride in particular) have access.
192 void SetImageOverridePreserveAspectRatio(const SVGPreserveAspectRatio& aPAR);
193 void ClearImageOverridePreserveAspectRatio();
195 // Set/Clear properties to hold old version of preserveAspectRatio
196 // when it's being overridden by an <image> element that we are inside of.
197 bool SetPreserveAspectRatioProperty(const SVGPreserveAspectRatio& aPAR);
198 const SVGPreserveAspectRatio* GetPreserveAspectRatioProperty() const;
199 bool ClearPreserveAspectRatioProperty();
201 const SVGAnimatedViewBox& GetViewBoxInternal() const override;
202 SVGAnimatedTransformList* GetTransformInternal() const override;
204 EnumAttributesInfo GetEnumInfo() override;
206 enum { ZOOMANDPAN };
207 SVGAnimatedEnumeration mEnumAttributes[1];
208 static SVGEnumMapping sZoomAndPanMap[];
209 static EnumInfo sEnumInfo[1];
211 // The time container for animations within this SVG document fragment. Set
212 // for all outermost <svg> elements (not nested <svg> elements).
213 UniquePtr<SMILTimeContainer> mTimedDocumentRoot;
215 SVGPoint mCurrentTranslate;
216 float mCurrentScale;
218 // For outermost <svg> elements created from parsing, animation is started by
219 // the onload event in accordance with the SVG spec, but for <svg> elements
220 // created by script or promoted from inner <svg> to outermost <svg> we need
221 // to manually kick off animation when they are bound to the tree.
222 bool mStartAnimationOnBindToTree;
224 bool mImageNeedsTransformInvalidation;
226 // mCurrentViewID and mSVGView are mutually exclusive; we can have
227 // at most one non-null.
228 UniquePtr<nsString> mCurrentViewID;
229 UniquePtr<SVGView> mSVGView;
232 NS_DEFINE_STATIC_IID_ACCESSOR(SVGSVGElement, MOZILLA_SVGSVGELEMENT_IID)
234 } // namespace dom
236 class MOZ_RAII AutoSVGTimeSetRestore {
237 public:
238 AutoSVGTimeSetRestore(dom::SVGSVGElement* aRootElem, float aFrameTime)
239 : mRootElem(aRootElem),
240 mOriginalTime(mRootElem->GetCurrentTimeAsFloat()) {
241 mRootElem->SetCurrentTime(
242 aFrameTime); // Does nothing if there's no change.
245 ~AutoSVGTimeSetRestore() { mRootElem->SetCurrentTime(mOriginalTime); }
247 private:
248 const RefPtr<dom::SVGSVGElement> mRootElem;
249 const float mOriginalTime;
252 class MOZ_RAII AutoPreserveAspectRatioOverride {
253 public:
254 AutoPreserveAspectRatioOverride(const SVGImageContext& aSVGContext,
255 dom::SVGSVGElement* aRootElem)
256 : mRootElem(aRootElem), mDidOverride(false) {
257 MOZ_ASSERT(mRootElem, "No SVG/Symbol node to manage?");
259 if (aSVGContext.GetPreserveAspectRatio().isSome()) {
260 // Override preserveAspectRatio in our helper document.
261 // XXXdholbert We should technically be overriding the helper doc's clip
262 // and overflow properties here, too. See bug 272288 comment 36.
263 mRootElem->SetImageOverridePreserveAspectRatio(
264 *aSVGContext.GetPreserveAspectRatio());
265 mDidOverride = true;
269 ~AutoPreserveAspectRatioOverride() {
270 if (mDidOverride) {
271 mRootElem->ClearImageOverridePreserveAspectRatio();
275 private:
276 const RefPtr<dom::SVGSVGElement> mRootElem;
277 bool mDidOverride;
280 } // namespace mozilla
282 #endif // DOM_SVG_SVGSVGELEMENT_H_