2 * Copyright (C) 2006 Apple Computer, Inc.
3 * Copyright (C) 2009 Google, Inc.
4 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
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.
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.
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.
24 #include "core/rendering/svg/RenderSVGForeignObject.h"
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"
36 RenderSVGForeignObject::RenderSVGForeignObject(SVGForeignObjectElement* node)
37 : RenderSVGBlock(node)
38 , m_needsTransformUpdate(true)
42 RenderSVGForeignObject::~RenderSVGForeignObject()
46 bool RenderSVGForeignObject::isChildAllowed(RenderObject* child, RenderStyle* style) const
48 // Disallow arbitary SVG content. Only allow proper <svg xmlns="svgNS"> subdocuments.
49 return !child->isSVG() || child->isSVGRoot();
52 void RenderSVGForeignObject::paint(PaintInfo& paintInfo, const LayoutPoint&)
54 if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection)
57 PaintInfo childPaintInfo(paintInfo);
58 GraphicsContextStateSaver stateSaver(*childPaintInfo.context);
59 childPaintInfo.applyTransform(localTransform());
61 if (SVGRenderSupport::isOverflowHidden(this))
62 childPaintInfo.context->clip(m_viewport);
64 SVGRenderingContext renderingContext;
65 bool continueRendering = true;
66 if (paintInfo.phase == PaintPhaseForeground) {
67 renderingContext.prepareToRenderSVGContent(this, childPaintInfo);
68 continueRendering = renderingContext.isRenderingPrepared();
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());
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);
91 const AffineTransform& RenderSVGForeignObject::localToParentTransform() const
93 m_localToParentTransform = localTransform();
94 m_localToParentTransform.translate(m_viewport.x(), m_viewport.y());
95 return m_localToParentTransform;
98 void RenderSVGForeignObject::updateLogicalWidth()
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())));
105 void RenderSVGForeignObject::computeLogicalHeight(LayoutUnit, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
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;
114 void RenderSVGForeignObject::layout()
116 ASSERT(needsLayout());
118 SVGForeignObjectElement* foreign = toSVGForeignObjectElement(node());
120 bool updateCachedBoundariesInParents = false;
121 if (m_needsTransformUpdate) {
122 m_localTransform = foreign->animatedLocalTransform();
123 m_needsTransformUpdate = false;
124 updateCachedBoundariesInParents = true;
127 FloatRect oldViewport = m_viewport;
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;
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.
140 // FIXME: Investigate in location rounding issues - only affects RenderSVGForeignObject & RenderSVGText
141 setLocation(roundedIntPoint(viewportLocation));
143 bool layoutChanged = everHadLayout() && selfNeedsLayout();
144 RenderBlock::layout();
145 ASSERT(!needsLayout());
147 // If our bounds changed, notify the parents.
148 if (updateCachedBoundariesInParents)
149 RenderSVGBlock::setNeedsBoundariesUpdate();
151 // Invalidate all resources of this client if our layout changed.
153 SVGResourcesCache::clientLayoutChanged(this);
156 void RenderSVGForeignObject::mapRectToPaintInvalidationBacking(const RenderLayerModelObject* paintInvalidationContainer, LayoutRect& rect, ViewportConstrainedPosition, const PaintInvalidationState* paintInvalidationState) const
159 SVGRenderSupport::computeFloatRectForPaintInvalidation(this, paintInvalidationContainer, r, paintInvalidationState);
160 rect = enclosingLayoutRect(r);
163 bool RenderSVGForeignObject::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
165 // Embedded content is drawn in the foreground phase.
166 if (hitTestAction != HitTestForeground)
169 AffineTransform localTransform = this->localTransform();
170 if (!localTransform.isInvertible())
173 FloatPoint localPoint = localTransform.inverse().mapPoint(pointInParent);
175 // Early exit if local point is not contained in clipped viewport area
176 if (SVGRenderSupport::isOverflowHidden(this) && !m_viewport.contains(localPoint))
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);