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 void Init(nsIContent
* aContent
, nsContainerFrame
* aParent
,
39 nsIFrame
* aPrevInFlow
) override
;
42 #ifdef DEBUG_FRAME_DUMP
43 nsresult
GetFrameName(nsAString
& aResult
) const override
{
44 return MakeFrameName(u
"SVGSwitch"_ns
, aResult
);
48 void BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
49 const nsDisplayListSet
& aLists
) override
;
51 // ISVGDisplayableFrame interface:
52 void PaintSVG(gfxContext
& aContext
, const gfxMatrix
& aTransform
,
53 imgDrawingParams
& aImgParams
) override
;
54 nsIFrame
* GetFrameForPoint(const gfxPoint
& aPoint
) override
;
55 void ReflowSVG() override
;
56 SVGBBox
GetBBoxContribution(const Matrix
& aToBBoxUserspace
,
57 uint32_t aFlags
) override
;
60 nsIFrame
* GetActiveChildFrame();
61 void ReflowAllSVGTextFramesInsideNonActiveChildren(nsIFrame
* aActiveChild
);
62 static void AlwaysReflowSVGTextFrameDoForOneKid(nsIFrame
* aKid
);
65 } // namespace mozilla
67 //----------------------------------------------------------------------
70 nsIFrame
* NS_NewSVGSwitchFrame(mozilla::PresShell
* aPresShell
,
71 mozilla::ComputedStyle
* aStyle
) {
72 return new (aPresShell
)
73 mozilla::SVGSwitchFrame(aStyle
, aPresShell
->GetPresContext());
78 NS_IMPL_FRAMEARENA_HELPERS(SVGSwitchFrame
)
81 void SVGSwitchFrame::Init(nsIContent
* aContent
, nsContainerFrame
* aParent
,
82 nsIFrame
* aPrevInFlow
) {
83 NS_ASSERTION(aContent
->IsSVGElement(nsGkAtoms::svgSwitch
),
84 "Content is not an SVG switch");
86 SVGGFrame::Init(aContent
, aParent
, aPrevInFlow
);
90 void SVGSwitchFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
91 const nsDisplayListSet
& aLists
) {
92 nsIFrame
* kid
= GetActiveChildFrame();
94 BuildDisplayListForChild(aBuilder
, kid
, aLists
);
98 void SVGSwitchFrame::PaintSVG(gfxContext
& aContext
, const gfxMatrix
& aTransform
,
99 imgDrawingParams
& aImgParams
) {
100 NS_ASSERTION(HasAnyStateBits(NS_FRAME_IS_NONDISPLAY
),
101 "Only painting of non-display SVG should take this code path");
103 if (StyleEffects()->IsTransparent()) {
107 nsIFrame
* kid
= GetActiveChildFrame();
109 gfxMatrix tm
= aTransform
;
110 if (kid
->GetContent()->IsSVGElement()) {
111 tm
= SVGUtils::GetTransformMatrixInUserSpace(kid
) * tm
;
113 SVGUtils::PaintFrameWithEffects(kid
, aContext
, tm
, aImgParams
);
117 nsIFrame
* SVGSwitchFrame::GetFrameForPoint(const gfxPoint
& aPoint
) {
118 MOZ_ASSERT_UNREACHABLE("A clipPath cannot contain an SVGSwitch element");
122 static bool ShouldReflowSVGTextFrameInside(nsIFrame
* aFrame
) {
123 return aFrame
->IsSVGContainerFrame() || aFrame
->IsSVGForeignObjectFrame() ||
124 !aFrame
->IsSVGFrame();
127 void SVGSwitchFrame::AlwaysReflowSVGTextFrameDoForOneKid(nsIFrame
* aKid
) {
128 if (!aKid
->IsSubtreeDirty()) {
132 if (aKid
->IsSVGTextFrame()) {
133 MOZ_ASSERT(!aKid
->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY
),
134 "A non-display SVGTextFrame directly contained in a display "
136 static_cast<SVGTextFrame
*>(aKid
)->ReflowSVG();
137 } else if (ShouldReflowSVGTextFrameInside(aKid
)) {
138 if (!aKid
->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY
)) {
139 for (nsIFrame
* kid
: aKid
->PrincipalChildList()) {
140 AlwaysReflowSVGTextFrameDoForOneKid(kid
);
143 // This child is in a nondisplay context, something like:
146 // <g><mask><text></text></mask></g>
148 // We should not call ReflowSVG on it.
149 SVGContainerFrame::ReflowSVGNonDisplayText(aKid
);
154 void SVGSwitchFrame::ReflowAllSVGTextFramesInsideNonActiveChildren(
155 nsIFrame
* aActiveChild
) {
156 for (nsIFrame
* kid
= mFrames
.FirstChild(); kid
; kid
= kid
->GetNextSibling()) {
157 if (aActiveChild
== kid
) {
161 AlwaysReflowSVGTextFrameDoForOneKid(kid
);
165 void SVGSwitchFrame::ReflowSVG() {
166 NS_ASSERTION(SVGUtils::OuterSVGIsCallingReflowSVG(this),
167 "This call is probably a wasteful mistake");
169 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_IS_NONDISPLAY
),
170 "ReflowSVG mechanism not designed for this");
172 if (!SVGUtils::NeedsReflowSVG(this)) {
176 // If the NS_FRAME_FIRST_REFLOW bit has been removed from our parent frame,
177 // then our outer-<svg> has previously had its initial reflow. In that case
178 // we need to make sure that that bit has been removed from ourself _before_
179 // recursing over our children to ensure that they know too. Otherwise, we
180 // need to remove it _after_ recursing over our children so that they know
181 // the initial reflow is currently underway.
183 bool isFirstReflow
= HasAnyStateBits(NS_FRAME_FIRST_REFLOW
);
185 bool outerSVGHasHadFirstReflow
=
186 !GetParent()->HasAnyStateBits(NS_FRAME_FIRST_REFLOW
);
188 if (outerSVGHasHadFirstReflow
) {
189 RemoveStateBits(NS_FRAME_FIRST_REFLOW
); // tell our children
192 OverflowAreas overflowRects
;
194 nsIFrame
* child
= GetActiveChildFrame();
195 ReflowAllSVGTextFramesInsideNonActiveChildren(child
);
197 ISVGDisplayableFrame
* svgChild
= do_QueryFrame(child
);
199 MOZ_ASSERT(!child
->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY
),
200 "Check for this explicitly in the |if|, then");
201 svgChild
->ReflowSVG();
203 // We build up our child frame overflows here instead of using
204 // nsLayoutUtils::UnionChildOverflow since SVG frame's all use the same
205 // frame list, and we're iterating over that list now anyway.
206 ConsiderChildOverflow(overflowRects
, child
);
207 } else if (child
&& ShouldReflowSVGTextFrameInside(child
)) {
209 child
->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY
) || !child
->IsSVGFrame(),
210 "Check for this explicitly in the |if|, then");
211 ReflowSVGNonDisplayText(child
);
215 // Make sure we have our filter property (if any) before calling
216 // FinishAndStoreOverflow (subsequent filter changes are handled off
217 // nsChangeHint_UpdateEffects):
218 SVGObserverUtils::UpdateEffects(this);
221 FinishAndStoreOverflow(overflowRects
, mRect
.Size());
223 // Remove state bits after FinishAndStoreOverflow so that it doesn't
224 // invalidate on first reflow:
225 RemoveStateBits(NS_FRAME_FIRST_REFLOW
| NS_FRAME_IS_DIRTY
|
226 NS_FRAME_HAS_DIRTY_CHILDREN
);
229 SVGBBox
SVGSwitchFrame::GetBBoxContribution(const Matrix
& aToBBoxUserspace
,
231 nsIFrame
* kid
= GetActiveChildFrame();
232 ISVGDisplayableFrame
* svgKid
= do_QueryFrame(kid
);
234 nsIContent
* content
= kid
->GetContent();
235 gfxMatrix transform
= ThebesMatrix(aToBBoxUserspace
);
236 if (content
->IsSVGElement()) {
237 transform
= static_cast<SVGElement
*>(content
)->PrependLocalTransformsTo(
238 {}, eChildToUserSpace
) *
239 SVGUtils::GetTransformMatrixInUserSpace(kid
) * transform
;
241 return svgKid
->GetBBoxContribution(ToMatrix(transform
), aFlags
);
246 nsIFrame
* SVGSwitchFrame::GetActiveChildFrame() {
247 nsIContent
* activeChild
=
248 static_cast<dom::SVGSwitchElement
*>(GetContent())->GetActiveChild();
251 for (nsIFrame
* kid
= mFrames
.FirstChild(); kid
;
252 kid
= kid
->GetNextSibling()) {
253 if (activeChild
== kid
->GetContent()) {
261 } // namespace mozilla