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 "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>
37
38namespace WebCore {
39
40RenderSVGContainer::RenderSVGContainer(SVGElement& element, PassRef<RenderStyle> style)
41    : RenderSVGModelObject(element, WTF::move(style))
42    , m_objectBoundingBoxValid(false)
43    , m_needsBoundariesUpdate(true)
44{
45}
46
47RenderSVGContainer::~RenderSVGContainer()
48{
49}
50
51void RenderSVGContainer::layout()
52{
53    StackStats::LayoutCheckPoint layoutCheckPoint;
54    ASSERT(needsLayout());
55
56    // RenderSVGRoot disables layoutState for the SVG rendering tree.
57    ASSERT(!view().layoutStateEnabled());
58
59    LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(*this) || selfWillPaint());
60
61    // Allow RenderSVGViewportContainer to update its viewport.
62    calcViewport();
63
64    // Allow RenderSVGTransformableContainer to update its transform.
65    bool updatedTransform = calculateLocalTransform();
66
67    // RenderSVGViewportContainer needs to set the 'layout size changed' flag.
68    determineIfLayoutSizeChanged();
69
70    SVGRenderSupport::layoutChildren(*this, selfNeedsLayout() || SVGRenderSupport::filtersForceContainerLayout(*this));
71
72    // Invalidate all resources of this client if our layout changed.
73    if (everHadLayout() && needsLayout())
74        SVGResourcesCache::clientLayoutChanged(*this);
75
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;
81
82        // If our bounds changed, notify the parents.
83        RenderSVGModelObject::setNeedsBoundariesUpdate();
84    }
85
86    repainter.repaintAfterLayout();
87    clearNeedsLayout();
88}
89
90void RenderSVGContainer::addChild(RenderObject* child, RenderObject* beforeChild)
91{
92    RenderSVGModelObject::addChild(child, beforeChild);
93    SVGResourcesCache::clientWasAddedToTree(*child);
94}
95
96RenderObject* RenderSVGContainer::removeChild(RenderObject& child)
97{
98    SVGResourcesCache::clientWillBeRemovedFromTree(child);
99    return RenderSVGModelObject::removeChild(child);
100}
101
102
103bool RenderSVGContainer::selfWillPaint()
104{
105    SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(*this);
106    return resources && resources->filter();
107}
108
109void RenderSVGContainer::paint(PaintInfo& paintInfo, const LayoutPoint&)
110{
111    if (paintInfo.context->paintingDisabled())
112        return;
113
114    // Spec: groups w/o children still may render filter content.
115    if (!firstChild() && !selfWillPaint())
116        return;
117
118    FloatRect repaintRect = repaintRectInLocalCoordinates();
119    if (!SVGRenderSupport::paintInfoIntersectsRepaintRect(repaintRect, localToParentTransform(), paintInfo))
120        return;
121
122    PaintInfo childPaintInfo(paintInfo);
123    {
124        GraphicsContextStateSaver stateSaver(*childPaintInfo.context);
125
126        // Let the RenderSVGViewportContainer subclass clip if necessary
127        applyViewportClip(childPaintInfo);
128
129        childPaintInfo.applyTransform(localToParentTransform());
130
131        SVGRenderingContext renderingContext;
132        bool continueRendering = true;
133        if (childPaintInfo.phase == PaintPhaseForeground) {
134            renderingContext.prepareToRenderSVGContent(*this, childPaintInfo);
135            continueRendering = renderingContext.isRenderingPrepared();
136        }
137
138        if (continueRendering) {
139            childPaintInfo.updateSubtreePaintRootForChildren(this);
140            for (auto& child : childrenOfType<RenderElement>(*this))
141                child.paint(childPaintInfo, IntPoint());
142        }
143    }
144
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);
153    }
154}
155
156// addFocusRingRects is called from paintOutline and needs to be in the same coordinates as the paintOuline call
157void RenderSVGContainer::addFocusRingRects(Vector<IntRect>& rects, const LayoutPoint&, const RenderLayerModelObject*)
158{
159    IntRect paintRectInParent = enclosingIntRect(localToParentTransform().mapRect(repaintRectInLocalCoordinates()));
160    if (!paintRectInParent.isEmpty())
161        rects.append(paintRectInParent);
162}
163
164void RenderSVGContainer::updateCachedBoundaries()
165{
166    SVGRenderSupport::computeContainerBoundingBoxes(*this, m_objectBoundingBox, m_objectBoundingBoxValid, m_strokeBoundingBox, m_repaintBoundingBox);
167    SVGRenderSupport::intersectRepaintRectWithResources(*this, m_repaintBoundingBox);
168}
169
170bool RenderSVGContainer::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
171{
172    // Give RenderSVGViewportContainer a chance to apply its viewport clip
173    if (!pointIsInsideViewportClip(pointInParent))
174        return false;
175
176    FloatPoint localPoint = localToParentTransform().inverse().mapPoint(pointInParent);
177
178    if (!SVGRenderSupport::pointInClippingArea(*this, localPoint))
179        return false;
180
181    for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
182        if (child->nodeAtFloatPoint(request, result, localPoint, hitTestAction)) {
183            updateHitTestResult(result, roundedLayoutPoint(localPoint));
184            return true;
185        }
186    }
187
188    // Accessibility wants to return SVG containers, if appropriate.
189    if (request.type() & HitTestRequest::AccessibilityHitTest && m_objectBoundingBox.contains(localPoint)) {
190        updateHitTestResult(result, roundedLayoutPoint(localPoint));
191        return true;
192    }
193
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."
196    return false;
197}
198
199}
200