Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / dom / svg / SVGViewportElement.h
blob32fe08f5e48a9e65dc77c46134da4c2f7dc87297
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_SVGVIEWPORTELEMENT_H_
8 #define DOM_SVG_SVGVIEWPORTELEMENT_H_
10 #include "mozilla/Attributes.h"
11 #include "mozilla/SVGImageContext.h"
12 #include "mozilla/UniquePtr.h"
13 #include "mozilla/dom/FromParser.h"
14 #include "nsIContentInlines.h"
15 #include "SVGAnimatedEnumeration.h"
16 #include "SVGAnimatedLength.h"
17 #include "SVGAnimatedPreserveAspectRatio.h"
18 #include "SVGAnimatedViewBox.h"
19 #include "SVGGraphicsElement.h"
20 #include "SVGPoint.h"
21 #include "SVGPreserveAspectRatio.h"
23 namespace mozilla {
24 class AutoPreserveAspectRatioOverride;
25 class SVGOuterSVGFrame;
26 class SVGViewportFrame;
28 namespace dom {
29 class DOMSVGAnimatedPreserveAspectRatio;
30 class SVGAnimatedRect;
31 class SVGViewElement;
32 class SVGViewportElement;
34 class svgFloatSize {
35 public:
36 svgFloatSize(float aWidth, float aHeight) : width(aWidth), height(aHeight) {}
37 bool operator!=(const svgFloatSize& rhs) {
38 return width != rhs.width || height != rhs.height;
40 float width;
41 float height;
44 class SVGViewportElement : public SVGGraphicsElement {
45 friend class mozilla::SVGOuterSVGFrame;
46 friend class mozilla::SVGViewportFrame;
48 protected:
49 explicit SVGViewportElement(
50 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
51 ~SVGViewportElement() = default;
53 public:
54 // nsIContent interface
55 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const override;
57 // SVGElement specializations:
58 gfxMatrix PrependLocalTransformsTo(
59 const gfxMatrix& aMatrix,
60 SVGTransformTypes aWhich = eAllTransforms) const override;
62 bool HasValidDimensions() const override;
64 // SVGViewportElement methods:
66 float GetLength(uint8_t aCtxType) const;
68 // public helpers:
70 /**
71 * Returns true if this element has a base/anim value for its "viewBox"
72 * attribute that defines a viewBox rectangle with finite values, or
73 * if there is a view element overriding this element's viewBox and it
74 * has a valid viewBox.
76 * Note that this does not check whether we need to synthesize a viewBox,
77 * so you must call ShouldSynthesizeViewBox() if you need to chck that too.
79 * Note also that this method does not pay attention to whether the width or
80 * height values of the viewBox rect are positive!
82 bool HasViewBox() const { return GetViewBoxInternal().HasRect(); }
84 /**
85 * Returns true if we should synthesize a viewBox for ourselves (that is, if
86 * we're the root element in an image document, and we're not currently being
87 * painted for an <svg:image> element).
89 * Only call this method if HasViewBox() returns false.
91 bool ShouldSynthesizeViewBox() const;
93 bool HasViewBoxOrSyntheticViewBox() const {
94 return HasViewBox() || ShouldSynthesizeViewBox();
97 bool HasChildrenOnlyTransform() const { return mHasChildrenOnlyTransform; }
99 void UpdateHasChildrenOnlyTransform();
101 enum ChildrenOnlyTransformChangedFlags { eDuringReflow = 1 };
104 * This method notifies the style system that the overflow rects of our
105 * immediate childrens' frames need to be updated. It is called by our own
106 * frame when changes (e.g. to currentScale) cause our children-only
107 * transform to change.
109 * The reason we have this method instead of overriding
110 * GetAttributeChangeHint is because we need to act on non-attribute (e.g.
111 * currentScale) changes in addition to attribute (e.g. viewBox) changes.
113 void ChildrenOnlyTransformChanged(uint32_t aFlags = 0);
115 gfx::Matrix GetViewBoxTransform() const;
117 svgFloatSize GetViewportSize() const {
118 return svgFloatSize(mViewportWidth, mViewportHeight);
121 void SetViewportSize(const svgFloatSize& aSize) {
122 mViewportWidth = aSize.width;
123 mViewportHeight = aSize.height;
127 * Returns true if either this is an SVG <svg> element that is the child of
128 * another non-foreignObject SVG element, or this is a SVG <symbol> element
129 * that is the root of a use-element shadow tree.
131 bool IsInner() const {
132 const nsIContent* parent = GetFlattenedTreeParent();
133 return parent && parent->IsSVGElement() &&
134 !parent->IsSVGElement(nsGkAtoms::foreignObject);
137 // WebIDL
138 already_AddRefed<SVGAnimatedRect> ViewBox();
139 already_AddRefed<DOMSVGAnimatedPreserveAspectRatio> PreserveAspectRatio();
140 SVGAnimatedViewBox* GetAnimatedViewBox() override;
142 protected:
143 // implementation helpers:
145 bool IsRootSVGSVGElement() const {
146 NS_ASSERTION((IsInUncomposedDoc() && !GetParent()) ==
147 (OwnerDoc()->GetRootElement() == this),
148 "Can't determine if we're root");
149 return !GetParent() && IsInUncomposedDoc() && IsSVGElement(nsGkAtoms::svg);
153 * Returns the explicit or default preserveAspectRatio, unless we're
154 * synthesizing a viewBox, in which case it returns the "none" value.
156 virtual SVGPreserveAspectRatio GetPreserveAspectRatioWithOverride() const {
157 return mPreserveAspectRatio.GetAnimValue();
161 * Returns the explicit viewBox rect, if specified, or else a synthesized
162 * viewBox, if appropriate, or else a viewBox matching the dimensions of the
163 * SVG viewport.
165 SVGViewBox GetViewBoxWithSynthesis(float aViewportWidth,
166 float aViewportHeight) const;
168 enum { ATTR_X, ATTR_Y, ATTR_WIDTH, ATTR_HEIGHT };
169 SVGAnimatedLength mLengthAttributes[4];
170 static LengthInfo sLengthInfo[4];
171 LengthAttributesInfo GetLengthInfo() override;
173 SVGAnimatedPreserveAspectRatio* GetAnimatedPreserveAspectRatio() override;
175 virtual const SVGAnimatedViewBox& GetViewBoxInternal() const {
176 return mViewBox;
178 virtual SVGAnimatedTransformList* GetTransformInternal() const {
179 return mTransforms.get();
181 SVGAnimatedViewBox mViewBox;
182 SVGAnimatedPreserveAspectRatio mPreserveAspectRatio;
184 // The size of the rectangular SVG viewport into which we render. This is
185 // not (necessarily) the same as the content area. See:
187 // http://www.w3.org/TR/SVG11/coords.html#ViewportSpace
189 // XXXjwatt Currently only used for outer <svg>, but maybe we could use -1 to
190 // flag this as an inner <svg> to save the overhead of GetCtx calls?
191 // XXXjwatt our frame should probably reset these when it's destroyed.
192 float mViewportWidth, mViewportHeight;
194 bool mHasChildrenOnlyTransform;
197 } // namespace dom
199 } // namespace mozilla
201 #endif // DOM_SVG_SVGVIEWPORTELEMENT_H_