diff options
Diffstat (limited to 'Source/WebCore/rendering/svg/RenderSVGRoot.cpp')
-rw-r--r-- | Source/WebCore/rendering/svg/RenderSVGRoot.cpp | 247 |
1 files changed, 121 insertions, 126 deletions
diff --git a/Source/WebCore/rendering/svg/RenderSVGRoot.cpp b/Source/WebCore/rendering/svg/RenderSVGRoot.cpp index 6de3aa3c6..0ddbbbcac 100644 --- a/Source/WebCore/rendering/svg/RenderSVGRoot.cpp +++ b/Source/WebCore/rendering/svg/RenderSVGRoot.cpp @@ -22,21 +22,21 @@ */ #include "config.h" - -#if ENABLE(SVG) #include "RenderSVGRoot.h" -#include "Chrome.h" -#include "ChromeClient.h" #include "Frame.h" #include "GraphicsContext.h" #include "HitTestResult.h" #include "LayoutRepainter.h" #include "Page.h" #include "RenderIterator.h" +#include "RenderLayer.h" +#include "RenderNamedFlowFragment.h" +#include "RenderSVGResource.h" +#include "RenderSVGResourceContainer.h" +#include "RenderSVGResourceFilter.h" #include "RenderView.h" #include "SVGImage.h" -#include "SVGLength.h" #include "SVGRenderingContext.h" #include "SVGResources.h" #include "SVGResourcesCache.h" @@ -45,18 +45,15 @@ #include "TransformState.h" #include <wtf/StackStats.h> -#if ENABLE(FILTERS) -#include "RenderSVGResourceFilter.h" -#endif - namespace WebCore { -RenderSVGRoot::RenderSVGRoot(SVGSVGElement& element, PassRef<RenderStyle> style) - : RenderReplaced(element, std::move(style)) +RenderSVGRoot::RenderSVGRoot(SVGSVGElement& element, RenderStyle&& style) + : RenderReplaced(element, WTFMove(style)) , m_objectBoundingBoxValid(false) , m_isLayoutSizeChanged(false) , m_needsBoundariesOrTransformUpdate(true) , m_hasSVGShadow(false) + , m_hasBoxDecorations(false) { } @@ -66,20 +63,13 @@ RenderSVGRoot::~RenderSVGRoot() SVGSVGElement& RenderSVGRoot::svgSVGElement() const { - return toSVGSVGElement(nodeForNonAnonymous()); + return downcast<SVGSVGElement>(nodeForNonAnonymous()); } -void RenderSVGRoot::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const +void RenderSVGRoot::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio) const { // Spec: http://www.w3.org/TR/SVG/coords.html#IntrinsicSizing // SVG needs to specify how to calculate some intrinsic sizing properties to enable inclusion within other languages. - // The intrinsic width and height of the viewport of SVG content must be determined from the ‘width’ and ‘height’ attributes. - // If either of these are not specified, a value of '100%' must be assumed. Note: the ‘width’ and ‘height’ attributes are not - // the same as the CSS width and height properties. Specifically, percentage values do not provide an intrinsic width or height, - // and do not indicate a percentage of the containing block. Rather, once the viewport is established, they indicate the portion - // of the viewport that is actually covered by image data. - Length intrinsicWidthAttribute = svgSVGElement().intrinsicWidth(SVGSVGElement::IgnoreCSSProperties); - Length intrinsicHeightAttribute = svgSVGElement().intrinsicHeight(SVGSVGElement::IgnoreCSSProperties); // The intrinsic aspect ratio of the viewport of SVG content is necessary for example, when including SVG from an ‘object’ // element in HTML styled with CSS. It is possible (indeed, common) for an SVG graphic to have an intrinsic aspect ratio but @@ -88,33 +78,22 @@ void RenderSVGRoot::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, d // - If the ‘width’ and ‘height’ of the rootmost ‘svg’ element are both specified with unit identifiers (in, mm, cm, pt, pc, // px, em, ex) or in user units, then the aspect ratio is calculated from the ‘width’ and ‘height’ attributes after // resolving both values to user units. - if (intrinsicWidthAttribute.isFixed() || intrinsicHeightAttribute.isFixed()) { - if (intrinsicWidthAttribute.isFixed()) - intrinsicSize.setWidth(floatValueForLength(intrinsicWidthAttribute, 0)); - if (intrinsicHeightAttribute.isFixed()) - intrinsicSize.setHeight(floatValueForLength(intrinsicHeightAttribute, 0)); - if (!intrinsicSize.isEmpty()) - intrinsicRatio = intrinsicSize.width() / static_cast<double>(intrinsicSize.height()); - return; - } + intrinsicSize.setWidth(floatValueForLength(svgSVGElement().intrinsicWidth(), 0)); + intrinsicSize.setHeight(floatValueForLength(svgSVGElement().intrinsicHeight(), 0)); - // - If either/both of the ‘width’ and ‘height’ of the rootmost ‘svg’ element are in percentage units (or omitted), the - // aspect ratio is calculated from the width and height values of the ‘viewBox’ specified for the current SVG document - // fragment. If the ‘viewBox’ is not correctly specified, or set to 'none', the intrinsic aspect ratio cannot be - // calculated and is considered unspecified. - intrinsicSize = svgSVGElement().viewBox().size(); - if (!intrinsicSize.isEmpty()) { - // The viewBox can only yield an intrinsic ratio, not an intrinsic size. - intrinsicRatio = intrinsicSize.width() / static_cast<double>(intrinsicSize.height()); - intrinsicSize = FloatSize(); - return; - } - // If our intrinsic size is in percentage units, return those to the caller through the intrinsicSize. Notify the caller - // about the special situation, by setting isPercentageIntrinsicSize=true, so it knows how to interpret the return values. - if (intrinsicWidthAttribute.isPercent() && intrinsicHeightAttribute.isPercent()) { - isPercentageIntrinsicSize = true; - intrinsicSize = FloatSize(intrinsicWidthAttribute.percent(), intrinsicHeightAttribute.percent()); + if (!intrinsicSize.isEmpty()) + intrinsicRatio = intrinsicSize.width() / static_cast<double>(intrinsicSize.height()); + else { + // - If either/both of the ‘width’ and ‘height’ of the rootmost ‘svg’ element are in percentage units (or omitted), the + // aspect ratio is calculated from the width and height values of the ‘viewBox’ specified for the current SVG document + // fragment. If the ‘viewBox’ is not correctly specified, or set to 'none', the intrinsic aspect ratio cannot be + // calculated and is considered unspecified. + FloatSize viewBoxSize = svgSVGElement().viewBox().size(); + if (!viewBoxSize.isEmpty()) { + // The viewBox can only yield an intrinsic ratio, not an intrinsic size. + intrinsicRatio = viewBoxSize.width() / static_cast<double>(viewBoxSize.height()); + } } } @@ -132,9 +111,9 @@ bool RenderSVGRoot::isEmbeddedThroughFrameContainingSVGDocument() const return frame().document()->isSVGDocument(); } -static inline LayoutUnit resolveLengthAttributeForSVG(const Length& length, float scale, float maxSize, RenderView* renderView) +static inline LayoutUnit resolveLengthAttributeForSVG(const Length& length, float scale, float maxSize) { - return static_cast<LayoutUnit>(valueForLength(length, maxSize, renderView) * (length.isFixed() ? scale : 1)); + return valueForLength(length, maxSize) * (length.isFixed() ? scale : 1); } LayoutUnit RenderSVGRoot::computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const @@ -143,50 +122,24 @@ LayoutUnit RenderSVGRoot::computeReplacedLogicalWidth(ShouldComputePreferred sho if (!m_containerSize.isEmpty()) return m_containerSize.width(); - if (style().logicalWidth().isSpecified() || style().logicalMaxWidth().isSpecified()) - return RenderReplaced::computeReplacedLogicalWidth(shouldComputePreferred); - - if (svgSVGElement().widthAttributeEstablishesViewport()) - return resolveLengthAttributeForSVG(svgSVGElement().intrinsicWidth(SVGSVGElement::IgnoreCSSProperties), style().effectiveZoom(), containingBlock()->availableLogicalWidth(), &view()); - - // SVG embedded through object/embed/iframe. if (isEmbeddedThroughFrameContainingSVGDocument()) - return frame().ownerRenderer()->availableLogicalWidth(); + return containingBlock()->availableLogicalWidth(); // SVG embedded via SVGImage (background-image/border-image/etc) / Inline SVG. return RenderReplaced::computeReplacedLogicalWidth(shouldComputePreferred); } -LayoutUnit RenderSVGRoot::computeReplacedLogicalHeight() const +LayoutUnit RenderSVGRoot::computeReplacedLogicalHeight(std::optional<LayoutUnit> estimatedUsedWidth) const { // When we're embedded through SVGImage (border-image/background-image/<html:img>/...) we're forced to resize to a specific size. if (!m_containerSize.isEmpty()) return m_containerSize.height(); - if (style().logicalHeight().isSpecified() || style().logicalMaxHeight().isSpecified()) - return RenderReplaced::computeReplacedLogicalHeight(); - - if (svgSVGElement().heightAttributeEstablishesViewport()) { - Length height = svgSVGElement().intrinsicHeight(SVGSVGElement::IgnoreCSSProperties); - if (height.isPercent()) { - RenderBlock* cb = containingBlock(); - ASSERT(cb); - while (cb->isAnonymous() && !cb->isRenderView()) { - cb = cb->containingBlock(); - cb->addPercentHeightDescendant(const_cast<RenderSVGRoot&>(*this)); - } - } else - RenderBlock::removePercentHeightDescendant(const_cast<RenderSVGRoot&>(*this)); - - return resolveLengthAttributeForSVG(height, style().effectiveZoom(), containingBlock()->availableLogicalHeight(IncludeMarginBorderPadding), &view()); - } - - // SVG embedded through object/embed/iframe. if (isEmbeddedThroughFrameContainingSVGDocument()) - return frame().ownerRenderer()->availableLogicalHeight(IncludeMarginBorderPadding); + return containingBlock()->availableLogicalHeight(IncludeMarginBorderPadding); // SVG embedded via SVGImage (background-image/border-image/etc) / Inline SVG. - return RenderReplaced::computeReplacedLogicalHeight(); + return RenderReplaced::computeReplacedLogicalHeight(estimatedUsedWidth); } void RenderSVGRoot::layout() @@ -197,7 +150,7 @@ void RenderSVGRoot::layout() m_resourcesNeedingToInvalidateClients.clear(); // Arbitrary affine transforms are incompatible with LayoutState. - LayoutStateDisabler layoutStateDisabler(&view()); + LayoutStateDisabler layoutStateDisabler(view()); bool needsLayout = selfNeedsLayout(); LayoutRepainter repainter(*this, checkForRepaintDuringLayout() && needsLayout); @@ -212,9 +165,10 @@ void RenderSVGRoot::layout() if (!m_resourcesNeedingToInvalidateClients.isEmpty()) { // Invalidate resource clients, which may mark some nodes for layout. - HashSet<RenderSVGResourceContainer*>::iterator end = m_resourcesNeedingToInvalidateClients.end(); - for (HashSet<RenderSVGResourceContainer*>::iterator it = m_resourcesNeedingToInvalidateClients.begin(); it != end; ++it) - (*it)->removeAllClientsFromCache(); + for (auto& resource : m_resourcesNeedingToInvalidateClients) { + resource->removeAllClientsFromCache(); + SVGResourcesCache::clientStyleChanged(*resource, StyleDifferenceLayout, resource->style()); + } m_isLayoutSizeChanged = false; SVGRenderSupport::layoutChildren(*this, false); @@ -227,21 +181,45 @@ void RenderSVGRoot::layout() m_needsBoundariesOrTransformUpdate = false; } + clearOverflow(); + if (!shouldApplyViewportClip()) { + FloatRect contentRepaintRect = repaintRectInLocalCoordinates(); + contentRepaintRect = m_localToBorderBoxTransform.mapRect(contentRepaintRect); + addVisualOverflow(enclosingLayoutRect(contentRepaintRect)); + } + updateLayerTransform(); + m_hasBoxDecorations = isDocumentElementRenderer() ? hasVisibleBoxDecorationStyle() : hasVisibleBoxDecorations(); + invalidateBackgroundObscurationStatus(); repainter.repaintAfterLayout(); clearNeedsLayout(); } +bool RenderSVGRoot::shouldApplyViewportClip() const +{ + // the outermost svg is clipped if auto, and svg document roots are always clipped + // When the svg is stand-alone (isDocumentElement() == true) the viewport clipping should always + // be applied, noting that the window scrollbars should be hidden if overflow=hidden. + return style().overflowX() == OHIDDEN + || style().overflowX() == OAUTO + || style().overflowX() == OSCROLL + || this->isDocumentElementRenderer(); +} + void RenderSVGRoot::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset) { // An empty viewport disables rendering. - if (pixelSnappedBorderBoxRect().isEmpty()) + if (borderBoxRect().isEmpty()) return; // Don't paint, if the context explicitly disabled it. - if (paintInfo.context->paintingDisabled()) + if (paintInfo.context().paintingDisabled()) + return; + + // SVG outlines are painted during PaintPhaseForeground. + if (paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) return; // An empty viewBox also disables rendering. @@ -249,34 +227,33 @@ void RenderSVGRoot::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paint if (svgSVGElement().hasEmptyViewBox()) return; - Page* page = frame().page(); - // Don't paint if we don't have kids, except if we have filters we should paint those. if (!firstChild()) { - SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(*this); + auto* resources = SVGResourcesCache::cachedResourcesForRenderer(*this); if (!resources || !resources->filter()) { - if (page && paintInfo.phase == PaintPhaseForeground) - page->addRelevantUnpaintedObject(this, visualOverflowRect()); + if (paintInfo.phase == PaintPhaseForeground) + page().addRelevantUnpaintedObject(this, visualOverflowRect()); return; } } - if (page && paintInfo.phase == PaintPhaseForeground) - page->addRelevantRepaintedObject(this, visualOverflowRect()); + if (paintInfo.phase == PaintPhaseForeground) + page().addRelevantRepaintedObject(this, visualOverflowRect()); // Make a copy of the PaintInfo because applyTransform will modify the damage rect. PaintInfo childPaintInfo(paintInfo); - childPaintInfo.context->save(); + childPaintInfo.context().save(); - // Apply initial viewport clip - not affected by overflow handling - childPaintInfo.context->clip(pixelSnappedIntRect(overflowClipRect(paintOffset, paintInfo.renderRegion))); + // Apply initial viewport clip + if (shouldApplyViewportClip()) + childPaintInfo.context().clip(snappedIntRect(overflowClipRect(paintOffset, currentRenderNamedFlowFragment()))); // Convert from container offsets (html renderers) to a relative transform (svg renderers). // Transform from our paint container's coordinate system to our local coords. IntPoint adjustedPaintOffset = roundedIntPoint(paintOffset); childPaintInfo.applyTransform(AffineTransform::translation(adjustedPaintOffset.x(), adjustedPaintOffset.y()) * localToBorderBoxTransform()); - // SVGRenderingContext must be destroyed before we restore the childPaintInfo.context, because a filter may have + // SVGRenderingContext must be destroyed before we restore the childPaintInfo.context(), because a filter may have // changed the context and it is only reverted when the SVGRenderingContext destructor finishes applying the filter. { SVGRenderingContext renderingContext; @@ -293,7 +270,7 @@ void RenderSVGRoot::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paint } } - childPaintInfo.context->restore(); + childPaintInfo.context().restore(); } void RenderSVGRoot::willBeDestroyed() @@ -304,10 +281,27 @@ void RenderSVGRoot::willBeDestroyed() RenderReplaced::willBeDestroyed(); } +void RenderSVGRoot::insertedIntoTree() +{ + RenderReplaced::insertedIntoTree(); + SVGResourcesCache::clientWasAddedToTree(*this); +} + +void RenderSVGRoot::willBeRemovedFromTree() +{ + SVGResourcesCache::clientWillBeRemovedFromTree(*this); + RenderReplaced::willBeRemovedFromTree(); +} + void RenderSVGRoot::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle) { if (diff == StyleDifferenceLayout) setNeedsBoundariesUpdate(); + + // Box decorations may have appeared/disappeared - recompute status. + if (diff == StyleDifferenceRepaint) + m_hasBoxDecorations = hasVisibleBoxDecorationStyle(); + RenderReplaced::styleDidChange(diff, oldStyle); SVGResourcesCache::clientStyleChanged(*this, diff, style()); } @@ -329,10 +323,10 @@ void RenderSVGRoot::removeChild(RenderObject& child) void RenderSVGRoot::buildLocalToBorderBoxTransform() { float scale = style().effectiveZoom(); - SVGPoint translate = svgSVGElement().currentTranslate(); + FloatPoint translate = svgSVGElement().currentTranslateValue(); LayoutSize borderAndPadding(borderLeft() + paddingLeft(), borderTop() + paddingTop()); m_localToBorderBoxTransform = svgSVGElement().viewBoxToViewTransform(contentWidth() / scale, contentHeight() / scale); - if (borderAndPadding.isEmpty() && scale == 1 && translate == SVGPoint::zero()) + if (borderAndPadding.isZero() && scale == 1 && translate == FloatPoint::zero()) return; m_localToBorderBoxTransform = AffineTransform(scale, 0, 0, scale, borderAndPadding.width() + translate.x(), borderAndPadding.height() + translate.y()) * m_localToBorderBoxTransform; } @@ -350,25 +344,40 @@ const AffineTransform& RenderSVGRoot::localToParentTransform() const LayoutRect RenderSVGRoot::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const { - return SVGRenderSupport::clippedOverflowRectForRepaint(*this, repaintContainer); + if (style().visibility() != VISIBLE && !enclosingLayer()->hasVisibleContent()) + return LayoutRect(); + + FloatRect contentRepaintRect = m_localToBorderBoxTransform.mapRect(repaintRectInLocalCoordinates()); + contentRepaintRect.intersect(snappedIntRect(borderBoxRect())); + + LayoutRect repaintRect = enclosingLayoutRect(contentRepaintRect); + if (m_hasBoxDecorations || hasRenderOverflow()) + repaintRect.unite(unionRect(localSelectionRect(false), visualOverflowRect())); + + return RenderReplaced::computeRectForRepaint(enclosingIntRect(repaintRect), repaintContainer); } -void RenderSVGRoot::computeFloatRectForRepaint(const RenderLayerModelObject* repaintContainer, FloatRect& repaintRect, bool fixed) const +FloatRect RenderSVGRoot::computeFloatRectForRepaint(const FloatRect& repaintRect, const RenderLayerModelObject* repaintContainer, bool fixed) const { // Apply our local transforms (except for x/y translation), then our shadow, // and then call RenderBox's method to handle all the normal CSS Box model bits - repaintRect = m_localToBorderBoxTransform.mapRect(repaintRect); + FloatRect adjustedRect = m_localToBorderBoxTransform.mapRect(repaintRect); const SVGRenderStyle& svgStyle = style().svgStyle(); if (const ShadowData* shadow = svgStyle.shadow()) - shadow->adjustRectForShadow(repaintRect); + shadow->adjustRectForShadow(adjustedRect); - // Apply initial viewport clip - not affected by overflow settings - repaintRect.intersect(pixelSnappedBorderBoxRect()); + // Apply initial viewport clip + if (shouldApplyViewportClip()) + adjustedRect.intersect(snappedIntRect(borderBoxRect())); - LayoutRect rect = enclosingIntRect(repaintRect); - RenderReplaced::computeRectForRepaint(repaintContainer, rect, fixed); - repaintRect = rect; + if (m_hasBoxDecorations || hasRenderOverflow()) { + // The selectionRect can project outside of the overflowRect, so take their union + // for repainting to avoid selection painting glitches. + LayoutRect decoratedRepaintRect = unionRect(localSelectionRect(false), visualOverflowRect()); + adjustedRect.unite(decoratedRepaintRect); + } + return RenderReplaced::computeRectForRepaint(enclosingIntRect(adjustedRect), repaintContainer, {fixed, false}); } // This method expects local CSS box coordinates. @@ -391,7 +400,7 @@ void RenderSVGRoot::updateCachedBoundaries() { SVGRenderSupport::computeContainerBoundingBoxes(*this, m_objectBoundingBox, m_objectBoundingBoxValid, m_strokeBoundingBox, m_repaintBoundingBoxExcludingShadow); SVGRenderSupport::intersectRepaintRectWithResources(*this, m_repaintBoundingBoxExcludingShadow); - m_repaintBoundingBoxExcludingShadow.inflate(borderAndPaddingWidth()); + m_repaintBoundingBoxExcludingShadow.inflate(horizontalBorderAndPaddingExtent()); m_repaintBoundingBox = m_repaintBoundingBoxExcludingShadow; SVGRenderSupport::intersectRepaintRectWithShadows(*this, m_repaintBoundingBox); @@ -405,7 +414,7 @@ bool RenderSVGRoot::nodeAtPoint(const HitTestRequest& request, HitTestResult& re // Only test SVG content if the point is in our content box. // FIXME: This should be an intersection when rect-based hit tests are supported by nodeAtFloatPoint. if (contentBoxRect().contains(pointInBorderBox)) { - FloatPoint localPoint = localToParentTransform().inverse().mapPoint(FloatPoint(pointInParent)); + FloatPoint localPoint = localToParentTransform().inverse().value_or(AffineTransform()).mapPoint(FloatPoint(pointInParent)); for (RenderObject* child = lastChild(); child; child = child->previousSibling()) { // FIXME: nodeAtFloatPoint() doesn't handle rect-based hit tests yet. @@ -418,7 +427,7 @@ bool RenderSVGRoot::nodeAtPoint(const HitTestRequest& request, HitTestResult& re } // 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. - if (hitTestAction == HitTestBlockBackground && visibleToHitTesting()) { + if ((hitTestAction == HitTestBlockBackground || hitTestAction == HitTestChildBlockBackground) && visibleToHitTesting()) { // Only return true here, if the last hit testing phase 'BlockBackground' is executed. If we'd return true in the 'Foreground' phase, // hit testing would stop immediately. For SVG only trees this doesn't matter. Though when we have a <foreignObject> subtree we need // 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 @@ -436,29 +445,15 @@ bool RenderSVGRoot::nodeAtPoint(const HitTestRequest& request, HitTestResult& re bool RenderSVGRoot::hasRelativeDimensions() const { - return svgSVGElement().intrinsicHeight(SVGSVGElement::IgnoreCSSProperties).isPercent() || svgSVGElement().intrinsicWidth(SVGSVGElement::IgnoreCSSProperties).isPercent(); -} - -bool RenderSVGRoot::hasRelativeIntrinsicLogicalWidth() const -{ - return svgSVGElement().intrinsicWidth(SVGSVGElement::IgnoreCSSProperties).isPercent(); -} - -bool RenderSVGRoot::hasRelativeLogicalHeight() const -{ - return svgSVGElement().intrinsicHeight(SVGSVGElement::IgnoreCSSProperties).isPercent(); + return svgSVGElement().intrinsicHeight().isPercentOrCalculated() || svgSVGElement().intrinsicWidth().isPercentOrCalculated(); } void RenderSVGRoot::addResourceForClientInvalidation(RenderSVGResourceContainer* resource) { - RenderObject* svgRoot = resource->parent(); - while (svgRoot && !svgRoot->isSVGRoot()) - svgRoot = svgRoot->parent(); + RenderSVGRoot* svgRoot = SVGRenderSupport::findTreeRootObject(*resource); if (!svgRoot) return; - toRenderSVGRoot(svgRoot)->m_resourcesNeedingToInvalidateClients.add(resource); + svgRoot->m_resourcesNeedingToInvalidateClients.add(resource); } } - -#endif // ENABLE(SVG) |