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/paint/SVGForeignObjectPainter.h"
27#include "core/rendering/HitTestResult.h"
28#include "core/rendering/RenderView.h"
29#include "core/rendering/svg/SVGRenderSupport.h"
30#include "core/rendering/svg/SVGResourcesCache.h"
31#include "core/svg/SVGForeignObjectElement.h"
32
33namespace blink {
34
35RenderSVGForeignObject::RenderSVGForeignObject(SVGForeignObjectElement* node)
36    : RenderSVGBlock(node)
37    , m_needsTransformUpdate(true)
38{
39}
40
41RenderSVGForeignObject::~RenderSVGForeignObject()
42{
43}
44
45bool RenderSVGForeignObject::isChildAllowed(RenderObject* child, RenderStyle* style) const
46{
47    // Disallow arbitary SVG content. Only allow proper <svg xmlns="svgNS"> subdocuments.
48    return !child->isSVG() || child->isSVGRoot();
49}
50
51void RenderSVGForeignObject::paint(PaintInfo& paintInfo, const LayoutPoint&)
52{
53    SVGForeignObjectPainter(*this).paint(paintInfo);
54}
55
56const AffineTransform& RenderSVGForeignObject::localToParentTransform() const
57{
58    m_localToParentTransform = localTransform();
59    m_localToParentTransform.translate(m_viewport.x(), m_viewport.y());
60    return m_localToParentTransform;
61}
62
63void RenderSVGForeignObject::updateLogicalWidth()
64{
65    // FIXME: Investigate in size rounding issues
66    // FIXME: Remove unnecessary rounding when layout is off ints: webkit.org/b/63656
67    setWidth(static_cast<int>(roundf(m_viewport.width())));
68}
69
70void RenderSVGForeignObject::computeLogicalHeight(LayoutUnit, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
71{
72    // FIXME: Investigate in size rounding issues
73    // FIXME: Remove unnecessary rounding when layout is off ints: webkit.org/b/63656
74    // FIXME: Is this correct for vertical writing mode?
75    computedValues.m_extent = static_cast<int>(roundf(m_viewport.height()));
76    computedValues.m_position = logicalTop;
77}
78
79void RenderSVGForeignObject::layout()
80{
81    ASSERT(needsLayout());
82
83    SVGForeignObjectElement* foreign = toSVGForeignObjectElement(node());
84
85    bool updateCachedBoundariesInParents = false;
86    if (m_needsTransformUpdate) {
87        m_localTransform = foreign->animatedLocalTransform();
88        m_needsTransformUpdate = false;
89        updateCachedBoundariesInParents = true;
90    }
91
92    FloatRect oldViewport = m_viewport;
93
94    // Cache viewport boundaries
95    SVGLengthContext lengthContext(foreign);
96    FloatPoint viewportLocation(foreign->x()->currentValue()->value(lengthContext), foreign->y()->currentValue()->value(lengthContext));
97    m_viewport = FloatRect(viewportLocation, FloatSize(foreign->width()->currentValue()->value(lengthContext), foreign->height()->currentValue()->value(lengthContext)));
98    if (!updateCachedBoundariesInParents)
99        updateCachedBoundariesInParents = oldViewport != m_viewport;
100
101    // Set box origin to the foreignObject x/y translation, so positioned objects in XHTML content get correct
102    // positions. A regular RenderBoxModelObject would pull this information from RenderStyle - in SVG those
103    // properties are ignored for non <svg> elements, so we mimic what happens when specifying them through CSS.
104
105    // FIXME: Investigate in location rounding issues - only affects RenderSVGForeignObject & RenderSVGText
106    setLocation(roundedIntPoint(viewportLocation));
107
108    bool layoutChanged = everHadLayout() && selfNeedsLayout();
109    RenderBlock::layout();
110    ASSERT(!needsLayout());
111
112    // If our bounds changed, notify the parents.
113    if (updateCachedBoundariesInParents)
114        RenderSVGBlock::setNeedsBoundariesUpdate();
115
116    // Invalidate all resources of this client if our layout changed.
117    if (layoutChanged)
118        SVGResourcesCache::clientLayoutChanged(this);
119}
120
121void RenderSVGForeignObject::mapRectToPaintInvalidationBacking(const RenderLayerModelObject* paintInvalidationContainer, LayoutRect& rect, const PaintInvalidationState* paintInvalidationState) const
122{
123    FloatRect r(rect);
124    r.inflate(style()->outlineWidth());
125    SVGRenderSupport::computeFloatRectForPaintInvalidation(this, paintInvalidationContainer, r, paintInvalidationState);
126    rect = enclosingLayoutRect(r);
127}
128
129bool RenderSVGForeignObject::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
130{
131    // Embedded content is drawn in the foreground phase.
132    if (hitTestAction != HitTestForeground)
133        return false;
134
135    AffineTransform localTransform = this->localTransform();
136    if (!localTransform.isInvertible())
137        return false;
138
139    FloatPoint localPoint = localTransform.inverse().mapPoint(pointInParent);
140
141    // Early exit if local point is not contained in clipped viewport area
142    if (SVGRenderSupport::isOverflowHidden(this) && !m_viewport.contains(localPoint))
143        return false;
144
145    // FOs establish a stacking context, so we need to hit-test all layers.
146    HitTestLocation hitTestLocation(roundedLayoutPoint(localPoint));
147    return RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestForeground)
148        || RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestFloat)
149        || RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestChildBlockBackgrounds);
150}
151
152}
153