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/. */
8 #include "SVGMarkerFrame.h"
10 // Keep others in (case-insensitive) order:
11 #include "gfxContext.h"
12 #include "mozilla/PresShell.h"
13 #include "mozilla/SVGContextPaint.h"
14 #include "mozilla/SVGGeometryFrame.h"
15 #include "mozilla/SVGObserverUtils.h"
16 #include "mozilla/SVGUtils.h"
17 #include "mozilla/dom/SVGGeometryElement.h"
18 #include "mozilla/dom/SVGMarkerElement.h"
20 using namespace mozilla::dom
;
21 using namespace mozilla::gfx
;
22 using namespace mozilla::image
;
24 nsContainerFrame
* NS_NewSVGMarkerFrame(mozilla::PresShell
* aPresShell
,
25 mozilla::ComputedStyle
* aStyle
) {
26 return new (aPresShell
)
27 mozilla::SVGMarkerFrame(aStyle
, aPresShell
->GetPresContext());
32 NS_IMPL_FRAMEARENA_HELPERS(SVGMarkerFrame
)
34 //----------------------------------------------------------------------
37 nsresult
SVGMarkerFrame::AttributeChanged(int32_t aNameSpaceID
,
40 if (aNameSpaceID
== kNameSpaceID_None
&&
41 (aAttribute
== nsGkAtoms::markerUnits
|| aAttribute
== nsGkAtoms::refX
||
42 aAttribute
== nsGkAtoms::refY
|| aAttribute
== nsGkAtoms::markerWidth
||
43 aAttribute
== nsGkAtoms::markerHeight
||
44 aAttribute
== nsGkAtoms::orient
||
45 aAttribute
== nsGkAtoms::preserveAspectRatio
||
46 aAttribute
== nsGkAtoms::viewBox
)) {
47 SVGObserverUtils::InvalidateRenderingObservers(this);
50 return SVGContainerFrame::AttributeChanged(aNameSpaceID
, aAttribute
,
55 void SVGMarkerFrame::Init(nsIContent
* aContent
, nsContainerFrame
* aParent
,
56 nsIFrame
* aPrevInFlow
) {
57 NS_ASSERTION(aContent
->IsSVGElement(nsGkAtoms::marker
),
58 "Content is not an SVG marker");
60 SVGContainerFrame::Init(aContent
, aParent
, aPrevInFlow
);
64 //----------------------------------------------------------------------
65 // SVGContainerFrame methods:
67 gfxMatrix
SVGMarkerFrame::GetCanvasTM() {
68 NS_ASSERTION(mMarkedFrame
, "null SVGGeometry frame");
71 // We're going to be bailing drawing the marker, so return an identity.
75 SVGMarkerElement
* content
= static_cast<SVGMarkerElement
*>(GetContent());
78 gfxMatrix markedTM
= mMarkedFrame
->GetCanvasTM();
81 Matrix viewBoxTM
= content
->GetViewBoxTransform();
83 return ThebesMatrix(viewBoxTM
* mMarkerTM
) * markedTM
;
86 static nsIFrame
* GetAnonymousChildFrame(nsIFrame
* aFrame
) {
87 nsIFrame
* kid
= aFrame
->PrincipalChildList().FirstChild();
88 MOZ_ASSERT(kid
&& kid
->IsSVGMarkerAnonChildFrame(),
89 "expected to find anonymous child of marker frame");
93 void SVGMarkerFrame::PaintMark(gfxContext
& aContext
,
94 const gfxMatrix
& aToMarkedFrameUserSpace
,
95 SVGGeometryFrame
* aMarkedFrame
,
96 const SVGMark
& aMark
, float aStrokeWidth
,
97 imgDrawingParams
& aImgParams
) {
98 // If the flag is set when we get here, it means this marker frame
99 // has already been used painting the current mark, and the document
100 // has a marker reference loop.
105 AutoMarkerReferencer
markerRef(this, aMarkedFrame
);
107 SVGMarkerElement
* marker
= static_cast<SVGMarkerElement
*>(GetContent());
108 if (!marker
->HasValidDimensions()) {
112 const SVGViewBox viewBox
= marker
->GetViewBox();
114 if (viewBox
.width
<= 0.0f
|| viewBox
.height
<= 0.0f
) {
115 // We must disable rendering if the viewBox width or height are zero.
119 Matrix viewBoxTM
= marker
->GetViewBoxTransform();
121 mMarkerTM
= marker
->GetMarkerTransform(aStrokeWidth
, aMark
);
123 gfxMatrix markTM
= ThebesMatrix(viewBoxTM
) * ThebesMatrix(mMarkerTM
) *
124 aToMarkedFrameUserSpace
;
126 gfxClipAutoSaveRestore
autoSaveClip(&aContext
);
127 if (StyleDisplay()->IsScrollableOverflow()) {
128 gfxRect clipRect
= SVGUtils::GetClipRectForFrame(
129 this, viewBox
.x
, viewBox
.y
, viewBox
.width
, viewBox
.height
);
130 autoSaveClip
.TransformedClip(markTM
, clipRect
);
133 nsIFrame
* kid
= GetAnonymousChildFrame(this);
134 ISVGDisplayableFrame
* SVGFrame
= do_QueryFrame(kid
);
135 // The CTM of each frame referencing us may be different.
136 SVGFrame
->NotifySVGChanged(ISVGDisplayableFrame::TRANSFORM_CHANGED
);
137 auto contextPaint
= MakeRefPtr
<SVGContextPaintImpl
>();
138 contextPaint
->Init(aContext
.GetDrawTarget(), aContext
.CurrentMatrixDouble(),
139 aMarkedFrame
, SVGContextPaint::GetContextPaint(marker
),
141 AutoSetRestoreSVGContextPaint
autoSetRestore(contextPaint
,
143 SVGUtils::PaintFrameWithEffects(kid
, aContext
, markTM
, aImgParams
);
146 SVGBBox
SVGMarkerFrame::GetMarkBBoxContribution(const Matrix
& aToBBoxUserspace
,
148 SVGGeometryFrame
* aMarkedFrame
,
149 const SVGMark
& aMark
,
150 float aStrokeWidth
) {
153 // If the flag is set when we get here, it means this marker frame
154 // has already been used in calculating the current mark bbox, and
155 // the document has a marker reference loop.
160 AutoMarkerReferencer
markerRef(this, aMarkedFrame
);
162 SVGMarkerElement
* content
= static_cast<SVGMarkerElement
*>(GetContent());
163 if (!content
->HasValidDimensions()) {
167 const SVGViewBox viewBox
= content
->GetViewBox();
169 if (viewBox
.width
<= 0.0f
|| viewBox
.height
<= 0.0f
) {
173 mMarkerTM
= content
->GetMarkerTransform(aStrokeWidth
, aMark
);
174 Matrix viewBoxTM
= content
->GetViewBoxTransform();
176 Matrix tm
= viewBoxTM
* mMarkerTM
* aToBBoxUserspace
;
178 ISVGDisplayableFrame
* child
= do_QueryFrame(GetAnonymousChildFrame(this));
179 // When we're being called to obtain the invalidation area, we need to
180 // pass down all the flags so that stroke is included. However, once DOM
181 // getBBox() accepts flags, maybe we should strip some of those here?
183 // We need to include zero width/height vertical/horizontal lines, so we have
184 // to use UnionEdges.
185 bbox
.UnionEdges(child
->GetBBoxContribution(tm
, aFlags
));
190 void SVGMarkerFrame::SetParentCoordCtxProvider(SVGViewportElement
* aContext
) {
191 SVGMarkerElement
* marker
= static_cast<SVGMarkerElement
*>(GetContent());
192 marker
->SetParentCoordCtxProvider(aContext
);
195 void SVGMarkerFrame::AppendDirectlyOwnedAnonBoxes(
196 nsTArray
<OwnedAnonBox
>& aResult
) {
197 aResult
.AppendElement(OwnedAnonBox(GetAnonymousChildFrame(this)));
200 //----------------------------------------------------------------------
203 SVGMarkerFrame::AutoMarkerReferencer::AutoMarkerReferencer(
204 SVGMarkerFrame
* aFrame
, SVGGeometryFrame
* aMarkedFrame
)
206 mFrame
->mInUse
= true;
207 mFrame
->mMarkedFrame
= aMarkedFrame
;
209 SVGViewportElement
* ctx
=
210 static_cast<SVGElement
*>(aMarkedFrame
->GetContent())->GetCtx();
211 mFrame
->SetParentCoordCtxProvider(ctx
);
214 SVGMarkerFrame::AutoMarkerReferencer::~AutoMarkerReferencer() {
215 mFrame
->SetParentCoordCtxProvider(nullptr);
217 mFrame
->mMarkedFrame
= nullptr;
218 mFrame
->mInUse
= false;
221 } // namespace mozilla
223 //----------------------------------------------------------------------
224 // Implementation of SVGMarkerAnonChildFrame
226 nsContainerFrame
* NS_NewSVGMarkerAnonChildFrame(
227 mozilla::PresShell
* aPresShell
, mozilla::ComputedStyle
* aStyle
) {
228 return new (aPresShell
)
229 mozilla::SVGMarkerAnonChildFrame(aStyle
, aPresShell
->GetPresContext());
234 NS_IMPL_FRAMEARENA_HELPERS(SVGMarkerAnonChildFrame
)
237 void SVGMarkerAnonChildFrame::Init(nsIContent
* aContent
,
238 nsContainerFrame
* aParent
,
239 nsIFrame
* aPrevInFlow
) {
240 MOZ_ASSERT(aParent
->IsSVGMarkerFrame(), "Unexpected parent");
241 SVGDisplayContainerFrame::Init(aContent
, aParent
, aPrevInFlow
);
245 } // namespace mozilla