1/*
2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2007, 2008 Rob Buis <buis@kde.org>
4 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
5 * Copyright (C) 2009 Google, Inc. All rights reserved.
6 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24#include "config.h"
25#include "RenderSVGContainer.h"
26
27#include "GraphicsContext.h"
28#include "HitTestRequest.h"
29#include "HitTestResult.h"
30#include "LayoutRepainter.h"
31#include "RenderIterator.h"
32#include "RenderSVGResourceFilter.h"
33#include "RenderTreeBuilder.h"
34#include "RenderView.h"
35#include "SVGRenderingContext.h"
36#include "SVGResources.h"
37#include "SVGResourcesCache.h"
38#include <wtf/IsoMallocInlines.h>
39#include <wtf/StackStats.h>
40
41namespace WebCore {
42
43WTF_MAKE_ISO_ALLOCATED_IMPL(RenderSVGContainer);
44
45RenderSVGContainer::RenderSVGContainer(SVGElement& element, RenderStyle&& style)
46 : RenderSVGModelObject(element, WTFMove(style))
47{
48}
49
50RenderSVGContainer::~RenderSVGContainer() = default;
51
52void RenderSVGContainer::layout()
53{
54 StackStats::LayoutCheckPoint layoutCheckPoint;
55 ASSERT(needsLayout());
56
57 // RenderSVGRoot disables paint offset cache for the SVG rendering tree.
58 ASSERT(!view().frameView().layoutContext().isPaintOffsetCacheEnabled());
59
60 LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(*this) || selfWillPaint());
61
62 // Allow RenderSVGViewportContainer to update its viewport.
63 calcViewport();
64
65 // Allow RenderSVGTransformableContainer to update its transform.
66 bool updatedTransform = calculateLocalTransform();
67
68 // RenderSVGViewportContainer needs to set the 'layout size changed' flag.
69 determineIfLayoutSizeChanged();
70
71 SVGRenderSupport::layoutChildren(*this, selfNeedsLayout() || SVGRenderSupport::filtersForceContainerLayout(*this));
72
73 // Invalidate all resources of this client if our layout changed.
74 if (everHadLayout() && needsLayout())
75 SVGResourcesCache::clientLayoutChanged(*this);
76
77 // At this point LayoutRepainter already grabbed the old bounds,
78 // recalculate them now so repaintAfterLayout() uses the new bounds.
79 if (m_needsBoundariesUpdate || updatedTransform) {
80 updateCachedBoundaries();
81 m_needsBoundariesUpdate = false;
82
83 // If our bounds changed, notify the parents.
84 RenderSVGModelObject::setNeedsBoundariesUpdate();
85 }
86
87 repainter.repaintAfterLayout();
88 clearNeedsLayout();
89}
90
91bool RenderSVGContainer::selfWillPaint()
92{
93 auto* resources = SVGResourcesCache::cachedResourcesForRenderer(*this);
94 return resources && resources->filter();
95}
96
97void RenderSVGContainer::paint(PaintInfo& paintInfo, const LayoutPoint&)
98{
99 if (paintInfo.context().paintingDisabled())
100 return;
101
102 // Spec: groups w/o children still may render filter content.
103 if (!firstChild() && !selfWillPaint())
104 return;
105
106 FloatRect repaintRect = repaintRectInLocalCoordinates();
107 if (!SVGRenderSupport::paintInfoIntersectsRepaintRect(repaintRect, localToParentTransform(), paintInfo))
108 return;
109
110 PaintInfo childPaintInfo(paintInfo);
111 {
112 GraphicsContextStateSaver stateSaver(childPaintInfo.context());
113
114 // Let the RenderSVGViewportContainer subclass clip if necessary
115 applyViewportClip(childPaintInfo);
116
117 childPaintInfo.applyTransform(localToParentTransform());
118
119 SVGRenderingContext renderingContext;
120 bool continueRendering = true;
121 if (childPaintInfo.phase == PaintPhase::Foreground) {
122 renderingContext.prepareToRenderSVGContent(*this, childPaintInfo);
123 continueRendering = renderingContext.isRenderingPrepared();
124 }
125
126 if (continueRendering) {
127 childPaintInfo.updateSubtreePaintRootForChildren(this);
128 for (auto& child : childrenOfType<RenderElement>(*this))
129 child.paint(childPaintInfo, IntPoint());
130 }
131 }
132
133 // FIXME: This really should be drawn from local coordinates, but currently we hack it
134 // to avoid our clip killing our outline rect. Thus we translate our
135 // outline rect into parent coords before drawing.
136 // FIXME: This means our focus ring won't share our rotation like it should.
137 // We should instead disable our clip during PaintPhase::Outline
138 if (paintInfo.phase == PaintPhase::SelfOutline && style().outlineWidth() && style().visibility() == Visibility::Visible) {
139 IntRect paintRectInParent = enclosingIntRect(localToParentTransform().mapRect(repaintRect));
140 paintOutline(paintInfo, paintRectInParent);
141 }
142}
143
144// addFocusRingRects is called from paintOutline and needs to be in the same coordinates as the paintOuline call
145void RenderSVGContainer::addFocusRingRects(Vector<LayoutRect>& rects, const LayoutPoint&, const RenderLayerModelObject*)
146{
147 LayoutRect paintRectInParent = LayoutRect(localToParentTransform().mapRect(repaintRectInLocalCoordinates()));
148 if (!paintRectInParent.isEmpty())
149 rects.append(paintRectInParent);
150}
151
152void RenderSVGContainer::updateCachedBoundaries()
153{
154 SVGRenderSupport::computeContainerBoundingBoxes(*this, m_objectBoundingBox, m_objectBoundingBoxValid, m_strokeBoundingBox, m_repaintBoundingBox);
155 SVGRenderSupport::intersectRepaintRectWithResources(*this, m_repaintBoundingBox);
156}
157
158bool RenderSVGContainer::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
159{
160 // Give RenderSVGViewportContainer a chance to apply its viewport clip
161 if (!pointIsInsideViewportClip(pointInParent))
162 return false;
163
164 FloatPoint localPoint = localToParentTransform().inverse().valueOr(AffineTransform()).mapPoint(pointInParent);
165
166 if (!SVGRenderSupport::pointInClippingArea(*this, localPoint))
167 return false;
168
169 for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
170 if (child->nodeAtFloatPoint(request, result, localPoint, hitTestAction)) {
171 updateHitTestResult(result, LayoutPoint(localPoint));
172 if (result.addNodeToListBasedTestResult(child->node(), request, localPoint) == HitTestProgress::Stop)
173 return true;
174 }
175 }
176
177 // Accessibility wants to return SVG containers, if appropriate.
178 if (request.type() & HitTestRequest::AccessibilityHitTest && m_objectBoundingBox.contains(localPoint)) {
179 updateHitTestResult(result, LayoutPoint(localPoint));
180 if (result.addNodeToListBasedTestResult(&element(), request, localPoint) == HitTestProgress::Stop)
181 return true;
182 }
183
184 // Spec: Only graphical elements can be targeted by the mouse, period.
185 // 16.4: "If there are no graphics elements whose relevant graphics content is under the pointer (i.e., there is no target element), the event is not dispatched."
186 return false;
187}
188
189}
190