Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: Source/core/rendering/svg/RenderSVGRoot.cpp

Issue 933953003: Move the remaining rendering/svg/RenderSVG* files to layout. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/rendering/svg/RenderSVGRoot.h ('k') | Source/core/svg/SVGLengthContext.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include "config.h"
25
26 #include "core/rendering/svg/RenderSVGRoot.h"
27
28 #include "core/frame/LocalFrame.h"
29 #include "core/layout/HitTestResult.h"
30 #include "core/layout/Layer.h"
31 #include "core/layout/LayoutPart.h"
32 #include "core/layout/svg/SVGLayoutSupport.h"
33 #include "core/layout/svg/SVGResourcesCache.h"
34 #include "core/paint/SVGRootPainter.h"
35 #include "core/rendering/RenderView.h"
36 #include "core/svg/SVGElement.h"
37 #include "core/svg/SVGSVGElement.h"
38 #include "core/svg/graphics/SVGImage.h"
39 #include "platform/LengthFunctions.h"
40
41 namespace blink {
42
43 RenderSVGRoot::RenderSVGRoot(SVGElement* node)
44 : LayoutReplaced(node)
45 , m_objectBoundingBoxValid(false)
46 , m_isLayoutSizeChanged(false)
47 , m_needsBoundariesOrTransformUpdate(true)
48 , m_hasBoxDecorationBackground(false)
49 , m_hasNonIsolatedBlendingDescendants(false)
50 , m_hasNonIsolatedBlendingDescendantsDirty(false)
51 {
52 }
53
54 RenderSVGRoot::~RenderSVGRoot()
55 {
56 }
57
58 void RenderSVGRoot::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, d ouble& intrinsicRatio) const
59 {
60 // Spec: http://www.w3.org/TR/SVG/coords.html#IntrinsicSizing
61 // SVG needs to specify how to calculate some intrinsic sizing properties to enable inclusion within other languages.
62 // The intrinsic width and height of the viewport of SVG content must be det ermined from the 'width' and 'height' attributes.
63 SVGSVGElement* svg = toSVGSVGElement(node());
64 ASSERT(svg);
65
66 // The intrinsic aspect ratio of the viewport of SVG content is necessary fo r example, when including SVG from an 'object'
67 // element in HTML styled with CSS. It is possible (indeed, common) for an S VG graphic to have an intrinsic aspect ratio but
68 // not to have an intrinsic width or height. The intrinsic aspect ratio must be calculated based upon the following rules:
69 // - The aspect ratio is calculated by dividing a width by a height.
70 // - If the 'width' and 'height' of the rootmost 'svg' element are both spec ified with unit identifiers (in, mm, cm, pt, pc,
71 // px, em, ex) or in user units, then the aspect ratio is calculated from the 'width' and 'height' attributes after
72 // resolving both values to user units.
73 intrinsicSize.setWidth(floatValueForLength(svg->intrinsicWidth(), 0));
74 intrinsicSize.setHeight(floatValueForLength(svg->intrinsicHeight(), 0));
75
76 if (!intrinsicSize.isEmpty()) {
77 intrinsicRatio = intrinsicSize.width() / static_cast<double>(intrinsicSi ze.height());
78 } else {
79 // - If either/both of the 'width' and 'height' of the rootmost 'svg' el ement are in percentage units (or omitted), the
80 // aspect ratio is calculated from the width and height values of the 'viewBox' specified for the current SVG document
81 // fragment. If the 'viewBox' is not correctly specified, or set to 'n one', the intrinsic aspect ratio cannot be
82 // calculated and is considered unspecified.
83 FloatSize viewBoxSize = svg->viewBox()->currentValue()->value().size();
84 if (!viewBoxSize.isEmpty()) {
85 // The viewBox can only yield an intrinsic ratio, not an intrinsic s ize.
86 intrinsicRatio = viewBoxSize.width() / static_cast<double>(viewBoxSi ze.height());
87 }
88 }
89 }
90
91 bool RenderSVGRoot::isEmbeddedThroughSVGImage() const
92 {
93 return SVGImage::isInSVGImage(toSVGSVGElement(node()));
94 }
95
96 bool RenderSVGRoot::isEmbeddedThroughFrameContainingSVGDocument() const
97 {
98 if (!node())
99 return false;
100
101 LocalFrame* frame = node()->document().frame();
102 if (!frame)
103 return false;
104
105 // If our frame has an owner renderer, we're embedded through eg. object/emb ed/iframe,
106 // but we only negotiate if we're in an SVG document inside a embedded objec t (object/embed).
107 if (!frame->ownerRenderer() || !frame->ownerRenderer()->isEmbeddedObject())
108 return false;
109 return frame->document()->isSVGDocument();
110 }
111
112 static inline LayoutUnit resolveLengthAttributeForSVG(const Length& length, floa t scale, float maxSize)
113 {
114 return static_cast<LayoutUnit>(valueForLength(length, maxSize) * (length.isF ixed() ? scale : 1));
115 }
116
117 LayoutUnit RenderSVGRoot::computeReplacedLogicalWidth(ShouldComputePreferred sho uldComputePreferred) const
118 {
119 SVGSVGElement* svg = toSVGSVGElement(node());
120 ASSERT(svg);
121
122 // When we're embedded through SVGImage (border-image/background-image/<html :img>/...) we're forced to resize to a specific size.
123 if (!m_containerSize.isEmpty())
124 return m_containerSize.width();
125
126 if (isEmbeddedThroughFrameContainingSVGDocument())
127 return containingBlock()->availableLogicalWidth();
128
129 if (style()->logicalWidth().isSpecified() || style()->logicalMaxWidth().isSp ecified())
130 return LayoutReplaced::computeReplacedLogicalWidth(shouldComputePreferre d);
131
132 if (svg->hasIntrinsicWidth())
133 return resolveLengthAttributeForSVG(svg->intrinsicWidth(), style()->effe ctiveZoom(), containingBlock()->availableLogicalWidth().toFloat());
134
135 // SVG embedded via SVGImage (background-image/border-image/etc) / Inline SV G.
136 return LayoutReplaced::computeReplacedLogicalWidth(shouldComputePreferred);
137 }
138
139 LayoutUnit RenderSVGRoot::computeReplacedLogicalHeight() const
140 {
141 SVGSVGElement* svg = toSVGSVGElement(node());
142 ASSERT(svg);
143
144 // When we're embedded through SVGImage (border-image/background-image/<html :img>/...) we're forced to resize to a specific size.
145 if (!m_containerSize.isEmpty())
146 return m_containerSize.height();
147
148 if (isEmbeddedThroughFrameContainingSVGDocument())
149 return containingBlock()->availableLogicalHeight(IncludeMarginBorderPadd ing);
150
151 if (style()->logicalHeight().isSpecified() || style()->logicalMaxHeight().is Specified())
152 return LayoutReplaced::computeReplacedLogicalHeight();
153
154 if (svg->hasIntrinsicHeight())
155 return resolveLengthAttributeForSVG(svg->intrinsicHeight(), style()->eff ectiveZoom(), containingBlock()->availableLogicalHeight(IncludeMarginBorderPaddi ng).toFloat());
156
157 // SVG embedded via SVGImage (background-image/border-image/etc) / Inline SV G.
158 return LayoutReplaced::computeReplacedLogicalHeight();
159 }
160
161 void RenderSVGRoot::layout()
162 {
163 ASSERT(needsLayout());
164
165 bool needsLayout = selfNeedsLayout();
166
167 LayoutSize oldSize = size();
168 updateLogicalWidth();
169 updateLogicalHeight();
170 buildLocalToBorderBoxTransform();
171
172 SVGLayoutSupport::layoutResourcesIfNeeded(this);
173
174 SVGSVGElement* svg = toSVGSVGElement(node());
175 ASSERT(svg);
176 m_isLayoutSizeChanged = needsLayout || (svg->hasRelativeLengths() && oldSize != size());
177 SVGLayoutSupport::layoutChildren(this, needsLayout || SVGLayoutSupport::filt ersForceContainerLayout(this));
178
179 if (m_needsBoundariesOrTransformUpdate) {
180 updateCachedBoundaries();
181 m_needsBoundariesOrTransformUpdate = false;
182 }
183
184 m_overflow.clear();
185 addVisualEffectOverflow();
186
187 if (!shouldApplyViewportClip()) {
188 FloatRect contentPaintInvalidationRect = paintInvalidationRectInLocalCoo rdinates();
189 contentPaintInvalidationRect = m_localToBorderBoxTransform.mapRect(conte ntPaintInvalidationRect);
190 addVisualOverflow(enclosingLayoutRect(contentPaintInvalidationRect));
191 }
192
193 updateLayerTransformAfterLayout();
194 m_hasBoxDecorationBackground = isDocumentElement() ? calculateHasBoxDecorati ons() : hasBoxDecorationBackground();
195 invalidateBackgroundObscurationStatus();
196
197 clearNeedsLayout();
198 }
199
200 bool RenderSVGRoot::shouldApplyViewportClip() const
201 {
202 // the outermost svg is clipped if auto, and svg document roots are always c lipped
203 // When the svg is stand-alone (isDocumentElement() == true) the viewport cl ipping should always
204 // be applied, noting that the window scrollbars should be hidden if overflo w=hidden.
205 return style()->overflowX() == OHIDDEN
206 || style()->overflowX() == OAUTO
207 || style()->overflowX() == OSCROLL
208 || this->isDocumentElement();
209 }
210
211 void RenderSVGRoot::paintReplaced(const PaintInfo& paintInfo, const LayoutPoint& paintOffset)
212 {
213 SVGRootPainter(*this).paint(paintInfo, paintOffset);
214 }
215
216 void RenderSVGRoot::willBeDestroyed()
217 {
218 RenderBlock::removePercentHeightDescendant(const_cast<RenderSVGRoot*>(this)) ;
219
220 SVGResourcesCache::clientDestroyed(this);
221 LayoutReplaced::willBeDestroyed();
222 }
223
224 void RenderSVGRoot::styleDidChange(StyleDifference diff, const LayoutStyle* oldS tyle)
225 {
226 if (diff.needsFullLayout())
227 setNeedsBoundariesUpdate();
228 if (diff.needsPaintInvalidation()) {
229 // Box decorations may have appeared/disappeared - recompute status.
230 m_hasBoxDecorationBackground = calculateHasBoxDecorations();
231 }
232
233 LayoutReplaced::styleDidChange(diff, oldStyle);
234 SVGResourcesCache::clientStyleChanged(this, diff, styleRef());
235 }
236
237 bool RenderSVGRoot::isChildAllowed(LayoutObject* child, const LayoutStyle&) cons t
238 {
239 return child->isSVG() && !(child->isSVGInline() || child->isSVGInlineText()) ;
240 }
241
242 void RenderSVGRoot::addChild(LayoutObject* child, LayoutObject* beforeChild)
243 {
244 LayoutReplaced::addChild(child, beforeChild);
245 SVGResourcesCache::clientWasAddedToTree(child, child->styleRef());
246
247 bool shouldIsolateDescendants = (child->isBlendingAllowed() && child->style( )->hasBlendMode()) || child->hasNonIsolatedBlendingDescendants();
248 if (shouldIsolateDescendants)
249 descendantIsolationRequirementsChanged(DescendantIsolationRequired);
250 }
251
252 void RenderSVGRoot::removeChild(LayoutObject* child)
253 {
254 SVGResourcesCache::clientWillBeRemovedFromTree(child);
255 LayoutReplaced::removeChild(child);
256
257 bool hadNonIsolatedDescendants = (child->isBlendingAllowed() && child->style ()->hasBlendMode()) || child->hasNonIsolatedBlendingDescendants();
258 if (hadNonIsolatedDescendants)
259 descendantIsolationRequirementsChanged(DescendantIsolationNeedsUpdate);
260 }
261
262 bool RenderSVGRoot::hasNonIsolatedBlendingDescendants() const
263 {
264 if (m_hasNonIsolatedBlendingDescendantsDirty) {
265 m_hasNonIsolatedBlendingDescendants = SVGLayoutSupport::computeHasNonIso latedBlendingDescendants(this);
266 m_hasNonIsolatedBlendingDescendantsDirty = false;
267 }
268 return m_hasNonIsolatedBlendingDescendants;
269 }
270
271 void RenderSVGRoot::descendantIsolationRequirementsChanged(DescendantIsolationSt ate state)
272 {
273 switch (state) {
274 case DescendantIsolationRequired:
275 m_hasNonIsolatedBlendingDescendants = true;
276 m_hasNonIsolatedBlendingDescendantsDirty = false;
277 break;
278 case DescendantIsolationNeedsUpdate:
279 m_hasNonIsolatedBlendingDescendantsDirty = true;
280 break;
281 }
282 }
283
284 void RenderSVGRoot::insertedIntoTree()
285 {
286 LayoutReplaced::insertedIntoTree();
287 SVGResourcesCache::clientWasAddedToTree(this, styleRef());
288 }
289
290 void RenderSVGRoot::willBeRemovedFromTree()
291 {
292 SVGResourcesCache::clientWillBeRemovedFromTree(this);
293 LayoutReplaced::willBeRemovedFromTree();
294 }
295
296 // RenderBox methods will expect coordinates w/o any transforms in coordinates
297 // relative to our borderBox origin. This method gives us exactly that.
298 void RenderSVGRoot::buildLocalToBorderBoxTransform()
299 {
300 SVGSVGElement* svg = toSVGSVGElement(node());
301 ASSERT(svg);
302 float scale = style()->effectiveZoom();
303 FloatPoint translate = svg->currentTranslate();
304 LayoutSize borderAndPadding(borderLeft() + paddingLeft(), borderTop() + padd ingTop());
305 m_localToBorderBoxTransform = svg->viewBoxToViewTransform(contentWidth() / s cale, contentHeight() / scale);
306 AffineTransform viewToBorderBoxTransform(scale, 0, 0, scale, borderAndPaddin g.width() + translate.x(), borderAndPadding.height() + translate.y());
307 if (viewToBorderBoxTransform.isIdentity())
308 return;
309 m_localToBorderBoxTransform = viewToBorderBoxTransform * m_localToBorderBoxT ransform;
310 }
311
312 const AffineTransform& RenderSVGRoot::localToParentTransform() const
313 {
314 // Slightly optimized version of m_localToParentTransform = AffineTransform: :translation(x(), y()) * m_localToBorderBoxTransform;
315 m_localToParentTransform = m_localToBorderBoxTransform;
316 if (location().x())
317 m_localToParentTransform.setE(m_localToParentTransform.e() + roundToInt( location().x()));
318 if (location().y())
319 m_localToParentTransform.setF(m_localToParentTransform.f() + roundToInt( location().y()));
320 return m_localToParentTransform;
321 }
322
323 LayoutRect RenderSVGRoot::clippedOverflowRectForPaintInvalidation(const LayoutLa yerModelObject* paintInvalidationContainer, const PaintInvalidationState* paintI nvalidationState) const
324 {
325 // This is an open-coded aggregate of SVGLayoutSupport::clippedOverflowRectF orPaintInvalidation,
326 // RenderSVGRoot::mapRectToPaintInvalidationBacking and LayoutReplaced::clip pedOverflowRectForPaintInvalidation.
327 // The reason for this is to optimize/minimize the paint invalidation rect w hen the box is not "decorated"
328 // (does not have background/border/etc.)
329
330 // Return early for any cases where we don't actually paint.
331 if (style()->visibility() != VISIBLE && !enclosingLayer()->hasVisibleContent ())
332 return LayoutRect();
333
334 // Compute the paint invalidation rect of the content of the SVG in the bord er-box coordinate space.
335 FloatRect contentPaintInvalidationRect = paintInvalidationRectInLocalCoordin ates();
336 contentPaintInvalidationRect = m_localToBorderBoxTransform.mapRect(contentPa intInvalidationRect);
337
338 // Apply initial viewport clip, overflow:visible content is added to visualO verflow
339 // but the most common case is that overflow is hidden, so always intersect.
340 contentPaintInvalidationRect.intersect(pixelSnappedBorderBoxRect());
341
342 LayoutRect paintInvalidationRect = enclosingLayoutRect(contentPaintInvalidat ionRect);
343 // If the box is decorated or is overflowing, extend it to include the borde r-box and overflow.
344 if (m_hasBoxDecorationBackground || hasRenderOverflow()) {
345 // The selectionRect can project outside of the overflowRect, so take th eir union
346 // for paint invalidation to avoid selection painting glitches.
347 LayoutRect decoratedPaintInvalidationRect = unionRect(localSelectionRect (false), visualOverflowRect());
348 paintInvalidationRect.unite(decoratedPaintInvalidationRect);
349 }
350
351 // Compute the paint invalidation rect in the parent coordinate space.
352 LayoutRect rect = enclosingIntRect(paintInvalidationRect);
353 LayoutReplaced::mapRectToPaintInvalidationBacking(paintInvalidationContainer , rect, paintInvalidationState);
354 return rect;
355 }
356
357 void RenderSVGRoot::mapRectToPaintInvalidationBacking(const LayoutLayerModelObje ct* paintInvalidationContainer, LayoutRect& rect, const PaintInvalidationState* paintInvalidationState) const
358 {
359 // Note that we don't apply the border-box transform here - it's assumed
360 // that whoever called us has done that already.
361
362 // Apply initial viewport clip
363 if (shouldApplyViewportClip())
364 rect.intersect(pixelSnappedBorderBoxRect());
365
366 LayoutReplaced::mapRectToPaintInvalidationBacking(paintInvalidationContainer , rect, paintInvalidationState);
367 }
368
369 // This method expects local CSS box coordinates.
370 // Callers with local SVG viewport coordinates should first apply the localToBor derBoxTransform
371 // to convert from SVG viewport coordinates to local CSS box coordinates.
372 void RenderSVGRoot::mapLocalToContainer(const LayoutLayerModelObject* paintInval idationContainer, TransformState& transformState, MapCoordinatesFlags mode, bool * wasFixed, const PaintInvalidationState* paintInvalidationState) const
373 {
374 ASSERT(mode & ~IsFixed); // We should have no fixed content in the SVG rende ring tree.
375 // We used to have this ASSERT here, but we removed it when enabling layer s quashing.
376 // See http://crbug.com/364901
377 // ASSERT(mode & UseTransforms); // mapping a point through SVG w/o respecti ng trasnforms is useless.
378
379 LayoutReplaced::mapLocalToContainer(paintInvalidationContainer, transformSta te, mode | ApplyContainerFlip, wasFixed, paintInvalidationState);
380 }
381
382 const LayoutObject* RenderSVGRoot::pushMappingToContainer(const LayoutLayerModel Object* ancestorToStopAt, LayoutGeometryMap& geometryMap) const
383 {
384 return LayoutReplaced::pushMappingToContainer(ancestorToStopAt, geometryMap) ;
385 }
386
387 void RenderSVGRoot::updateCachedBoundaries()
388 {
389 SVGLayoutSupport::computeContainerBoundingBoxes(this, m_objectBoundingBox, m _objectBoundingBoxValid, m_strokeBoundingBox, m_paintInvalidationBoundingBox);
390 SVGLayoutSupport::intersectPaintInvalidationRectWithResources(this, m_paintI nvalidationBoundingBox);
391 }
392
393 bool RenderSVGRoot::nodeAtPoint(const HitTestRequest& request, HitTestResult& re sult, const HitTestLocation& locationInContainer, const LayoutPoint& accumulated Offset, HitTestAction hitTestAction)
394 {
395 LayoutPoint pointInParent = locationInContainer.point() - toLayoutSize(accum ulatedOffset);
396 LayoutPoint pointInBorderBox = pointInParent - toLayoutSize(location());
397
398 // Only test SVG content if the point is in our content box, or in case we
399 // don't clip to the viewport, the visual overflow rect.
400 // FIXME: This should be an intersection when rect-based hit tests are suppo rted by nodeAtFloatPoint.
401 if (contentBoxRect().contains(pointInBorderBox) || (!shouldApplyViewportClip () && visualOverflowRect().contains(pointInBorderBox))) {
402 const AffineTransform& localToParentTransform = this->localToParentTrans form();
403 if (localToParentTransform.isInvertible()) {
404 FloatPoint localPoint = localToParentTransform.inverse().mapPoint(Fl oatPoint(pointInParent));
405
406 for (LayoutObject* child = lastChild(); child; child = child->previo usSibling()) {
407 // FIXME: nodeAtFloatPoint() doesn't handle rect-based hit tests yet.
408 if (child->nodeAtFloatPoint(request, result, localPoint, hitTest Action)) {
409 updateHitTestResult(result, pointInBorderBox);
410 if (!result.addNodeToRectBasedTestResult(child->node(), requ est, locationInContainer))
411 return true;
412 }
413 }
414 }
415 }
416
417 // 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.
418 if ((hitTestAction == HitTestBlockBackground || hitTestAction == HitTestChil dBlockBackground) && visibleToHitTestRequest(request)) {
419 // Only return true here, if the last hit testing phase 'BlockBackground ' (or 'ChildBlockBackground' - depending on context) is executed.
420 // If we'd return true in the 'Foreground' phase, hit testing would stop immediately. For SVG only trees this doesn't matter.
421 // Though when we have a <foreignObject> subtree we need to be able to d etect hits on the background of a <div> element.
422 // If we'd return true here in the 'Foreground' phase, we are not able t o detect these hits anymore.
423 LayoutRect boundsRect(accumulatedOffset + location(), size());
424 if (locationInContainer.intersects(boundsRect)) {
425 updateHitTestResult(result, pointInBorderBox);
426 if (!result.addNodeToRectBasedTestResult(node(), request, locationIn Container, boundsRect))
427 return true;
428 }
429 }
430
431 return false;
432 }
433
434 }
OLDNEW
« no previous file with comments | « Source/core/rendering/svg/RenderSVGRoot.h ('k') | Source/core/svg/SVGLengthContext.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698