1/*
2 * Copyright (C) 2006 Apple Computer, Inc.
3 * Copyright (C) 2009 Google, Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "config.h"
23
24#if ENABLE(SVG) && ENABLE(SVG_FOREIGN_OBJECT)
25#include "RenderForeignObject.h"
26
27#include "GraphicsContext.h"
28#include "RenderView.h"
29#include "SVGForeignObjectElement.h"
30#include "SVGLength.h"
31#include "SVGRenderSupport.h"
32#include "SVGTransformList.h"
33
34namespace WebCore {
35
36RenderForeignObject::RenderForeignObject(SVGForeignObjectElement* node)
37    : RenderSVGBlock(node)
38{
39}
40
41FloatPoint RenderForeignObject::translationForAttributes() const
42{
43    SVGForeignObjectElement* foreign = static_cast<SVGForeignObjectElement*>(node());
44    return FloatPoint(foreign->x().value(foreign), foreign->y().value(foreign));
45}
46
47void RenderForeignObject::paint(PaintInfo& paintInfo, int, int)
48{
49    if (paintInfo.context->paintingDisabled())
50        return;
51
52    // Copy the paint info so that modifications to the damage rect do not affect callers
53    PaintInfo childPaintInfo = paintInfo;
54    childPaintInfo.context->save();
55    applyTransformToPaintInfo(childPaintInfo, localToParentTransform());
56    childPaintInfo.context->clip(clipRect(0, 0));
57
58    float opacity = style()->opacity();
59    if (opacity < 1.0f)
60        childPaintInfo.context->beginTransparencyLayer(opacity);
61
62    RenderBlock::paint(childPaintInfo, 0, 0);
63
64    if (opacity < 1.0f)
65        childPaintInfo.context->endTransparencyLayer();
66
67    childPaintInfo.context->restore();
68}
69
70FloatRect RenderForeignObject::objectBoundingBox() const
71{
72    return borderBoxRect();
73}
74
75FloatRect RenderForeignObject::repaintRectInLocalCoordinates() const
76{
77    // HACK: to maintain historical LayoutTest results for now.
78    // RenderForeignObject is a RenderBlock (not a RenderSVGModelObject) so this
79    // should not affect repaint correctness.  But it should really be:
80    // return borderBoxRect();
81    return FloatRect();
82}
83
84void RenderForeignObject::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& rect, bool fixed)
85{
86    rect = localToParentTransform().mapRect(rect);
87    style()->svgStyle()->inflateForShadow(rect);
88    RenderBlock::computeRectForRepaint(repaintContainer, rect, fixed);
89}
90
91const AffineTransform& RenderForeignObject::localToParentTransform() const
92{
93    FloatPoint attributeTranslation(translationForAttributes());
94    m_localToParentTransform = localTransform().translateRight(attributeTranslation.x(), attributeTranslation.y());
95    return m_localToParentTransform;
96}
97
98void RenderForeignObject::layout()
99{
100    ASSERT(needsLayout());
101    ASSERT(!view()->layoutStateEnabled()); // RenderSVGRoot disables layoutState for the SVG rendering tree.
102
103    LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
104    m_localTransform = static_cast<SVGForeignObjectElement*>(node())->animatedLocalTransform();
105
106    RenderBlock::layout();
107    repainter.repaintAfterLayout();
108
109    setNeedsLayout(false);
110}
111
112bool RenderForeignObject::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
113{
114    FloatPoint localPoint = localToParentTransform().inverse().mapPoint(pointInParent);
115    return RenderBlock::nodeAtPoint(request, result, static_cast<int>(localPoint.x()), static_cast<int>(localPoint.y()), 0, 0, hitTestAction);
116}
117
118bool RenderForeignObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, int, int, int, int, HitTestAction)
119{
120    ASSERT_NOT_REACHED();
121    return false;
122}
123
124void RenderForeignObject::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed , bool useTransforms, TransformState& transformState) const
125{
126    SVGRenderBase::mapLocalToContainer(this, repaintContainer, fixed, useTransforms, transformState);
127}
128
129} // namespace WebCore
130
131#endif // ENABLE(SVG) && ENABLE(SVG_FOREIGN_OBJECT)
132