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#include "core/rendering/svg/RenderSVGForeignObject.h"
25
26#include "core/rendering/HitTestResult.h"
27#include "core/rendering/LayoutRectRecorder.h"
28#include "core/rendering/LayoutRepainter.h"
29#include "core/rendering/RenderView.h"
30#include "core/rendering/svg/SVGRenderSupport.h"
31#include "core/rendering/svg/SVGRenderingContext.h"
32#include "core/rendering/svg/SVGResourcesCache.h"
33#include "core/svg/SVGForeignObjectElement.h"
34#include "platform/graphics/GraphicsContextStateSaver.h"
35
36namespace WebCore {
37
38RenderSVGForeignObject::RenderSVGForeignObject(SVGForeignObjectElement* node)
39    : RenderSVGBlock(node)
40    , m_needsTransformUpdate(true)
41{
42}
43
44RenderSVGForeignObject::~RenderSVGForeignObject()
45{
46}
47
48void RenderSVGForeignObject::paint(PaintInfo& paintInfo, const LayoutPoint&)
49{
50    if (paintInfo.context->paintingDisabled()
51        || (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection))
52        return;
53
54    PaintInfo childPaintInfo(paintInfo);
55    GraphicsContextStateSaver stateSaver(*childPaintInfo.context);
56    childPaintInfo.applyTransform(localTransform());
57
58    if (SVGRenderSupport::isOverflowHidden(this))
59        childPaintInfo.context->clip(m_viewport);
60
61    SVGRenderingContext renderingContext;
62    bool continueRendering = true;
63    if (paintInfo.phase == PaintPhaseForeground) {
64        renderingContext.prepareToRenderSVGContent(this, childPaintInfo);
65        continueRendering = renderingContext.isRenderingPrepared();
66    }
67
68    if (continueRendering) {
69        // Paint all phases of FO elements atomically, as though the FO element established its
70        // own stacking context.
71        bool preservePhase = paintInfo.phase == PaintPhaseSelection || paintInfo.phase == PaintPhaseTextClip;
72        LayoutPoint childPoint = IntPoint();
73        childPaintInfo.phase = preservePhase ? paintInfo.phase : PaintPhaseBlockBackground;
74        RenderBlock::paint(childPaintInfo, IntPoint());
75        if (!preservePhase) {
76            childPaintInfo.phase = PaintPhaseChildBlockBackgrounds;
77            RenderBlock::paint(childPaintInfo, childPoint);
78            childPaintInfo.phase = PaintPhaseFloat;
79            RenderBlock::paint(childPaintInfo, childPoint);
80            childPaintInfo.phase = PaintPhaseForeground;
81            RenderBlock::paint(childPaintInfo, childPoint);
82            childPaintInfo.phase = PaintPhaseOutline;
83            RenderBlock::paint(childPaintInfo, childPoint);
84        }
85    }
86}
87
88LayoutRect RenderSVGForeignObject::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
89{
90    return SVGRenderSupport::clippedOverflowRectForRepaint(this, repaintContainer);
91}
92
93void RenderSVGForeignObject::computeFloatRectForRepaint(const RenderLayerModelObject* repaintContainer, FloatRect& repaintRect, bool fixed) const
94{
95    SVGRenderSupport::computeFloatRectForRepaint(this, repaintContainer, repaintRect, fixed);
96}
97
98const AffineTransform& RenderSVGForeignObject::localToParentTransform() const
99{
100    m_localToParentTransform = localTransform();
101    m_localToParentTransform.translate(m_viewport.x(), m_viewport.y());
102    return m_localToParentTransform;
103}
104
105void RenderSVGForeignObject::updateLogicalWidth()
106{
107    // FIXME: Investigate in size rounding issues
108    // FIXME: Remove unnecessary rounding when layout is off ints: webkit.org/b/63656
109    setWidth(static_cast<int>(roundf(m_viewport.width())));
110}
111
112void RenderSVGForeignObject::computeLogicalHeight(LayoutUnit, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
113{
114    // FIXME: Investigate in size rounding issues
115    // FIXME: Remove unnecessary rounding when layout is off ints: webkit.org/b/63656
116    // FIXME: Is this correct for vertical writing mode?
117    computedValues.m_extent = static_cast<int>(roundf(m_viewport.height()));
118    computedValues.m_position = logicalTop;
119}
120
121void RenderSVGForeignObject::layout()
122{
123    ASSERT(needsLayout());
124    ASSERT(!view()->layoutStateEnabled()); // RenderSVGRoot disables layoutState for the SVG rendering tree.
125
126    LayoutRectRecorder recorder(*this);
127    LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(this));
128    SVGForeignObjectElement* foreign = toSVGForeignObjectElement(node());
129
130    bool updateCachedBoundariesInParents = false;
131    if (m_needsTransformUpdate) {
132        m_localTransform = foreign->animatedLocalTransform();
133        m_needsTransformUpdate = false;
134        updateCachedBoundariesInParents = true;
135    }
136
137    FloatRect oldViewport = m_viewport;
138
139    // Cache viewport boundaries
140    SVGLengthContext lengthContext(foreign);
141    FloatPoint viewportLocation(foreign->xCurrentValue().value(lengthContext), foreign->yCurrentValue().value(lengthContext));
142    m_viewport = FloatRect(viewportLocation, FloatSize(foreign->widthCurrentValue().value(lengthContext), foreign->heightCurrentValue().value(lengthContext)));
143    if (!updateCachedBoundariesInParents)
144        updateCachedBoundariesInParents = oldViewport != m_viewport;
145
146    // Set box origin to the foreignObject x/y translation, so positioned objects in XHTML content get correct
147    // positions. A regular RenderBoxModelObject would pull this information from RenderStyle - in SVG those
148    // properties are ignored for non <svg> elements, so we mimic what happens when specifying them through CSS.
149
150    // FIXME: Investigate in location rounding issues - only affects RenderSVGForeignObject & RenderSVGText
151    setLocation(roundedIntPoint(viewportLocation));
152
153    bool layoutChanged = everHadLayout() && selfNeedsLayout();
154    RenderBlock::layout();
155    ASSERT(!needsLayout());
156
157    // If our bounds changed, notify the parents.
158    if (updateCachedBoundariesInParents)
159        RenderSVGBlock::setNeedsBoundariesUpdate();
160
161    // Invalidate all resources of this client if our layout changed.
162    if (layoutChanged)
163        SVGResourcesCache::clientLayoutChanged(this);
164
165    repainter.repaintAfterLayout();
166}
167
168bool RenderSVGForeignObject::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
169{
170    // Embedded content is drawn in the foreground phase.
171    if (hitTestAction != HitTestForeground)
172        return false;
173
174    FloatPoint localPoint = localTransform().inverse().mapPoint(pointInParent);
175
176    // Early exit if local point is not contained in clipped viewport area
177    if (SVGRenderSupport::isOverflowHidden(this) && !m_viewport.contains(localPoint))
178        return false;
179
180    // FOs establish a stacking context, so we need to hit-test all layers.
181    HitTestLocation hitTestLocation(roundedLayoutPoint(localPoint));
182    return RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestForeground)
183        || RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestFloat)
184        || RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestChildBlockBackgrounds);
185}
186
187bool RenderSVGForeignObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction)
188{
189    ASSERT_NOT_REACHED();
190    return false;
191}
192
193void RenderSVGForeignObject::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags, bool* wasFixed) const
194{
195    SVGRenderSupport::mapLocalToContainer(this, repaintContainer, transformState, wasFixed);
196}
197
198const RenderObject* RenderSVGForeignObject::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
199{
200    return SVGRenderSupport::pushMappingToContainer(this, ancestorToStopAt, geometryMap);
201}
202
203}
204