#include "config.h"
#if ENABLE(SVG)
#include "RenderSVGRoot.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "RenderSVGContainer.h"
#include "RenderSVGResource.h"
#include "RenderView.h"
#include "SVGLength.h"
#include "SVGRenderSupport.h"
#include "SVGResources.h"
#include "SVGSVGElement.h"
#include "SVGStyledElement.h"
#include "TransformState.h"
#if ENABLE(FILTERS)
#include "RenderSVGResourceFilter.h"
#endif
using namespace std;
namespace WebCore {
RenderSVGRoot::RenderSVGRoot(SVGStyledElement* node)
: RenderBox(node)
, m_isLayoutSizeChanged(false)
, m_needsBoundariesOrTransformUpdate(true)
{
setReplaced(true);
}
RenderSVGRoot::~RenderSVGRoot()
{
}
void RenderSVGRoot::computePreferredLogicalWidths()
{
ASSERT(preferredLogicalWidthsDirty());
int borderAndPadding = borderAndPaddingWidth();
int width = computeReplacedLogicalWidth(false) + borderAndPadding;
if (style()->maxWidth().isFixed() && style()->maxWidth().value() != undefinedLength)
width = min(width, style()->maxWidth().value() + (style()->boxSizing() == CONTENT_BOX ? borderAndPadding : 0));
if (style()->width().isPercent() || (style()->width().isAuto() && style()->height().isPercent())) {
m_minPreferredLogicalWidth = 0;
m_maxPreferredLogicalWidth = width;
} else
m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = width;
setPreferredLogicalWidthsDirty(false);
}
int RenderSVGRoot::computeReplacedLogicalWidth(bool includeMaxWidth) const
{
int replacedWidth = RenderBox::computeReplacedLogicalWidth(includeMaxWidth);
if (!style()->logicalWidth().isPercent())
return replacedWidth;
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
return static_cast<int>(roundf(replacedWidth * svg->currentScale()));
}
int RenderSVGRoot::computeReplacedLogicalHeight() const
{
int replacedHeight = RenderBox::computeReplacedLogicalHeight();
if (!style()->logicalHeight().isPercent())
return replacedHeight;
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
return static_cast<int>(roundf(replacedHeight * svg->currentScale()));
}
void RenderSVGRoot::layout()
{
ASSERT(needsLayout());
view()->disableLayoutState();
bool needsLayout = selfNeedsLayout();
LayoutRepainter repainter(*this, checkForRepaintDuringLayout() && needsLayout);
IntSize oldSize(width(), height());
computeLogicalWidth();
computeLogicalHeight();
calcViewport();
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
m_isLayoutSizeChanged = svg->hasRelativeLengths() && oldSize != size();
SVGRenderSupport::layoutChildren(this, needsLayout);
m_isLayoutSizeChanged = false;
if (m_needsBoundariesOrTransformUpdate) {
updateCachedBoundaries();
m_needsBoundariesOrTransformUpdate = false;
}
repainter.repaintAfterLayout();
view()->enableLayoutState();
setNeedsLayout(false);
}
bool RenderSVGRoot::selfWillPaint()
{
#if ENABLE(FILTERS)
SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(this);
return resources && resources->filter();
#else
return false;
#endif
}
void RenderSVGRoot::paint(PaintInfo& paintInfo, int parentX, int parentY)
{
if (paintInfo.context->paintingDisabled())
return;
bool isVisible = style()->visibility() == VISIBLE;
IntPoint parentOriginInContainer(parentX, parentY);
IntPoint borderBoxOriginInContainer = parentOriginInContainer + parentOriginToBorderBox();
if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseBlockBackground || paintInfo.phase == PaintPhaseChildBlockBackground) && isVisible)
paintBoxDecorations(paintInfo, borderBoxOriginInContainer.x(), borderBoxOriginInContainer.y());
if (paintInfo.phase == PaintPhaseBlockBackground)
return;
if (m_viewportSize.isEmpty())
return;
if (!firstChild() && !selfWillPaint())
return;
PaintInfo childPaintInfo(paintInfo);
childPaintInfo.context->save();
childPaintInfo.context->clip(overflowClipRect(borderBoxOriginInContainer.x(), borderBoxOriginInContainer.y()));
childPaintInfo.applyTransform(localToRepaintContainerTransform(parentOriginInContainer));
bool continueRendering = true;
if (childPaintInfo.phase == PaintPhaseForeground)
continueRendering = SVGRenderSupport::prepareToRenderSVGContent(this, childPaintInfo);
if (continueRendering)
RenderBox::paint(childPaintInfo, 0, 0);
if (childPaintInfo.phase == PaintPhaseForeground)
SVGRenderSupport::finishRenderSVGContent(this, childPaintInfo, paintInfo.context);
childPaintInfo.context->restore();
if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && style()->outlineWidth() && isVisible)
paintOutline(paintInfo.context, IntRect(borderBoxOriginInContainer, size()));
}
void RenderSVGRoot::willBeDestroyed()
{
SVGResourcesCache::clientDestroyed(this);
RenderBox::willBeDestroyed();
}
void RenderSVGRoot::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
{
if (diff == StyleDifferenceLayout)
setNeedsBoundariesUpdate();
RenderBox::styleWillChange(diff, newStyle);
}
void RenderSVGRoot::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBox::styleDidChange(diff, oldStyle);
SVGResourcesCache::clientStyleChanged(this, diff, style());
}
void RenderSVGRoot::updateFromElement()
{
RenderBox::updateFromElement();
SVGResourcesCache::clientUpdatedFromElement(this, style());
}
void RenderSVGRoot::calcViewport()
{
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
if (!svg->hasSetContainerSize()) {
m_viewportSize = FloatSize(width(), height());
return;
}
SVGLength width = svg->width();
SVGLength height = svg->height();
m_viewportSize = FloatSize(width.unitType() == LengthTypePercentage ? svg->relativeWidthValue() : width.value(svg),
height.unitType() == LengthTypePercentage ? svg->relativeHeightValue() : height.value(svg));
}
AffineTransform RenderSVGRoot::localToBorderBoxTransform() const
{
IntSize borderAndPadding = borderOriginToContentBox();
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
float scale = svg->currentScale();
FloatPoint translate = svg->currentTranslate();
AffineTransform ctm(scale, 0, 0, scale, borderAndPadding.width() + translate.x(), borderAndPadding.height() + translate.y());
return ctm * svg->viewBoxToViewTransform(width() / scale, height() / scale);
}
IntSize RenderSVGRoot::parentOriginToBorderBox() const
{
return IntSize(x(), y());
}
IntSize RenderSVGRoot::borderOriginToContentBox() const
{
return IntSize(borderLeft() + paddingLeft(), borderTop() + paddingTop());
}
AffineTransform RenderSVGRoot::localToRepaintContainerTransform(const IntPoint& parentOriginInContainer) const
{
return AffineTransform::translation(parentOriginInContainer.x(), parentOriginInContainer.y()) * localToParentTransform();
}
const AffineTransform& RenderSVGRoot::localToParentTransform() const
{
IntSize parentToBorderBoxOffset = parentOriginToBorderBox();
m_localToParentTransform = AffineTransform::translation(parentToBorderBoxOffset.width(), parentToBorderBoxOffset.height()) * localToBorderBoxTransform();
return m_localToParentTransform;
}
IntRect RenderSVGRoot::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer)
{
return SVGRenderSupport::clippedOverflowRectForRepaint(this, repaintContainer);
}
void RenderSVGRoot::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& repaintRect, bool fixed)
{
repaintRect = localToBorderBoxTransform().mapRect(repaintRect);
repaintRect.intersect(enclosingIntRect(FloatRect(FloatPoint(), m_viewportSize)));
const SVGRenderStyle* svgStyle = style()->svgStyle();
if (const ShadowData* shadow = svgStyle->shadow())
shadow->adjustRectForShadow(repaintRect);
RenderBox::computeRectForRepaint(repaintContainer, repaintRect, fixed);
}
void RenderSVGRoot::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed, bool useTransforms, TransformState& transformState, bool* wasFixed) const
{
ASSERT(!fixed); ASSERT(useTransforms);
transformState.applyTransform(localToBorderBoxTransform());
RenderBox::mapLocalToContainer(repaintContainer, fixed, useTransforms, transformState, wasFixed);
}
void RenderSVGRoot::updateCachedBoundaries()
{
m_objectBoundingBox = FloatRect();
m_strokeBoundingBox = FloatRect();
m_repaintBoundingBox = FloatRect();
SVGRenderSupport::computeContainerBoundingBoxes(this, m_objectBoundingBox, m_strokeBoundingBox, m_repaintBoundingBox);
SVGRenderSupport::intersectRepaintRectWithResources(this, m_repaintBoundingBox);
m_repaintBoundingBox.inflate(borderAndPaddingWidth());
}
bool RenderSVGRoot::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const IntPoint& pointInContainer, int tx, int ty, HitTestAction hitTestAction)
{
IntSize containerToParentOffset(tx, ty);
IntPoint pointInParent = pointInContainer - containerToParentOffset;
IntPoint pointInBorderBox = pointInParent - parentOriginToBorderBox();
IntPoint pointInContentBox = pointInBorderBox - borderOriginToContentBox();
if (!contentBoxRect().contains(pointInContentBox))
return false;
IntPoint localPoint = localToParentTransform().inverse().mapPoint(pointInParent);
for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
if (child->nodeAtFloatPoint(request, result, localPoint, hitTestAction)) {
updateHitTestResult(result, pointInBorderBox);
result.addNodeToRectBasedTestResult(child->node(), pointInContainer.x(), pointInContainer.y());
return true;
}
}
if (hitTestAction == HitTestBlockBackground) {
updateHitTestResult(result, roundedIntPoint(localPoint));
return true;
}
return false;
}
}
#endif // ENABLE(SVG)