Save all modification
[mozilla-1.9/m8.git] / layout / svg / base / src / nsSVGMarkerFrame.cpp
blob1ad476ddb88edd98ae10d8cda0c5f3b7caaa60ad
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
13 * License.
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.
21 * Contributor(s):
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 "nsSVGMarkerElement.h"
44 #include "nsSVGPathGeometryElement.h"
45 #include "gfxContext.h"
47 nsIFrame*
48 NS_NewSVGMarkerFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext)
50 nsCOMPtr<nsIDOMSVGMarkerElement> marker = do_QueryInterface(aContent);
51 if (!marker) {
52 NS_ASSERTION(marker, "Can't create frame! Content is not an SVG marker");
53 return nsnull;
56 return new (aPresShell) nsSVGMarkerFrame(aContext);
59 nsIContent *
60 NS_GetSVGMarkerElement(nsIURI *aURI, nsIContent *aContent)
62 nsIContent* content = nsContentUtils::GetReferencedElement(aURI, aContent);
64 nsCOMPtr<nsIDOMSVGMarkerElement> marker = do_QueryInterface(content);
66 if (marker)
67 return content;
69 return nsnull;
72 //----------------------------------------------------------------------
73 // nsSVGContainerFrame methods:
75 already_AddRefed<nsIDOMSVGMatrix>
76 nsSVGMarkerFrame::GetCanvasTM()
78 if (mInUse2) {
79 // really we should return null, but the rest of the SVG code
80 // isn't set up for that. We're going to be bailing drawing the
81 // marker anyway, so return an identity.
82 nsCOMPtr<nsIDOMSVGMatrix> ident;
83 NS_NewSVGMatrix(getter_AddRefs(ident));
85 nsIDOMSVGMatrix *retval = ident.get();
86 NS_IF_ADDREF(retval);
87 return retval;
90 mInUse2 = PR_TRUE;
92 // get the tm from the path geometry frame and append local transform
94 NS_ASSERTION(mMarkedFrame, "null nsSVGPathGeometry frame");
95 nsCOMPtr<nsIDOMSVGMatrix> markedTM;
96 mMarkedFrame->GetCanvasTM(getter_AddRefs(markedTM));
97 NS_ASSERTION(markedTM, "null marked TM");
99 // get element
100 nsSVGMarkerElement *element = static_cast<nsSVGMarkerElement*>(mContent);
102 // scale/move marker
103 nsCOMPtr<nsIDOMSVGMatrix> markerTM;
104 element->GetMarkerTransform(mStrokeWidth, mX, mY, mAngle, getter_AddRefs(markerTM));
106 // viewport marker
107 nsCOMPtr<nsIDOMSVGMatrix> viewTM;
108 element->GetViewboxToViewportTransform(getter_AddRefs(viewTM));
110 nsCOMPtr<nsIDOMSVGMatrix> tmpTM;
111 nsCOMPtr<nsIDOMSVGMatrix> resultTM;
113 markedTM->Multiply(markerTM, getter_AddRefs(tmpTM));
114 tmpTM->Multiply(viewTM, getter_AddRefs(resultTM));
116 nsIDOMSVGMatrix *retval = resultTM.get();
117 NS_IF_ADDREF(retval);
119 mInUse2 = PR_FALSE;
121 return retval;
125 nsresult
126 nsSVGMarkerFrame::PaintMark(nsSVGRenderState *aContext,
127 nsSVGPathGeometryFrame *aMarkedFrame,
128 nsSVGMark *aMark, float aStrokeWidth)
130 // If the flag is set when we get here, it means this marker frame
131 // has already been used painting the current mark, and the document
132 // has a marker reference loop.
133 if (mInUse)
134 return NS_OK;
136 AutoMarkerReferencer markerRef(this, aMarkedFrame);
138 mStrokeWidth = aStrokeWidth;
139 mX = aMark->x;
140 mY = aMark->y;
141 mAngle = aMark->angle;
143 gfxContext *gfx = aContext->GetGfxContext();
145 if (GetStyleDisplay()->IsScrollableOverflow()) {
146 nsSVGMarkerElement *marker = static_cast<nsSVGMarkerElement*>(mContent);
148 nsCOMPtr<nsIDOMSVGAnimatedRect> arect;
149 nsresult rv = marker->GetViewBox(getter_AddRefs(arect));
150 NS_ENSURE_SUCCESS(rv, rv);
152 nsCOMPtr<nsIDOMSVGRect> rect;
153 rv = arect->GetAnimVal(getter_AddRefs(rect));
154 NS_ENSURE_SUCCESS(rv, rv);
156 float x, y, width, height;
157 rect->GetX(&x);
158 rect->GetY(&y);
159 rect->GetWidth(&width);
160 rect->GetHeight(&height);
162 nsCOMPtr<nsIDOMSVGMatrix> matrix = GetCanvasTM();
163 NS_ENSURE_TRUE(matrix, NS_ERROR_OUT_OF_MEMORY);
165 gfx->Save();
166 nsSVGUtils::SetClipRect(gfx, matrix, x, y, width, height);
169 for (nsIFrame* kid = mFrames.FirstChild(); kid;
170 kid = kid->GetNextSibling()) {
171 nsISVGChildFrame* SVGFrame = nsnull;
172 CallQueryInterface(kid, &SVGFrame);
173 if (SVGFrame) {
174 SVGFrame->NotifyCanvasTMChanged(PR_TRUE);
175 nsSVGUtils::PaintChildWithEffects(aContext, nsnull, kid);
179 if (GetStyleDisplay()->IsScrollableOverflow())
180 gfx->Restore();
182 return NS_OK;
186 nsRect
187 nsSVGMarkerFrame::RegionMark(nsSVGPathGeometryFrame *aMarkedFrame,
188 const nsSVGMark *aMark, float aStrokeWidth)
190 // If the flag is set when we get here, it means this marker frame
191 // has already been used in calculating the current mark region, and
192 // the document has a marker reference loop.
193 if (mInUse)
194 return nsRect();
196 AutoMarkerReferencer markerRef(this, aMarkedFrame);
198 mStrokeWidth = aStrokeWidth;
199 mX = aMark->x;
200 mY = aMark->y;
201 mAngle = aMark->angle;
203 // Force children to update their covered region
204 for (nsIFrame* kid = mFrames.FirstChild();
205 kid;
206 kid = kid->GetNextSibling()) {
207 nsISVGChildFrame* child = nsnull;
208 CallQueryInterface(kid, &child);
209 if (child)
210 child->UpdateCoveredRegion();
213 // Now get the combined covered region
214 return nsSVGUtils::GetCoveredRegion(mFrames);
218 nsIAtom *
219 nsSVGMarkerFrame::GetType() const
221 return nsGkAtoms::svgMarkerFrame;
224 void
225 nsSVGMarkerFrame::SetParentCoordCtxProvider(nsSVGSVGElement *aContext)
227 nsSVGMarkerElement *marker = static_cast<nsSVGMarkerElement*>(mContent);
228 marker->SetParentCoordCtxProvider(aContext);
231 //----------------------------------------------------------------------
232 // helper class
234 nsSVGMarkerFrame::AutoMarkerReferencer::AutoMarkerReferencer(
235 nsSVGMarkerFrame *aFrame,
236 nsSVGPathGeometryFrame *aMarkedFrame)
237 : mFrame(aFrame)
239 mFrame->mInUse = PR_TRUE;
240 mFrame->mMarkedFrame = aMarkedFrame;
242 nsSVGSVGElement *ctx =
243 static_cast<nsSVGElement*>(aMarkedFrame->GetContent())->GetCtx();
244 mFrame->SetParentCoordCtxProvider(ctx);
247 nsSVGMarkerFrame::AutoMarkerReferencer::~AutoMarkerReferencer()
249 mFrame->SetParentCoordCtxProvider(nsnull);
251 mFrame->mMarkedFrame = nsnull;
252 mFrame->mInUse = PR_FALSE;