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 "nsIDocument.h"
38 #include "nsSVGMaskFrame.h"
39 #include "nsSVGContainerFrame.h"
40 #include "nsSVGMaskElement.h"
41 #include "nsIDOMSVGMatrix.h"
42 #include "gfxContext.h"
43 #include "nsIDOMSVGRect.h"
44 #include "gfxImageSurface.h"
46 //----------------------------------------------------------------------
50 NS_NewSVGMaskFrame(nsIPresShell
* aPresShell
, nsIContent
* aContent
, nsStyleContext
* aContext
)
52 nsCOMPtr
<nsIDOMSVGMaskElement
> mask
= do_QueryInterface(aContent
);
55 NS_ERROR("Can't create frame! Content is not an SVG mask");
59 return new (aPresShell
) nsSVGMaskFrame(aContext
);
62 already_AddRefed
<gfxPattern
>
63 nsSVGMaskFrame::ComputeMaskAlpha(nsSVGRenderState
*aContext
,
65 nsIDOMSVGMatrix
* aMatrix
,
68 // If the flag is set when we get here, it means this mask frame
69 // has already been used painting the current mask, and the document
70 // has a mask reference loop.
72 NS_WARNING("Mask loop detected!");
75 AutoMaskReferencer
maskRef(this);
77 gfxContext
*gfx
= aContext
->GetGfxContext();
79 gfx
->PushGroup(gfxASurface::CONTENT_COLOR_ALPHA
);
82 nsSVGMaskElement
*mask
= static_cast<nsSVGMaskElement
*>(mContent
);
85 mask
->mEnumAttributes
[nsSVGMaskElement::MASKUNITS
].GetAnimValue();
86 nsCOMPtr
<nsIDOMSVGRect
> bbox
;
87 if (units
== nsIDOMSVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX
) {
88 bbox
= nsSVGUtils::GetBBox(aParent
);
93 gfxRect maskArea
= nsSVGUtils::GetRelativeRect(units
,
94 &mask
->mLengthAttributes
[nsSVGMaskElement::X
], bbox
, aParent
);
97 nsSVGUtils::SetClipRect(gfx
, aMatrix
, maskArea
.X(), maskArea
.Y(),
98 maskArea
.Width(), maskArea
.Height());
101 mMaskParent
= aParent
;
102 mMaskParentMatrix
= aMatrix
;
104 for (nsIFrame
* kid
= mFrames
.FirstChild(); kid
;
105 kid
= kid
->GetNextSibling()) {
106 nsSVGUtils::PaintFrameWithEffects(aContext
, nsnull
, kid
);
109 gfxRect clipExtents
= gfx
->GetClipExtents();
112 nsRefPtr
<gfxPattern
> pattern
= gfx
->PopGroup();
113 if (!pattern
|| pattern
->CairoStatus())
117 fprintf(stderr
, "clip extent: %f,%f %fx%f\n",
118 clipExtents
.X(), clipExtents
.Y(),
119 clipExtents
.Width(), clipExtents
.Height());
122 PRBool resultOverflows
;
123 gfxIntSize surfaceSize
=
124 nsSVGUtils::ConvertToSurfaceSize(gfxSize(clipExtents
.Width(),
125 clipExtents
.Height()),
128 // 0 disables mask, < 0 is an error
129 if (surfaceSize
.width
<= 0 || surfaceSize
.height
<= 0)
135 nsRefPtr
<gfxImageSurface
> image
=
136 new gfxImageSurface(surfaceSize
, gfxASurface::ImageFormatARGB32
);
137 if (!image
|| image
->CairoStatus())
139 image
->SetDeviceOffset(-clipExtents
.pos
);
141 gfxContext
transferCtx(image
);
142 transferCtx
.SetOperator(gfxContext::OPERATOR_SOURCE
);
143 transferCtx
.SetPattern(pattern
);
146 PRUint8
*data
= image
->Data();
147 PRInt32 stride
= image
->Stride();
149 nsIntRect
rect(0, 0, surfaceSize
.width
, surfaceSize
.height
);
150 nsSVGUtils::UnPremultiplyImageDataAlpha(data
, stride
, rect
);
151 nsSVGUtils::ConvertImageDataToLinearRGB(data
, stride
, rect
);
153 for (PRInt32 y
= 0; y
< surfaceSize
.height
; y
++)
154 for (PRInt32 x
= 0; x
< surfaceSize
.width
; x
++) {
155 PRUint8
*pixel
= data
+ stride
* y
+ 4 * x
;
157 /* linearRGB -> intensity */
160 ((pixel
[GFX_ARGB32_OFFSET_R
] * 0.2125 +
161 pixel
[GFX_ARGB32_OFFSET_G
] * 0.7154 +
162 pixel
[GFX_ARGB32_OFFSET_B
] * 0.0721) *
163 (pixel
[GFX_ARGB32_OFFSET_A
] / 255.0) * aOpacity
);
165 memset(pixel
, alpha
, 4);
168 gfxPattern
*retval
= new gfxPattern(image
);
169 NS_IF_ADDREF(retval
);
174 nsSVGMaskFrame::GetType() const
176 return nsGkAtoms::svgMaskFrame
;
179 already_AddRefed
<nsIDOMSVGMatrix
>
180 nsSVGMaskFrame::GetCanvasTM()
182 NS_ASSERTION(mMaskParentMatrix
, "null parent matrix");
184 nsSVGMaskElement
*mask
= static_cast<nsSVGMaskElement
*>(mContent
);
186 return nsSVGUtils::AdjustMatrixForUnits(mMaskParentMatrix
,
187 &mask
->mEnumAttributes
[nsSVGMaskElement::MASKCONTENTUNITS
],