2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is the Mozilla SVG project.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is IBM Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2004
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsSVGMaskFrame.h"
|
|
|
|
#include "nsIDOMSVGAnimatedEnum.h"
|
|
|
|
#include "nsSVGContainerFrame.h"
|
|
|
|
#include "nsSVGMaskElement.h"
|
|
|
|
#include "nsIDOMSVGMatrix.h"
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "nsIDOMSVGRect.h"
|
2007-05-22 12:31:04 -07:00
|
|
|
#include "gfxImageSurface.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Implementation
|
|
|
|
|
|
|
|
nsIFrame*
|
|
|
|
NS_NewSVGMaskFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext)
|
|
|
|
{
|
|
|
|
return new (aPresShell) nsSVGMaskFrame(aContext);
|
|
|
|
}
|
|
|
|
|
2007-04-26 01:36:16 -07:00
|
|
|
nsIContent *
|
|
|
|
NS_GetSVGMaskElement(nsIURI *aURI, nsIContent *aContent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-04-26 01:36:16 -07:00
|
|
|
nsIContent* content = nsContentUtils::GetReferencedElement(aURI, aContent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-26 01:36:16 -07:00
|
|
|
nsCOMPtr<nsIDOMSVGMaskElement> mask = do_QueryInterface(content);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-26 01:36:16 -07:00
|
|
|
if (mask)
|
|
|
|
return content;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-26 01:36:16 -07:00
|
|
|
return nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSVGMaskFrame::InitSVG()
|
|
|
|
{
|
|
|
|
nsresult rv = nsSVGMaskFrameBase::InitSVG();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
mMaskParentMatrix = nsnull;
|
2007-06-21 04:01:41 -07:00
|
|
|
mInUse = PR_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMSVGMaskElement> mask = do_QueryInterface(mContent);
|
|
|
|
NS_ASSERTION(mask, "wrong content element");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-22 12:31:04 -07:00
|
|
|
already_AddRefed<gfxPattern>
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSVGMaskFrame::ComputeMaskAlpha(nsSVGRenderState *aContext,
|
|
|
|
nsISVGChildFrame* aParent,
|
|
|
|
nsIDOMSVGMatrix* aMatrix,
|
|
|
|
float aOpacity)
|
|
|
|
{
|
2007-06-21 04:01:41 -07:00
|
|
|
// If the flag is set when we get here, it means this mask frame
|
|
|
|
// has already been used painting the current mask, and the document
|
|
|
|
// has a mask reference loop.
|
|
|
|
if (mInUse) {
|
|
|
|
NS_WARNING("Mask loop detected!");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
AutoMaskReferencer maskRef(this);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxContext *gfx = aContext->GetGfxContext();
|
|
|
|
|
2007-05-22 12:31:04 -07:00
|
|
|
gfx->PushGroup(gfxASurface::CONTENT_COLOR_ALPHA);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
nsIFrame *frame;
|
|
|
|
CallQueryInterface(aParent, &frame);
|
2007-07-08 00:08:04 -07:00
|
|
|
nsSVGElement *parent = static_cast<nsSVGElement*>(frame->GetContent());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
float x, y, width, height;
|
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
nsSVGMaskElement *mask = static_cast<nsSVGMaskElement*>(mContent);
|
2007-04-26 01:36:16 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSVGLength2 *tmpX, *tmpY, *tmpWidth, *tmpHeight;
|
|
|
|
tmpX = &mask->mLengthAttributes[nsSVGMaskElement::X];
|
|
|
|
tmpY = &mask->mLengthAttributes[nsSVGMaskElement::Y];
|
|
|
|
tmpWidth = &mask->mLengthAttributes[nsSVGMaskElement::WIDTH];
|
|
|
|
tmpHeight = &mask->mLengthAttributes[nsSVGMaskElement::HEIGHT];
|
|
|
|
|
2007-04-26 01:36:16 -07:00
|
|
|
PRUint16 units;
|
|
|
|
mask->mMaskUnits->GetAnimVal(&units);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (units == nsIDOMSVGMaskElement::SVG_MUNITS_OBJECTBOUNDINGBOX) {
|
|
|
|
|
|
|
|
aParent->SetMatrixPropagation(PR_FALSE);
|
|
|
|
aParent->NotifyCanvasTMChanged(PR_TRUE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMSVGRect> bbox;
|
|
|
|
aParent->GetBBox(getter_AddRefs(bbox));
|
|
|
|
|
|
|
|
aParent->SetMatrixPropagation(PR_TRUE);
|
|
|
|
aParent->NotifyCanvasTMChanged(PR_TRUE);
|
|
|
|
|
|
|
|
if (!bbox)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
#ifdef DEBUG_tor
|
|
|
|
bbox->GetX(&x);
|
|
|
|
bbox->GetY(&y);
|
|
|
|
bbox->GetWidth(&width);
|
|
|
|
bbox->GetHeight(&height);
|
|
|
|
|
|
|
|
fprintf(stderr, "mask bbox: %f,%f %fx%f\n", x, y, width, height);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bbox->GetX(&x);
|
|
|
|
x += nsSVGUtils::ObjectSpace(bbox, tmpX);
|
|
|
|
bbox->GetY(&y);
|
|
|
|
y += nsSVGUtils::ObjectSpace(bbox, tmpY);
|
|
|
|
width = nsSVGUtils::ObjectSpace(bbox, tmpWidth);
|
|
|
|
height = nsSVGUtils::ObjectSpace(bbox, tmpHeight);
|
|
|
|
} else {
|
|
|
|
x = nsSVGUtils::UserSpace(parent, tmpX);
|
|
|
|
y = nsSVGUtils::UserSpace(parent, tmpY);
|
|
|
|
width = nsSVGUtils::UserSpace(parent, tmpWidth);
|
|
|
|
height = nsSVGUtils::UserSpace(parent, tmpHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_tor
|
|
|
|
fprintf(stderr, "mask clip: %f,%f %fx%f\n", x, y, width, height);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
gfx->Save();
|
|
|
|
nsSVGUtils::SetClipRect(gfx, aMatrix, x, y, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
mMaskParent = aParent,
|
|
|
|
mMaskParentMatrix = aMatrix;
|
|
|
|
|
|
|
|
for (nsIFrame* kid = mFrames.FirstChild(); kid;
|
|
|
|
kid = kid->GetNextSibling()) {
|
|
|
|
nsSVGUtils::PaintChildWithEffects(aContext, nsnull, kid);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx->Restore();
|
|
|
|
|
2007-05-22 12:31:04 -07:00
|
|
|
nsRefPtr<gfxPattern> pattern = gfx->PopGroup();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!pattern)
|
|
|
|
return nsnull;
|
|
|
|
|
2007-05-22 12:31:04 -07:00
|
|
|
nsRefPtr<gfxASurface> surface = pattern->GetSurface();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-22 12:31:04 -07:00
|
|
|
gfxRect clipExtents = gfx->GetClipExtents();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-22 12:31:04 -07:00
|
|
|
#ifdef DEBUG_tor
|
|
|
|
fprintf(stderr, "clip extent: %f,%f %fx%f\n",
|
|
|
|
clipExtents.X(), clipExtents.Y(),
|
|
|
|
clipExtents.Width(), clipExtents.Height());
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-06-13 02:02:48 -07:00
|
|
|
PRBool resultOverflows;
|
|
|
|
gfxIntSize surfaceSize =
|
|
|
|
nsSVGUtils::ConvertToSurfaceSize(gfxSize(clipExtents.Width(),
|
|
|
|
clipExtents.Height()),
|
|
|
|
&resultOverflows);
|
|
|
|
|
|
|
|
// 0 disables mask, < 0 is an error
|
|
|
|
if (surfaceSize.width <= 0 || surfaceSize.height <= 0)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
if (resultOverflows)
|
|
|
|
return nsnull;
|
|
|
|
|
2007-05-22 12:31:04 -07:00
|
|
|
nsRefPtr<gfxImageSurface> image =
|
2007-06-13 02:02:48 -07:00
|
|
|
new gfxImageSurface(surfaceSize, gfxASurface::ImageFormatARGB32);
|
|
|
|
if (!image || !image->Data())
|
2007-03-22 10:30:00 -07:00
|
|
|
return nsnull;
|
|
|
|
|
2007-05-22 12:31:04 -07:00
|
|
|
gfxContext transferCtx(image);
|
|
|
|
transferCtx.SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
transferCtx.SetSource(surface, -clipExtents.pos);
|
|
|
|
transferCtx.Paint();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-06-13 02:02:48 -07:00
|
|
|
PRUint32 width = surfaceSize.width;
|
|
|
|
PRUint32 height = surfaceSize.height;
|
2007-05-22 12:31:04 -07:00
|
|
|
PRUint8 *data = image->Data();
|
|
|
|
PRInt32 stride = image->Stride();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsRect rect(0, 0, width, height);
|
|
|
|
nsSVGUtils::UnPremultiplyImageDataAlpha(data, stride, rect);
|
|
|
|
nsSVGUtils::ConvertImageDataToLinearRGB(data, stride, rect);
|
|
|
|
|
|
|
|
for (PRUint32 y = 0; y < height; y++)
|
|
|
|
for (PRUint32 x = 0; x < width; x++) {
|
|
|
|
PRUint8 *pixel = data + stride * y + 4 * x;
|
|
|
|
|
|
|
|
/* linearRGB -> intensity */
|
|
|
|
PRUint8 alpha =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<PRUint8>
|
|
|
|
((pixel[GFX_ARGB32_OFFSET_R] * 0.2125 +
|
2007-03-22 10:30:00 -07:00
|
|
|
pixel[GFX_ARGB32_OFFSET_G] * 0.7154 +
|
|
|
|
pixel[GFX_ARGB32_OFFSET_B] * 0.0721) *
|
|
|
|
(pixel[GFX_ARGB32_OFFSET_A] / 255.0) * aOpacity);
|
|
|
|
|
|
|
|
memset(pixel, alpha, 4);
|
|
|
|
}
|
|
|
|
|
2007-05-22 12:31:04 -07:00
|
|
|
gfxPattern *retval = new gfxPattern(image);
|
|
|
|
if (retval) {
|
|
|
|
retval->SetMatrix(gfxMatrix().Translate(-clipExtents.pos));
|
|
|
|
NS_ADDREF(retval);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom *
|
|
|
|
nsSVGMaskFrame::GetType() const
|
|
|
|
{
|
|
|
|
return nsGkAtoms::svgMaskFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIDOMSVGMatrix>
|
|
|
|
nsSVGMaskFrame::GetCanvasTM()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mMaskParentMatrix, "null parent matrix");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMSVGMatrix> canvasTM = mMaskParentMatrix;
|
|
|
|
|
|
|
|
/* object bounding box? */
|
2007-07-08 00:08:04 -07:00
|
|
|
nsSVGMaskElement *mask = static_cast<nsSVGMaskElement*>(mContent);
|
2007-04-26 01:36:16 -07:00
|
|
|
|
|
|
|
PRUint16 contentUnits;
|
|
|
|
mask->mMaskContentUnits->GetAnimVal(&contentUnits);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mMaskParent &&
|
2007-04-26 01:36:16 -07:00
|
|
|
contentUnits == nsIDOMSVGMaskElement::SVG_MUNITS_OBJECTBOUNDINGBOX) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMSVGRect> rect;
|
|
|
|
nsresult rv = mMaskParent->GetBBox(getter_AddRefs(rect));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
float minx, miny, width, height;
|
|
|
|
rect->GetX(&minx);
|
|
|
|
rect->GetY(&miny);
|
|
|
|
rect->GetWidth(&width);
|
|
|
|
rect->GetHeight(&height);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMSVGMatrix> tmp, fini;
|
|
|
|
canvasTM->Translate(minx, miny, getter_AddRefs(tmp));
|
|
|
|
tmp->ScaleNonUniform(width, height, getter_AddRefs(fini));
|
|
|
|
canvasTM = fini;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMSVGMatrix* retval = canvasTM.get();
|
|
|
|
NS_IF_ADDREF(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|