RenderSVGResourceClipper.cpp [plain text]
#include "config.h"
#include "RenderSVGResourceClipper.h"
#include "AffineTransform.h"
#include "FloatRect.h"
#include "GraphicsContext.h"
#include "ImageBuffer.h"
#include "IntRect.h"
#include "RenderObject.h"
#include "RenderStyle.h"
#include "RenderSVGResource.h"
#include "SVGClipPathElement.h"
#include "SVGElement.h"
#include "SVGRenderSupport.h"
#include "SVGStyledElement.h"
#include "SVGStyledTransformableElement.h"
#include "SVGUnitTypes.h"
#include "SVGUseElement.h"
#include <wtf/UnusedParam.h>
namespace WebCore {
RenderSVGResourceType RenderSVGResourceClipper::s_resourceType = ClipperResourceType;
RenderSVGResourceClipper::RenderSVGResourceClipper(SVGClipPathElement* node)
: RenderSVGResourceContainer(node)
{
}
RenderSVGResourceClipper::~RenderSVGResourceClipper()
{
deleteAllValues(m_clipper);
m_clipper.clear();
}
void RenderSVGResourceClipper::invalidateClients()
{
HashMap<RenderObject*, ClipperData*>::const_iterator end = m_clipper.end();
for (HashMap<RenderObject*, ClipperData*>::const_iterator it = m_clipper.begin(); it != end; ++it) {
RenderObject* renderer = it->first;
renderer->setNeedsBoundariesUpdate();
renderer->setNeedsLayout(true);
}
deleteAllValues(m_clipper);
m_clipper.clear();
}
void RenderSVGResourceClipper::invalidateClient(RenderObject* object)
{
ASSERT(object);
if (!m_clipper.contains(object))
return;
delete m_clipper.take(object);
markForLayoutAndResourceInvalidation(object);
}
bool RenderSVGResourceClipper::applyResource(RenderObject* object, RenderStyle*, GraphicsContext*& context, unsigned short resourceMode)
{
ASSERT(object);
ASSERT(context);
#ifndef NDEBUG
ASSERT(resourceMode == ApplyToDefaultMode);
#else
UNUSED_PARAM(resourceMode);
#endif
applyClippingToContext(object, object->objectBoundingBox(), object->repaintRectInLocalCoordinates(), context);
return true;
}
bool RenderSVGResourceClipper::pathOnlyClipping(GraphicsContext* context, const FloatRect& objectBoundingBox)
{
if (!style()->svgStyle()->clipperResource().isEmpty())
return false;
WindRule clipRule = RULE_NONZERO;
Path clipPath = Path();
for (Node* childNode = node()->firstChild(); childNode; childNode = childNode->nextSibling()) {
RenderObject* renderer = childNode->renderer();
if (!renderer)
continue;
if (renderer->isSVGText())
return false;
if (!childNode->isSVGElement() || !static_cast<SVGElement*>(childNode)->isStyledTransformable())
continue;
SVGStyledTransformableElement* styled = static_cast<SVGStyledTransformableElement*>(childNode);
RenderStyle* style = renderer->style();
if (!style || style->display() == NONE || style->visibility() != VISIBLE)
continue;
const SVGRenderStyle* svgStyle = style->svgStyle();
if (!svgStyle->clipperResource().isEmpty())
return false;
if (clipPath.isEmpty()) {
clipPath = styled->toClipPath();
clipRule = svgStyle->clipRule();
} else
return false;
}
if (static_cast<SVGClipPathElement*>(node())->clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
AffineTransform transform;
transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
clipPath.transform(transform);
}
if (clipPath.isEmpty())
clipPath.addRect(FloatRect());
context->beginPath();
context->addPath(clipPath);
context->clipPath(clipRule);
return true;
}
bool RenderSVGResourceClipper::applyClippingToContext(RenderObject* object, const FloatRect& objectBoundingBox,
const FloatRect& repaintRect, GraphicsContext* context)
{
if (!m_clipper.contains(object))
m_clipper.set(object, new ClipperData);
ClipperData* clipperData = m_clipper.get(object);
if (!clipperData->clipMaskImage) {
if (pathOnlyClipping(context, objectBoundingBox))
return true;
createClipData(clipperData, objectBoundingBox, repaintRect);
}
if (!clipperData->clipMaskImage)
return false;
context->clipToImageBuffer(repaintRect, clipperData->clipMaskImage.get());
return true;
}
bool RenderSVGResourceClipper::createClipData(ClipperData* clipperData, const FloatRect& objectBoundingBox, const FloatRect& repaintRect)
{
IntRect clipMaskRect = enclosingIntRect(repaintRect);
clipperData->clipMaskImage = ImageBuffer::create(clipMaskRect.size());
if (!clipperData->clipMaskImage)
return false;
GraphicsContext* maskContext = clipperData->clipMaskImage->context();
ASSERT(maskContext);
maskContext->save();
maskContext->translate(-repaintRect.x(), -repaintRect.y());
bool clipperGetsClipped = false;
if (RenderSVGResourceClipper* clipper = getRenderSVGResourceById<RenderSVGResourceClipper>(this->document(), style()->svgStyle()->clipperResource())) {
clipperGetsClipped = true;
if (!clipper->applyClippingToContext(this, objectBoundingBox, repaintRect, maskContext)) {
maskContext->restore();
return false;
}
}
SVGClipPathElement* clipPath = static_cast<SVGClipPathElement*>(node());
if (clipPath->clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
maskContext->translate(objectBoundingBox.x(), objectBoundingBox.y());
maskContext->scale(objectBoundingBox.size());
}
for (Node* childNode = node()->firstChild(); childNode; childNode = childNode->nextSibling()) {
RenderObject* renderer = childNode->renderer();
if (!childNode->isSVGElement() || !static_cast<SVGElement*>(childNode)->isStyled() || !renderer)
continue;
RenderStyle* style = renderer->style();
if (!style || style->display() == NONE || style->visibility() != VISIBLE)
continue;
WindRule newClipRule = style->svgStyle()->clipRule();
bool isUseElement = renderer->isSVGShadowTreeRootContainer();
if (isUseElement) {
SVGUseElement* useElement = static_cast<SVGUseElement*>(childNode);
renderer = useElement->rendererClipChild();
if (!renderer)
continue;
if (!useElement->hasAttribute(SVGNames::clip_ruleAttr))
newClipRule = renderer->style()->svgStyle()->clipRule();
}
if (!renderer->isRenderPath() && !renderer->isSVGText())
continue;
RefPtr<RenderStyle> oldRenderStyle = renderer->style();
RefPtr<RenderStyle> newRenderStyle = RenderStyle::clone(oldRenderStyle.get());
SVGRenderStyle* svgStyle = newRenderStyle.get()->accessSVGStyle();
svgStyle->setFillPaint(SVGPaint::defaultFill());
svgStyle->setStrokePaint(SVGPaint::defaultStroke());
svgStyle->setFillRule(newClipRule);
newRenderStyle.get()->setOpacity(1.0f);
svgStyle->setFillOpacity(1.0f);
svgStyle->setStrokeOpacity(1.0f);
svgStyle->setFilterResource(String());
svgStyle->setMaskerResource(String());
renderer->setStyle(newRenderStyle.release());
renderSubtreeToImage(clipperData->clipMaskImage.get(), isUseElement ? childNode->renderer() : renderer);
renderer->setStyle(oldRenderStyle.release());
}
maskContext->restore();
return true;
}
FloatRect RenderSVGResourceClipper::resourceBoundingBox(const FloatRect& objectBoundingBox) const
{
FloatRect clipRect;
for (Node* childNode = node()->firstChild(); childNode; childNode = childNode->nextSibling()) {
RenderObject* renderer = childNode->renderer();
if (!childNode->isSVGElement() || !static_cast<SVGElement*>(childNode)->isStyled() || !renderer)
continue;
if (!renderer->isRenderPath() && !renderer->isSVGText() && !renderer->isSVGShadowTreeRootContainer())
continue;
clipRect.unite(renderer->localToParentTransform().mapRect(renderer->repaintRectInLocalCoordinates()));
}
if (static_cast<SVGClipPathElement*>(node())->clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
AffineTransform transform;
transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
return transform.mapRect(clipRect);
}
return clipRect;
}
}