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 "nsSVGContainerFrame.h"
|
|
|
|
#include "nsSVGMaskElement.h"
|
|
|
|
#include "nsIDOMSVGMatrix.h"
|
|
|
|
#include "gfxContext.h"
|
2007-05-22 12:31:04 -07:00
|
|
|
#include "gfxImageSurface.h"
|
2009-07-23 01:35:59 -07:00
|
|
|
#include "nsSVGMatrix.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Implementation
|
|
|
|
|
|
|
|
nsIFrame*
|
2009-01-19 10:31:34 -08:00
|
|
|
NS_NewSVGMaskFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return new (aPresShell) nsSVGMaskFrame(aContext);
|
|
|
|
}
|
|
|
|
|
2007-05-22 12:31:04 -07:00
|
|
|
already_AddRefed<gfxPattern>
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSVGMaskFrame::ComputeMaskAlpha(nsSVGRenderState *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
|
|
|
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
|
|
|
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
nsSVGMaskElement *mask = static_cast<nsSVGMaskElement*>(mContent);
|
2007-04-26 01:36:16 -07:00
|
|
|
|
2007-08-27 16:11:14 -07:00
|
|
|
PRUint16 units =
|
|
|
|
mask->mEnumAttributes[nsSVGMaskElement::MASKUNITS].GetAnimValue();
|
2009-06-11 08:21:03 -07:00
|
|
|
gfxRect bbox;
|
2007-08-27 16:11:14 -07:00
|
|
|
if (units == nsIDOMSVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
|
2008-09-10 17:24:16 -07:00
|
|
|
bbox = nsSVGUtils::GetBBox(aParent);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-09-10 17:24:16 -07:00
|
|
|
gfxRect maskArea = nsSVGUtils::GetRelativeRect(units,
|
|
|
|
&mask->mLengthAttributes[nsSVGMaskElement::X], bbox, aParent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
gfx->Save();
|
2009-07-23 01:35:59 -07:00
|
|
|
nsSVGUtils::SetClipRect(gfx, aMatrix, maskArea);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-09-07 02:30:51 -07:00
|
|
|
mMaskParent = aParent;
|
2009-07-23 01:35:59 -07:00
|
|
|
mMaskParentMatrix = NS_NewSVGMatrix(aMatrix);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (nsIFrame* kid = mFrames.FirstChild(); kid;
|
|
|
|
kid = kid->GetNextSibling()) {
|
2008-10-20 01:42:03 -07:00
|
|
|
nsSVGUtils::PaintFrameWithEffects(aContext, nsnull, kid);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-09-21 19:14:54 -07:00
|
|
|
gfxRect clipExtents = gfx->GetClipExtents();
|
2007-03-22 10:30:00 -07:00
|
|
|
gfx->Restore();
|
|
|
|
|
2007-05-22 12:31:04 -07:00
|
|
|
nsRefPtr<gfxPattern> pattern = gfx->PopGroup();
|
2008-03-17 10:15:43 -07:00
|
|
|
if (!pattern || pattern->CairoStatus())
|
2007-03-22 10:30:00 -07:00
|
|
|
return nsnull;
|
|
|
|
|
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);
|
2007-07-17 02:24:27 -07:00
|
|
|
if (!image || image->CairoStatus())
|
2007-03-22 10:30:00 -07:00
|
|
|
return nsnull;
|
2008-09-21 19:14:54 -07:00
|
|
|
image->SetDeviceOffset(-clipExtents.pos);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-22 12:31:04 -07:00
|
|
|
gfxContext transferCtx(image);
|
|
|
|
transferCtx.SetOperator(gfxContext::OPERATOR_SOURCE);
|
2008-09-21 19:14:54 -07:00
|
|
|
transferCtx.SetPattern(pattern);
|
2007-05-22 12:31:04 -07:00
|
|
|
transferCtx.Paint();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-22 12:31:04 -07:00
|
|
|
PRUint8 *data = image->Data();
|
|
|
|
PRInt32 stride = image->Stride();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-25 02:23:54 -07:00
|
|
|
nsIntRect rect(0, 0, surfaceSize.width, surfaceSize.height);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSVGUtils::UnPremultiplyImageDataAlpha(data, stride, rect);
|
|
|
|
nsSVGUtils::ConvertImageDataToLinearRGB(data, stride, rect);
|
|
|
|
|
2008-03-17 10:15:43 -07:00
|
|
|
for (PRInt32 y = 0; y < surfaceSize.height; y++)
|
|
|
|
for (PRInt32 x = 0; x < surfaceSize.width; x++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
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);
|
2008-09-21 19:14:54 -07:00
|
|
|
NS_IF_ADDREF(retval);
|
2007-03-22 10:30:00 -07:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2009-01-19 10:31:34 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSVGMaskFrame::Init(nsIContent* aContent,
|
|
|
|
nsIFrame* aParent,
|
|
|
|
nsIFrame* aPrevInFlow)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMSVGMaskElement> mask = do_QueryInterface(aContent);
|
|
|
|
NS_ASSERTION(mask, "Content is not an SVG mask");
|
|
|
|
|
|
|
|
return nsSVGMaskFrameBase::Init(aContent, aParent, aPrevInFlow);
|
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIAtom *
|
|
|
|
nsSVGMaskFrame::GetType() const
|
|
|
|
{
|
|
|
|
return nsGkAtoms::svgMaskFrame;
|
|
|
|
}
|
|
|
|
|
2009-04-28 21:31:34 -07:00
|
|
|
gfxMatrix
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSVGMaskFrame::GetCanvasTM()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mMaskParentMatrix, "null parent matrix");
|
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
nsSVGMaskElement *mask = static_cast<nsSVGMaskElement*>(mContent);
|
2007-04-26 01:36:16 -07:00
|
|
|
|
2009-07-23 01:35:59 -07:00
|
|
|
return nsSVGUtils::AdjustMatrixForUnits(nsSVGUtils::ConvertSVGMatrixToThebes(mMaskParentMatrix),
|
2007-12-03 20:40:52 -08:00
|
|
|
&mask->mEnumAttributes[nsSVGMaskElement::MASKCONTENTUNITS],
|
|
|
|
mMaskParent);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|