#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 "RenderPart.h"
#include "RenderSVGContainer.h"
#include "RenderSVGResource.h"
#include "RenderSVGResourceContainer.h"
#include "RenderView.h"
#include "SVGLength.h"
#include "SVGRenderingContext.h"
#include "SVGResources.h"
#include "SVGResourcesCache.h"
#include "SVGSVGElement.h"
#include "SVGStyledElement.h"
#include "SVGViewSpec.h"
#include "TransformState.h"
#if ENABLE(FILTERS)
#include "RenderSVGResourceFilter.h"
#endif
using namespace std;
namespace WebCore {
RenderSVGRoot::RenderSVGRoot(SVGStyledElement* node)
: RenderReplaced(node)
, m_objectBoundingBoxValid(false)
, m_isLayoutSizeChanged(false)
, m_needsBoundariesOrTransformUpdate(true)
, m_hasSVGShadow(false)
{
}
RenderSVGRoot::~RenderSVGRoot()
{
}
void RenderSVGRoot::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const
{
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
Length intrinsicWidthAttribute = svg->intrinsicWidth(SVGSVGElement::IgnoreCSSProperties);
Length intrinsicHeightAttribute = svg->intrinsicHeight(SVGSVGElement::IgnoreCSSProperties);
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 = svg->viewBox().size();
if (!intrinsicSize.isEmpty()) {
intrinsicRatio = intrinsicSize.width() / static_cast<double>(intrinsicSize.height());
intrinsicSize = FloatSize();
return;
}
if (intrinsicWidthAttribute.isPercent() && intrinsicHeightAttribute.isPercent()) {
isPercentageIntrinsicSize = true;
intrinsicSize = FloatSize(intrinsicWidthAttribute.percent(), intrinsicHeightAttribute.percent());
}
}
bool RenderSVGRoot::isEmbeddedThroughSVGImage() const
{
if (!node())
return false;
Frame* frame = node()->document()->frame();
if (!frame)
return false;
if (!frame->page() || !frame->page()->chrome())
return false;
ChromeClient* chromeClient = frame->page()->chrome()->client();
if (!chromeClient || !chromeClient->isSVGImageChromeClient())
return false;
return true;
}
bool RenderSVGRoot::isEmbeddedThroughFrameContainingSVGDocument() const
{
if (!node())
return false;
Frame* frame = node()->document()->frame();
if (!frame)
return false;
if (!frame->ownerRenderer())
return false;
return frame->document()->isSVGDocument();
}
static inline LayoutUnit resolveLengthAttributeForSVG(const Length& length, float scale, float maxSize, RenderView* renderView)
{
return static_cast<LayoutUnit>(valueForLength(length, maxSize, renderView) * (length.isFixed() ? scale : 1));
}
LayoutUnit RenderSVGRoot::computeReplacedLogicalWidth(bool includeMaxWidth) const
{
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
ASSERT(svg);
if (!m_containerSize.isEmpty())
return m_containerSize.width();
if (style()->logicalWidth().isSpecified())
return RenderReplaced::computeReplacedLogicalWidth(includeMaxWidth);
if (svg->widthAttributeEstablishesViewport())
return resolveLengthAttributeForSVG(svg->intrinsicWidth(SVGSVGElement::IgnoreCSSProperties), style()->effectiveZoom(), containingBlock()->availableLogicalWidth(), view());
ASSERT(isEmbeddedThroughFrameContainingSVGDocument());
return document()->frame()->ownerRenderer()->availableLogicalWidth();
}
LayoutUnit RenderSVGRoot::computeReplacedLogicalHeight() const
{
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
ASSERT(svg);
if (!m_containerSize.isEmpty())
return m_containerSize.height();
if (hasReplacedLogicalHeight())
return RenderReplaced::computeReplacedLogicalHeight();
if (svg->heightAttributeEstablishesViewport()) {
Length height = svg->intrinsicHeight(SVGSVGElement::IgnoreCSSProperties);
if (height.isPercent()) {
RenderBlock* cb = containingBlock();
ASSERT(cb);
while (cb->isAnonymous()) {
cb = cb->containingBlock();
cb->addPercentHeightDescendant(const_cast<RenderSVGRoot*>(this));
}
} else
RenderBlock::removePercentHeightDescendant(const_cast<RenderSVGRoot*>(this));
return resolveLengthAttributeForSVG(height, style()->effectiveZoom(), containingBlock()->availableLogicalHeight(), view());
}
ASSERT(isEmbeddedThroughFrameContainingSVGDocument());
return document()->frame()->ownerRenderer()->availableLogicalHeight();
}
void RenderSVGRoot::layout()
{
ASSERT(needsLayout());
m_resourcesNeedingToInvalidateClients.clear();
LayoutStateDisabler layoutStateDisabler(view());
bool needsLayout = selfNeedsLayout();
LayoutRepainter repainter(*this, checkForRepaintDuringLayout() && needsLayout);
LayoutSize oldSize = size();
computeLogicalWidth();
computeLogicalHeight();
buildLocalToBorderBoxTransform();
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
m_isLayoutSizeChanged = needsLayout || (svg->hasRelativeLengths() && oldSize != size());
SVGRenderSupport::layoutChildren(this, needsLayout || SVGRenderSupport::filtersForceContainerLayout(this));
if (!m_resourcesNeedingToInvalidateClients.isEmpty()) {
HashSet<RenderSVGResourceContainer*>::iterator end = m_resourcesNeedingToInvalidateClients.end();
for (HashSet<RenderSVGResourceContainer*>::iterator it = m_resourcesNeedingToInvalidateClients.begin(); it != end; ++it)
(*it)->removeAllClientsFromCache();
m_isLayoutSizeChanged = false;
SVGRenderSupport::layoutChildren(this, false);
}
if (m_needsBoundariesOrTransformUpdate) {
updateCachedBoundaries();
m_needsBoundariesOrTransformUpdate = false;
}
repainter.repaintAfterLayout();
setNeedsLayout(false);
}
void RenderSVGRoot::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (pixelSnappedBorderBoxRect().isEmpty())
return;
if (paintInfo.context->paintingDisabled())
return;
Page* page = 0;
if (Frame* frame = this->frame())
page = frame->page();
if (!firstChild()) {
SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(this);
if (!resources || !resources->filter()) {
if (page && paintInfo.phase == PaintPhaseForeground)
page->addRelevantUnpaintedObject(this, visualOverflowRect());
return;
}
}
if (page && paintInfo.phase == PaintPhaseForeground)
page->addRelevantRepaintedObject(this, visualOverflowRect());
PaintInfo childPaintInfo(paintInfo);
childPaintInfo.context->save();
childPaintInfo.context->clip(pixelSnappedIntRect(overflowClipRect(paintOffset, paintInfo.renderRegion)));
IntPoint adjustedPaintOffset = roundedIntPoint(paintOffset);
childPaintInfo.applyTransform(AffineTransform::translation(adjustedPaintOffset.x() - x(), adjustedPaintOffset.y() - y()) * localToParentTransform());
SVGRenderingContext renderingContext;
bool continueRendering = true;
if (childPaintInfo.phase == PaintPhaseForeground) {
renderingContext.prepareToRenderSVGContent(this, childPaintInfo);
continueRendering = renderingContext.isRenderingPrepared();
}
if (continueRendering)
RenderBox::paint(childPaintInfo, LayoutPoint());
childPaintInfo.context->restore();
}
void RenderSVGRoot::willBeDestroyed()
{
RenderBlock::removePercentHeightDescendant(const_cast<RenderSVGRoot*>(this));
SVGResourcesCache::clientDestroyed(this);
RenderReplaced::willBeDestroyed();
}
void RenderSVGRoot::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
{
if (diff == StyleDifferenceLayout)
setNeedsBoundariesUpdate();
RenderReplaced::styleWillChange(diff, newStyle);
}
void RenderSVGRoot::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderReplaced::styleDidChange(diff, oldStyle);
SVGResourcesCache::clientStyleChanged(this, diff, style());
}
void RenderSVGRoot::updateFromElement()
{
RenderReplaced::updateFromElement();
SVGResourcesCache::clientUpdatedFromElement(this, style());
}
void RenderSVGRoot::buildLocalToBorderBoxTransform()
{
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
float scale = style()->effectiveZoom();
FloatPoint translate = svg->currentTranslate();
LayoutSize borderAndPadding(borderLeft() + paddingLeft(), borderTop() + paddingTop());
m_localToBorderBoxTransform = svg->viewBoxToViewTransform(contentWidth() / scale, contentHeight() / scale);
if (borderAndPadding.isEmpty() && scale == 1 && translate == FloatPoint::zero())
return;
m_localToBorderBoxTransform = AffineTransform(scale, 0, 0, scale, borderAndPadding.width() + translate.x(), borderAndPadding.height() + translate.y()) * m_localToBorderBoxTransform;
}
const AffineTransform& RenderSVGRoot::localToParentTransform() const
{
m_localToParentTransform = m_localToBorderBoxTransform;
if (x())
m_localToParentTransform.setE(m_localToParentTransform.e() + roundToInt(x()));
if (y())
m_localToParentTransform.setF(m_localToParentTransform.f() + roundToInt(y()));
return m_localToParentTransform;
}
LayoutRect RenderSVGRoot::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer) const
{
return SVGRenderSupport::clippedOverflowRectForRepaint(this, repaintContainer);
}
void RenderSVGRoot::computeFloatRectForRepaint(RenderBoxModelObject* repaintContainer, FloatRect& repaintRect, bool fixed) const
{
repaintRect = m_localToBorderBoxTransform.mapRect(repaintRect);
const SVGRenderStyle* svgStyle = style()->svgStyle();
if (const ShadowData* shadow = svgStyle->shadow())
shadow->adjustRectForShadow(repaintRect);
repaintRect.intersect(pixelSnappedBorderBoxRect());
LayoutRect rect = enclosingIntRect(repaintRect);
RenderReplaced::computeRectForRepaint(repaintContainer, rect, fixed);
repaintRect = rect;
}
void RenderSVGRoot::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed, bool useTransforms, TransformState& transformState, ApplyContainerFlipOrNot, bool* wasFixed) const
{
ASSERT(!fixed); ASSERT(useTransforms);
RenderReplaced::mapLocalToContainer(repaintContainer, fixed, useTransforms, transformState, ApplyContainerFlip, wasFixed);
}
const RenderObject* RenderSVGRoot::pushMappingToContainer(const RenderBoxModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
{
return RenderReplaced::pushMappingToContainer(ancestorToStopAt, geometryMap);
}
void RenderSVGRoot::updateCachedBoundaries()
{
m_objectBoundingBox = FloatRect();
m_objectBoundingBoxValid = false;
m_strokeBoundingBox = FloatRect();
m_repaintBoundingBox = FloatRect();
SVGRenderSupport::computeContainerBoundingBoxes(this, m_objectBoundingBox, m_objectBoundingBoxValid, m_strokeBoundingBox, m_repaintBoundingBoxExcludingShadow);
SVGRenderSupport::intersectRepaintRectWithResources(this, m_repaintBoundingBoxExcludingShadow);
m_repaintBoundingBoxExcludingShadow.inflate(borderAndPaddingWidth());
m_repaintBoundingBox = m_repaintBoundingBoxExcludingShadow;
SVGRenderSupport::intersectRepaintRectWithShadows(this, m_repaintBoundingBox);
}
bool RenderSVGRoot::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
{
LayoutPoint pointInParent = pointInContainer - toLayoutSize(accumulatedOffset);
LayoutPoint pointInBorderBox(pointInParent.x() - x(), pointInParent.y() - y());
if (!contentBoxRect().contains(pointInBorderBox))
return false;
FloatPoint localPoint = localToParentTransform().inverse().mapPoint(FloatPoint(pointInParent));
for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
if (child->nodeAtFloatPoint(request, result, localPoint, hitTestAction)) {
updateHitTestResult(result, pointInBorderBox);
result.addNodeToRectBasedTestResult(child->node(), pointInContainer);
return true;
}
}
if (hitTestAction == HitTestBlockBackground && style()->pointerEvents() != PE_NONE) {
updateHitTestResult(result, roundedLayoutPoint(localPoint));
return true;
}
return false;
}
bool RenderSVGRoot::hasRelativeDimensions() const
{
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
ASSERT(svg);
return svg->intrinsicHeight(SVGSVGElement::IgnoreCSSProperties).isPercent() || svg->intrinsicWidth(SVGSVGElement::IgnoreCSSProperties).isPercent();
}
bool RenderSVGRoot::hasRelativeLogicalHeight() const
{
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
ASSERT(svg);
return svg->intrinsicHeight(SVGSVGElement::IgnoreCSSProperties).isPercent();
}
void RenderSVGRoot::addResourceForClientInvalidation(RenderSVGResourceContainer* resource)
{
RenderObject* svgRoot = resource->parent();
while (svgRoot && !svgRoot->isSVGRoot())
svgRoot = svgRoot->parent();
if (!svgRoot)
return;
static_cast<RenderSVGRoot*>(svgRoot)->m_resourcesNeedingToInvalidateClients.add(resource);
}
}
#endif // ENABLE(SVG)