1/*
2 * Copyright (C) 2006 Apple Computer, Inc.
3 * Copyright (C) 2009 Google, Inc.
4 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#include "config.h"
23
24#if ENABLE(SVG)
25#include "RenderSVGForeignObject.h"
26
27#include "GraphicsContext.h"
28#include "HitTestResult.h"
29#include "LayoutRepainter.h"
30#include "RenderObject.h"
31#include "RenderSVGResource.h"
32#include "RenderView.h"
33#include "SVGForeignObjectElement.h"
34#include "SVGRenderingContext.h"
35#include "SVGResourcesCache.h"
36#include "SVGSVGElement.h"
37#include "TransformState.h"
38#include <wtf/StackStats.h>
39
40namespace WebCore {
41
42RenderSVGForeignObject::RenderSVGForeignObject(SVGForeignObjectElement* node)
43    : RenderSVGBlock(node)
44    , m_needsTransformUpdate(true)
45{
46}
47
48RenderSVGForeignObject::~RenderSVGForeignObject()
49{
50}
51
52void RenderSVGForeignObject::paint(PaintInfo& paintInfo, const LayoutPoint&)
53{
54    if (paintInfo.context->paintingDisabled()
55        || (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection))
56        return;
57
58    PaintInfo childPaintInfo(paintInfo);
59    GraphicsContextStateSaver stateSaver(*childPaintInfo.context);
60    childPaintInfo.applyTransform(localTransform());
61
62    if (SVGRenderSupport::isOverflowHidden(this))
63        childPaintInfo.context->clip(m_viewport);
64
65    SVGRenderingContext renderingContext;
66    bool continueRendering = true;
67    if (paintInfo.phase == PaintPhaseForeground) {
68        renderingContext.prepareToRenderSVGContent(this, childPaintInfo);
69        continueRendering = renderingContext.isRenderingPrepared();
70    }
71
72    if (continueRendering) {
73        // Paint all phases of FO elements atomically, as though the FO element established its
74        // own stacking context.
75        bool preservePhase = paintInfo.phase == PaintPhaseSelection || paintInfo.phase == PaintPhaseTextClip;
76        LayoutPoint childPoint = IntPoint();
77        childPaintInfo.phase = preservePhase ? paintInfo.phase : PaintPhaseBlockBackground;
78        RenderBlock::paint(childPaintInfo, IntPoint());
79        if (!preservePhase) {
80            childPaintInfo.phase = PaintPhaseChildBlockBackgrounds;
81            RenderBlock::paint(childPaintInfo, childPoint);
82            childPaintInfo.phase = PaintPhaseFloat;
83            RenderBlock::paint(childPaintInfo, childPoint);
84            childPaintInfo.phase = PaintPhaseForeground;
85            RenderBlock::paint(childPaintInfo, childPoint);
86            childPaintInfo.phase = PaintPhaseOutline;
87            RenderBlock::paint(childPaintInfo, childPoint);
88        }
89    }
90}
91
92LayoutRect RenderSVGForeignObject::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
93{
94    return SVGRenderSupport::clippedOverflowRectForRepaint(this, repaintContainer);
95}
96
97void RenderSVGForeignObject::computeFloatRectForRepaint(const RenderLayerModelObject* repaintContainer, FloatRect& repaintRect, bool fixed) const
98{
99    SVGRenderSupport::computeFloatRectForRepaint(this, repaintContainer, repaintRect, fixed);
100}
101
102const AffineTransform& RenderSVGForeignObject::localToParentTransform() const
103{
104    m_localToParentTransform = localTransform();
105    m_localToParentTransform.translate(m_viewport.x(), m_viewport.y());
106    return m_localToParentTransform;
107}
108
109void RenderSVGForeignObject::updateLogicalWidth()
110{
111    // FIXME: Investigate in size rounding issues
112    // FIXME: Remove unnecessary rounding when layout is off ints: webkit.org/b/63656
113    setWidth(static_cast<int>(roundf(m_viewport.width())));
114}
115
116void RenderSVGForeignObject::computeLogicalHeight(LayoutUnit, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
117{
118    // FIXME: Investigate in size rounding issues
119    // FIXME: Remove unnecessary rounding when layout is off ints: webkit.org/b/63656
120    // FIXME: Is this correct for vertical writing mode?
121    computedValues.m_extent = static_cast<int>(roundf(m_viewport.height()));
122    computedValues.m_position = logicalTop;
123}
124
125void RenderSVGForeignObject::layout()
126{
127    StackStats::LayoutCheckPoint layoutCheckPoint;
128    ASSERT(needsLayout());
129    ASSERT(!view()->layoutStateEnabled()); // RenderSVGRoot disables layoutState for the SVG rendering tree.
130
131    LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(this));
132    SVGForeignObjectElement* foreign = static_cast<SVGForeignObjectElement*>(node());
133
134    bool updateCachedBoundariesInParents = false;
135    if (m_needsTransformUpdate) {
136        m_localTransform = foreign->animatedLocalTransform();
137        m_needsTransformUpdate = false;
138        updateCachedBoundariesInParents = true;
139    }
140
141    FloatRect oldViewport = m_viewport;
142
143    // Cache viewport boundaries
144    SVGLengthContext lengthContext(foreign);
145    FloatPoint viewportLocation(foreign->x().value(lengthContext), foreign->y().value(lengthContext));
146    m_viewport = FloatRect(viewportLocation, FloatSize(foreign->width().value(lengthContext), foreign->height().value(lengthContext)));
147    if (!updateCachedBoundariesInParents)
148        updateCachedBoundariesInParents = oldViewport != m_viewport;
149
150    // Set box origin to the foreignObject x/y translation, so positioned objects in XHTML content get correct
151    // positions. A regular RenderBoxModelObject would pull this information from RenderStyle - in SVG those
152    // properties are ignored for non <svg> elements, so we mimic what happens when specifying them through CSS.
153
154    // FIXME: Investigate in location rounding issues - only affects RenderSVGForeignObject & RenderSVGText
155    setLocation(roundedIntPoint(viewportLocation));
156
157    bool layoutChanged = everHadLayout() && selfNeedsLayout();
158    RenderBlock::layout();
159    ASSERT(!needsLayout());
160
161    // If our bounds changed, notify the parents.
162    if (updateCachedBoundariesInParents)
163        RenderSVGBlock::setNeedsBoundariesUpdate();
164
165    // Invalidate all resources of this client if our layout changed.
166    if (layoutChanged)
167        SVGResourcesCache::clientLayoutChanged(this);
168
169    repainter.repaintAfterLayout();
170}
171
172bool RenderSVGForeignObject::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
173{
174    // Embedded content is drawn in the foreground phase.
175    if (hitTestAction != HitTestForeground)
176        return false;
177
178    FloatPoint localPoint = localTransform().inverse().mapPoint(pointInParent);
179
180    // Early exit if local point is not contained in clipped viewport area
181    if (SVGRenderSupport::isOverflowHidden(this) && !m_viewport.contains(localPoint))
182        return false;
183
184    // FOs establish a stacking context, so we need to hit-test all layers.
185    HitTestLocation hitTestLocation(roundedLayoutPoint(localPoint));
186    return RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestForeground)
187        || RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestFloat)
188        || RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestChildBlockBackgrounds);
189}
190
191bool RenderSVGForeignObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction)
192{
193    ASSERT_NOT_REACHED();
194    return false;
195}
196
197void RenderSVGForeignObject::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags, bool* wasFixed) const
198{
199    SVGRenderSupport::mapLocalToContainer(this, repaintContainer, transformState, wasFixed);
200}
201
202const RenderObject* RenderSVGForeignObject::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
203{
204    return SVGRenderSupport::pushMappingToContainer(this, ancestorToStopAt, geometryMap);
205}
206
207}
208
209#endif
210