2 * Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Rob Buis <buis@kde.org>
4 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
5 * Copyright (C) 2011 Dirk Schulze <krit@webkit.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #include "RenderSVGResourceClipper.h"
26 #include "ElementIterator.h"
28 #include "FrameView.h"
29 #include "HitTestRequest.h"
30 #include "HitTestResult.h"
32 #include "RenderObject.h"
33 #include "RenderStyle.h"
34 #include "RenderView.h"
36 #include "SVGRenderingContext.h"
37 #include "SVGResources.h"
38 #include "SVGResourcesCache.h"
39 #include "SVGUseElement.h"
43 RenderSVGResourceClipper::RenderSVGResourceClipper(SVGClipPathElement& element, Ref<RenderStyle>&& style)
44 : RenderSVGResourceContainer(element, WTF::move(style))
48 RenderSVGResourceClipper::~RenderSVGResourceClipper()
52 void RenderSVGResourceClipper::removeAllClientsFromCache(bool markForInvalidation)
54 m_clipBoundaries = FloatRect();
57 markAllClientsForInvalidation(markForInvalidation ? LayoutAndBoundariesInvalidation : ParentOnlyInvalidation);
60 void RenderSVGResourceClipper::removeClientFromCache(RenderElement& client, bool markForInvalidation)
62 m_clipper.remove(&client);
64 markClientForInvalidation(client, markForInvalidation ? BoundariesInvalidation : ParentOnlyInvalidation);
67 bool RenderSVGResourceClipper::applyResource(RenderElement& renderer, const RenderStyle&, GraphicsContext*& context, unsigned short resourceMode)
70 ASSERT_UNUSED(resourceMode, resourceMode == ApplyToDefaultMode);
72 return applyClippingToContext(renderer, renderer.objectBoundingBox(), renderer.repaintRectInLocalCoordinates(), context);
75 bool RenderSVGResourceClipper::pathOnlyClipping(GraphicsContext* context, const AffineTransform& animatedLocalTransform, const FloatRect& objectBoundingBox)
77 // If the current clip-path gets clipped itself, we have to fallback to masking.
78 if (!style().svgStyle().clipperResource().isEmpty())
80 WindRule clipRule = RULE_NONZERO;
81 Path clipPath = Path();
83 // If clip-path only contains one visible shape or path, we can use path-based clipping. Invisible
84 // shapes don't affect the clipping and can be ignored. If clip-path contains more than one
85 // visible shape, the additive clipping may not work, caused by the clipRule. EvenOdd
86 // as well as NonZero can cause self-clipping of the elements.
87 // See also http://www.w3.org/TR/SVG/painting.html#FillRuleProperty
88 for (Node* childNode = clipPathElement().firstChild(); childNode; childNode = childNode->nextSibling()) {
89 RenderObject* renderer = childNode->renderer();
92 // Only shapes or paths are supported for direct clipping. We need to fallback to masking for texts.
93 if (renderer->isSVGText())
95 if (!childNode->isSVGElement() || !downcast<SVGElement>(*childNode).isSVGGraphicsElement())
97 SVGGraphicsElement& styled = downcast<SVGGraphicsElement>(*childNode);
98 const RenderStyle& style = renderer->style();
99 if (style.display() == NONE || style.visibility() != VISIBLE)
101 const SVGRenderStyle& svgStyle = style.svgStyle();
102 // Current shape in clip-path gets clipped too. Fallback to masking.
103 if (!svgStyle.clipperResource().isEmpty())
105 // Fallback to masking, if there is more than one clipping path.
106 if (clipPath.isEmpty()) {
107 styled.toClipPath(clipPath);
108 clipRule = svgStyle.clipRule();
112 // Only one visible shape/path was found. Directly continue clipping and transform the content to userspace if necessary.
113 if (clipPathElement().clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
114 AffineTransform transform;
115 transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
116 transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
117 clipPath.transform(transform);
120 // Transform path by animatedLocalTransform.
121 clipPath.transform(animatedLocalTransform);
123 // The SVG specification wants us to clip everything, if clip-path doesn't have a child.
124 if (clipPath.isEmpty())
125 clipPath.addRect(FloatRect());
126 context->clipPath(clipPath, clipRule);
130 bool RenderSVGResourceClipper::applyClippingToContext(RenderElement& renderer, const FloatRect& objectBoundingBox,
131 const FloatRect& repaintRect, GraphicsContext* context)
133 ClipperMaskImage& clipperMaskImage = addRendererToClipper(renderer);
134 bool shouldCreateClipperMaskImage = !clipperMaskImage;
136 AffineTransform animatedLocalTransform = clipPathElement().animatedLocalTransform();
138 if (shouldCreateClipperMaskImage && pathOnlyClipping(context, animatedLocalTransform, objectBoundingBox))
141 AffineTransform absoluteTransform = SVGRenderingContext::calculateTransformationToOutermostCoordinateSystem(renderer);
143 if (shouldCreateClipperMaskImage && !repaintRect.isEmpty()) {
144 clipperMaskImage = SVGRenderingContext::createImageBuffer(repaintRect, absoluteTransform, ColorSpaceDeviceRGB, Unaccelerated);
145 if (!clipperMaskImage)
148 GraphicsContext* maskContext = clipperMaskImage->context();
151 maskContext->concatCTM(animatedLocalTransform);
153 // clipPath can also be clipped by another clipPath.
154 auto* resources = SVGResourcesCache::cachedResourcesForRenderer(*this);
155 RenderSVGResourceClipper* clipper;
157 if (resources && (clipper = resources->clipper())) {
158 GraphicsContextStateSaver stateSaver(*maskContext);
160 if (!clipper->applyClippingToContext(*this, objectBoundingBox, repaintRect, maskContext))
163 succeeded = drawContentIntoMaskImage(clipperMaskImage, objectBoundingBox);
164 // The context restore applies the clipping on non-CG platforms.
166 succeeded = drawContentIntoMaskImage(clipperMaskImage, objectBoundingBox);
169 clipperMaskImage.reset();
172 if (!clipperMaskImage)
175 SVGRenderingContext::clipToImageBuffer(context, absoluteTransform, repaintRect, clipperMaskImage, shouldCreateClipperMaskImage);
179 bool RenderSVGResourceClipper::drawContentIntoMaskImage(const ClipperMaskImage& clipperMaskImage, const FloatRect& objectBoundingBox)
181 ASSERT(clipperMaskImage);
183 GraphicsContext* maskContext = clipperMaskImage->context();
186 AffineTransform maskContentTransformation;
187 if (clipPathElement().clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
188 maskContentTransformation.translate(objectBoundingBox.x(), objectBoundingBox.y());
189 maskContentTransformation.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
190 maskContext->concatCTM(maskContentTransformation);
193 // Switch to a paint behavior where all children of this <clipPath> will be rendered using special constraints:
194 // - fill-opacity/stroke-opacity/opacity set to 1
195 // - masker/filter not applied when rendering the children
196 // - fill is set to the initial fill paint server (solid, black)
197 // - stroke is set to the initial stroke paint server (none)
198 PaintBehavior oldBehavior = view().frameView().paintBehavior();
199 view().frameView().setPaintBehavior(oldBehavior | PaintBehaviorRenderingSVGMask);
201 // Draw all clipPath children into a global mask.
202 for (auto& child : childrenOfType<SVGElement>(clipPathElement())) {
203 auto renderer = child.renderer();
206 if (renderer->needsLayout()) {
207 view().frameView().setPaintBehavior(oldBehavior);
210 const RenderStyle& style = renderer->style();
211 if (style.display() == NONE || style.visibility() != VISIBLE)
214 WindRule newClipRule = style.svgStyle().clipRule();
215 bool isUseElement = child.hasTagName(SVGNames::useTag);
217 SVGUseElement& useElement = downcast<SVGUseElement>(child);
218 renderer = useElement.rendererClipChild();
221 if (!useElement.fastHasAttribute(SVGNames::clip_ruleAttr))
222 newClipRule = renderer->style().svgStyle().clipRule();
225 // Only shapes, paths and texts are allowed for clipping.
226 if (!renderer->isSVGShape() && !renderer->isSVGText())
229 maskContext->setFillRule(newClipRule);
231 // In the case of a <use> element, we obtained its renderere above, to retrieve its clipRule.
232 // We have to pass the <use> renderer itself to renderSubtreeToImageBuffer() to apply it's x/y/transform/etc. values when rendering.
233 // So if isUseElement is true, refetch the childNode->renderer(), as renderer got overriden above.
234 SVGRenderingContext::renderSubtreeToImageBuffer(clipperMaskImage.get(), isUseElement ? *child.renderer() : *renderer, maskContentTransformation);
237 view().frameView().setPaintBehavior(oldBehavior);
241 void RenderSVGResourceClipper::calculateClipContentRepaintRect()
243 // This is a rough heuristic to appraise the clip size and doesn't consider clip on clip.
244 for (Node* childNode = clipPathElement().firstChild(); childNode; childNode = childNode->nextSibling()) {
245 RenderObject* renderer = childNode->renderer();
246 if (!childNode->isSVGElement() || !renderer)
248 if (!renderer->isSVGShape() && !renderer->isSVGText() && !childNode->hasTagName(SVGNames::useTag))
250 const RenderStyle& style = renderer->style();
251 if (style.display() == NONE || style.visibility() != VISIBLE)
253 m_clipBoundaries.unite(renderer->localToParentTransform().mapRect(renderer->repaintRectInLocalCoordinates()));
255 m_clipBoundaries = clipPathElement().animatedLocalTransform().mapRect(m_clipBoundaries);
258 ClipperMaskImage& RenderSVGResourceClipper::addRendererToClipper(const RenderObject& object)
260 return m_clipper.add(&object, ClipperMaskImage()).iterator->value;
263 bool RenderSVGResourceClipper::hitTestClipContent(const FloatRect& objectBoundingBox, const FloatPoint& nodeAtPoint)
265 FloatPoint point = nodeAtPoint;
266 if (!SVGRenderSupport::pointInClippingArea(*this, point))
269 if (clipPathElement().clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
270 AffineTransform transform;
271 transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
272 transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
273 point = transform.inverse().mapPoint(point);
276 point = clipPathElement().animatedLocalTransform().inverse().mapPoint(point);
278 for (Node* childNode = clipPathElement().firstChild(); childNode; childNode = childNode->nextSibling()) {
279 RenderObject* renderer = childNode->renderer();
280 if (!childNode->isSVGElement() || !renderer)
282 if (!renderer->isSVGShape() && !renderer->isSVGText() && !childNode->hasTagName(SVGNames::useTag))
285 HitTestResult result(hitPoint);
286 if (renderer->nodeAtFloatPoint(HitTestRequest(HitTestRequest::SVGClipContent | HitTestRequest::DisallowShadowContent), result, point, HitTestForeground))
293 FloatRect RenderSVGResourceClipper::resourceBoundingBox(const RenderObject& object)
295 // Resource was not layouted yet. Give back the boundingBox of the object.
296 if (selfNeedsLayout()) {
297 addRendererToClipper(object);
298 return object.objectBoundingBox();
301 if (m_clipBoundaries.isEmpty())
302 calculateClipContentRepaintRect();
304 if (clipPathElement().clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
305 FloatRect objectBoundingBox = object.objectBoundingBox();
306 AffineTransform transform;
307 transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
308 transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
309 return transform.mapRect(m_clipBoundaries);
312 return m_clipBoundaries;