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
26#include "core/rendering/svg/RenderSVGContainer.h"
27
28#include "core/rendering/GraphicsContextAnnotator.h"
29#include "core/rendering/LayoutRectRecorder.h"
30#include "core/rendering/LayoutRepainter.h"
31#include "core/rendering/RenderView.h"
32#include "core/rendering/svg/SVGRenderingContext.h"
33#include "core/rendering/svg/SVGResources.h"
34#include "core/rendering/svg/SVGResourcesCache.h"
35#include "platform/graphics/GraphicsContextStateSaver.h"
36
37namespace WebCore {
38
39RenderSVGContainer::RenderSVGContainer(SVGElement* node)
40    : RenderSVGModelObject(node)
41    , m_objectBoundingBoxValid(false)
42    , m_needsBoundariesUpdate(true)
43{
44}
45
46RenderSVGContainer::~RenderSVGContainer()
47{
48}
49
50void RenderSVGContainer::layout()
51{
52    ASSERT(needsLayout());
53
54    // RenderSVGRoot disables layoutState for the SVG rendering tree.
55    ASSERT(!view()->layoutStateEnabled());
56
57    LayoutRectRecorder recorder(*this);
58    LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(this) || selfWillPaint());
59
60    // Allow RenderSVGViewportContainer to update its viewport.
61    calcViewport();
62
63    // Allow RenderSVGTransformableContainer to update its transform.
64    bool updatedTransform = calculateLocalTransform();
65
66    // RenderSVGViewportContainer needs to set the 'layout size changed' flag.
67    determineIfLayoutSizeChanged();
68
69    SVGRenderSupport::layoutChildren(this, selfNeedsLayout() || SVGRenderSupport::filtersForceContainerLayout(this));
70
71    // Invalidate all resources of this client if our layout changed.
72    if (everHadLayout() && needsLayout())
73        SVGResourcesCache::clientLayoutChanged(this);
74
75    // At this point LayoutRepainter already grabbed the old bounds,
76    // recalculate them now so repaintAfterLayout() uses the new bounds.
77    if (m_needsBoundariesUpdate || updatedTransform) {
78        updateCachedBoundaries();
79        m_needsBoundariesUpdate = false;
80
81        // If our bounds changed, notify the parents.
82        RenderSVGModelObject::setNeedsBoundariesUpdate();
83    }
84
85    repainter.repaintAfterLayout();
86    clearNeedsLayout();
87}
88
89void RenderSVGContainer::addChild(RenderObject* child, RenderObject* beforeChild)
90{
91    RenderSVGModelObject::addChild(child, beforeChild);
92    SVGResourcesCache::clientWasAddedToTree(child, child->style());
93}
94
95void RenderSVGContainer::removeChild(RenderObject* child)
96{
97    SVGResourcesCache::clientWillBeRemovedFromTree(child);
98    RenderSVGModelObject::removeChild(child);
99}
100
101
102bool RenderSVGContainer::selfWillPaint()
103{
104    SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(this);
105    return resources && resources->filter();
106}
107
108void RenderSVGContainer::paint(PaintInfo& paintInfo, const LayoutPoint&)
109{
110    ANNOTATE_GRAPHICS_CONTEXT(paintInfo, this);
111
112    if (paintInfo.context->paintingDisabled())
113        return;
114
115    // Spec: groups w/o children still may render filter content.
116    if (!firstChild() && !selfWillPaint())
117        return;
118
119    FloatRect repaintRect = repaintRectInLocalCoordinates();
120    if (!SVGRenderSupport::paintInfoIntersectsRepaintRect(repaintRect, localToParentTransform(), paintInfo))
121        return;
122
123    PaintInfo childPaintInfo(paintInfo);
124    {
125        GraphicsContextStateSaver stateSaver(*childPaintInfo.context);
126
127        // Let the RenderSVGViewportContainer subclass clip if necessary
128        applyViewportClip(childPaintInfo);
129
130        childPaintInfo.applyTransform(localToParentTransform());
131
132        SVGRenderingContext renderingContext;
133        bool continueRendering = true;
134        if (childPaintInfo.phase == PaintPhaseForeground) {
135            renderingContext.prepareToRenderSVGContent(this, childPaintInfo);
136            continueRendering = renderingContext.isRenderingPrepared();
137        }
138
139        if (continueRendering) {
140            childPaintInfo.updatePaintingRootForChildren(this);
141            for (RenderObject* child = firstChild(); child; child = child->nextSibling())
142                child->paint(childPaintInfo, IntPoint());
143        }
144    }
145
146    // FIXME: This really should be drawn from local coordinates, but currently we hack it
147    // to avoid our clip killing our outline rect.  Thus we translate our
148    // outline rect into parent coords before drawing.
149    // FIXME: This means our focus ring won't share our rotation like it should.
150    // We should instead disable our clip during PaintPhaseOutline
151    if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && style()->outlineWidth() && style()->visibility() == VISIBLE) {
152        IntRect paintRectInParent = enclosingIntRect(localToParentTransform().mapRect(repaintRect));
153        paintOutline(paintInfo, paintRectInParent);
154    }
155}
156
157// addFocusRingRects is called from paintOutline and needs to be in the same coordinates as the paintOuline call
158void RenderSVGContainer::addFocusRingRects(Vector<IntRect>& rects, const LayoutPoint&, const RenderLayerModelObject*)
159{
160    IntRect paintRectInParent = enclosingIntRect(localToParentTransform().mapRect(repaintRectInLocalCoordinates()));
161    if (!paintRectInParent.isEmpty())
162        rects.append(paintRectInParent);
163}
164
165void RenderSVGContainer::updateCachedBoundaries()
166{
167    SVGRenderSupport::computeContainerBoundingBoxes(this, m_objectBoundingBox, m_objectBoundingBoxValid, m_strokeBoundingBox, m_repaintBoundingBox);
168    SVGRenderSupport::intersectRepaintRectWithResources(this, m_repaintBoundingBox);
169}
170
171bool RenderSVGContainer::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
172{
173    // Give RenderSVGViewportContainer a chance to apply its viewport clip
174    if (!pointIsInsideViewportClip(pointInParent))
175        return false;
176
177    FloatPoint localPoint = localToParentTransform().inverse().mapPoint(pointInParent);
178
179    if (!SVGRenderSupport::pointInClippingArea(this, localPoint))
180        return false;
181
182    for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
183        if (child->nodeAtFloatPoint(request, result, localPoint, hitTestAction)) {
184            updateHitTestResult(result, roundedLayoutPoint(localPoint));
185            return true;
186        }
187    }
188
189    // pointer-events=boundingBox makes it possible for containers to be direct targets
190    if (style()->pointerEvents() == PE_BOUNDINGBOX) {
191        ASSERT(isObjectBoundingBoxValid());
192        if (objectBoundingBox().contains(localPoint)) {
193            updateHitTestResult(result, roundedLayoutPoint(localPoint));
194            return true;
195        }
196    }
197    // 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."
198    return false;
199}
200
201}
202