SVGRenderSupport.cpp [plain text]
#include "config.h"
#if ENABLE(SVG)
#include "SVGRenderSupport.h"
#include "NodeRenderStyle.h"
#include "RenderGeometryMap.h"
#include "RenderLayer.h"
#include "RenderSVGResource.h"
#include "RenderSVGResourceClipper.h"
#include "RenderSVGResourceFilter.h"
#include "RenderSVGResourceMarker.h"
#include "RenderSVGResourceMasker.h"
#include "RenderSVGRoot.h"
#include "RenderSVGText.h"
#include "RenderSVGViewportContainer.h"
#include "SVGResources.h"
#include "SVGResourcesCache.h"
#include "SVGStyledElement.h"
#include "TransformState.h"
namespace WebCore {
FloatRect SVGRenderSupport::repaintRectForRendererInLocalCoordinatesExcludingSVGShadow(const RenderObject* object)
{
if (object->isSVGShape() || object->isSVGImage() || object->isSVGContainer())
return static_cast<const RenderSVGModelObject*>(object)->repaintRectInLocalCoordinatesExcludingSVGShadow();
return object->repaintRectInLocalCoordinates();
}
LayoutRect SVGRenderSupport::clippedOverflowRectForRepaint(const RenderObject* object, const RenderLayerModelObject* repaintContainer)
{
if (object->style()->visibility() != VISIBLE && !object->enclosingLayer()->hasVisibleContent())
return LayoutRect();
FloatRect repaintRect = repaintRectForRendererInLocalCoordinatesExcludingSVGShadow(object);
const SVGRenderStyle* svgStyle = object->style()->svgStyle();
if (const ShadowData* shadow = svgStyle->shadow())
shadow->adjustRectForShadow(repaintRect);
object->computeFloatRectForRepaint(repaintContainer, repaintRect);
return enclosingLayoutRect(repaintRect);
}
void SVGRenderSupport::computeFloatRectForRepaint(const RenderObject* object, const RenderLayerModelObject* repaintContainer, FloatRect& repaintRect, bool fixed)
{
const SVGRenderStyle* svgStyle = object->style()->svgStyle();
if (const ShadowData* shadow = svgStyle->shadow())
shadow->adjustRectForShadow(repaintRect);
repaintRect.inflate(object->style()->outlineWidth());
repaintRect = object->localToParentTransform().mapRect(repaintRect);
object->parent()->computeFloatRectForRepaint(repaintContainer, repaintRect, fixed);
}
void SVGRenderSupport::mapLocalToContainer(const RenderObject* object, const RenderLayerModelObject* repaintContainer, TransformState& transformState, bool* wasFixed)
{
transformState.applyTransform(object->localToParentTransform());
RenderObject* parent = object->parent();
if (parent->isSVGRoot())
transformState.applyTransform(toRenderSVGRoot(parent)->localToBorderBoxTransform());
MapCoordinatesFlags mode = UseTransforms;
parent->mapLocalToContainer(repaintContainer, transformState, mode, wasFixed);
}
const RenderObject* SVGRenderSupport::pushMappingToContainer(const RenderObject* object, const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap)
{
ASSERT_UNUSED(ancestorToStopAt, ancestorToStopAt != object);
RenderObject* parent = object->parent();
if (parent->isSVGRoot()) {
TransformationMatrix matrix(object->localToParentTransform());
matrix.multiply(toRenderSVGRoot(parent)->localToBorderBoxTransform());
geometryMap.push(object, matrix);
} else
geometryMap.push(object, object->localToParentTransform());
return parent;
}
bool SVGRenderSupport::checkForSVGRepaintDuringLayout(RenderObject* object)
{
if (!object->checkForRepaintDuringLayout())
return false;
RenderObject* parent = object->parent();
return !(parent && parent->isSVGContainer() && toRenderSVGContainer(parent)->didTransformToRootUpdate());
}
static inline void updateObjectBoundingBox(FloatRect& objectBoundingBox, bool& objectBoundingBoxValid, RenderObject* other, FloatRect otherBoundingBox)
{
bool otherValid = other->isSVGContainer() ? toRenderSVGContainer(other)->isObjectBoundingBoxValid() : true;
if (!otherValid)
return;
if (!objectBoundingBoxValid) {
objectBoundingBox = otherBoundingBox;
objectBoundingBoxValid = true;
return;
}
objectBoundingBox.uniteEvenIfEmpty(otherBoundingBox);
}
void SVGRenderSupport::computeContainerBoundingBoxes(const RenderObject* container, FloatRect& objectBoundingBox, bool& objectBoundingBoxValid, FloatRect& strokeBoundingBox, FloatRect& repaintBoundingBox)
{
objectBoundingBox = FloatRect();
objectBoundingBoxValid = false;
strokeBoundingBox = FloatRect();
for (RenderObject* current = container->firstChild(); current; current = current->nextSibling()) {
if (current->isSVGHiddenContainer())
continue;
const AffineTransform& transform = current->localToParentTransform();
if (transform.isIdentity()) {
updateObjectBoundingBox(objectBoundingBox, objectBoundingBoxValid, current, current->objectBoundingBox());
strokeBoundingBox.unite(current->repaintRectInLocalCoordinates());
} else {
updateObjectBoundingBox(objectBoundingBox, objectBoundingBoxValid, current, transform.mapRect(current->objectBoundingBox()));
strokeBoundingBox.unite(transform.mapRect(current->repaintRectInLocalCoordinates()));
}
}
repaintBoundingBox = strokeBoundingBox;
}
bool SVGRenderSupport::paintInfoIntersectsRepaintRect(const FloatRect& localRepaintRect, const AffineTransform& localTransform, const PaintInfo& paintInfo)
{
if (localTransform.isIdentity())
return localRepaintRect.intersects(paintInfo.rect);
return localTransform.mapRect(localRepaintRect).intersects(paintInfo.rect);
}
const RenderSVGRoot* SVGRenderSupport::findTreeRootObject(const RenderObject* start)
{
while (start && !start->isSVGRoot())
start = start->parent();
ASSERT(start);
ASSERT(start->isSVGRoot());
return toRenderSVGRoot(start);
}
static inline void invalidateResourcesOfChildren(RenderObject* start)
{
ASSERT(!start->needsLayout());
if (SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(start))
resources->removeClientFromCache(start, false);
for (RenderObject* child = start->firstChild(); child; child = child->nextSibling())
invalidateResourcesOfChildren(child);
}
static inline bool layoutSizeOfNearestViewportChanged(const RenderObject* start)
{
while (start && !start->isSVGRoot() && !start->isSVGViewportContainer())
start = start->parent();
ASSERT(start);
ASSERT(start->isSVGRoot() || start->isSVGViewportContainer());
if (start->isSVGViewportContainer())
return toRenderSVGViewportContainer(start)->isLayoutSizeChanged();
return toRenderSVGRoot(start)->isLayoutSizeChanged();
}
bool SVGRenderSupport::transformToRootChanged(RenderObject* ancestor)
{
while (ancestor && !ancestor->isSVGRoot()) {
if (ancestor->isSVGTransformableContainer())
return toRenderSVGContainer(ancestor)->didTransformToRootUpdate();
if (ancestor->isSVGViewportContainer())
return toRenderSVGViewportContainer(ancestor)->didTransformToRootUpdate();
ancestor = ancestor->parent();
}
return false;
}
void SVGRenderSupport::layoutChildren(RenderObject* start, bool selfNeedsLayout)
{
bool layoutSizeChanged = layoutSizeOfNearestViewportChanged(start);
bool transformChanged = transformToRootChanged(start);
bool hasSVGShadow = rendererHasSVGShadow(start);
bool needsBoundariesUpdate = start->needsBoundariesUpdate();
HashSet<RenderObject*> notlayoutedObjects;
for (RenderObject* child = start->firstChild(); child; child = child->nextSibling()) {
bool needsLayout = selfNeedsLayout;
bool childEverHadLayout = child->everHadLayout();
if (needsBoundariesUpdate && hasSVGShadow) {
child->setNeedsBoundariesUpdate();
needsLayout = true;
}
if (transformChanged) {
if (child->isSVGText())
toRenderSVGText(child)->setNeedsTextMetricsUpdate();
needsLayout = true;
}
if (layoutSizeChanged) {
if (SVGElement* element = child->node()->isSVGElement() ? toSVGElement(child->node()) : 0) {
if (element->isSVGStyledElement() && toSVGStyledElement(element)->hasRelativeLengths()) {
if (child->isSVGShape())
toRenderSVGShape(child)->setNeedsShapeUpdate();
else if (child->isSVGText()) {
toRenderSVGText(child)->setNeedsTextMetricsUpdate();
toRenderSVGText(child)->setNeedsPositioningValuesUpdate();
}
needsLayout = true;
}
}
}
if (needsLayout)
child->setNeedsLayout(true, MarkOnlyThis);
if (child->needsLayout()) {
child->layout();
if (!childEverHadLayout)
child->repaint();
} else if (layoutSizeChanged)
notlayoutedObjects.add(child);
ASSERT(!child->needsLayout());
}
if (!layoutSizeChanged) {
ASSERT(notlayoutedObjects.isEmpty());
return;
}
HashSet<RenderObject*>::iterator end = notlayoutedObjects.end();
for (HashSet<RenderObject*>::iterator it = notlayoutedObjects.begin(); it != end; ++it)
invalidateResourcesOfChildren(*it);
}
bool SVGRenderSupport::isOverflowHidden(const RenderObject* object)
{
ASSERT(object->style()->overflowX() == object->style()->overflowY());
ASSERT(object->style()->overflowX() != OSCROLL);
ASSERT(!object->isRoot());
return object->style()->overflowX() == OHIDDEN;
}
bool SVGRenderSupport::rendererHasSVGShadow(const RenderObject* object)
{
if (object->isSVGShape() || object->isSVGImage() || object->isSVGContainer())
return static_cast<const RenderSVGModelObject*>(object)->hasSVGShadow();
if (object->isSVGRoot())
return toRenderSVGRoot(object)->hasSVGShadow();
return false;
}
void SVGRenderSupport::setRendererHasSVGShadow(RenderObject* object, bool hasShadow)
{
if (object->isSVGShape() || object->isSVGImage() || object->isSVGContainer())
return static_cast<RenderSVGModelObject*>(object)->setHasSVGShadow(hasShadow);
if (object->isSVGRoot())
return static_cast<RenderSVGRoot*>(object)->setHasSVGShadow(hasShadow);
}
void SVGRenderSupport::intersectRepaintRectWithShadows(const RenderObject* object, FloatRect& repaintRect)
{
ASSERT(object);
const RenderObject* currentObject = object;
AffineTransform localToRootTransform;
while (currentObject && rendererHasSVGShadow(currentObject)) {
RenderStyle* style = currentObject->style();
if (style) {
const SVGRenderStyle* svgStyle = style->svgStyle();
if (svgStyle) {
if (const ShadowData* shadow = svgStyle->shadow())
shadow->adjustRectForShadow(repaintRect);
}
}
repaintRect = currentObject->localToParentTransform().mapRect(repaintRect);
localToRootTransform *= currentObject->localToParentTransform();
currentObject = currentObject->parent();
};
if (localToRootTransform.isIdentity())
return;
AffineTransform rootToLocalTransform = localToRootTransform.inverse();
repaintRect = rootToLocalTransform.mapRect(repaintRect);
}
void SVGRenderSupport::intersectRepaintRectWithResources(const RenderObject* object, FloatRect& repaintRect)
{
ASSERT(object);
RenderObject* renderer = const_cast<RenderObject*>(object);
SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(renderer);
if (!resources)
return;
#if ENABLE(FILTERS)
if (RenderSVGResourceFilter* filter = resources->filter())
repaintRect = filter->resourceBoundingBox(renderer);
#endif
if (RenderSVGResourceClipper* clipper = resources->clipper())
repaintRect.intersect(clipper->resourceBoundingBox(renderer));
if (RenderSVGResourceMasker* masker = resources->masker())
repaintRect.intersect(masker->resourceBoundingBox(renderer));
}
bool SVGRenderSupport::filtersForceContainerLayout(RenderObject* object)
{
if (!object->normalChildNeedsLayout())
return false;
SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(object);
if (!resources || !resources->filter())
return false;
return true;
}
bool SVGRenderSupport::pointInClippingArea(RenderObject* object, const FloatPoint& point)
{
ASSERT(object);
SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(object);
if (!resources)
return true;
if (RenderSVGResourceClipper* clipper = resources->clipper())
return clipper->hitTestClipContent(object->objectBoundingBox(), point);
return true;
}
void SVGRenderSupport::applyStrokeStyleToContext(GraphicsContext* context, const RenderStyle* style, const RenderObject* object)
{
ASSERT(context);
ASSERT(style);
ASSERT(object);
ASSERT(object->node());
ASSERT(object->node()->isSVGElement());
const SVGRenderStyle* svgStyle = style->svgStyle();
ASSERT(svgStyle);
SVGLengthContext lengthContext(toSVGElement(object->node()));
context->setStrokeThickness(svgStyle->strokeWidth().value(lengthContext));
context->setLineCap(svgStyle->capStyle());
context->setLineJoin(svgStyle->joinStyle());
if (svgStyle->joinStyle() == MiterJoin)
context->setMiterLimit(svgStyle->strokeMiterLimit());
const Vector<SVGLength>& dashes = svgStyle->strokeDashArray();
if (dashes.isEmpty())
context->setStrokeStyle(SolidStroke);
else {
DashArray dashArray;
const Vector<SVGLength>::const_iterator end = dashes.end();
for (Vector<SVGLength>::const_iterator it = dashes.begin(); it != end; ++it)
dashArray.append((*it).value(lengthContext));
context->setLineDash(dashArray, svgStyle->strokeDashOffset().value(lengthContext));
}
}
void SVGRenderSupport::childAdded(RenderObject* parent, RenderObject* child)
{
SVGRenderSupport::setRendererHasSVGShadow(child, SVGRenderSupport::rendererHasSVGShadow(parent) || SVGRenderSupport::rendererHasSVGShadow(child));
}
void SVGRenderSupport::styleChanged(RenderObject* object)
{
RenderObject* parent = object->parent();
SVGRenderSupport::setRendererHasSVGShadow(object, (parent && SVGRenderSupport::rendererHasSVGShadow(parent)) || (object->style()->svgStyle() && object->style()->svgStyle()->shadow()));
}
}
#endif