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/. */
6
7 // Main header first:
8 #include "nsSVGMarkerFrame.h"
9
10 // Keep others in (case-insensitive) order:
11 #include "gfxContext.h"
12 #include "SVGObserverUtils.h"
13 #include "mozilla/dom/SVGMarkerElement.h"
14 #include "SVGGeometryElement.h"
15 #include "SVGGeometryFrame.h"
16
17 using namespace mozilla::dom;
18 using namespace mozilla::gfx;
19 using namespace mozilla::image;
20
NS_NewSVGMarkerFrame(nsIPresShell * aPresShell,nsStyleContext * aContext)21 nsContainerFrame* NS_NewSVGMarkerFrame(nsIPresShell* aPresShell,
22 nsStyleContext* aContext) {
23 return new (aPresShell) nsSVGMarkerFrame(aContext);
24 }
25
NS_IMPL_FRAMEARENA_HELPERS(nsSVGMarkerFrame)26 NS_IMPL_FRAMEARENA_HELPERS(nsSVGMarkerFrame)
27
28 //----------------------------------------------------------------------
29 // nsIFrame methods:
30
31 nsresult nsSVGMarkerFrame::AttributeChanged(int32_t aNameSpaceID,
32 nsAtom* aAttribute,
33 int32_t aModType) {
34 if (aNameSpaceID == kNameSpaceID_None &&
35 (aAttribute == nsGkAtoms::markerUnits || aAttribute == nsGkAtoms::refX ||
36 aAttribute == nsGkAtoms::refY || aAttribute == nsGkAtoms::markerWidth ||
37 aAttribute == nsGkAtoms::markerHeight ||
38 aAttribute == nsGkAtoms::orient ||
39 aAttribute == nsGkAtoms::preserveAspectRatio ||
40 aAttribute == nsGkAtoms::viewBox)) {
41 SVGObserverUtils::InvalidateDirectRenderingObservers(this);
42 }
43
44 return nsSVGContainerFrame::AttributeChanged(aNameSpaceID, aAttribute,
45 aModType);
46 }
47
48 #ifdef DEBUG
Init(nsIContent * aContent,nsContainerFrame * aParent,nsIFrame * aPrevInFlow)49 void nsSVGMarkerFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
50 nsIFrame* aPrevInFlow) {
51 NS_ASSERTION(aContent->IsSVGElement(nsGkAtoms::marker),
52 "Content is not an SVG marker");
53
54 nsSVGContainerFrame::Init(aContent, aParent, aPrevInFlow);
55 }
56 #endif /* DEBUG */
57
58 //----------------------------------------------------------------------
59 // nsSVGContainerFrame methods:
60
GetCanvasTM()61 gfxMatrix nsSVGMarkerFrame::GetCanvasTM() {
62 NS_ASSERTION(mMarkedFrame, "null SVGGeometry frame");
63
64 if (mInUse2) {
65 // We're going to be bailing drawing the marker, so return an identity.
66 return gfxMatrix();
67 }
68
69 SVGMarkerElement* content = static_cast<SVGMarkerElement*>(GetContent());
70
71 mInUse2 = true;
72 gfxMatrix markedTM = mMarkedFrame->GetCanvasTM();
73 mInUse2 = false;
74
75 Matrix viewBoxTM = content->GetViewBoxTransform();
76
77 return ThebesMatrix(viewBoxTM * mMarkerTM) * markedTM;
78 }
79
GetAnonymousChildFrame(nsIFrame * aFrame)80 static nsIFrame* GetAnonymousChildFrame(nsIFrame* aFrame) {
81 nsIFrame* kid = aFrame->PrincipalChildList().FirstChild();
82 MOZ_ASSERT(kid && kid->IsSVGMarkerAnonChildFrame(),
83 "expected to find anonymous child of marker frame");
84 return kid;
85 }
86
PaintMark(gfxContext & aContext,const gfxMatrix & aToMarkedFrameUserSpace,SVGGeometryFrame * aMarkedFrame,const nsSVGMark & aMark,float aStrokeWidth,imgDrawingParams & aImgParams)87 void nsSVGMarkerFrame::PaintMark(gfxContext& aContext,
88 const gfxMatrix& aToMarkedFrameUserSpace,
89 SVGGeometryFrame* aMarkedFrame,
90 const nsSVGMark& aMark, float aStrokeWidth,
91 imgDrawingParams& aImgParams) {
92 // If the flag is set when we get here, it means this marker frame
93 // has already been used painting the current mark, and the document
94 // has a marker reference loop.
95 if (mInUse) {
96 return;
97 }
98
99 AutoMarkerReferencer markerRef(this, aMarkedFrame);
100
101 SVGMarkerElement* marker = static_cast<SVGMarkerElement*>(GetContent());
102 if (!marker->HasValidDimensions()) {
103 return;
104 }
105
106 const nsSVGViewBoxRect viewBox = marker->GetViewBoxRect();
107
108 if (viewBox.width <= 0.0f || viewBox.height <= 0.0f) {
109 // We must disable rendering if the viewBox width or height are zero.
110 return;
111 }
112
113 Matrix viewBoxTM = marker->GetViewBoxTransform();
114
115 mMarkerTM = marker->GetMarkerTransform(aStrokeWidth, aMark);
116
117 gfxMatrix markTM = ThebesMatrix(viewBoxTM) * ThebesMatrix(mMarkerTM) *
118 aToMarkedFrameUserSpace;
119
120 if (StyleDisplay()->IsScrollableOverflow()) {
121 aContext.Save();
122 gfxRect clipRect = nsSVGUtils::GetClipRectForFrame(
123 this, viewBox.x, viewBox.y, viewBox.width, viewBox.height);
124 nsSVGUtils::SetClipRect(&aContext, markTM, clipRect);
125 }
126
127 nsIFrame* kid = GetAnonymousChildFrame(this);
128 nsSVGDisplayableFrame* SVGFrame = do_QueryFrame(kid);
129 // The CTM of each frame referencing us may be different.
130 SVGFrame->NotifySVGChanged(nsSVGDisplayableFrame::TRANSFORM_CHANGED);
131 nsSVGUtils::PaintFrameWithEffects(kid, aContext, markTM, aImgParams);
132
133 if (StyleDisplay()->IsScrollableOverflow()) aContext.Restore();
134 }
135
GetMarkBBoxContribution(const Matrix & aToBBoxUserspace,uint32_t aFlags,SVGGeometryFrame * aMarkedFrame,const nsSVGMark & aMark,float aStrokeWidth)136 SVGBBox nsSVGMarkerFrame::GetMarkBBoxContribution(
137 const Matrix& aToBBoxUserspace, uint32_t aFlags,
138 SVGGeometryFrame* aMarkedFrame, const nsSVGMark& aMark,
139 float aStrokeWidth) {
140 SVGBBox bbox;
141
142 // If the flag is set when we get here, it means this marker frame
143 // has already been used in calculating the current mark bbox, and
144 // the document has a marker reference loop.
145 if (mInUse) return bbox;
146
147 AutoMarkerReferencer markerRef(this, aMarkedFrame);
148
149 SVGMarkerElement* content = static_cast<SVGMarkerElement*>(GetContent());
150 if (!content->HasValidDimensions()) {
151 return bbox;
152 }
153
154 const nsSVGViewBoxRect viewBox = content->GetViewBoxRect();
155
156 if (viewBox.width <= 0.0f || viewBox.height <= 0.0f) {
157 return bbox;
158 }
159
160 mMarkerTM = content->GetMarkerTransform(aStrokeWidth, aMark);
161 Matrix viewBoxTM = content->GetViewBoxTransform();
162
163 Matrix tm = viewBoxTM * mMarkerTM * aToBBoxUserspace;
164
165 nsSVGDisplayableFrame* child = do_QueryFrame(GetAnonymousChildFrame(this));
166 // When we're being called to obtain the invalidation area, we need to
167 // pass down all the flags so that stroke is included. However, once DOM
168 // getBBox() accepts flags, maybe we should strip some of those here?
169
170 // We need to include zero width/height vertical/horizontal lines, so we have
171 // to use UnionEdges.
172 bbox.UnionEdges(child->GetBBoxContribution(tm, aFlags));
173
174 return bbox;
175 }
176
SetParentCoordCtxProvider(SVGViewportElement * aContext)177 void nsSVGMarkerFrame::SetParentCoordCtxProvider(SVGViewportElement* aContext) {
178 SVGMarkerElement* marker = static_cast<SVGMarkerElement*>(GetContent());
179 marker->SetParentCoordCtxProvider(aContext);
180 }
181
AppendDirectlyOwnedAnonBoxes(nsTArray<OwnedAnonBox> & aResult)182 void nsSVGMarkerFrame::AppendDirectlyOwnedAnonBoxes(
183 nsTArray<OwnedAnonBox>& aResult) {
184 aResult.AppendElement(OwnedAnonBox(GetAnonymousChildFrame(this)));
185 }
186
187 //----------------------------------------------------------------------
188 // helper class
189
AutoMarkerReferencer(nsSVGMarkerFrame * aFrame,SVGGeometryFrame * aMarkedFrame MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)190 nsSVGMarkerFrame::AutoMarkerReferencer::AutoMarkerReferencer(
191 nsSVGMarkerFrame* aFrame,
192 SVGGeometryFrame* aMarkedFrame MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
193 : mFrame(aFrame) {
194 MOZ_GUARD_OBJECT_NOTIFIER_INIT;
195 mFrame->mInUse = true;
196 mFrame->mMarkedFrame = aMarkedFrame;
197
198 SVGViewportElement* ctx =
199 static_cast<nsSVGElement*>(aMarkedFrame->GetContent())->GetCtx();
200 mFrame->SetParentCoordCtxProvider(ctx);
201 }
202
~AutoMarkerReferencer()203 nsSVGMarkerFrame::AutoMarkerReferencer::~AutoMarkerReferencer() {
204 mFrame->SetParentCoordCtxProvider(nullptr);
205
206 mFrame->mMarkedFrame = nullptr;
207 mFrame->mInUse = false;
208 }
209
210 //----------------------------------------------------------------------
211 // Implementation of nsSVGMarkerAnonChildFrame
212
NS_NewSVGMarkerAnonChildFrame(nsIPresShell * aPresShell,nsStyleContext * aContext)213 nsContainerFrame* NS_NewSVGMarkerAnonChildFrame(nsIPresShell* aPresShell,
214 nsStyleContext* aContext) {
215 return new (aPresShell) nsSVGMarkerAnonChildFrame(aContext);
216 }
217
NS_IMPL_FRAMEARENA_HELPERS(nsSVGMarkerAnonChildFrame)218 NS_IMPL_FRAMEARENA_HELPERS(nsSVGMarkerAnonChildFrame)
219
220 #ifdef DEBUG
221 void nsSVGMarkerAnonChildFrame::Init(nsIContent* aContent,
222 nsContainerFrame* aParent,
223 nsIFrame* aPrevInFlow) {
224 MOZ_ASSERT(aParent->IsSVGMarkerFrame(), "Unexpected parent");
225 nsSVGDisplayContainerFrame::Init(aContent, aParent, aPrevInFlow);
226 }
227 #endif
228