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/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"
38 RenderSVGForeignObject::RenderSVGForeignObject(SVGForeignObjectElement* node)
39 : RenderSVGBlock(node)
40 , m_needsTransformUpdate(true)
44 RenderSVGForeignObject::~RenderSVGForeignObject()
48 bool RenderSVGForeignObject::isChildAllowed(RenderObject* child, RenderStyle* style) const
50 // Disallow arbitary SVG content. Only allow proper <svg xmlns="svgNS"> subdocuments.
51 return !child->isSVG() || child->isSVGRoot();
54 void RenderSVGForeignObject::paint(PaintInfo& paintInfo, const LayoutPoint&)
56 if (paintInfo.context->paintingDisabled()
57 || (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection))
60 PaintInfo childPaintInfo(paintInfo);
61 GraphicsContextStateSaver stateSaver(*childPaintInfo.context);
62 childPaintInfo.applyTransform(localTransform());
64 if (SVGRenderSupport::isOverflowHidden(this))
65 childPaintInfo.context->clip(m_viewport);
67 SVGRenderingContext renderingContext;
68 bool continueRendering = true;
69 if (paintInfo.phase == PaintPhaseForeground) {
70 renderingContext.prepareToRenderSVGContent(this, childPaintInfo);
71 continueRendering = renderingContext.isRenderingPrepared();
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());
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);
94 const AffineTransform& RenderSVGForeignObject::localToParentTransform() const
96 m_localToParentTransform = localTransform();
97 m_localToParentTransform.translate(m_viewport.x(), m_viewport.y());
98 return m_localToParentTransform;
101 void RenderSVGForeignObject::updateLogicalWidth()
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())));
108 void RenderSVGForeignObject::computeLogicalHeight(LayoutUnit, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
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;
117 void RenderSVGForeignObject::layout()
119 ASSERT(needsLayout());
120 ASSERT(!view()->layoutStateEnabled()); // RenderSVGRoot disables layoutState for the SVG rendering tree.
122 LayoutRectRecorder recorder(*this);
123 LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(this));
124 SVGForeignObjectElement* foreign = toSVGForeignObjectElement(node());
126 bool updateCachedBoundariesInParents = false;
127 if (m_needsTransformUpdate) {
128 m_localTransform = foreign->animatedLocalTransform();
129 m_needsTransformUpdate = false;
130 updateCachedBoundariesInParents = true;
133 FloatRect oldViewport = m_viewport;
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;
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.
146 // FIXME: Investigate in location rounding issues - only affects RenderSVGForeignObject & RenderSVGText
147 setLocation(roundedIntPoint(viewportLocation));
149 bool layoutChanged = everHadLayout() && selfNeedsLayout();
150 RenderBlock::layout();
151 ASSERT(!needsLayout());
153 // If our bounds changed, notify the parents.
154 if (updateCachedBoundariesInParents)
155 RenderSVGBlock::setNeedsBoundariesUpdate();
157 // Invalidate all resources of this client if our layout changed.
159 SVGResourcesCache::clientLayoutChanged(this);
161 repainter.repaintAfterLayout();
164 bool RenderSVGForeignObject::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
166 // Embedded content is drawn in the foreground phase.
167 if (hitTestAction != HitTestForeground)
170 FloatPoint localPoint = localTransform().inverse().mapPoint(pointInParent);
172 // Early exit if local point is not contained in clipped viewport area
173 if (SVGRenderSupport::isOverflowHidden(this) && !m_viewport.contains(localPoint))
176 // FOs establish a stacking context, so we need to hit-test all layers.
177 HitTestLocation hitTestLocation(roundedLayoutPoint(localPoint));
178 return RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestForeground)
179 || RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestFloat)
180 || RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), HitTestChildBlockBackgrounds);