#include "config.h"
#if ENABLE(SVG)
#include "RenderSVGRoot.h"
#include "GraphicsContext.h"
#include "RenderSVGContainer.h"
#include "RenderView.h"
#include "SVGLength.h"
#include "SVGRenderSupport.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)
{
setReplaced(true);
}
int RenderSVGRoot::lineHeight(bool, bool) const
{
return height() + marginTop() + marginBottom();
}
int RenderSVGRoot::baselinePosition(bool, bool) const
{
return height() + marginTop() + marginBottom();
}
void RenderSVGRoot::calcPrefWidths()
{
ASSERT(prefWidthsDirty());
int borderAndPadding = borderAndPaddingWidth();
int width = calcReplacedWidth(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_minPrefWidth = 0;
m_maxPrefWidth = width;
} else
m_minPrefWidth = m_maxPrefWidth = width;
setPrefWidthsDirty(false);
}
int RenderSVGRoot::calcReplacedWidth(bool includeMaxWidth) const
{
int replacedWidth = RenderBox::calcReplacedWidth(includeMaxWidth);
if (!style()->width().isPercent())
return replacedWidth;
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
return static_cast<int>(roundf(replacedWidth * svg->currentScale()));
}
int RenderSVGRoot::calcReplacedHeight() const
{
int replacedHeight = RenderBox::calcReplacedHeight();
if (!style()->height().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());
calcWidth();
calcHeight();
calcViewport();
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
layoutChildren(this, needsLayout || (svg->hasRelativeValues() && oldSize != size()));
repainter.repaintAfterLayout();
view()->enableLayoutState();
setNeedsLayout(false);
}
bool RenderSVGRoot::selfWillPaint() const
{
#if ENABLE(FILTERS)
const SVGRenderStyle* svgStyle = style()->svgStyle();
RenderSVGResourceFilter* filter = getRenderSVGResourceById<RenderSVGResourceFilter>(document(), svgStyle->filterResource());
if (filter)
return true;
#endif
return false;
}
void RenderSVGRoot::paint(PaintInfo& paintInfo, int parentX, int parentY)
{
if (paintInfo.context->paintingDisabled())
return;
IntPoint parentOriginInContainer(parentX, parentY);
IntPoint borderBoxOriginInContainer = parentOriginInContainer + parentOriginToBorderBox();
if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
paintBoxDecorations(paintInfo, borderBoxOriginInContainer.x(), borderBoxOriginInContainer.y());
if (m_viewportSize.isEmpty())
return;
if (!firstChild() && !selfWillPaint())
return;
RenderObject::PaintInfo childPaintInfo(paintInfo);
childPaintInfo.context->save();
childPaintInfo.context->clip(overflowClipRect(borderBoxOriginInContainer.x(), borderBoxOriginInContainer.y()));
applyTransformToPaintInfo(childPaintInfo, localToRepaintContainerTransform(parentOriginInContainer));
RenderSVGResourceFilter* filter = 0;
FloatRect boundingBox = repaintRectInLocalCoordinates();
bool continueRendering = true;
if (childPaintInfo.phase == PaintPhaseForeground)
continueRendering = prepareToRenderSVGContent(this, childPaintInfo, boundingBox, filter);
if (continueRendering)
RenderBox::paint(childPaintInfo, 0, 0);
if (childPaintInfo.phase == PaintPhaseForeground)
finishRenderSVGContent(this, childPaintInfo, filter, paintInfo.context);
childPaintInfo.context->restore();
if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && style()->outlineWidth() && style()->visibility() == VISIBLE)
paintOutline(paintInfo.context, borderBoxOriginInContainer.x(), borderBoxOriginInContainer.y(), width(), height());
}
void RenderSVGRoot::destroy()
{
deregisterFromResources(this);
RenderBox::destroy();
}
void RenderSVGRoot::calcViewport()
{
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
if (!selfNeedsLayout() && !svg->hasRelativeValues())
return;
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 svg->viewBoxToViewTransform(width() / scale, height() / scale) * ctm;
}
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
{
AffineTransform parentToContainer(localToParentTransform());
return parentToContainer.translateRight(parentOriginInContainer.x(), parentOriginInContainer.y());
}
const AffineTransform& RenderSVGRoot::localToParentTransform() const
{
IntSize parentToBorderBoxOffset = parentOriginToBorderBox();
AffineTransform borderBoxOriginToParentOrigin(localToBorderBoxTransform());
borderBoxOriginToParentOrigin.translateRight(parentToBorderBoxOffset.width(), parentToBorderBoxOffset.height());
m_localToParentTransform = borderBoxOriginToParentOrigin;
return m_localToParentTransform;
}
FloatRect RenderSVGRoot::objectBoundingBox() const
{
return computeContainerBoundingBox(this, false);
}
FloatRect RenderSVGRoot::repaintRectInLocalCoordinates() const
{
FloatRect repaintRect = computeContainerBoundingBox(this, true);
style()->svgStyle()->inflateForShadow(repaintRect);
return repaintRect;
}
AffineTransform RenderSVGRoot::localTransform() const
{
return AffineTransform();
}
void RenderSVGRoot::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& repaintRect, bool fixed)
{
repaintRect = localToBorderBoxTransform().mapRect(repaintRect);
repaintRect.intersect(enclosingIntRect(FloatRect(FloatPoint(), m_viewportSize)));
style()->svgStyle()->inflateForShadow(repaintRect);
RenderBox::computeRectForRepaint(repaintContainer, repaintRect, fixed);
}
void RenderSVGRoot::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed , bool useTransforms, TransformState& transformState) const
{
ASSERT(!fixed); ASSERT(useTransforms);
transformState.applyTransform(localToBorderBoxTransform());
RenderBox::mapLocalToContainer(repaintContainer, fixed, useTransforms, transformState);
}
bool RenderSVGRoot::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int _x, int _y, int _tx, int _ty, HitTestAction hitTestAction)
{
IntPoint pointInContainer(_x, _y);
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);
return true;
}
}
return false;
}
}
#endif // ENABLE(SVG)