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) && ENABLE(SVG_FOREIGN_OBJECT)
25#include "RenderSVGForeignObject.h"
26
27#include "GraphicsContext.h"
28#include "RenderSVGResource.h"
29#include "RenderView.h"
30#include "SVGForeignObjectElement.h"
31#include "SVGRenderSupport.h"
32#include "SVGSVGElement.h"
33#include "TransformState.h"
34
35namespace WebCore {
36
37RenderSVGForeignObject::RenderSVGForeignObject(SVGForeignObjectElement* node)
38    : RenderSVGBlock(node)
39    , m_needsTransformUpdate(true)
40{
41}
42
43RenderSVGForeignObject::~RenderSVGForeignObject()
44{
45}
46
47void RenderSVGForeignObject::paint(PaintInfo& paintInfo, int, int)
48{
49    if (paintInfo.context->paintingDisabled())
50        return;
51
52    PaintInfo childPaintInfo(paintInfo);
53    childPaintInfo.context->save();
54    childPaintInfo.applyTransform(localTransform());
55
56    if (SVGRenderSupport::isOverflowHidden(this))
57        childPaintInfo.context->clip(m_viewport);
58
59    float opacity = style()->opacity();
60    if (opacity < 1.0f)
61        childPaintInfo.context->beginTransparencyLayer(opacity);
62
63    RenderBlock::paint(childPaintInfo, 0, 0);
64
65    if (opacity < 1.0f)
66        childPaintInfo.context->endTransparencyLayer();
67
68    childPaintInfo.context->restore();
69}
70
71IntRect RenderSVGForeignObject::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer)
72{
73    return SVGRenderSupport::clippedOverflowRectForRepaint(this, repaintContainer);
74}
75
76void RenderSVGForeignObject::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& repaintRect, bool fixed)
77{
78    SVGRenderSupport::computeRectForRepaint(this, repaintContainer, repaintRect, fixed);
79}
80
81const AffineTransform& RenderSVGForeignObject::localToParentTransform() const
82{
83    m_localToParentTransform = localTransform();
84    m_localToParentTransform.translate(m_viewport.x(), m_viewport.y());
85    return m_localToParentTransform;
86}
87
88void RenderSVGForeignObject::computeLogicalWidth()
89{
90    // FIXME: Investigate in size rounding issues
91    setWidth(static_cast<int>(roundf(m_viewport.width())));
92}
93
94void RenderSVGForeignObject::computeLogicalHeight()
95{
96    // FIXME: Investigate in size rounding issues
97    setHeight(static_cast<int>(roundf(m_viewport.height())));
98}
99
100void RenderSVGForeignObject::layout()
101{
102    ASSERT(needsLayout());
103    ASSERT(!view()->layoutStateEnabled()); // RenderSVGRoot disables layoutState for the SVG rendering tree.
104
105    LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
106    SVGForeignObjectElement* foreign = static_cast<SVGForeignObjectElement*>(node());
107
108    bool updateCachedBoundariesInParents = false;
109    if (m_needsTransformUpdate) {
110        m_localTransform = foreign->animatedLocalTransform();
111        m_needsTransformUpdate = false;
112        updateCachedBoundariesInParents = true;
113    }
114
115    FloatRect oldViewport = m_viewport;
116
117    // Cache viewport boundaries
118    FloatPoint viewportLocation(foreign->x().value(foreign), foreign->y().value(foreign));
119    m_viewport = FloatRect(viewportLocation, FloatSize(foreign->width().value(foreign), foreign->height().value(foreign)));
120    if (!updateCachedBoundariesInParents)
121        updateCachedBoundariesInParents = oldViewport != m_viewport;
122
123    // Set box origin to the foreignObject x/y translation, so positioned objects in XHTML content get correct
124    // positions. A regular RenderBoxModelObject would pull this information from RenderStyle - in SVG those
125    // properties are ignored for non <svg> elements, so we mimic what happens when specifying them through CSS.
126
127    // FIXME: Investigate in location rounding issues - only affects RenderSVGForeignObject & RenderSVGText
128    setLocation(roundedIntPoint(viewportLocation));
129
130    bool layoutChanged = m_everHadLayout && selfNeedsLayout();
131    RenderBlock::layout();
132    ASSERT(!needsLayout());
133
134    // If our bounds changed, notify the parents.
135    if (updateCachedBoundariesInParents)
136        RenderSVGBlock::setNeedsBoundariesUpdate();
137
138    // Invalidate all resources of this client if our layout changed.
139    if (layoutChanged)
140        SVGResourcesCache::clientLayoutChanged(this);
141
142    repainter.repaintAfterLayout();
143}
144
145bool RenderSVGForeignObject::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
146{
147    FloatPoint localPoint = localTransform().inverse().mapPoint(pointInParent);
148
149    // Early exit if local point is not contained in clipped viewport area
150    if (SVGRenderSupport::isOverflowHidden(this) && !m_viewport.contains(localPoint))
151        return false;
152
153    IntPoint roundedLocalPoint = roundedIntPoint(localPoint);
154    return RenderBlock::nodeAtPoint(request, result, roundedLocalPoint.x(), roundedLocalPoint.y(), 0, 0, hitTestAction);
155}
156
157bool RenderSVGForeignObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, int, int, int, int, HitTestAction)
158{
159    ASSERT_NOT_REACHED();
160    return false;
161}
162
163void RenderSVGForeignObject::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed, bool useTransforms, TransformState& transformState) const
164{
165    // When crawling up the hierachy starting from foreignObject child content, useTransforms may not be set to true.
166    if (!useTransforms)
167        useTransforms = true;
168    SVGRenderSupport::mapLocalToContainer(this, repaintContainer, fixed, useTransforms, transformState);
169}
170
171}
172
173#endif
174