SVGMaskElement.cpp [plain text]
#include "config.h"
#if SVG_SUPPORT
#include "SVGMaskElement.h"
#include "GraphicsContext.h"
#include "RenderSVGContainer.h"
#include "KCanvasImage.h"
#include "KCanvasPath.h"
#include "KRenderingDevice.h"
#include "SVGAnimatedLength.h"
#include "SVGHelper.h"
#include "SVGNames.h"
#include "SVGRenderStyle.h"
#include "cssstyleselector.h"
#include "ksvg.h"
#include "Attr.h"
#include <wtf/OwnPtr.h>
namespace WebCore {
SVGMaskElement::SVGMaskElement(const QualifiedName& tagName, Document *doc) : SVGStyledLocatableElement(tagName, doc), SVGURIReference(), SVGTests(), SVGLangSpace(), SVGExternalResourcesRequired(), m_masker(0), m_dirty(true)
{
}
SVGMaskElement::~SVGMaskElement()
{
delete m_masker;
}
SVGAnimatedLength *SVGMaskElement::x() const
{
return lazy_create<SVGAnimatedLength>(m_x, this, LM_WIDTH, viewportElement());
}
SVGAnimatedLength *SVGMaskElement::y() const
{
return lazy_create<SVGAnimatedLength>(m_y, this, LM_HEIGHT, viewportElement());
}
SVGAnimatedLength *SVGMaskElement::width() const
{
return lazy_create<SVGAnimatedLength>(m_width, this, LM_WIDTH, viewportElement());
}
SVGAnimatedLength *SVGMaskElement::height() const
{
return lazy_create<SVGAnimatedLength>(m_height, this, LM_HEIGHT, viewportElement());
}
void SVGMaskElement::attributeChanged(Attribute* attr, bool preserveDecls)
{
IntSize newSize = IntSize(lroundf(width()->baseVal()->value()), lroundf(height()->baseVal()->value()));
if (!m_masker || !m_masker->mask() || (m_masker->mask()->size() != newSize))
m_dirty = true;
SVGStyledLocatableElement::attributeChanged(attr, preserveDecls);
}
void SVGMaskElement::childrenChanged()
{
m_dirty = true;
SVGStyledLocatableElement::childrenChanged();
}
void SVGMaskElement::parseMappedAttribute(MappedAttribute *attr)
{
const String& value = attr->value();
if (attr->name() == SVGNames::xAttr)
x()->baseVal()->setValueAsString(value.impl());
else if (attr->name() == SVGNames::yAttr)
y()->baseVal()->setValueAsString(value.impl());
else if (attr->name() == SVGNames::widthAttr)
width()->baseVal()->setValueAsString(value.impl());
else if (attr->name() == SVGNames::heightAttr)
height()->baseVal()->setValueAsString(value.impl());
else {
if (SVGURIReference::parseMappedAttribute(attr))
return;
if (SVGTests::parseMappedAttribute(attr))
return;
if (SVGLangSpace::parseMappedAttribute(attr))
return;
if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
return;
SVGStyledElement::parseMappedAttribute(attr);
}
}
KCanvasImage *SVGMaskElement::drawMaskerContent()
{
KRenderingDevice *device = renderingDevice();
if (!device->currentContext()) return 0;
if (!renderer())
return 0;
KCanvasImage *maskImage = static_cast<KCanvasImage *>(device->createResource(RS_IMAGE));
IntSize size = IntSize(lroundf(width()->baseVal()->value()), lroundf(height()->baseVal()->value()));
maskImage->init(size);
KRenderingDeviceContext *patternContext = device->contextForImage(maskImage);
device->pushContext(patternContext);
OwnPtr<GraphicsContext> context(patternContext->createGraphicsContext());
RenderSVGContainer *maskContainer = static_cast<RenderSVGContainer *>(renderer());
RenderObject::PaintInfo info(context.get(), IntRect(), PaintPhaseForeground, 0, 0, 0);
maskContainer->setDrawsContents(true);
maskContainer->paint(info, 0, 0);
maskContainer->setDrawsContents(false);
device->popContext();
delete patternContext;
return maskImage;
}
RenderObject* SVGMaskElement::createRenderer(RenderArena* arena, RenderStyle*)
{
RenderSVGContainer* maskContainer = new (arena) RenderSVGContainer(this);
maskContainer->setDrawsContents(false);
return maskContainer;
}
KCanvasMasker *SVGMaskElement::canvasResource()
{
if (!m_masker) {
m_masker = static_cast<KCanvasMasker *>(renderingDevice()->createResource(RS_MASKER));
m_dirty = true;
}
if (m_dirty) {
KCanvasImage *newMaskImage = drawMaskerContent();
m_masker->setMask(newMaskImage);
m_dirty = (newMaskImage != 0);
}
return m_masker;
}
}
#endif // SVG_SUPPORT