• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2007, 2008, 2009 Rob Buis <buis@kde.org>
4  * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
5  * Copyright (C) 2009 Google, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include "config.h"
24 
25 #if ENABLE(SVG)
26 #include "RenderSVGRoot.h"
27 
28 #include "GraphicsContext.h"
29 #include "HitTestResult.h"
30 #include "RenderSVGContainer.h"
31 #include "RenderSVGResource.h"
32 #include "RenderView.h"
33 #include "SVGLength.h"
34 #include "SVGRenderSupport.h"
35 #include "SVGResources.h"
36 #include "SVGSVGElement.h"
37 #include "SVGStyledElement.h"
38 #include "TransformState.h"
39 
40 #if ENABLE(FILTERS)
41 #include "RenderSVGResourceFilter.h"
42 #endif
43 
44 using namespace std;
45 
46 namespace WebCore {
47 
RenderSVGRoot(SVGStyledElement * node)48 RenderSVGRoot::RenderSVGRoot(SVGStyledElement* node)
49     : RenderBox(node)
50     , m_isLayoutSizeChanged(false)
51     , m_needsBoundariesOrTransformUpdate(true)
52 {
53     setReplaced(true);
54 }
55 
~RenderSVGRoot()56 RenderSVGRoot::~RenderSVGRoot()
57 {
58 }
59 
computePreferredLogicalWidths()60 void RenderSVGRoot::computePreferredLogicalWidths()
61 {
62     ASSERT(preferredLogicalWidthsDirty());
63 
64     int borderAndPadding = borderAndPaddingWidth();
65     int width = computeReplacedLogicalWidth(false) + borderAndPadding;
66 
67     if (style()->maxWidth().isFixed() && style()->maxWidth().value() != undefinedLength)
68         width = min(width, style()->maxWidth().value() + (style()->boxSizing() == CONTENT_BOX ? borderAndPadding : 0));
69 
70     if (style()->width().isPercent() || (style()->width().isAuto() && style()->height().isPercent())) {
71         m_minPreferredLogicalWidth = 0;
72         m_maxPreferredLogicalWidth = width;
73     } else
74         m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = width;
75 
76     setPreferredLogicalWidthsDirty(false);
77 }
78 
computeReplacedLogicalWidth(bool includeMaxWidth) const79 int RenderSVGRoot::computeReplacedLogicalWidth(bool includeMaxWidth) const
80 {
81     int replacedWidth = RenderBox::computeReplacedLogicalWidth(includeMaxWidth);
82     if (!style()->logicalWidth().isPercent())
83         return replacedWidth;
84 
85     // FIXME: Investigate in size rounding issues
86     SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
87     return static_cast<int>(roundf(replacedWidth * svg->currentScale()));
88 }
89 
computeReplacedLogicalHeight() const90 int RenderSVGRoot::computeReplacedLogicalHeight() const
91 {
92     int replacedHeight = RenderBox::computeReplacedLogicalHeight();
93     if (!style()->logicalHeight().isPercent())
94         return replacedHeight;
95 
96     // FIXME: Investigate in size rounding issues
97     SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
98     return static_cast<int>(roundf(replacedHeight * svg->currentScale()));
99 }
100 
layout()101 void RenderSVGRoot::layout()
102 {
103     ASSERT(needsLayout());
104 
105     // Arbitrary affine transforms are incompatible with LayoutState.
106     view()->disableLayoutState();
107 
108     bool needsLayout = selfNeedsLayout();
109     LayoutRepainter repainter(*this, checkForRepaintDuringLayout() && needsLayout);
110 
111     IntSize oldSize(width(), height());
112     computeLogicalWidth();
113     computeLogicalHeight();
114     calcViewport();
115 
116     SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
117     m_isLayoutSizeChanged = svg->hasRelativeLengths() && oldSize != size();
118 
119     SVGRenderSupport::layoutChildren(this, needsLayout);
120     m_isLayoutSizeChanged = false;
121 
122     // At this point LayoutRepainter already grabbed the old bounds,
123     // recalculate them now so repaintAfterLayout() uses the new bounds.
124     if (m_needsBoundariesOrTransformUpdate) {
125         updateCachedBoundaries();
126         m_needsBoundariesOrTransformUpdate = false;
127     }
128 
129     repainter.repaintAfterLayout();
130 
131     view()->enableLayoutState();
132     setNeedsLayout(false);
133 }
134 
selfWillPaint()135 bool RenderSVGRoot::selfWillPaint()
136 {
137 #if ENABLE(FILTERS)
138     SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(this);
139     return resources && resources->filter();
140 #else
141     return false;
142 #endif
143 }
144 
paint(PaintInfo & paintInfo,int parentX,int parentY)145 void RenderSVGRoot::paint(PaintInfo& paintInfo, int parentX, int parentY)
146 {
147     if (paintInfo.context->paintingDisabled())
148         return;
149 
150     bool isVisible = style()->visibility() == VISIBLE;
151     IntPoint parentOriginInContainer(parentX, parentY);
152     IntPoint borderBoxOriginInContainer = parentOriginInContainer + parentOriginToBorderBox();
153 
154     if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseBlockBackground || paintInfo.phase == PaintPhaseChildBlockBackground) && isVisible)
155         paintBoxDecorations(paintInfo, borderBoxOriginInContainer.x(), borderBoxOriginInContainer.y());
156 
157     if (paintInfo.phase == PaintPhaseBlockBackground)
158         return;
159 
160     // An empty viewport disables rendering.  FIXME: Should we still render filters?
161     if (m_viewportSize.isEmpty())
162         return;
163 
164     // Don't paint if we don't have kids, except if we have filters we should paint those.
165     if (!firstChild() && !selfWillPaint())
166         return;
167 
168     // Make a copy of the PaintInfo because applyTransform will modify the damage rect.
169     PaintInfo childPaintInfo(paintInfo);
170     childPaintInfo.context->save();
171 
172     // Apply initial viewport clip - not affected by overflow handling
173     childPaintInfo.context->clip(overflowClipRect(borderBoxOriginInContainer.x(), borderBoxOriginInContainer.y()));
174 
175     // Convert from container offsets (html renderers) to a relative transform (svg renderers).
176     // Transform from our paint container's coordinate system to our local coords.
177     childPaintInfo.applyTransform(localToRepaintContainerTransform(parentOriginInContainer));
178 
179     bool continueRendering = true;
180     if (childPaintInfo.phase == PaintPhaseForeground)
181         continueRendering = SVGRenderSupport::prepareToRenderSVGContent(this, childPaintInfo);
182 
183     if (continueRendering)
184         RenderBox::paint(childPaintInfo, 0, 0);
185 
186     if (childPaintInfo.phase == PaintPhaseForeground)
187         SVGRenderSupport::finishRenderSVGContent(this, childPaintInfo, paintInfo.context);
188 
189     childPaintInfo.context->restore();
190 
191     if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && style()->outlineWidth() && isVisible)
192         paintOutline(paintInfo.context, borderBoxOriginInContainer.x(), borderBoxOriginInContainer.y(), width(), height());
193 }
194 
destroy()195 void RenderSVGRoot::destroy()
196 {
197     SVGResourcesCache::clientDestroyed(this);
198     RenderBox::destroy();
199 }
200 
styleWillChange(StyleDifference diff,const RenderStyle * newStyle)201 void RenderSVGRoot::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
202 {
203     if (diff == StyleDifferenceLayout)
204         setNeedsBoundariesUpdate();
205     RenderBox::styleWillChange(diff, newStyle);
206 }
207 
styleDidChange(StyleDifference diff,const RenderStyle * oldStyle)208 void RenderSVGRoot::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
209 {
210     RenderBox::styleDidChange(diff, oldStyle);
211     SVGResourcesCache::clientStyleChanged(this, diff, style());
212 }
213 
updateFromElement()214 void RenderSVGRoot::updateFromElement()
215 {
216     RenderBox::updateFromElement();
217     SVGResourcesCache::clientUpdatedFromElement(this, style());
218 }
219 
calcViewport()220 void RenderSVGRoot::calcViewport()
221 {
222     SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
223 
224     if (!svg->hasSetContainerSize()) {
225         // In the normal case of <svg> being stand-alone or in a CSSBoxModel object we use
226         // RenderBox::width()/height() (which pulls data from RenderStyle)
227         m_viewportSize = FloatSize(width(), height());
228         return;
229     }
230 
231     // In the SVGImage case grab the SVGLength values off of SVGSVGElement and use
232     // the special relativeWidthValue accessors which respect the specified containerSize
233     // FIXME: Check how SVGImage + zooming is supposed to be handled?
234     SVGLength width = svg->width();
235     SVGLength height = svg->height();
236     m_viewportSize = FloatSize(width.unitType() == LengthTypePercentage ? svg->relativeWidthValue() : width.value(svg),
237                                height.unitType() == LengthTypePercentage ? svg->relativeHeightValue() : height.value(svg));
238 }
239 
240 // RenderBox methods will expect coordinates w/o any transforms in coordinates
241 // relative to our borderBox origin.  This method gives us exactly that.
localToBorderBoxTransform() const242 AffineTransform RenderSVGRoot::localToBorderBoxTransform() const
243 {
244     IntSize borderAndPadding = borderOriginToContentBox();
245     SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
246     float scale = svg->currentScale();
247     FloatPoint translate = svg->currentTranslate();
248     AffineTransform ctm(scale, 0, 0, scale, borderAndPadding.width() + translate.x(), borderAndPadding.height() + translate.y());
249     return ctm * svg->viewBoxToViewTransform(width() / scale, height() / scale);
250 }
251 
parentOriginToBorderBox() const252 IntSize RenderSVGRoot::parentOriginToBorderBox() const
253 {
254     return IntSize(x(), y());
255 }
256 
borderOriginToContentBox() const257 IntSize RenderSVGRoot::borderOriginToContentBox() const
258 {
259     return IntSize(borderLeft() + paddingLeft(), borderTop() + paddingTop());
260 }
261 
localToRepaintContainerTransform(const IntPoint & parentOriginInContainer) const262 AffineTransform RenderSVGRoot::localToRepaintContainerTransform(const IntPoint& parentOriginInContainer) const
263 {
264     return AffineTransform::translation(parentOriginInContainer.x(), parentOriginInContainer.y()) * localToParentTransform();
265 }
266 
localToParentTransform() const267 const AffineTransform& RenderSVGRoot::localToParentTransform() const
268 {
269     IntSize parentToBorderBoxOffset = parentOriginToBorderBox();
270 
271     m_localToParentTransform = AffineTransform::translation(parentToBorderBoxOffset.width(), parentToBorderBoxOffset.height()) * localToBorderBoxTransform();
272 
273     return m_localToParentTransform;
274 }
275 
clippedOverflowRectForRepaint(RenderBoxModelObject * repaintContainer)276 IntRect RenderSVGRoot::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer)
277 {
278     return SVGRenderSupport::clippedOverflowRectForRepaint(this, repaintContainer);
279 }
280 
computeRectForRepaint(RenderBoxModelObject * repaintContainer,IntRect & repaintRect,bool fixed)281 void RenderSVGRoot::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& repaintRect, bool fixed)
282 {
283     // Apply our local transforms (except for x/y translation), then our shadow,
284     // and then call RenderBox's method to handle all the normal CSS Box model bits
285     repaintRect = localToBorderBoxTransform().mapRect(repaintRect);
286 
287     // Apply initial viewport clip - not affected by overflow settings
288     repaintRect.intersect(enclosingIntRect(FloatRect(FloatPoint(), m_viewportSize)));
289 
290     const SVGRenderStyle* svgStyle = style()->svgStyle();
291     if (const ShadowData* shadow = svgStyle->shadow())
292         shadow->adjustRectForShadow(repaintRect);
293 
294     RenderBox::computeRectForRepaint(repaintContainer, repaintRect, fixed);
295 }
296 
mapLocalToContainer(RenderBoxModelObject * repaintContainer,bool fixed,bool useTransforms,TransformState & transformState) const297 void RenderSVGRoot::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed , bool useTransforms, TransformState& transformState) const
298 {
299     ASSERT(!fixed); // We should have no fixed content in the SVG rendering tree.
300     ASSERT(useTransforms); // mapping a point through SVG w/o respecting trasnforms is useless.
301 
302     // Transform to our border box and let RenderBox transform the rest of the way.
303     transformState.applyTransform(localToBorderBoxTransform());
304     RenderBox::mapLocalToContainer(repaintContainer, fixed, useTransforms, transformState);
305 }
306 
updateCachedBoundaries()307 void RenderSVGRoot::updateCachedBoundaries()
308 {
309     m_objectBoundingBox = FloatRect();
310     m_strokeBoundingBox = FloatRect();
311     m_repaintBoundingBox = FloatRect();
312 
313     SVGRenderSupport::computeContainerBoundingBoxes(this, m_objectBoundingBox, m_strokeBoundingBox, m_repaintBoundingBox);
314     SVGRenderSupport::intersectRepaintRectWithResources(this, m_repaintBoundingBox);
315     m_repaintBoundingBox.inflate(borderAndPaddingWidth());
316 }
317 
nodeAtPoint(const HitTestRequest & request,HitTestResult & result,int x,int y,int tx,int ty,HitTestAction hitTestAction)318 bool RenderSVGRoot::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int x, int y, int tx, int ty, HitTestAction hitTestAction)
319 {
320     IntPoint pointInContainer(x, y);
321     IntSize containerToParentOffset(tx, ty);
322 
323     IntPoint pointInParent = pointInContainer - containerToParentOffset;
324     IntPoint pointInBorderBox = pointInParent - parentOriginToBorderBox();
325 
326     // Note: For now, we're ignoring hits to border and padding for <svg>
327     IntPoint pointInContentBox = pointInBorderBox - borderOriginToContentBox();
328     if (!contentBoxRect().contains(pointInContentBox))
329         return false;
330 
331     IntPoint localPoint = localToParentTransform().inverse().mapPoint(pointInParent);
332 
333     for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
334         if (child->nodeAtFloatPoint(request, result, localPoint, hitTestAction)) {
335             // FIXME: CSS/HTML assumes the local point is relative to the border box, right?
336             updateHitTestResult(result, pointInBorderBox);
337             // FIXME: nodeAtFloatPoint() doesn't handle rect-based hit tests yet.
338             result.addNodeToRectBasedTestResult(child->node(), x, y);
339             return true;
340         }
341     }
342 
343     // If we didn't early exit above, we've just hit the container <svg> element. Unlike SVG 1.1, 2nd Edition allows container elements to be hit.
344     if (hitTestAction == HitTestBlockBackground) {
345         // Only return true here, if the last hit testing phase 'BlockBackground' is executed. If we'd return true in the 'Foreground' phase,
346         // hit testing would stop immediately. For SVG only trees this doesn't matter. Though when we have a <foreignObject> subtree we need
347         // to be able to detect hits on the background of a <div> element. If we'd return true here in the 'Foreground' phase, we are not able
348         // to detect these hits anymore.
349         updateHitTestResult(result, roundedIntPoint(localPoint));
350         return true;
351     }
352 
353     return false;
354 }
355 
356 }
357 
358 #endif // ENABLE(SVG)
359