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>
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.
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.
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.
25 #include "RenderSVGContainer.h"
27 #include "GraphicsContext.h"
28 #include "HitTestRequest.h"
29 #include "LayoutRepainter.h"
30 #include "RenderIterator.h"
31 #include "RenderSVGResourceFilter.h"
32 #include "RenderView.h"
33 #include "SVGRenderingContext.h"
34 #include "SVGResources.h"
35 #include "SVGResourcesCache.h"
36 #include <wtf/StackStats.h>
40 RenderSVGContainer::RenderSVGContainer(SVGElement& element, PassRef<RenderStyle> style)
41 : RenderSVGModelObject(element, WTF::move(style))
42 , m_objectBoundingBoxValid(false)
43 , m_needsBoundariesUpdate(true)
47 RenderSVGContainer::~RenderSVGContainer()
51 void RenderSVGContainer::layout()
53 StackStats::LayoutCheckPoint layoutCheckPoint;
54 ASSERT(needsLayout());
56 // RenderSVGRoot disables layoutState for the SVG rendering tree.
57 ASSERT(!view().layoutStateEnabled());
59 LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(*this) || selfWillPaint());
61 // Allow RenderSVGViewportContainer to update its viewport.
64 // Allow RenderSVGTransformableContainer to update its transform.
65 bool updatedTransform = calculateLocalTransform();
67 // RenderSVGViewportContainer needs to set the 'layout size changed' flag.
68 determineIfLayoutSizeChanged();
70 SVGRenderSupport::layoutChildren(*this, selfNeedsLayout() || SVGRenderSupport::filtersForceContainerLayout(*this));
72 // Invalidate all resources of this client if our layout changed.
73 if (everHadLayout() && needsLayout())
74 SVGResourcesCache::clientLayoutChanged(*this);
76 // At this point LayoutRepainter already grabbed the old bounds,
77 // recalculate them now so repaintAfterLayout() uses the new bounds.
78 if (m_needsBoundariesUpdate || updatedTransform) {
79 updateCachedBoundaries();
80 m_needsBoundariesUpdate = false;
82 // If our bounds changed, notify the parents.
83 RenderSVGModelObject::setNeedsBoundariesUpdate();
86 repainter.repaintAfterLayout();
90 void RenderSVGContainer::addChild(RenderObject* child, RenderObject* beforeChild)
92 RenderSVGModelObject::addChild(child, beforeChild);
93 SVGResourcesCache::clientWasAddedToTree(*child);
96 RenderObject* RenderSVGContainer::removeChild(RenderObject& child)
98 SVGResourcesCache::clientWillBeRemovedFromTree(child);
99 return RenderSVGModelObject::removeChild(child);
103 bool RenderSVGContainer::selfWillPaint()
105 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(*this);
106 return resources && resources->filter();
109 void RenderSVGContainer::paint(PaintInfo& paintInfo, const LayoutPoint&)
111 if (paintInfo.context->paintingDisabled())
114 // Spec: groups w/o children still may render filter content.
115 if (!firstChild() && !selfWillPaint())
118 FloatRect repaintRect = repaintRectInLocalCoordinates();
119 if (!SVGRenderSupport::paintInfoIntersectsRepaintRect(repaintRect, localToParentTransform(), paintInfo))
122 PaintInfo childPaintInfo(paintInfo);
124 GraphicsContextStateSaver stateSaver(*childPaintInfo.context);
126 // Let the RenderSVGViewportContainer subclass clip if necessary
127 applyViewportClip(childPaintInfo);
129 childPaintInfo.applyTransform(localToParentTransform());
131 SVGRenderingContext renderingContext;
132 bool continueRendering = true;
133 if (childPaintInfo.phase == PaintPhaseForeground) {
134 renderingContext.prepareToRenderSVGContent(*this, childPaintInfo);
135 continueRendering = renderingContext.isRenderingPrepared();
138 if (continueRendering) {
139 childPaintInfo.updateSubtreePaintRootForChildren(this);
140 for (auto& child : childrenOfType<RenderElement>(*this))
141 child.paint(childPaintInfo, IntPoint());
145 // FIXME: This really should be drawn from local coordinates, but currently we hack it
146 // to avoid our clip killing our outline rect. Thus we translate our
147 // outline rect into parent coords before drawing.
148 // FIXME: This means our focus ring won't share our rotation like it should.
149 // We should instead disable our clip during PaintPhaseOutline
150 if (paintInfo.phase == PaintPhaseSelfOutline && style().outlineWidth() && style().visibility() == VISIBLE) {
151 IntRect paintRectInParent = enclosingIntRect(localToParentTransform().mapRect(repaintRect));
152 paintOutline(paintInfo, paintRectInParent);
156 // addFocusRingRects is called from paintOutline and needs to be in the same coordinates as the paintOuline call
157 void RenderSVGContainer::addFocusRingRects(Vector<IntRect>& rects, const LayoutPoint&, const RenderLayerModelObject*)
159 IntRect paintRectInParent = enclosingIntRect(localToParentTransform().mapRect(repaintRectInLocalCoordinates()));
160 if (!paintRectInParent.isEmpty())
161 rects.append(paintRectInParent);
164 void RenderSVGContainer::updateCachedBoundaries()
166 SVGRenderSupport::computeContainerBoundingBoxes(*this, m_objectBoundingBox, m_objectBoundingBoxValid, m_strokeBoundingBox, m_repaintBoundingBox);
167 SVGRenderSupport::intersectRepaintRectWithResources(*this, m_repaintBoundingBox);
170 bool RenderSVGContainer::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
172 // Give RenderSVGViewportContainer a chance to apply its viewport clip
173 if (!pointIsInsideViewportClip(pointInParent))
176 FloatPoint localPoint = localToParentTransform().inverse().mapPoint(pointInParent);
178 if (!SVGRenderSupport::pointInClippingArea(*this, localPoint))
181 for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
182 if (child->nodeAtFloatPoint(request, result, localPoint, hitTestAction)) {
183 updateHitTestResult(result, LayoutPoint(localPoint));
188 // Accessibility wants to return SVG containers, if appropriate.
189 if (request.type() & HitTestRequest::AccessibilityHitTest && m_objectBoundingBox.contains(localPoint)) {
190 updateHitTestResult(result, LayoutPoint(localPoint));
194 // Spec: Only graphical elements can be targeted by the mouse, period.
195 // 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."