#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 "SVGResourceFilter.h"
#endif
using namespace std;
namespace WebCore {
RenderSVGRoot::RenderSVGRoot(SVGStyledElement* node)
: RenderBox(node)
{
setReplaced(true);
}
RenderSVGRoot::~RenderSVGRoot()
{
}
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 paddingAndBorders = paddingLeft() + paddingRight() + borderLeft() + borderRight();
int width = calcReplacedWidth(false) + paddingAndBorders;
if (style()->maxWidth().isFixed() && style()->maxWidth().value() != undefinedLength)
width = min(width, style()->maxWidth().value() + (style()->boxSizing() == CONTENT_BOX ? paddingAndBorders : 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);
}
void RenderSVGRoot::layout()
{
ASSERT(needsLayout());
view()->disableLayoutState();
LayoutRepainter repainter(*this, checkForRepaintDuringLayout() && selfNeedsLayout());
calcWidth();
calcHeight();
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
setWidth(static_cast<int>(width() * svg->currentScale()));
setHeight(static_cast<int>(height() * svg->currentScale()));
calcViewport();
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (selfNeedsLayout()) child->setNeedsLayout(true, false);
child->layoutIfNeeded();
ASSERT(!child->needsLayout());
}
repainter.repaintAfterLayout();
view()->enableLayoutState();
setNeedsLayout(false);
}
bool RenderSVGRoot::selfWillPaint() const
{
#if ENABLE(FILTERS)
const SVGRenderStyle* svgStyle = style()->svgStyle();
SVGResourceFilter* filter = getFilterById(document(), svgStyle->filter());
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 + IntSize(x(), y());
if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
paintBoxDecorations(paintInfo, borderBoxOriginInContainer.x(), borderBoxOriginInContainer.y());
if (viewportSize().isEmpty())
return;
if (!firstChild() && !selfWillPaint())
return;
RenderObject::PaintInfo childPaintInfo(paintInfo);
childPaintInfo.context->save();
if (style()->overflowX() != OVISIBLE)
childPaintInfo.context->clip(overflowClipRect(borderBoxOriginInContainer.x(), borderBoxOriginInContainer.y()));
applyTransformToPaintInfo(childPaintInfo, localToRepaintContainerTransform(parentOriginInContainer));
SVGResourceFilter* filter = 0;
FloatRect boundingBox = repaintRectInLocalCoordinates();
if (childPaintInfo.phase == PaintPhaseForeground)
prepareToRenderSVGContent(this, childPaintInfo, boundingBox, filter);
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(), style());
}
const FloatSize& RenderSVGRoot::viewportSize() const
{
return m_viewportSize;
}
void RenderSVGRoot::calcViewport()
{
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
if (!selfNeedsLayout() && !svg->hasRelativeValues())
return;
if (!svg->hasSetContainerSize()) {
m_viewportSize = FloatSize(width(), height());
} else {
SVGLength width = svg->width();
SVGLength height = svg->height();
float viewportWidth = (width.unitType() == LengthTypePercentage) ? svg->relativeWidthValue() : width.value(svg);
float viewportHeight = (height.unitType() == LengthTypePercentage) ? svg->relativeHeightValue() : height.value(svg);
m_viewportSize = FloatSize(viewportWidth, viewportHeight);
}
}
TransformationMatrix RenderSVGRoot::localToBorderBoxTransform() const
{
TransformationMatrix ctm;
IntSize borderAndPadding = borderOriginToContentBox();
ctm.translate(borderAndPadding.width(), borderAndPadding.height());
SVGSVGElement* svg = static_cast<SVGSVGElement*>(node());
ctm.scale(svg->currentScale());
ctm.translate(svg->currentTranslate().x(), svg->currentTranslate().y());
return svg->viewBoxToViewTransform(width(), height()) * ctm;
}
IntSize RenderSVGRoot::parentOriginToBorderBox() const
{
return IntSize(x(), y());
}
IntSize RenderSVGRoot::borderOriginToContentBox() const
{
return IntSize(borderLeft() + paddingLeft(), borderTop() + paddingTop());
}
TransformationMatrix RenderSVGRoot::localToRepaintContainerTransform(const IntPoint& parentOriginInContainer) const
{
TransformationMatrix parentToContainer;
parentToContainer.translate(parentOriginInContainer.x(), parentOriginInContainer.y());
return localToParentTransform() * parentToContainer;
}
TransformationMatrix RenderSVGRoot::localToParentTransform() const
{
IntSize parentToBorderBoxOffset = parentOriginToBorderBox();
TransformationMatrix borderBoxOriginToParentOrigin;
borderBoxOriginToParentOrigin.translate(parentToBorderBoxOffset.width(), parentToBorderBoxOffset.height());
return localToBorderBoxTransform() * borderBoxOriginToParentOrigin;
}
TransformationMatrix RenderSVGRoot::absoluteTransform() const
{
return localToParentTransform() * RenderBox::absoluteTransform();
}
FloatRect RenderSVGRoot::objectBoundingBox() const
{
return computeContainerBoundingBox(this, false);
}
FloatRect RenderSVGRoot::repaintRectInLocalCoordinates() const
{
return computeContainerBoundingBox(this, true);
}
TransformationMatrix RenderSVGRoot::localTransform() const
{
return TransformationMatrix();
}
void RenderSVGRoot::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& repaintRect, bool fixed)
{
repaintRect = localToBorderBoxTransform().mapRect(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();
if (style()->overflowX() == OHIDDEN) {
ASSERT(style()->overflowY() == OHIDDEN);
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)