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 the GNU General Public License Version 2 or later (the "GPL"), or
25 * 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 "nsIDOMDocument.h"
38 #include "nsIDocument.h"
39 #include "nsIDOMSVGClipPathElement.h"
40 #include "nsSVGClipPathFrame.h"
41 #include "nsGkAtoms.h"
42 #include "nsSVGUtils.h"
43 #include "nsSVGClipPathElement.h"
44 #include "gfxContext.h"
45 #include "nsIDOMSVGRect.h"
47 //----------------------------------------------------------------------
51 NS_NewSVGClipPathFrame(nsIPresShell
* aPresShell
, nsIContent
* aContent
, nsStyleContext
* aContext
)
53 nsCOMPtr
<nsIDOMSVGClipPathElement
> clipPath
= do_QueryInterface(aContent
);
55 NS_ERROR("Can't create frame! Content is not an SVG clipPath!");
59 return new (aPresShell
) nsSVGClipPathFrame(aContext
);
63 nsSVGClipPathFrame::ClipPaint(nsSVGRenderState
* aContext
,
65 nsIDOMSVGMatrix
*aMatrix
)
67 // If the flag is set when we get here, it means this clipPath frame
68 // has already been used painting the current clip, and the document
69 // has a clip reference loop.
71 NS_WARNING("Clip loop detected!");
74 AutoClipPathReferencer
clipRef(this);
76 mClipParent
= aParent
,
77 mClipParentMatrix
= aMatrix
;
79 PRBool isTrivial
= IsTrivial();
81 nsAutoSVGRenderMode
mode(aContext
,
82 isTrivial
? nsSVGRenderState::CLIP
83 : nsSVGRenderState::CLIP_MASK
);
85 for (nsIFrame
* kid
= mFrames
.FirstChild(); kid
;
86 kid
= kid
->GetNextSibling()) {
87 nsISVGChildFrame
* SVGFrame
= nsnull
;
88 CallQueryInterface(kid
, &SVGFrame
);
90 // The CTM of each frame referencing us can be different.
91 SVGFrame
->NotifySVGChanged(nsISVGChildFrame::SUPPRESS_INVALIDATION
|
92 nsISVGChildFrame::TRANSFORM_CHANGED
);
93 SVGFrame
->PaintSVG(aContext
, nsnull
);
98 aContext
->GetGfxContext()->Clip();
99 aContext
->GetGfxContext()->NewPath();
106 nsSVGClipPathFrame::ClipHitTest(nsIFrame
* aParent
,
107 nsIDOMSVGMatrix
*aMatrix
,
108 const nsPoint
&aPoint
)
110 // If the flag is set when we get here, it means this clipPath frame
111 // has already been used in hit testing against the current clip,
112 // and the document has a clip reference loop.
114 NS_WARNING("Clip loop detected!");
117 AutoClipPathReferencer
clipRef(this);
119 mClipParent
= aParent
,
120 mClipParentMatrix
= aMatrix
;
122 for (nsIFrame
* kid
= mFrames
.FirstChild(); kid
;
123 kid
= kid
->GetNextSibling()) {
124 nsISVGChildFrame
* SVGFrame
= nsnull
;
125 CallQueryInterface(kid
, &SVGFrame
);
127 // Notify the child frame that we may be working with a
128 // different transform, so it can update its covered region
129 // (used to shortcut hit testing).
130 SVGFrame
->NotifySVGChanged(nsISVGChildFrame::TRANSFORM_CHANGED
);
132 if (SVGFrame
->GetFrameForPoint(aPoint
))
140 nsSVGClipPathFrame::IsTrivial()
142 PRBool foundChild
= PR_FALSE
;
144 for (nsIFrame
* kid
= mFrames
.FirstChild(); kid
;
145 kid
= kid
->GetNextSibling()) {
146 nsISVGChildFrame
*svgChild
= nsnull
;
147 CallQueryInterface(kid
, &svgChild
);
150 // We consider a non-trivial clipPath to be one containing
151 // either more than one svg child and/or a svg container
152 if (foundChild
|| svgChild
->IsDisplayContainer())
154 foundChild
= PR_TRUE
;
161 nsSVGClipPathFrame::GetType() const
163 return nsGkAtoms::svgClipPathFrame
;
166 already_AddRefed
<nsIDOMSVGMatrix
>
167 nsSVGClipPathFrame::GetCanvasTM()
169 NS_ASSERTION(mClipParentMatrix
, "null parent matrix");
171 nsSVGClipPathElement
*clipPath
= static_cast<nsSVGClipPathElement
*>
174 nsCOMPtr
<nsIDOMSVGMatrix
> localTM
= clipPath
->GetLocalTransformMatrix();
176 nsCOMPtr
<nsIDOMSVGMatrix
> canvasTM
;
179 mClipParentMatrix
->Multiply(localTM
, getter_AddRefs(canvasTM
));
181 canvasTM
= mClipParentMatrix
;
183 return nsSVGUtils::AdjustMatrixForUnits(canvasTM
,
184 &clipPath
->mEnumAttributes
[nsSVGClipPathElement::CLIPPATHUNITS
],