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
;
36 svgFloatSize(float aWidth
, float aHeight
) : width(aWidth
), height(aHeight
) {}
37 bool operator!=(const svgFloatSize
& rhs
) {
38 return width
!= rhs
.width
|| height
!= rhs
.height
;
44 class SVGViewportElement
: public SVGGraphicsElement
{
45 friend class mozilla::SVGOuterSVGFrame
;
46 friend class mozilla::SVGViewportFrame
;
49 explicit SVGViewportElement(
50 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
);
51 ~SVGViewportElement() = default;
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;
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(); }
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
);
138 already_AddRefed
<SVGAnimatedRect
> ViewBox();
139 already_AddRefed
<DOMSVGAnimatedPreserveAspectRatio
> PreserveAspectRatio();
140 SVGAnimatedViewBox
* GetAnimatedViewBox() override
;
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
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 {
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
;
199 } // namespace mozilla
201 #endif // DOM_SVG_SVGVIEWPORTELEMENT_H_