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 // Keep in (case-insensitive) order:
10 #include "mozilla/PresShell.h"
11 #include "mozilla/SVGContainerFrame.h"
12 #include "mozilla/SVGObserverUtils.h"
13 #include "mozilla/SVGTextFrame.h"
14 #include "mozilla/SVGUtils.h"
15 #include "mozilla/dom/SVGSwitchElement.h"
17 using namespace mozilla::dom
;
18 using namespace mozilla::gfx
;
19 using namespace mozilla::image
;
21 nsIFrame
* NS_NewSVGSwitchFrame(mozilla::PresShell
* aPresShell
,
22 mozilla::ComputedStyle
* aStyle
);
26 class SVGSwitchFrame final
: public SVGGFrame
{
27 friend nsIFrame
* ::NS_NewSVGSwitchFrame(mozilla::PresShell
* aPresShell
,
28 ComputedStyle
* aStyle
);
31 explicit SVGSwitchFrame(ComputedStyle
* aStyle
, nsPresContext
* aPresContext
)
32 : SVGGFrame(aStyle
, aPresContext
, kClassID
) {}
35 NS_DECL_FRAMEARENA_HELPERS(SVGSwitchFrame
)
38 virtual void Init(nsIContent
* aContent
, nsContainerFrame
* aParent
,
39 nsIFrame
* aPrevInFlow
) override
;
42 #ifdef DEBUG_FRAME_DUMP
43 virtual nsresult
GetFrameName(nsAString
& aResult
) const override
{
44 return MakeFrameName(u
"SVGSwitch"_ns
, aResult
);
48 virtual void BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
49 const nsDisplayListSet
& aLists
) override
;
51 // ISVGDisplayableFrame interface:
52 virtual void PaintSVG(gfxContext
& aContext
, const gfxMatrix
& aTransform
,
53 imgDrawingParams
& aImgParams
,
54 const nsIntRect
* aDirtyRect
= nullptr) override
;
55 nsIFrame
* GetFrameForPoint(const gfxPoint
& aPoint
) override
;
56 virtual void ReflowSVG() override
;
57 virtual SVGBBox
GetBBoxContribution(const Matrix
& aToBBoxUserspace
,
58 uint32_t aFlags
) override
;
61 nsIFrame
* GetActiveChildFrame();
62 void ReflowAllSVGTextFramesInsideNonActiveChildren(nsIFrame
* aActiveChild
);
63 static void AlwaysReflowSVGTextFrameDoForOneKid(nsIFrame
* aKid
);
66 } // namespace mozilla
68 //----------------------------------------------------------------------
71 nsIFrame
* NS_NewSVGSwitchFrame(mozilla::PresShell
* aPresShell
,
72 mozilla::ComputedStyle
* aStyle
) {
73 return new (aPresShell
)
74 mozilla::SVGSwitchFrame(aStyle
, aPresShell
->GetPresContext());
79 NS_IMPL_FRAMEARENA_HELPERS(SVGSwitchFrame
)
82 void SVGSwitchFrame::Init(nsIContent
* aContent
, nsContainerFrame
* aParent
,
83 nsIFrame
* aPrevInFlow
) {
84 NS_ASSERTION(aContent
->IsSVGElement(nsGkAtoms::svgSwitch
),
85 "Content is not an SVG switch");
87 SVGGFrame::Init(aContent
, aParent
, aPrevInFlow
);
91 void SVGSwitchFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
92 const nsDisplayListSet
& aLists
) {
93 nsIFrame
* kid
= GetActiveChildFrame();
95 BuildDisplayListForChild(aBuilder
, kid
, aLists
);
99 void SVGSwitchFrame::PaintSVG(gfxContext
& aContext
, const gfxMatrix
& aTransform
,
100 imgDrawingParams
& aImgParams
,
101 const nsIntRect
* aDirtyRect
) {
103 !NS_SVGDisplayListPaintingEnabled() || (mState
& NS_FRAME_IS_NONDISPLAY
),
104 "If display lists are enabled, only painting of non-display "
105 "SVG should take this code path");
107 if (StyleEffects()->mOpacity
== 0.0) {
111 nsIFrame
* kid
= GetActiveChildFrame();
113 gfxMatrix tm
= aTransform
;
114 if (kid
->GetContent()->IsSVGElement()) {
115 tm
= SVGUtils::GetTransformMatrixInUserSpace(kid
) * tm
;
117 SVGUtils::PaintFrameWithEffects(kid
, aContext
, tm
, aImgParams
, aDirtyRect
);
121 nsIFrame
* SVGSwitchFrame::GetFrameForPoint(const gfxPoint
& aPoint
) {
122 NS_ASSERTION(!NS_SVGDisplayListHitTestingEnabled() ||
123 (mState
& NS_FRAME_IS_NONDISPLAY
),
124 "If display lists are enabled, only hit-testing of non-display "
125 "SVG should take this code path");
127 nsIFrame
* kid
= GetActiveChildFrame();
128 ISVGDisplayableFrame
* svgFrame
= do_QueryFrame(kid
);
130 // Transform the point from our SVG user space to our child's.
131 gfxPoint point
= aPoint
;
133 static_cast<const SVGElement
*>(GetContent())
134 ->PrependLocalTransformsTo(gfxMatrix(), eChildToUserSpace
);
135 m
= static_cast<const SVGElement
*>(kid
->GetContent())
136 ->PrependLocalTransformsTo(m
, eUserSpaceToParent
);
137 if (!m
.IsIdentity()) {
141 point
= m
.TransformPoint(point
);
143 return svgFrame
->GetFrameForPoint(point
);
149 static bool shouldReflowSVGTextFrameInside(nsIFrame
* aFrame
) {
150 return aFrame
->IsFrameOfType(nsIFrame::eSVG
| nsIFrame::eSVGContainer
) ||
151 aFrame
->IsSVGForeignObjectFrame() ||
152 !aFrame
->IsFrameOfType(nsIFrame::eSVG
);
155 void SVGSwitchFrame::AlwaysReflowSVGTextFrameDoForOneKid(nsIFrame
* aKid
) {
156 if (!aKid
->IsSubtreeDirty()) {
160 if (aKid
->IsSVGTextFrame()) {
161 MOZ_ASSERT(!aKid
->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY
),
162 "A non-display SVGTextFrame directly contained in a display "
164 static_cast<SVGTextFrame
*>(aKid
)->ReflowSVG();
165 } else if (shouldReflowSVGTextFrameInside(aKid
)) {
166 if (!aKid
->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY
)) {
167 for (nsIFrame
* kid
: aKid
->PrincipalChildList()) {
168 AlwaysReflowSVGTextFrameDoForOneKid(kid
);
171 // This child is in a nondisplay context, something like:
174 // <g><mask><text></text></mask></g>
176 // We should not call ReflowSVG on it.
177 SVGContainerFrame::ReflowSVGNonDisplayText(aKid
);
182 void SVGSwitchFrame::ReflowAllSVGTextFramesInsideNonActiveChildren(
183 nsIFrame
* aActiveChild
) {
184 for (nsIFrame
* kid
= mFrames
.FirstChild(); kid
; kid
= kid
->GetNextSibling()) {
185 if (aActiveChild
== kid
) {
189 AlwaysReflowSVGTextFrameDoForOneKid(kid
);
193 void SVGSwitchFrame::ReflowSVG() {
194 NS_ASSERTION(SVGUtils::OuterSVGIsCallingReflowSVG(this),
195 "This call is probably a wasteful mistake");
197 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_IS_NONDISPLAY
),
198 "ReflowSVG mechanism not designed for this");
200 if (!SVGUtils::NeedsReflowSVG(this)) {
204 // If the NS_FRAME_FIRST_REFLOW bit has been removed from our parent frame,
205 // then our outer-<svg> has previously had its initial reflow. In that case
206 // we need to make sure that that bit has been removed from ourself _before_
207 // recursing over our children to ensure that they know too. Otherwise, we
208 // need to remove it _after_ recursing over our children so that they know
209 // the initial reflow is currently underway.
211 bool isFirstReflow
= (mState
& NS_FRAME_FIRST_REFLOW
);
213 bool outerSVGHasHadFirstReflow
=
214 !GetParent()->HasAnyStateBits(NS_FRAME_FIRST_REFLOW
);
216 if (outerSVGHasHadFirstReflow
) {
217 RemoveStateBits(NS_FRAME_FIRST_REFLOW
); // tell our children
220 OverflowAreas overflowRects
;
222 nsIFrame
* child
= GetActiveChildFrame();
223 ReflowAllSVGTextFramesInsideNonActiveChildren(child
);
225 ISVGDisplayableFrame
* svgChild
= do_QueryFrame(child
);
227 MOZ_ASSERT(!child
->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY
),
228 "Check for this explicitly in the |if|, then");
229 svgChild
->ReflowSVG();
231 // We build up our child frame overflows here instead of using
232 // nsLayoutUtils::UnionChildOverflow since SVG frame's all use the same
233 // frame list, and we're iterating over that list now anyway.
234 ConsiderChildOverflow(overflowRects
, child
);
235 } else if (child
&& shouldReflowSVGTextFrameInside(child
)) {
236 MOZ_ASSERT(child
->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY
) ||
237 !child
->IsFrameOfType(nsIFrame::eSVG
),
238 "Check for this explicitly in the |if|, then");
239 ReflowSVGNonDisplayText(child
);
243 // Make sure we have our filter property (if any) before calling
244 // FinishAndStoreOverflow (subsequent filter changes are handled off
245 // nsChangeHint_UpdateEffects):
246 SVGObserverUtils::UpdateEffects(this);
249 FinishAndStoreOverflow(overflowRects
, mRect
.Size());
251 // Remove state bits after FinishAndStoreOverflow so that it doesn't
252 // invalidate on first reflow:
253 RemoveStateBits(NS_FRAME_FIRST_REFLOW
| NS_FRAME_IS_DIRTY
|
254 NS_FRAME_HAS_DIRTY_CHILDREN
);
257 SVGBBox
SVGSwitchFrame::GetBBoxContribution(const Matrix
& aToBBoxUserspace
,
259 nsIFrame
* kid
= GetActiveChildFrame();
260 ISVGDisplayableFrame
* svgKid
= do_QueryFrame(kid
);
262 nsIContent
* content
= kid
->GetContent();
263 gfxMatrix transform
= ThebesMatrix(aToBBoxUserspace
);
264 if (content
->IsSVGElement()) {
265 transform
= static_cast<SVGElement
*>(content
)->PrependLocalTransformsTo(
266 {}, eChildToUserSpace
) *
267 SVGUtils::GetTransformMatrixInUserSpace(kid
) * transform
;
269 return svgKid
->GetBBoxContribution(ToMatrix(transform
), aFlags
);
274 nsIFrame
* SVGSwitchFrame::GetActiveChildFrame() {
275 nsIContent
* activeChild
=
276 static_cast<dom::SVGSwitchElement
*>(GetContent())->GetActiveChild();
279 for (nsIFrame
* kid
= mFrames
.FirstChild(); kid
;
280 kid
= kid
->GetNextSibling()) {
281 if (activeChild
== kid
->GetContent()) {
289 } // namespace mozilla