1/*
2 * Copyright (C) 2006 Apple 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#include "RenderSVGForeignObject.h"
24
25#include "GraphicsContext.h"
26#include "HitTestResult.h"
27#include "LayoutRepainter.h"
28#include "RenderObject.h"
29#include "RenderSVGResource.h"
30#include "RenderView.h"
31#include "SVGForeignObjectElement.h"
32#include "SVGRenderingContext.h"
33#include "SVGResourcesCache.h"
34#include "TransformState.h"
35#include <wtf/IsoMallocInlines.h>
36#include <wtf/StackStats.h>
37
38namespace WebCore {
39
40WTF_MAKE_ISO_ALLOCATED_IMPL(RenderSVGForeignObject);
41
42RenderSVGForeignObject::RenderSVGForeignObject(SVGForeignObjectElement& element, RenderStyle&& style)
43 : RenderSVGBlock(element, WTFMove(style))
44{
45}
46
47RenderSVGForeignObject::~RenderSVGForeignObject() = default;
48
49SVGForeignObjectElement& RenderSVGForeignObject::foreignObjectElement() const
50{
51 return downcast<SVGForeignObjectElement>(RenderSVGBlock::graphicsElement());
52}
53
54void RenderSVGForeignObject::paint(PaintInfo& paintInfo, const LayoutPoint&)
55{
56 if (paintInfo.context().paintingDisabled())
57 return;
58
59 if (paintInfo.phase != PaintPhase::Foreground && paintInfo.phase != PaintPhase::Selection)
60 return;
61
62 PaintInfo childPaintInfo(paintInfo);
63 GraphicsContextStateSaver stateSaver(childPaintInfo.context());
64 childPaintInfo.applyTransform(localTransform());
65
66 if (SVGRenderSupport::isOverflowHidden(*this))
67 childPaintInfo.context().clip(m_viewport);
68
69 SVGRenderingContext renderingContext;
70 if (paintInfo.phase == PaintPhase::Foreground) {
71 renderingContext.prepareToRenderSVGContent(*this, childPaintInfo);
72 if (!renderingContext.isRenderingPrepared())
73 return;
74 }
75
76 LayoutPoint childPoint = IntPoint();
77 if (paintInfo.phase == PaintPhase::Selection) {
78 RenderBlock::paint(childPaintInfo, childPoint);
79 return;
80 }
81
82 // Paint all phases of FO elements atomically, as though the FO element established its
83 // own stacking context.
84 childPaintInfo.phase = PaintPhase::BlockBackground;
85 RenderBlock::paint(childPaintInfo, childPoint);
86 childPaintInfo.phase = PaintPhase::ChildBlockBackgrounds;
87 RenderBlock::paint(childPaintInfo, childPoint);
88 childPaintInfo.phase = PaintPhase::Float;
89 RenderBlock::paint(childPaintInfo, childPoint);
90 childPaintInfo.phase = PaintPhase::Foreground;
91 RenderBlock::paint(childPaintInfo, childPoint);
92 childPaintInfo.phase = PaintPhase::Outline;
93 RenderBlock::paint(childPaintInfo, childPoint);
94}
95
96LayoutRect RenderSVGForeignObject::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
97{
98 return SVGRenderSupport::clippedOverflowRectForRepaint(*this, repaintContainer);
99}
100
101Optional<FloatRect> RenderSVGForeignObject::computeFloatVisibleRectInContainer(const FloatRect& rect, const RenderLayerModelObject* container, VisibleRectContext context) const
102{
103 return SVGRenderSupport::computeFloatVisibleRectInContainer(*this, rect, container, context);
104}
105
106Optional<LayoutRect> RenderSVGForeignObject::computeVisibleRectInContainer(const LayoutRect& rect, const RenderLayerModelObject* container, VisibleRectContext context) const
107{
108 Optional<FloatRect> adjustedRect = computeFloatVisibleRectInContainer(rect, container, context);
109 if (adjustedRect)
110 return enclosingLayoutRect(*adjustedRect);
111 return WTF::nullopt;
112}
113
114const AffineTransform& RenderSVGForeignObject::localToParentTransform() const
115{
116 m_localToParentTransform = localTransform();
117 m_localToParentTransform.translate(m_viewport.location());
118 return m_localToParentTransform;
119}
120
121void RenderSVGForeignObject::updateLogicalWidth()
122{
123 // FIXME: Investigate in size rounding issues
124 // FIXME: Remove unnecessary rounding when layout is off ints: webkit.org/b/63656
125 setWidth(static_cast<int>(roundf(m_viewport.width())));
126}
127
128RenderBox::LogicalExtentComputedValues RenderSVGForeignObject::computeLogicalHeight(LayoutUnit, LayoutUnit logicalTop) const
129{
130 // FIXME: Investigate in size rounding issues
131 // FIXME: Remove unnecessary rounding when layout is off ints: webkit.org/b/63656
132 // FIXME: Is this correct for vertical writing mode?
133 return { static_cast<int>(roundf(m_viewport.height())), logicalTop, ComputedMarginValues() };
134}
135
136void RenderSVGForeignObject::layout()
137{
138 StackStats::LayoutCheckPoint layoutCheckPoint;
139 ASSERT(needsLayout());
140 ASSERT(!view().frameView().layoutContext().isPaintOffsetCacheEnabled()); // RenderSVGRoot disables paint offset cache for the SVG rendering tree.
141
142 LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(*this));
143
144 bool updateCachedBoundariesInParents = false;
145 if (m_needsTransformUpdate) {
146 m_localTransform = foreignObjectElement().animatedLocalTransform();
147 m_needsTransformUpdate = false;
148 updateCachedBoundariesInParents = true;
149 }
150
151 FloatRect oldViewport = m_viewport;
152
153 // Cache viewport boundaries
154 SVGLengthContext lengthContext(&foreignObjectElement());
155 FloatPoint viewportLocation(foreignObjectElement().x().value(lengthContext), foreignObjectElement().y().value(lengthContext));
156 m_viewport = FloatRect(viewportLocation, FloatSize(foreignObjectElement().width().value(lengthContext), foreignObjectElement().height().value(lengthContext)));
157 if (!updateCachedBoundariesInParents)
158 updateCachedBoundariesInParents = oldViewport != m_viewport;
159
160 // Set box origin to the foreignObject x/y translation, so positioned objects in XHTML content get correct
161 // positions. A regular RenderBoxModelObject would pull this information from RenderStyle - in SVG those
162 // properties are ignored for non <svg> elements, so we mimic what happens when specifying them through CSS.
163
164 // FIXME: Investigate in location rounding issues - only affects RenderSVGForeignObject & RenderSVGText
165 setLocation(roundedIntPoint(viewportLocation));
166
167 bool layoutChanged = everHadLayout() && selfNeedsLayout();
168 RenderBlock::layout();
169 ASSERT(!needsLayout());
170
171 // If our bounds changed, notify the parents.
172 if (updateCachedBoundariesInParents)
173 RenderSVGBlock::setNeedsBoundariesUpdate();
174
175 // Invalidate all resources of this client if our layout changed.
176 if (layoutChanged)
177 SVGResourcesCache::clientLayoutChanged(*this);
178
179 repainter.repaintAfterLayout();
180}
181
182bool RenderSVGForeignObject::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
183{
184 // Embedded content is drawn in the foreground phase.
185 if (hitTestAction != HitTestForeground)
186 return false;
187
188 FloatPoint localPoint = localTransform().inverse().valueOr(AffineTransform()).mapPoint(pointInParent);
189
190 // Early exit if local point is not contained in clipped viewport area
191 if (SVGRenderSupport::isOverflowHidden(*this) && !m_viewport.contains(localPoint))
192 return false;
193
194 // FOs establish a stacking context, so we need to hit-test all layers.
195 HitTestLocation hitTestLocation(localPoint);
196 return RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestForeground)
197 || RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestFloat)
198 || RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestChildBlockBackgrounds);
199}
200
201bool RenderSVGForeignObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction)
202{
203 ASSERT_NOT_REACHED();
204 return false;
205}
206
207void RenderSVGForeignObject::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags, bool* wasFixed) const
208{
209 SVGRenderSupport::mapLocalToContainer(*this, repaintContainer, transformState, wasFixed);
210}
211
212const RenderObject* RenderSVGForeignObject::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
213{
214 return SVGRenderSupport::pushMappingToContainer(*this, ancestorToStopAt, geometryMap);
215}
216
217}
218