1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Mozilla SVG project.
17 * The Initial Developer of the Original Code is IBM Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2004
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either of the GNU General Public License Version 2 or later (the "GPL"),
25 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include "nsIDOMSVGAnimatedRect.h"
38 #include "nsIDOMSVGRect.h"
39 #include "nsIDocument.h"
40 #include "nsSVGMarkerFrame.h"
41 #include "nsSVGPathGeometryFrame.h"
42 #include "nsSVGMatrix.h"
43 #include "nsSVGEffects.h"
44 #include "nsSVGMarkerElement.h"
45 #include "nsSVGPathGeometryElement.h"
46 #include "gfxContext.h"
49 NS_NewSVGMarkerFrame(nsIPresShell
* aPresShell
, nsIContent
* aContent
, nsStyleContext
* aContext
)
51 nsCOMPtr
<nsIDOMSVGMarkerElement
> marker
= do_QueryInterface(aContent
);
53 NS_ASSERTION(marker
, "Can't create frame! Content is not an SVG marker");
57 return new (aPresShell
) nsSVGMarkerFrame(aContext
);
60 //----------------------------------------------------------------------
64 nsSVGMarkerFrame::AttributeChanged(PRInt32 aNameSpaceID
,
68 if (aNameSpaceID
== kNameSpaceID_None
&&
69 (aAttribute
== nsGkAtoms::markerUnits
||
70 aAttribute
== nsGkAtoms::refX
||
71 aAttribute
== nsGkAtoms::refY
||
72 aAttribute
== nsGkAtoms::markerWidth
||
73 aAttribute
== nsGkAtoms::markerHeight
||
74 aAttribute
== nsGkAtoms::orient
||
75 aAttribute
== nsGkAtoms::preserveAspectRatio
||
76 aAttribute
== nsGkAtoms::viewBox
)) {
77 nsSVGEffects::InvalidateRenderingObservers(this);
80 return nsSVGMarkerFrameBase::AttributeChanged(aNameSpaceID
,
81 aAttribute
, aModType
);
85 nsSVGMarkerFrame::GetType() const
87 return nsGkAtoms::svgMarkerFrame
;
90 //----------------------------------------------------------------------
91 // nsSVGContainerFrame methods:
93 already_AddRefed
<nsIDOMSVGMatrix
>
94 nsSVGMarkerFrame::GetCanvasTM()
97 // really we should return null, but the rest of the SVG code
98 // isn't set up for that. We're going to be bailing drawing the
99 // marker anyway, so return an identity.
100 nsCOMPtr
<nsIDOMSVGMatrix
> ident
;
101 NS_NewSVGMatrix(getter_AddRefs(ident
));
103 nsIDOMSVGMatrix
*retval
= ident
.get();
104 NS_IF_ADDREF(retval
);
110 // get the tm from the path geometry frame and append local transform
112 NS_ASSERTION(mMarkedFrame
, "null nsSVGPathGeometry frame");
113 nsCOMPtr
<nsIDOMSVGMatrix
> markedTM
;
114 mMarkedFrame
->GetCanvasTM(getter_AddRefs(markedTM
));
115 NS_ASSERTION(markedTM
, "null marked TM");
118 nsSVGMarkerElement
*element
= static_cast<nsSVGMarkerElement
*>(mContent
);
121 nsCOMPtr
<nsIDOMSVGMatrix
> markerTM
;
122 element
->GetMarkerTransform(mStrokeWidth
, mX
, mY
, mAngle
, getter_AddRefs(markerTM
));
125 nsCOMPtr
<nsIDOMSVGMatrix
> viewBoxTM
;
127 element
->GetViewboxToViewportTransform(getter_AddRefs(viewBoxTM
));
129 nsCOMPtr
<nsIDOMSVGMatrix
> tmpTM
;
130 nsCOMPtr
<nsIDOMSVGMatrix
> resultTM
;
132 markedTM
->Multiply(markerTM
, getter_AddRefs(tmpTM
));
134 if (NS_SUCCEEDED(res
) && viewBoxTM
) {
135 tmpTM
->Multiply(viewBoxTM
, getter_AddRefs(resultTM
));
137 NS_WARNING("We should propagate the fact that the viewBox is invalid.");
141 nsIDOMSVGMatrix
*retval
= resultTM
.get();
142 NS_IF_ADDREF(retval
);
151 nsSVGMarkerFrame::PaintMark(nsSVGRenderState
*aContext
,
152 nsSVGPathGeometryFrame
*aMarkedFrame
,
153 nsSVGMark
*aMark
, float aStrokeWidth
)
155 // If the flag is set when we get here, it means this marker frame
156 // has already been used painting the current mark, and the document
157 // has a marker reference loop.
161 nsSVGMarkerElement
*marker
= static_cast<nsSVGMarkerElement
*>(mContent
);
163 nsCOMPtr
<nsIDOMSVGAnimatedRect
> arect
;
164 nsresult rv
= marker
->GetViewBox(getter_AddRefs(arect
));
165 NS_ENSURE_SUCCESS(rv
, rv
);
167 nsCOMPtr
<nsIDOMSVGRect
> rect
;
168 rv
= arect
->GetAnimVal(getter_AddRefs(rect
));
169 NS_ENSURE_SUCCESS(rv
, rv
);
171 float x
, y
, width
, height
;
174 rect
->GetWidth(&width
);
175 rect
->GetHeight(&height
);
177 if (width
<= 0.0f
|| height
<= 0.0f
) {
178 // We must disable rendering if the viewBox width or height are zero.
182 AutoMarkerReferencer
markerRef(this, aMarkedFrame
);
184 mStrokeWidth
= aStrokeWidth
;
187 mAngle
= aMark
->angle
;
189 gfxContext
*gfx
= aContext
->GetGfxContext();
191 if (GetStyleDisplay()->IsScrollableOverflow()) {
192 nsCOMPtr
<nsIDOMSVGMatrix
> matrix
= GetCanvasTM();
193 NS_ENSURE_TRUE(matrix
, NS_ERROR_OUT_OF_MEMORY
);
196 nsSVGUtils::SetClipRect(gfx
, matrix
, x
, y
, width
, height
);
199 for (nsIFrame
* kid
= mFrames
.FirstChild(); kid
;
200 kid
= kid
->GetNextSibling()) {
201 nsISVGChildFrame
* SVGFrame
= nsnull
;
202 CallQueryInterface(kid
, &SVGFrame
);
204 // The CTM of each frame referencing us may be different.
205 SVGFrame
->NotifySVGChanged(nsISVGChildFrame::SUPPRESS_INVALIDATION
|
206 nsISVGChildFrame::TRANSFORM_CHANGED
);
207 nsSVGUtils::PaintFrameWithEffects(aContext
, nsnull
, kid
);
211 if (GetStyleDisplay()->IsScrollableOverflow())
219 nsSVGMarkerFrame::RegionMark(nsSVGPathGeometryFrame
*aMarkedFrame
,
220 const nsSVGMark
*aMark
, float aStrokeWidth
)
222 // If the flag is set when we get here, it means this marker frame
223 // has already been used in calculating the current mark region, and
224 // the document has a marker reference loop.
226 return nsRect(0,0,0,0);
228 AutoMarkerReferencer
markerRef(this, aMarkedFrame
);
230 mStrokeWidth
= aStrokeWidth
;
233 mAngle
= aMark
->angle
;
235 // Force children to update their covered region
236 for (nsIFrame
* kid
= mFrames
.FirstChild();
238 kid
= kid
->GetNextSibling()) {
239 nsISVGChildFrame
* child
= nsnull
;
240 CallQueryInterface(kid
, &child
);
242 child
->UpdateCoveredRegion();
245 // Now get the combined covered region
246 return nsSVGUtils::GetCoveredRegion(mFrames
);
250 nsSVGMarkerFrame::SetParentCoordCtxProvider(nsSVGSVGElement
*aContext
)
252 nsSVGMarkerElement
*marker
= static_cast<nsSVGMarkerElement
*>(mContent
);
253 marker
->SetParentCoordCtxProvider(aContext
);
256 //----------------------------------------------------------------------
259 nsSVGMarkerFrame::AutoMarkerReferencer::AutoMarkerReferencer(
260 nsSVGMarkerFrame
*aFrame
,
261 nsSVGPathGeometryFrame
*aMarkedFrame
)
264 mFrame
->mInUse
= PR_TRUE
;
265 mFrame
->mMarkedFrame
= aMarkedFrame
;
267 nsSVGSVGElement
*ctx
=
268 static_cast<nsSVGElement
*>(aMarkedFrame
->GetContent())->GetCtx();
269 mFrame
->SetParentCoordCtxProvider(ctx
);
272 nsSVGMarkerFrame::AutoMarkerReferencer::~AutoMarkerReferencer()
274 mFrame
->SetParentCoordCtxProvider(nsnull
);
276 mFrame
->mMarkedFrame
= nsnull
;
277 mFrame
->mInUse
= PR_FALSE
;