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"
21 #include "SVGPreserveAspectRatio.h"
24 class AutoPreserveAspectRatioOverride
;
25 class SVGOuterSVGFrame
;
26 class SVGViewportFrame
;
29 class DOMSVGAnimatedPreserveAspectRatio
;
30 class SVGAnimatedRect
;
32 class SVGViewportElement
;
34 class SVGViewportElement
: public SVGGraphicsElement
{
35 friend class mozilla::SVGOuterSVGFrame
;
36 friend class mozilla::SVGViewportFrame
;
39 explicit SVGViewportElement(
40 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
);
41 ~SVGViewportElement() = default;
44 // nsIContent interface
45 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom
* aAttribute
) const override
;
47 // SVGElement specializations:
48 gfxMatrix
PrependLocalTransformsTo(
49 const gfxMatrix
& aMatrix
,
50 SVGTransformTypes aWhich
= eAllTransforms
) const override
;
52 bool HasValidDimensions() const override
;
54 // SVGViewportElement methods:
56 float GetLength(uint8_t aCtxType
) const;
61 * Returns true if this element has a base/anim value for its "viewBox"
62 * attribute that defines a viewBox rectangle with finite values, or
63 * if there is a view element overriding this element's viewBox and it
64 * has a valid viewBox.
66 * Note that this does not check whether we need to synthesize a viewBox,
67 * so you must call ShouldSynthesizeViewBox() if you need to chck that too.
69 * Note also that this method does not pay attention to whether the width or
70 * height values of the viewBox rect are positive!
72 bool HasViewBox() const { return GetViewBoxInternal().HasRect(); }
75 * Returns true if we should synthesize a viewBox for ourselves (that is, if
76 * we're the root element in an image document, and we're not currently being
77 * painted for an <svg:image> element).
79 * Only call this method if HasViewBox() returns false.
81 bool ShouldSynthesizeViewBox() const;
83 bool HasViewBoxOrSyntheticViewBox() const {
84 return HasViewBox() || ShouldSynthesizeViewBox();
87 bool HasChildrenOnlyTransform() const { return mHasChildrenOnlyTransform
; }
89 void UpdateHasChildrenOnlyTransform();
91 enum ChildrenOnlyTransformChangedFlags
{ eDuringReflow
= 1 };
94 * This method notifies the style system that the overflow rects of our
95 * immediate childrens' frames need to be updated. It is called by our own
96 * frame when changes (e.g. to currentScale) cause our children-only
97 * transform to change.
99 * The reason we have this method instead of overriding
100 * GetAttributeChangeHint is because we need to act on non-attribute (e.g.
101 * currentScale) changes in addition to attribute (e.g. viewBox) changes.
103 void ChildrenOnlyTransformChanged(uint32_t aFlags
= 0);
105 gfx::Matrix
GetViewBoxTransform() const;
107 gfx::Size
GetViewportSize() const { return mViewportSize
; }
109 void SetViewportSize(const gfx::Size
& aSize
) { mViewportSize
= aSize
; }
112 * Returns true if either this is an SVG <svg> element that is the child of
113 * another non-foreignObject SVG element, or this is a SVG <symbol> element
114 * that is the root of a use-element shadow tree.
116 bool IsInner() const {
117 const nsIContent
* parent
= GetFlattenedTreeParent();
118 return parent
&& parent
->IsSVGElement() &&
119 !parent
->IsSVGElement(nsGkAtoms::foreignObject
);
123 already_AddRefed
<SVGAnimatedRect
> ViewBox();
124 already_AddRefed
<DOMSVGAnimatedPreserveAspectRatio
> PreserveAspectRatio();
125 SVGAnimatedViewBox
* GetAnimatedViewBox() override
;
128 // implementation helpers:
130 bool IsRootSVGSVGElement() const {
131 NS_ASSERTION((IsInUncomposedDoc() && !GetParent()) ==
132 (OwnerDoc()->GetRootElement() == this),
133 "Can't determine if we're root");
134 return !GetParent() && IsInUncomposedDoc() && IsSVGElement(nsGkAtoms::svg
);
138 * Returns the explicit or default preserveAspectRatio, unless we're
139 * synthesizing a viewBox, in which case it returns the "none" value.
141 virtual SVGPreserveAspectRatio
GetPreserveAspectRatioWithOverride() const {
142 return mPreserveAspectRatio
.GetAnimValue();
146 * Returns the explicit viewBox rect, if specified, or else a synthesized
147 * viewBox, if appropriate, or else a viewBox matching the dimensions of the
150 SVGViewBox
GetViewBoxWithSynthesis(float aViewportWidth
,
151 float aViewportHeight
) const;
153 enum { ATTR_X
, ATTR_Y
, ATTR_WIDTH
, ATTR_HEIGHT
};
154 SVGAnimatedLength mLengthAttributes
[4];
155 static LengthInfo sLengthInfo
[4];
156 LengthAttributesInfo
GetLengthInfo() override
;
158 SVGAnimatedPreserveAspectRatio
* GetAnimatedPreserveAspectRatio() override
;
160 virtual const SVGAnimatedViewBox
& GetViewBoxInternal() const {
163 virtual SVGAnimatedTransformList
* GetTransformInternal() const {
164 return mTransforms
.get();
166 SVGAnimatedViewBox mViewBox
;
167 SVGAnimatedPreserveAspectRatio mPreserveAspectRatio
;
169 // The size of the rectangular SVG viewport into which we render. This is
170 // not (necessarily) the same as the content area. See:
172 // http://www.w3.org/TR/SVG11/coords.html#ViewportSpace
174 // XXXjwatt Currently only used for outer <svg>, but maybe we could use -1 to
175 // flag this as an inner <svg> to save the overhead of GetCtx calls?
176 // XXXjwatt our frame should probably reset this when it's destroyed.
177 gfx::Size mViewportSize
;
179 bool mHasChildrenOnlyTransform
;
184 } // namespace mozilla
186 #endif // DOM_SVG_SVGVIEWPORTELEMENT_H_