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