29 #include "CSSStyleSelector.h"
30 #include "GraphicsContext.h"
31 #include "ImageBuffer.h"
32 #include "RenderSVGContainer.h"
35 #include "SVGRenderSupport.h"
38 #include <wtf/MathExtras.h>
39 #include <wtf/OwnPtr.h>
45 SVGMaskElement::SVGMaskElement(
const QualifiedName& tagName, Document* doc)
46 : SVGStyledLocatableElement(tagName, doc)
50 , SVGExternalResourcesRequired()
51 , m_maskUnits(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
52 , m_maskContentUnits(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE)
53 , m_x(this, LengthModeWidth)
54 , m_y(this, LengthModeHeight)
55 , m_width(this, LengthModeWidth)
56 , m_height(this, LengthModeHeight)
59 setXBaseValue(SVGLength(
this, LengthModeWidth,
"-10%"));
60 setYBaseValue(SVGLength(
this, LengthModeHeight,
"-10%"));
63 setWidthBaseValue(SVGLength(
this, LengthModeWidth,
"120%"));
64 setHeightBaseValue(SVGLength(
this, LengthModeHeight,
"120%"));
67 SVGMaskElement::~SVGMaskElement()
71 ANIMATED_PROPERTY_DEFINITIONS(SVGMaskElement,
int, Enumeration, enumeration, MaskUnits, maskUnits,
SVGNames::maskUnitsAttr, m_maskUnits)
72 ANIMATED_PROPERTY_DEFINITIONS(SVGMaskElement,
int, Enumeration, enumeration, MaskContentUnits, maskContentUnits, SVGNames::
maskContentUnitsAttr, m_maskContentUnits)
73 ANIMATED_PROPERTY_DEFINITIONS(SVGMaskElement, SVGLength, Length, length, X, x, SVGNames::
xAttr, m_x)
74 ANIMATED_PROPERTY_DEFINITIONS(SVGMaskElement, SVGLength, Length, length, Y, y, SVGNames::
yAttr, m_y)
75 ANIMATED_PROPERTY_DEFINITIONS(SVGMaskElement, SVGLength, Length, length, Width, width, SVGNames::
widthAttr, m_width)
76 ANIMATED_PROPERTY_DEFINITIONS(SVGMaskElement, SVGLength, Length, length, Height, height, SVGNames::
heightAttr, m_height)
78 void SVGMaskElement::parseMappedAttribute(MappedAttribute* attr)
81 if (attr->value() ==
"userSpaceOnUse")
82 setMaskUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
83 else if (attr->value() ==
"objectBoundingBox")
84 setMaskUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
86 if (attr->value() ==
"userSpaceOnUse")
87 setMaskContentUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
88 else if (attr->value() ==
"objectBoundingBox")
89 setMaskContentUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
91 setXBaseValue(SVGLength(
this, LengthModeWidth, attr->value()));
93 setYBaseValue(SVGLength(
this, LengthModeHeight, attr->value()));
95 setWidthBaseValue(SVGLength(
this, LengthModeWidth, attr->value()));
97 setHeightBaseValue(SVGLength(
this, LengthModeHeight, attr->value()));
99 if (SVGURIReference::parseMappedAttribute(attr))
101 if (SVGTests::parseMappedAttribute(attr))
103 if (SVGLangSpace::parseMappedAttribute(attr))
105 if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
107 SVGStyledElement::parseMappedAttribute(attr);
111 void SVGMaskElement::svgAttributeChanged(
const QualifiedName& attrName)
113 SVGStyledElement::svgAttributeChanged(attrName);
121 SVGURIReference::isKnownAttribute(attrName) ||
122 SVGTests::isKnownAttribute(attrName) ||
123 SVGLangSpace::isKnownAttribute(attrName) ||
124 SVGExternalResourcesRequired::isKnownAttribute(attrName) ||
125 SVGStyledElement::isKnownAttribute(attrName))
126 m_masker->invalidate();
129 void SVGMaskElement::childrenChanged(
bool changedByParser, Node* beforeChange, Node* afterChange,
int childCountDelta)
131 SVGStyledElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
136 m_masker->invalidate();
139 auto_ptr<ImageBuffer> SVGMaskElement::drawMaskerContent(
const FloatRect& targetRect, FloatRect& maskDestRect)
const
147 if (maskUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
148 xValue = x().valueAsPercentage() * targetRect.width();
149 yValue = y().valueAsPercentage() * targetRect.height();
150 widthValue = width().valueAsPercentage() * targetRect.width();
151 heightValue = height().valueAsPercentage() * targetRect.height();
153 xValue = x().value();
154 yValue = y().value();
155 widthValue = width().value();
156 heightValue = height().value();
159 IntSize imageSize(lroundf(widthValue), lroundf(heightValue));
160 clampImageBufferSizeToViewport(document()->renderer(), imageSize);
162 if (imageSize.width() <
static_cast<int>(widthValue))
163 widthValue = imageSize.width();
165 if (imageSize.height() <
static_cast<int>(heightValue))
166 heightValue = imageSize.height();
168 auto_ptr<ImageBuffer> maskImage = ImageBuffer::create(imageSize,
false);
169 if (!maskImage.get())
172 maskDestRect = FloatRect(xValue, yValue, widthValue, heightValue);
173 if (maskUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
174 maskDestRect.move(targetRect.x(), targetRect.y());
176 GraphicsContext* maskImageContext = maskImage->context();
177 ASSERT(maskImageContext);
179 maskImageContext->save();
180 maskImageContext->translate(-xValue, -yValue);
182 if (maskContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
183 maskImageContext->save();
184 maskImageContext->scale(FloatSize(targetRect.width(), targetRect.height()));
188 for (Node* n = firstChild(); n; n = n->nextSibling()) {
189 SVGElement* elem = 0;
190 if (n->isSVGElement())
191 elem = static_cast<SVGElement*>(n);
192 if (!elem || !elem->isStyled())
195 SVGStyledElement* e =
static_cast<SVGStyledElement*
>(elem);
196 RenderObject* item = e->renderer();
200 renderSubtreeToImage(maskImage.get(), item);
203 if (maskContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
204 maskImageContext->restore();
206 maskImageContext->restore();
210 RenderObject* SVGMaskElement::createRenderer(RenderArena* arena, RenderStyle*)
212 RenderSVGContainer* maskContainer =
new (arena) RenderSVGContainer(
this);
213 maskContainer->setDrawsContents(
false);
214 return maskContainer;
217 SVGResource* SVGMaskElement::canvasResource()
220 m_masker = SVGResourceMasker::create(
this);
221 return m_masker.get();
226 #endif // ENABLE(SVG)
DOM::QualifiedName widthAttr
DOM::QualifiedName heightAttr
DOM::QualifiedName maskContentUnitsAttr
DOM::QualifiedName maskUnitsAttr