2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-26 04:58:59 -07:00
|
|
|
// Main header first:
|
2012-03-20 05:15:55 -07:00
|
|
|
#include "nsSVGClipPathFrame.h"
|
|
|
|
|
2012-03-26 04:58:59 -07:00
|
|
|
// Keep others in (case-insensitive) order:
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMSVGClipPathElement.h"
|
2012-03-20 05:15:55 -07:00
|
|
|
#include "nsRenderingContext.h"
|
2007-04-27 01:42:51 -07:00
|
|
|
#include "nsSVGClipPathElement.h"
|
2012-03-26 04:58:59 -07:00
|
|
|
#include "nsSVGEffects.h"
|
|
|
|
#include "nsSVGUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Implementation
|
|
|
|
|
|
|
|
nsIFrame*
|
2009-01-19 10:31:34 -08:00
|
|
|
NS_NewSVGClipPathFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return new (aPresShell) nsSVGClipPathFrame(aContext);
|
|
|
|
}
|
|
|
|
|
2009-09-12 09:49:24 -07:00
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsSVGClipPathFrame)
|
|
|
|
|
2007-04-17 02:01:52 -07:00
|
|
|
nsresult
|
2012-03-02 00:28:59 -08:00
|
|
|
nsSVGClipPathFrame::ClipPaint(nsRenderingContext* aContext,
|
2008-09-10 17:24:16 -07:00
|
|
|
nsIFrame* aParent,
|
2009-07-23 01:35:59 -07:00
|
|
|
const gfxMatrix &aMatrix)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// If the flag is set when we get here, it means this clipPath frame
|
|
|
|
// has already been used painting the current clip, and the document
|
|
|
|
// has a clip reference loop.
|
|
|
|
if (mInUse) {
|
|
|
|
NS_WARNING("Clip loop detected!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-04-17 02:01:52 -07:00
|
|
|
AutoClipPathReferencer clipRef(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-02-26 01:58:42 -08:00
|
|
|
mClipParent = aParent;
|
2011-09-25 14:04:32 -07:00
|
|
|
if (mClipParentMatrix) {
|
|
|
|
*mClipParentMatrix = aMatrix;
|
|
|
|
} else {
|
|
|
|
mClipParentMatrix = new gfxMatrix(aMatrix);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-15 02:06:34 -07:00
|
|
|
gfxContext *gfx = aContext->ThebesContext();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsISVGChildFrame *singleClipPathChild = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-15 02:06:34 -07:00
|
|
|
if (IsTrivial(&singleClipPathChild)) {
|
|
|
|
// Notify our child that it's painting as part of a clipPath, and that
|
|
|
|
// we only require it to draw its path (it should skip filling, etc.):
|
|
|
|
SVGAutoRenderState mode(aContext, SVGAutoRenderState::CLIP);
|
|
|
|
|
|
|
|
if (!singleClipPathChild) {
|
|
|
|
// We have no children - the spec says clip away everything:
|
|
|
|
gfx->Rectangle(gfxRect());
|
|
|
|
} else {
|
|
|
|
singleClipPathChild->NotifySVGChanged(
|
|
|
|
nsISVGChildFrame::TRANSFORM_CHANGED);
|
2012-07-30 07:20:58 -07:00
|
|
|
singleClipPathChild->PaintSVG(aContext, nullptr);
|
2012-06-15 02:06:34 -07:00
|
|
|
}
|
|
|
|
gfx->Clip();
|
|
|
|
gfx->NewPath();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-02-26 01:58:42 -08:00
|
|
|
|
2012-06-15 02:06:34 -07:00
|
|
|
// Seems like this is a non-trivial clipPath, so we need to use a clip mask.
|
|
|
|
|
|
|
|
// Notify our children that they're painting into a clip mask:
|
|
|
|
SVGAutoRenderState mode(aContext, SVGAutoRenderState::CLIP_MASK);
|
|
|
|
|
|
|
|
// Check if this clipPath is itself clipped by another clipPath:
|
2010-02-26 01:58:42 -08:00
|
|
|
nsSVGClipPathFrame *clipPathFrame =
|
2012-07-30 07:20:58 -07:00
|
|
|
nsSVGEffects::GetEffectProperties(this).GetClipPathFrame(nullptr);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool referencedClipIsTrivial;
|
2010-02-26 01:58:42 -08:00
|
|
|
if (clipPathFrame) {
|
|
|
|
referencedClipIsTrivial = clipPathFrame->IsTrivial();
|
|
|
|
gfx->Save();
|
|
|
|
if (referencedClipIsTrivial) {
|
|
|
|
clipPathFrame->ClipPaint(aContext, aParent, aMatrix);
|
|
|
|
} else {
|
|
|
|
gfx->PushGroup(gfxASurface::CONTENT_ALPHA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
for (nsIFrame* kid = mFrames.FirstChild(); kid;
|
|
|
|
kid = kid->GetNextSibling()) {
|
2009-01-12 11:20:59 -08:00
|
|
|
nsISVGChildFrame* SVGFrame = do_QueryFrame(kid);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (SVGFrame) {
|
2008-01-25 01:27:03 -08:00
|
|
|
// The CTM of each frame referencing us can be different.
|
2012-07-23 04:00:43 -07:00
|
|
|
SVGFrame->NotifySVGChanged(nsISVGChildFrame::TRANSFORM_CHANGED);
|
2010-02-26 01:58:42 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isOK = true;
|
2010-02-26 01:58:42 -08:00
|
|
|
nsSVGClipPathFrame *clipPathFrame =
|
|
|
|
nsSVGEffects::GetEffectProperties(kid).GetClipPathFrame(&isOK);
|
2010-03-02 01:31:07 -08:00
|
|
|
if (!isOK) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-02-26 01:58:42 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isTrivial;
|
2010-02-26 01:58:42 -08:00
|
|
|
|
|
|
|
if (clipPathFrame) {
|
|
|
|
isTrivial = clipPathFrame->IsTrivial();
|
|
|
|
gfx->Save();
|
|
|
|
if (isTrivial) {
|
|
|
|
clipPathFrame->ClipPaint(aContext, aParent, aMatrix);
|
|
|
|
} else {
|
|
|
|
gfx->PushGroup(gfxASurface::CONTENT_ALPHA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
SVGFrame->PaintSVG(aContext, nullptr);
|
2010-02-26 01:58:42 -08:00
|
|
|
|
|
|
|
if (clipPathFrame) {
|
|
|
|
if (!isTrivial) {
|
|
|
|
gfx->PopGroupToSource();
|
|
|
|
|
|
|
|
nsRefPtr<gfxPattern> clipMaskSurface;
|
|
|
|
gfx->PushGroup(gfxASurface::CONTENT_ALPHA);
|
|
|
|
|
|
|
|
clipPathFrame->ClipPaint(aContext, aParent, aMatrix);
|
|
|
|
clipMaskSurface = gfx->PopGroup();
|
|
|
|
|
|
|
|
if (clipMaskSurface) {
|
|
|
|
gfx->Mask(clipMaskSurface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gfx->Restore();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-26 01:58:42 -08:00
|
|
|
if (clipPathFrame) {
|
|
|
|
if (!referencedClipIsTrivial) {
|
|
|
|
gfx->PopGroupToSource();
|
|
|
|
|
|
|
|
nsRefPtr<gfxPattern> clipMaskSurface;
|
|
|
|
gfx->PushGroup(gfxASurface::CONTENT_ALPHA);
|
|
|
|
|
|
|
|
clipPathFrame->ClipPaint(aContext, aParent, aMatrix);
|
|
|
|
clipMaskSurface = gfx->PopGroup();
|
|
|
|
|
|
|
|
if (clipMaskSurface) {
|
|
|
|
gfx->Mask(clipMaskSurface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gfx->Restore();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2008-09-10 17:24:16 -07:00
|
|
|
nsSVGClipPathFrame::ClipHitTest(nsIFrame* aParent,
|
2009-07-23 01:35:59 -07:00
|
|
|
const gfxMatrix &aMatrix,
|
2008-08-25 02:23:54 -07:00
|
|
|
const nsPoint &aPoint)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// If the flag is set when we get here, it means this clipPath frame
|
|
|
|
// has already been used in hit testing against the current clip,
|
|
|
|
// and the document has a clip reference loop.
|
|
|
|
if (mInUse) {
|
|
|
|
NS_WARNING("Clip loop detected!");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-04-17 02:01:52 -07:00
|
|
|
AutoClipPathReferencer clipRef(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-02-26 01:58:42 -08:00
|
|
|
mClipParent = aParent;
|
2011-09-25 14:04:32 -07:00
|
|
|
if (mClipParentMatrix) {
|
|
|
|
*mClipParentMatrix = aMatrix;
|
|
|
|
} else {
|
|
|
|
mClipParentMatrix = new gfxMatrix(aMatrix);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-02-26 01:58:42 -08:00
|
|
|
nsSVGClipPathFrame *clipPathFrame =
|
2012-07-30 07:20:58 -07:00
|
|
|
nsSVGEffects::GetEffectProperties(this).GetClipPathFrame(nullptr);
|
2010-02-26 01:58:42 -08:00
|
|
|
if (clipPathFrame && !clipPathFrame->ClipHitTest(aParent, aMatrix, aPoint))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-26 01:58:42 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
for (nsIFrame* kid = mFrames.FirstChild(); kid;
|
|
|
|
kid = kid->GetNextSibling()) {
|
2009-01-12 11:20:59 -08:00
|
|
|
nsISVGChildFrame* SVGFrame = do_QueryFrame(kid);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (SVGFrame) {
|
|
|
|
// Notify the child frame that we may be working with a
|
|
|
|
// different transform, so it can update its covered region
|
|
|
|
// (used to shortcut hit testing).
|
2008-01-25 01:27:03 -08:00
|
|
|
SVGFrame->NotifySVGChanged(nsISVGChildFrame::TRANSFORM_CHANGED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-25 02:23:54 -07:00
|
|
|
if (SVGFrame->GetFrameForPoint(aPoint))
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-06-15 02:06:34 -07:00
|
|
|
nsSVGClipPathFrame::IsTrivial(nsISVGChildFrame **aSingleChild)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-02-26 01:58:42 -08:00
|
|
|
// If the clip path is clipped then it's non-trivial
|
2012-07-30 07:20:58 -07:00
|
|
|
if (nsSVGEffects::GetEffectProperties(this).GetClipPathFrame(nullptr))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-26 01:58:42 -08:00
|
|
|
|
2012-06-15 02:06:34 -07:00
|
|
|
if (aSingleChild) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*aSingleChild = nullptr;
|
2012-06-15 02:06:34 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsISVGChildFrame *foundChild = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (nsIFrame* kid = mFrames.FirstChild(); kid;
|
|
|
|
kid = kid->GetNextSibling()) {
|
2009-01-12 11:20:59 -08:00
|
|
|
nsISVGChildFrame *svgChild = do_QueryFrame(kid);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (svgChild) {
|
|
|
|
// We consider a non-trivial clipPath to be one containing
|
|
|
|
// either more than one svg child and/or a svg container
|
2007-04-17 02:01:52 -07:00
|
|
|
if (foundChild || svgChild->IsDisplayContainer())
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-26 01:58:42 -08:00
|
|
|
|
|
|
|
// or where the child is itself clipped
|
2012-07-30 07:20:58 -07:00
|
|
|
if (nsSVGEffects::GetEffectProperties(kid).GetClipPathFrame(nullptr))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-26 01:58:42 -08:00
|
|
|
|
2012-06-15 02:06:34 -07:00
|
|
|
foundChild = svgChild;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2012-06-15 02:06:34 -07:00
|
|
|
if (aSingleChild) {
|
|
|
|
*aSingleChild = foundChild;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-03-02 01:31:07 -08:00
|
|
|
nsSVGClipPathFrame::IsValid()
|
|
|
|
{
|
|
|
|
if (mInUse) {
|
|
|
|
NS_WARNING("Clip loop detected!");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-03-02 01:31:07 -08:00
|
|
|
}
|
|
|
|
AutoClipPathReferencer clipRef(this);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isOK = true;
|
2010-03-02 01:31:07 -08:00
|
|
|
nsSVGEffects::GetEffectProperties(this).GetClipPathFrame(&isOK);
|
|
|
|
if (!isOK) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-03-02 01:31:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (nsIFrame* kid = mFrames.FirstChild(); kid;
|
|
|
|
kid = kid->GetNextSibling()) {
|
|
|
|
|
|
|
|
nsIAtom *type = kid->GetType();
|
|
|
|
|
|
|
|
if (type == nsGkAtoms::svgUseFrame) {
|
2011-08-24 13:54:30 -07:00
|
|
|
for (nsIFrame* grandKid = kid->GetFirstPrincipalChild(); grandKid;
|
2010-03-02 01:31:07 -08:00
|
|
|
grandKid = grandKid->GetNextSibling()) {
|
|
|
|
|
|
|
|
nsIAtom *type = grandKid->GetType();
|
|
|
|
|
|
|
|
if (type != nsGkAtoms::svgPathGeometryFrame &&
|
|
|
|
type != nsGkAtoms::svgTextFrame) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-03-02 01:31:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (type != nsGkAtoms::svgPathGeometryFrame &&
|
|
|
|
type != nsGkAtoms::svgTextFrame) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-03-02 01:31:07 -08:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-03-02 01:31:07 -08:00
|
|
|
}
|
|
|
|
|
2010-03-24 09:54:48 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsSVGClipPathFrame::AttributeChanged(int32_t aNameSpaceID,
|
2010-03-24 09:54:48 -07:00
|
|
|
nsIAtom* aAttribute,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aModType)
|
2010-03-24 09:54:48 -07:00
|
|
|
{
|
2011-09-19 05:59:52 -07:00
|
|
|
if (aNameSpaceID == kNameSpaceID_None) {
|
|
|
|
if (aAttribute == nsGkAtoms::transform) {
|
2012-06-23 07:18:49 -07:00
|
|
|
nsSVGEffects::InvalidateDirectRenderingObservers(this);
|
2011-09-19 05:59:52 -07:00
|
|
|
nsSVGUtils::NotifyChildrenOfSVGChange(this,
|
|
|
|
nsISVGChildFrame::TRANSFORM_CHANGED);
|
|
|
|
}
|
|
|
|
if (aAttribute == nsGkAtoms::clipPathUnits) {
|
|
|
|
nsSVGEffects::InvalidateRenderingObservers(this);
|
|
|
|
}
|
2010-03-24 09:54:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsSVGClipPathFrameBase::AttributeChanged(aNameSpaceID,
|
|
|
|
aAttribute, aModType);
|
|
|
|
}
|
|
|
|
|
2009-01-19 10:31:34 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSVGClipPathFrame::Init(nsIContent* aContent,
|
|
|
|
nsIFrame* aParent,
|
|
|
|
nsIFrame* aPrevInFlow)
|
|
|
|
{
|
2010-12-06 12:57:18 -08:00
|
|
|
#ifdef DEBUG
|
2009-01-19 10:31:34 -08:00
|
|
|
nsCOMPtr<nsIDOMSVGClipPathElement> clipPath = do_QueryInterface(aContent);
|
|
|
|
NS_ASSERTION(clipPath, "Content is not an SVG clipPath!");
|
2010-12-06 12:57:18 -08:00
|
|
|
#endif
|
2009-01-19 10:31:34 -08:00
|
|
|
|
2010-12-06 12:57:18 -08:00
|
|
|
AddStateBits(NS_STATE_SVG_CLIPPATH_CHILD);
|
2009-01-19 10:31:34 -08:00
|
|
|
return nsSVGClipPathFrameBase::Init(aContent, aParent, aPrevInFlow);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIAtom *
|
|
|
|
nsSVGClipPathFrame::GetType() const
|
|
|
|
{
|
|
|
|
return nsGkAtoms::svgClipPathFrame;
|
|
|
|
}
|
|
|
|
|
2009-04-28 21:31:34 -07:00
|
|
|
gfxMatrix
|
2012-08-22 08:56:38 -07:00
|
|
|
nsSVGClipPathFrame::GetCanvasTM(uint32_t aFor)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-28 21:31:34 -07:00
|
|
|
nsSVGClipPathElement *content = static_cast<nsSVGClipPathElement*>(mContent);
|
2007-04-27 01:42:51 -07:00
|
|
|
|
2011-09-25 14:04:32 -07:00
|
|
|
gfxMatrix tm =
|
2012-02-17 00:12:47 -08:00
|
|
|
content->PrependLocalTransformsTo(mClipParentMatrix ?
|
|
|
|
*mClipParentMatrix : gfxMatrix());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-23 01:35:59 -07:00
|
|
|
return nsSVGUtils::AdjustMatrixForUnits(tm,
|
2009-04-28 21:31:34 -07:00
|
|
|
&content->mEnumAttributes[nsSVGClipPathElement::CLIPPATHUNITS],
|
2007-12-03 20:40:52 -08:00
|
|
|
mClipParent);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|