Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef __NS_SVGCLIPPATHFRAME_H__
7 : #define __NS_SVGCLIPPATHFRAME_H__
8 :
9 : #include "gfxMatrix.h"
10 : #include "mozilla/Attributes.h"
11 : #include "nsSVGContainerFrame.h"
12 : #include "nsSVGUtils.h"
13 :
14 : class gfxContext;
15 : class nsSVGDisplayableFrame;
16 :
17 0 : class nsSVGClipPathFrame final : public nsSVGContainerFrame
18 : {
19 : friend nsIFrame*
20 : NS_NewSVGClipPathFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
21 :
22 : typedef mozilla::gfx::Matrix Matrix;
23 : typedef mozilla::gfx::SourceSurface SourceSurface;
24 : typedef mozilla::image::imgDrawingParams imgDrawingParams;
25 :
26 : protected:
27 6 : explicit nsSVGClipPathFrame(nsStyleContext* aContext)
28 6 : : nsSVGContainerFrame(aContext, kClassID)
29 6 : , mIsBeingProcessed(false)
30 : {
31 6 : AddStateBits(NS_FRAME_IS_NONDISPLAY);
32 6 : }
33 :
34 : public:
35 28 : NS_DECL_FRAMEARENA_HELPERS(nsSVGClipPathFrame)
36 :
37 : // nsIFrame methods:
38 0 : virtual void BuildDisplayList(nsDisplayListBuilder* aBuilder,
39 : const nsRect& aDirtyRect,
40 0 : const nsDisplayListSet& aLists) override {}
41 :
42 : // nsSVGClipPathFrame methods:
43 :
44 : /**
45 : * Applies the clipPath by pushing a clip path onto the DrawTarget.
46 : *
47 : * This method must only be used if IsTrivial() returns true, otherwise use
48 : * GetClipMask.
49 : *
50 : * @param aContext The context that the clip path is to be applied to.
51 : * @param aClippedFrame The/an nsIFrame of the element that references this
52 : * clipPath that is currently being processed.
53 : * @param aMatrix The transform from aClippedFrame's user space to aContext's
54 : * current transform.
55 : */
56 : void ApplyClipPath(gfxContext& aContext,
57 : nsIFrame* aClippedFrame,
58 : const gfxMatrix &aMatrix);
59 :
60 : /**
61 : * Returns an alpha mask surface containing the clipping geometry.
62 : *
63 : * This method must only be used if IsTrivial() returns false, otherwise use
64 : * ApplyClipPath.
65 : *
66 : * @param aReferenceContext Used to determine the backend for and size of the
67 : * returned SourceSurface, the size being limited to the device space clip
68 : * extents on the context.
69 : * @param aClippedFrame The/an nsIFrame of the element that references this
70 : * clipPath that is currently being processed.
71 : * @param aMatrix The transform from aClippedFrame's user space to aContext's
72 : * current transform.
73 : * @param [out] aMaskTransform The transform to use with the returned
74 : * surface.
75 : * @param [in, optional] aExtraMask An extra surface that the returned
76 : * surface should be masked with.
77 : * @param [in, optional] aExtraMasksTransform The transform to use with
78 : * aExtraMask. Should be passed when aExtraMask is passed.
79 : */
80 : already_AddRefed<SourceSurface>
81 : GetClipMask(gfxContext& aReferenceContext, nsIFrame* aClippedFrame,
82 : const gfxMatrix& aMatrix, Matrix* aMaskTransform,
83 : SourceSurface* aExtraMask = nullptr,
84 : const Matrix& aExtraMasksTransform = Matrix());
85 :
86 : /**
87 : * Paint mask directly onto a given context(aMaskContext).
88 : *
89 : * @param aMaskContext The target of mask been painting on.
90 : * @param aClippedFrame The/an nsIFrame of the element that references this
91 : * clipPath that is currently being processed.
92 : * @param aMatrix The transform from aClippedFrame's user space to
93 : * current transform.
94 : * @param [out] aMaskTransform The transform to use with the returned
95 : * surface.
96 : * @param [in, optional] aExtraMask An extra surface that the returned
97 : * surface should be masked with.
98 : * @param [in, optional] aExtraMasksTransform The transform to use with
99 : * aExtraMask. Should be passed when aExtraMask is passed.
100 : */
101 : void
102 : PaintClipMask(gfxContext& aMaskContext, nsIFrame* aClippedFrame,
103 : const gfxMatrix& aMatrix, Matrix* aMaskTransform,
104 : SourceSurface* aExtraMask, const Matrix& aExtraMasksTransform);
105 :
106 : /**
107 : * aPoint is expected to be in aClippedFrame's SVG user space.
108 : */
109 : bool PointIsInsideClipPath(nsIFrame* aClippedFrame, const gfxPoint &aPoint);
110 :
111 : // Check if this clipPath is made up of more than one geometry object.
112 : // If so, the clipping API in cairo isn't enough and we need to use
113 : // mask based clipping.
114 : bool IsTrivial(nsSVGDisplayableFrame **aSingleChild = nullptr);
115 :
116 : bool IsValid();
117 :
118 : // nsIFrame interface:
119 : virtual nsresult AttributeChanged(int32_t aNameSpaceID,
120 : nsIAtom* aAttribute,
121 : int32_t aModType) override;
122 :
123 : virtual void Init(nsIContent* aContent,
124 : nsContainerFrame* aParent,
125 : nsIFrame* aPrevInFlow) override;
126 :
127 : #ifdef DEBUG_FRAME_DUMP
128 0 : virtual nsresult GetFrameName(nsAString& aResult) const override
129 : {
130 0 : return MakeFrameName(NS_LITERAL_STRING("SVGClipPath"), aResult);
131 : }
132 : #endif
133 :
134 : SVGBBox GetBBoxForClipPathFrame(const SVGBBox& aBBox,
135 : const gfxMatrix& aMatrix);
136 :
137 : /**
138 : * If the clipPath element transforms its children due to
139 : * clipPathUnits="objectBoundingBox" being set on it and/or due to the
140 : * 'transform' attribute being set on it, this function returns the resulting
141 : * transform.
142 : */
143 : gfxMatrix GetClipPathTransform(nsIFrame* aClippedFrame);
144 :
145 : private:
146 :
147 : // nsSVGContainerFrame methods:
148 : virtual gfxMatrix GetCanvasTM() override;
149 :
150 : already_AddRefed<DrawTarget> CreateClipMask(gfxContext& aReferenceContext,
151 : mozilla::gfx::IntPoint& aOffset);
152 :
153 : void PaintFrameIntoMask(nsIFrame *aFrame, nsIFrame* aClippedFrame,
154 : gfxContext& aTarget, const gfxMatrix& aMatrix);
155 :
156 : // Set, during a GetClipMask() call, to the transform that still needs to be
157 : // concatenated to the transform of the DrawTarget that was passed to
158 : // GetClipMask in order to establish the coordinate space that the clipPath
159 : // establishes for its contents (i.e. including applying 'clipPathUnits' and
160 : // any 'transform' attribute set on the clipPath) specifically for clipping
161 : // the frame that was passed to GetClipMask at that moment in time. This is
162 : // set so that if our GetCanvasTM method is called while GetClipMask is
163 : // painting its children, the returned matrix will include the transforms
164 : // that should be used when creating the mask for the frame passed to
165 : // GetClipMask.
166 : //
167 : // Note: The removal of GetCanvasTM is nearly complete, so our GetCanvasTM
168 : // may not even be called soon/any more.
169 : gfxMatrix mMatrixForChildren;
170 :
171 : // Flag used to indicate whether a methods that may reenter due to
172 : // following a reference to another instance is currently executing.
173 : bool mIsBeingProcessed;
174 : };
175 :
176 : #endif
|