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