2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
4 * Copyright (C) 2010 Dirk Schulze <krit@webkit.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 #if ENABLE(SVG) && ENABLE(FILTERS)
25 #include "SVGFEImageElement.h"
28 #include "CachedImage.h"
29 #include "CachedResourceLoader.h"
30 #include "ColorSpace.h"
33 #include "RenderObject.h"
34 #include "RenderSVGResource.h"
35 #include "SVGElementInstance.h"
36 #include "SVGImageBufferTools.h"
38 #include "SVGPreserveAspectRatio.h"
42 // Animated property definitions
43 DEFINE_ANIMATED_PRESERVEASPECTRATIO(SVGFEImageElement, SVGNames::preserveAspectRatioAttr, PreserveAspectRatio, preserveAspectRatio)
44 DEFINE_ANIMATED_STRING(SVGFEImageElement, XLinkNames::hrefAttr, Href, href)
45 DEFINE_ANIMATED_BOOLEAN(SVGFEImageElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
47 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEImageElement)
48 REGISTER_LOCAL_ANIMATED_PROPERTY(preserveAspectRatio)
49 REGISTER_LOCAL_ANIMATED_PROPERTY(href)
50 REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
51 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
52 END_REGISTER_ANIMATED_PROPERTIES
54 inline SVGFEImageElement::SVGFEImageElement(const QualifiedName& tagName, Document* document)
55 : SVGFilterPrimitiveStandardAttributes(tagName, document)
57 ASSERT(hasTagName(SVGNames::feImageTag));
58 registerAnimatedPropertiesForSVGFEImageElement();
61 PassRefPtr<SVGFEImageElement> SVGFEImageElement::create(const QualifiedName& tagName, Document* document)
63 return adoptRef(new SVGFEImageElement(tagName, document));
66 SVGFEImageElement::~SVGFEImageElement()
69 m_cachedImage->removeClient(this);
72 void SVGFEImageElement::requestImageResource()
75 m_cachedImage->removeClient(this);
79 Element* hrefElement = SVGURIReference::targetElementFromIRIString(href(), document());
80 if (hrefElement && hrefElement->isSVGElement() && hrefElement->renderer())
83 ResourceRequest request(ownerDocument()->completeURL(href()));
84 m_cachedImage = ownerDocument()->cachedResourceLoader()->requestImage(request);
87 m_cachedImage->addClient(this);
90 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName)
92 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
93 if (supportedAttributes.isEmpty()) {
94 SVGURIReference::addSupportedAttributes(supportedAttributes);
95 SVGLangSpace::addSupportedAttributes(supportedAttributes);
96 SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
97 supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
99 return supportedAttributes.contains(attrName);
102 void SVGFEImageElement::parseMappedAttribute(Attribute* attr)
104 if (!isSupportedAttribute(attr->name())) {
105 SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
109 const AtomicString& value = attr->value();
110 if (attr->name() == SVGNames::preserveAspectRatioAttr) {
111 SVGPreserveAspectRatio::parsePreserveAspectRatio(this, value);
115 if (SVGURIReference::parseMappedAttribute(attr)) {
116 requestImageResource();
120 if (SVGLangSpace::parseMappedAttribute(attr))
122 if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
125 ASSERT_NOT_REACHED();
128 void SVGFEImageElement::svgAttributeChanged(const QualifiedName& attrName)
130 if (!isSupportedAttribute(attrName)) {
131 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
135 SVGElementInstance::InvalidationGuard invalidationGuard(this);
137 if (attrName == SVGNames::preserveAspectRatioAttr) {
142 // FIXME: This can't be correct, I'm just preserving existing code. <feImage> + SVG DOM 'href' changes need testing.
143 if (SVGLangSpace::isKnownAttribute(attrName) || SVGExternalResourcesRequired::isKnownAttribute(attrName) || SVGURIReference::isKnownAttribute(attrName))
146 ASSERT_NOT_REACHED();
149 void SVGFEImageElement::notifyFinished(CachedResource*)
154 Element* parent = parentElement();
157 if (!parent->hasTagName(SVGNames::filterTag) || !parent->renderer())
160 RenderSVGResource::markForLayoutAndParentResourceInvalidation(parent->renderer());
163 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter)
165 if (!m_cachedImage && !m_targetImage) {
166 Element* hrefElement = SVGURIReference::targetElementFromIRIString(href(), document());
167 if (!hrefElement || !hrefElement->isSVGElement())
170 RenderObject* renderer = hrefElement->renderer();
174 IntRect targetRect = enclosingIntRect(renderer->objectBoundingBox());
175 m_targetImage = ImageBuffer::create(targetRect.size(), ColorSpaceLinearRGB);
177 AffineTransform contentTransformation;
178 SVGImageBufferTools::renderSubtreeToImageBuffer(m_targetImage.get(), renderer, contentTransformation);
181 return FEImage::create(filter, m_targetImage ? m_targetImage->copyImage(CopyBackingStore) : m_cachedImage->image(), preserveAspectRatio());
184 void SVGFEImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
186 SVGFilterPrimitiveStandardAttributes::addSubresourceAttributeURLs(urls);
188 addSubresourceURL(urls, document()->completeURL(href()));
193 #endif // ENABLE(SVG)